Browse Source

添加一些调试信息与修正打包msgID和length顺序

master
aceld 6 years ago
parent
commit
ac08b6c4b2
  1. 4
      utils/globalobj.go
  2. 2
      znet/connection.go
  3. 16
      znet/datapack.go
  4. 4
      znet/message.go
  5. 1
      znet/server.go

4
utils/globalobj.go

@ -80,8 +80,8 @@ func init() {
//初始化GlobalObject变量,设置一些默认值
GlobalObject = &GlobalObj{
Name: "ZinxServerApp",
Version: "V0.4",
TcpPort: 7777,
Version: "V0.11",
TcpPort: 8999,
Host: "0.0.0.0",
MaxConn: 12000,
MaxPacketSize: 4096,

2
znet/connection.go

@ -69,6 +69,7 @@ func (c *Connection) StartWriter() {
fmt.Println("Send Data error:, ", err, " Conn Writer exit")
return
}
fmt.Printf("Send data succ! data = %+v\n", data)
case data, ok := <-c.msgBuffChan:
if ok {
//有数据要写给客户端
@ -104,6 +105,7 @@ func (c *Connection) StartReader() {
fmt.Println("read msg head error ", err)
break
}
fmt.Printf("read headData %+v\n", headData)
//拆包,得到msgid 和 datalen 放在msg中
msg, err := dp.Unpack(headData)

16
znet/datapack.go

@ -26,13 +26,13 @@ func(dp *DataPack) Pack(msg ziface.IMessage)([]byte, error) {
//创建一个存放bytes字节的缓冲
dataBuff := bytes.NewBuffer([]byte{})
//写msgID
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetMsgId()); err != nil {
//写dataLen
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetDataLen()); err != nil {
return nil, err
}
//写dataLen
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetDataLen()); err != nil {
//写msgID
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetMsgId()); err != nil {
return nil, err
}
@ -51,13 +51,13 @@ func(dp *DataPack) Unpack(binaryData []byte)(ziface.IMessage, error) {
//只解压head的信息,得到dataLen和msgID
msg := &Message{}
//读msgID
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.Id); err != nil {
//读dataLen
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.DataLen); err != nil {
return nil, err
}
//读dataLen
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.DataLen); err != nil {
//读msgID
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.Id); err != nil {
return nil, err
}

4
znet/message.go

@ -1,16 +1,16 @@
package znet
type Message struct {
Id uint32 //消息的ID
DataLen uint32 //消息的长度
Id uint32 //消息的ID
Data []byte //消息的内容
}
//创建一个Message消息包
func NewMsgPackage(id uint32, data []byte) *Message {
return &Message{
Id: id,
DataLen: uint32(len(data)),
Id: id,
Data: data,
}
}

1
znet/server.go

@ -87,6 +87,7 @@ func (s *Server) Start() {
fmt.Println("Accept err ", err)
continue
}
fmt.Println("Get conn remote addr = ", conn.RemoteAddr().String())
//3.2 设置服务器最大连接控制,如果超过最大连接,那么则关闭此新的连接
if s.ConnMgr.Len() >= utils.GlobalObject.MaxConn {

Loading…
Cancel
Save