mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Add peer version
This commit is contained in:
parent
0352d658fc
commit
e7c3edab82
2 changed files with 23 additions and 4 deletions
|
@ -17,10 +17,12 @@ const (
|
||||||
contextKeyIdentity
|
contextKeyIdentity
|
||||||
contextKeyPeerAddr
|
contextKeyPeerAddr
|
||||||
contextKeyPeerClientVersion
|
contextKeyPeerClientVersion
|
||||||
|
contextKeyPeerProtoVersion
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrPeerIdNotFoundInContext = errors.New("peer id not found in context")
|
ErrPeerIdNotFoundInContext = errors.New("peer id not found in context")
|
||||||
|
ErrProtoVersionNotFoundInContext = errors.New("proto version not found in context")
|
||||||
ErrIdentityNotFoundInContext = errors.New("identity not found in context")
|
ErrIdentityNotFoundInContext = errors.New("identity not found in context")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,6 +44,19 @@ func CtxWithPeerId(ctx context.Context, peerId string) context.Context {
|
||||||
return context.WithValue(ctx, contextKeyPeerId, peerId)
|
return context.WithValue(ctx, contextKeyPeerId, peerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CtxWithProtoVersion sets peer protocol version
|
||||||
|
func CtxWithProtoVersion(ctx context.Context, version uint32) context.Context {
|
||||||
|
return context.WithValue(ctx, contextKeyPeerProtoVersion, version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CtxProtoVersion returns peer protocol version
|
||||||
|
func CtxProtoVersion(ctx context.Context) (uint32, error) {
|
||||||
|
if protoVersion, ok := ctx.Value(contextKeyPeerProtoVersion).(uint32); ok {
|
||||||
|
return protoVersion, nil
|
||||||
|
}
|
||||||
|
return 0, ErrProtoVersionNotFoundInContext
|
||||||
|
}
|
||||||
|
|
||||||
// CtxPeerAddr returns peer address
|
// CtxPeerAddr returns peer address
|
||||||
func CtxPeerAddr(ctx context.Context) string {
|
func CtxPeerAddr(ctx context.Context) string {
|
||||||
if p, ok := ctx.Value(contextKeyPeerAddr).(string); ok {
|
if p, ok := ctx.Value(contextKeyPeerAddr).(string); ok {
|
||||||
|
|
|
@ -28,11 +28,13 @@ var (
|
||||||
// ProtoVersion 1 - version with yamux over tcp and quic
|
// ProtoVersion 1 - version with yamux over tcp and quic
|
||||||
// ProtoVersion 2 - acl compatible version
|
// ProtoVersion 2 - acl compatible version
|
||||||
// ProtoVersion 3 - acl with breaking changes / multiplayer
|
// ProtoVersion 3 - acl with breaking changes / multiplayer
|
||||||
ProtoVersion = uint32(3)
|
// ProtoVersion 4 - new sync compatible version
|
||||||
|
NewSyncCompatibleVersion = uint32(4)
|
||||||
|
ProtoVersion = uint32(5)
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
compatibleVersions = []uint32{2, ProtoVersion}
|
compatibleVersions = []uint32{NewSyncCompatibleVersion, ProtoVersion}
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() SecureService {
|
func New() SecureService {
|
||||||
|
@ -119,6 +121,7 @@ func (s *secureService) HandshakeInbound(ctx context.Context, conn io.ReadWriteC
|
||||||
cctx = peer.CtxWithPeerId(cctx, peerId)
|
cctx = peer.CtxWithPeerId(cctx, peerId)
|
||||||
cctx = peer.CtxWithIdentity(cctx, res.Identity)
|
cctx = peer.CtxWithIdentity(cctx, res.Identity)
|
||||||
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
|
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
|
||||||
|
cctx = peer.CtxWithProtoVersion(cctx, res.ProtoVersion)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,6 +149,7 @@ func (s *secureService) HandshakeOutbound(ctx context.Context, conn io.ReadWrite
|
||||||
cctx = peer.CtxWithPeerId(cctx, peerId)
|
cctx = peer.CtxWithPeerId(cctx, peerId)
|
||||||
cctx = peer.CtxWithIdentity(cctx, res.Identity)
|
cctx = peer.CtxWithIdentity(cctx, res.Identity)
|
||||||
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
|
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
|
||||||
|
cctx = peer.CtxWithProtoVersion(cctx, res.ProtoVersion)
|
||||||
return cctx, nil
|
return cctx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue