name5566 / leaf

A game server framework in Go (golang)
Apache License 2.0
5.26k stars 1.31k forks source link

为什么一直提示我没有注册, 我注册的啊,帮忙看下呢 #175

Open helloh2o opened 4 years ago

helloh2o commented 4 years ago

package msg

import ( "github.com/name5566/leaf/network/protobuf" pp "server/protobuf" )

var Processor = protobuf.NewProcessor()

func init() { Processor.Register(&pp.Action{}) Processor.Register(&pp.Hello{}) } 这是注册, 发消息也是按照文档上发送的 hello := protobuf.Hello{Info: "hello"} hello := protobuf.Hello{Info: "hello"} data, err := proto.Marshal(&hello) if err != nil { log.Fatal(err) } // len + data m := make([]byte, 2+len(data)) // 默认使用大端序 binary.BigEndian.PutUint16(m, uint16(len(data))) copy(m[2:], data) // 发送消息 conn.Write(m)

heguanfeng commented 3 years ago

protobuf 的协议头不一样,需要增加两个字节的MSG_ID,上行下行协议一致 len+msgId+pbdata,其中len=2+len(pbdata) 这里面服务器的消息注册有点问题,MSGID是与服务器端注册函数(p Processor) Register(msg proto.Message)的调用次序有关的 `func (p Processor) Register(msg proto.Message) uint16 { msgType := reflect.TypeOf(msg) if msgType == nil || msgType.Kind() != reflect.Ptr { log.Fatal("protobuf message pointer required") } if , ok := p.msgID[msgType]; ok { log.Fatal("message %s is already registered", msgType) } if len(p.msgInfo) >= math.MaxUint16 { log.Fatal("too many protobuf messages (max = %v)", math.MaxUint16) }

i := new(MsgInfo)
i.msgType = msgType
p.msgInfo = append(p.msgInfo, i)
id := uint16(len(p.msgInfo) - 1)
p.msgID[msgType] = id
return id

} ` 那么问题来了,当服务器端有很多协议需要注册,并且分布在各个模块的源文件的时候,客户端怎么才能知道proto message所对应的正确的MSG_ID?