|
@@ -236,23 +236,33 @@ func (c *Tcp2) writeDataLen(dlen int) (buf []byte, start int) {
|
|
// 建立连接后第一个发送的消息
|
|
// 建立连接后第一个发送的消息
|
|
func (c *Tcp2) WriteAuthInfo(channel string, auth []byte) (err error) {
|
|
func (c *Tcp2) WriteAuthInfo(channel string, auth []byte) (err error) {
|
|
protoLen := len(PROTO)
|
|
protoLen := len(PROTO)
|
|
|
|
+ if protoLen > 0xFF {
|
|
|
|
+ return errors.New("length of protocol over")
|
|
|
|
+ }
|
|
|
|
+
|
|
channelLen := len(channel)
|
|
channelLen := len(channel)
|
|
if channelLen > 0xFFFF {
|
|
if channelLen > 0xFFFF {
|
|
return errors.New("length of channel over")
|
|
return errors.New("length of channel over")
|
|
}
|
|
}
|
|
- dlen := 2 + 1 + 1 + protoLen + 2 + channelLen + len(auth)
|
|
|
|
|
|
+ // id(uint16)+proto(string)+version(uint8)+channel(string)+auth([]byte)
|
|
|
|
+ dlen := 2 + 1 + protoLen + 1 + 2 + channelLen + len(auth)
|
|
|
|
+
|
|
buf, start := c.writeDataLen(dlen)
|
|
buf, start := c.writeDataLen(dlen)
|
|
index := start
|
|
index := start
|
|
binary.BigEndian.PutUint16(buf[index:index+2], config.ID_AUTH)
|
|
binary.BigEndian.PutUint16(buf[index:index+2], config.ID_AUTH)
|
|
index += 2
|
|
index += 2
|
|
- buf[index] = VERSION
|
|
|
|
- index++
|
|
|
|
|
|
+
|
|
buf[index] = byte(protoLen)
|
|
buf[index] = byte(protoLen)
|
|
index++
|
|
index++
|
|
copy(buf[index:index+protoLen], []byte(PROTO))
|
|
copy(buf[index:index+protoLen], []byte(PROTO))
|
|
index += protoLen
|
|
index += protoLen
|
|
|
|
+
|
|
|
|
+ buf[index] = VERSION
|
|
|
|
+ index++
|
|
|
|
+
|
|
binary.BigEndian.PutUint16(buf[index:index+2], uint16(channelLen))
|
|
binary.BigEndian.PutUint16(buf[index:index+2], uint16(channelLen))
|
|
index += 2
|
|
index += 2
|
|
|
|
+
|
|
copy(buf[index:index+channelLen], []byte(channel))
|
|
copy(buf[index:index+channelLen], []byte(channel))
|
|
index += channelLen
|
|
index += channelLen
|
|
copy(buf[index:], auth)
|
|
copy(buf[index:], auth)
|
|
@@ -312,7 +322,7 @@ func (c *Tcp2) readMessage(deadline int) ([]byte, error) {
|
|
}
|
|
}
|
|
|
|
|
|
// 获取Auth信息
|
|
// 获取Auth信息
|
|
-// id(uint16)+version(uint8)+proto(string)+channel(string)+auth([]byte)
|
|
|
|
|
|
+// id(uint16)+proto(string)+version(uint8)+channel(string)+auth([]byte)
|
|
func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error) {
|
|
func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth []byte, err error) {
|
|
defer func() {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if r := recover(); r != nil {
|
|
@@ -337,12 +347,6 @@ func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth
|
|
}
|
|
}
|
|
start += 2
|
|
start += 2
|
|
|
|
|
|
- version = msg[start]
|
|
|
|
- if version != VERSION {
|
|
|
|
- err = fmt.Errorf("require version %d, get version: %d", VERSION, version)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
- start++
|
|
|
|
protoLen := int(msg[start])
|
|
protoLen := int(msg[start])
|
|
if protoLen < 2 {
|
|
if protoLen < 2 {
|
|
err = errors.New("wrong proto length")
|
|
err = errors.New("wrong proto length")
|
|
@@ -355,6 +359,14 @@ func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth
|
|
return
|
|
return
|
|
}
|
|
}
|
|
start += protoLen
|
|
start += protoLen
|
|
|
|
+
|
|
|
|
+ version = msg[start]
|
|
|
|
+ if version != VERSION {
|
|
|
|
+ err = fmt.Errorf("require version %d, get version: %d", VERSION, version)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ start++
|
|
|
|
+
|
|
channelLen := int(binary.BigEndian.Uint16(msg[start : start+2]))
|
|
channelLen := int(binary.BigEndian.Uint16(msg[start : start+2]))
|
|
if channelLen < 2 {
|
|
if channelLen < 2 {
|
|
err = errors.New("wrong channel length")
|
|
err = errors.New("wrong channel length")
|
|
@@ -363,6 +375,7 @@ func (c *Tcp2) ReadAuthInfo() (proto string, version uint8, channel string, auth
|
|
start += 2
|
|
start += 2
|
|
channel = string(msg[start : start+channelLen])
|
|
channel = string(msg[start : start+channelLen])
|
|
start += channelLen
|
|
start += channelLen
|
|
|
|
+
|
|
auth = msg[start:]
|
|
auth = msg[start:]
|
|
return
|
|
return
|
|
}
|
|
}
|