xtaci / smux

A Stream Multiplexing Library for golang with least memory usage(TDMA)
MIT License
1.27k stars 189 forks source link

能否支持自动匹配客户端版本 (version 1,2) #94

Open josh-chan opened 1 year ago

josh-chan commented 1 year ago

看了一下代码,不是很有信心能改对:

--- a/session.go
+++ b/session.go
@@ -325,10 +325,11 @@ func (s *Session) recvLoop() {
                // read header first
                if _, err := io.ReadFull(s.conn, hdr[:]); err == nil {
                        atomic.StoreInt32(&s.dataReady, 1)
-                       if hdr.Version() != byte(s.config.Version) {
+                       if !(hdr.Version() == byte(1) || hdr.Version() == byte(2)) {
                                s.notifyProtoError(ErrInvalidProtocol)
                                return
                        }
+                       s.config.Version = int(hdr.Version())
                        sid := hdr.StreamID()
                        switch hdr.Cmd() {
                        case cmdNOP:

server 能否通过这样自动适配客户端的协议版本? @xtaci

xtaci commented 1 year ago

嗯,貌似是可以的。

josh-chan commented 1 year ago

嗯,貌似是可以的。

多谢,试了一下,真的可以。还有一个地方要改,保证每个 session 用独立的 Config:

--- a/mux.go
+++ b/mux.go
@@ -94,7 +94,8 @@ func Server(conn io.ReadWriteCloser, config *Config) (*Session, error) {
        if err := VerifyConfig(config); err != nil {
                return nil, err
        }
-       return newSession(config, conn, false), nil
- 
+       cloneConfig := *config
+       return newSession(&cloneConfig, conn, false), nil
 }

觉得就像是个 trick,等大佬更新。