//序列化 func MarshalProtoMsg(args proto.Message) []byte { msgID := protos.GetMsgID(args) msgBody, _ := proto.Marshal(args) result := make([]byte, 2+len(msgBody)) binary.PutUint16LE(result[:2], msgID) copy(result[2:], msgBody) return result }
func createGetUserInfoBytes(userId uint64) packet.RAW { sendMsg := &gameProto.GetUserInfoC2S{ UserID: proto.Uint64(userId), } msgBody, _ := proto.Marshal(sendMsg) msg1 := make([]byte, 2+len(msgBody)) binary.PutUint16LE(msg1, gameProto.ID_GetUserInfoC2S) copy(msg1[2:], msgBody) return packet.RAW(msg1) }
func createLoginBytes(userName string) packet.RAW { sendMsg := &gameProto.UserLoginC2S{ UserName: proto.String(userName), } msgBody, _ := proto.Marshal(sendMsg) msg1 := make([]byte, 2+len(msgBody)) binary.PutUint16LE(msg1, gameProto.ID_UserLoginC2S) copy(msg1[2:], msgBody) return packet.RAW(msg1) }
//将消息写入Chan func (c *DummyConn) PutMsg(msg []byte) { msgLen := len(msg) - 8 msgID := binary.GetUint16LE(msg[:2]) msgBody := msg[10:] saveMsg := make([]byte, msgLen) binary.PutUint16LE(saveMsg[:2], msgID) copy(saveMsg[2:], msgBody) c.recvChan <- saveMsg }
//序列化 func MarshalProtoMsg(identification uint64, args proto.Message) []byte { msgID := protos.GetMsgID(args) msgBody, _ := proto.Marshal(args) result := make([]byte, 2+8+len(msgBody)) binary.PutUint16LE(result[:2], msgID) binary.PutUint64LE(result[2:10], identification) copy(result[10:], msgBody) return result }
//发送消息到用户客户端 func sendToGateServer(msg []byte) { if len(msg) < 10 { return } msgID := binary.GetUint16LE(msg[:2]) msgIdentification := binary.GetUint64LE(msg[2:10]) msgBody := msg[10:] userSession := global.GetSession(msgIdentification) if userSession == nil { return } result := make([]byte, len(msg)-8) binary.PutUint16LE(result[:2], msgID) copy(result[2:], msgBody) userSession.Send(result) }
func (c *TransferProxyConn) Receive(msg interface{}) error { data, ok := <-c.recvChan if !ok { return io.EOF } msgID := binary.GetUint16LE(data[:2]) msgBody := data[10:] result := make([]byte, len(data)-8) binary.PutUint16LE(result[:2], msgID) copy(result[2:], msgBody) if fast, ok := msg.(packet.FastInMessage); ok { return fast.Unmarshal( &io.LimitedReader{bytes.NewReader(result), int64(len(result))}, ) } msg.(packet.InMessage).Unmarshal(result) return nil }