1
0
Fork 0
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:
mcrakhman 2024-07-02 12:57:20 +02:00
parent 0352d658fc
commit e7c3edab82
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
2 changed files with 23 additions and 4 deletions

View file

@ -17,11 +17,13 @@ const (
contextKeyIdentity
contextKeyPeerAddr
contextKeyPeerClientVersion
contextKeyPeerProtoVersion
)
var (
ErrPeerIdNotFoundInContext = errors.New("peer id not found in context")
ErrIdentityNotFoundInContext = errors.New("identity 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")
)
const CtxResponsiblePeers = "*"
@ -42,6 +44,19 @@ func CtxWithPeerId(ctx context.Context, peerId string) context.Context {
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
func CtxPeerAddr(ctx context.Context) string {
if p, ok := ctx.Value(contextKeyPeerAddr).(string); ok {

View file

@ -28,11 +28,13 @@ var (
// ProtoVersion 1 - version with yamux over tcp and quic
// ProtoVersion 2 - acl compatible version
// ProtoVersion 3 - acl with breaking changes / multiplayer
ProtoVersion = uint32(3)
// ProtoVersion 4 - new sync compatible version
NewSyncCompatibleVersion = uint32(4)
ProtoVersion = uint32(5)
)
var (
compatibleVersions = []uint32{2, ProtoVersion}
compatibleVersions = []uint32{NewSyncCompatibleVersion, ProtoVersion}
)
func New() SecureService {
@ -119,6 +121,7 @@ func (s *secureService) HandshakeInbound(ctx context.Context, conn io.ReadWriteC
cctx = peer.CtxWithPeerId(cctx, peerId)
cctx = peer.CtxWithIdentity(cctx, res.Identity)
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
cctx = peer.CtxWithProtoVersion(cctx, res.ProtoVersion)
return
}
@ -146,6 +149,7 @@ func (s *secureService) HandshakeOutbound(ctx context.Context, conn io.ReadWrite
cctx = peer.CtxWithPeerId(cctx, peerId)
cctx = peer.CtxWithIdentity(cctx, res.Identity)
cctx = peer.CtxWithClientVersion(cctx, res.ClientVersion)
cctx = peer.CtxWithProtoVersion(cctx, res.ProtoVersion)
return cctx, nil
}