func init() { handle := dispatch.NewHandleConditions() //系统消息 handle.Add(dispatch.HandleCondition{ Condition: systemProto.IsValidID, H: dispatch.Handle{ systemProto.ID_System_ConnectDBServerS2C: connectDBServerCallBack, }, }) //DB消息 handle.Add(dispatch.HandleFuncCondition{ Condition: dbProto.IsValidID, H: func(session *link.Session, msg []byte) { identification := binary.GetUint64LE(msg[2:10]) var userSession *link.Session = global.GetSession(identification) if userSession == nil { return } dbMsgChan := userSession.State.(chan []byte) dbMsgChan <- msg }, }) //创建消息分派 clientMsgDispatch = dispatch.NewDispatch(handle) }
//反序列化消息 func UnmarshalProtoMsg(msg []byte) ProtoMsg { if len(msg) < 10 { return NullProtoMsg } msgID := binary.GetUint16LE(msg[:2]) if !IsValidID(msgID) { return NullProtoMsg } identification := binary.GetUint64LE(msg[2:10]) msgBody := protos.GetMsgObject(msgID) if msgBody == nil { return NullProtoMsg } err := proto.Unmarshal(msg[10:], msgBody) if err != nil { return NullProtoMsg } return ProtoMsg{ ID: msgID, Body: msgBody, Identification: identification, } }
func init() { handle := dispatch.NewHandleConditions() //系统消息处理 handle.Add(dispatch.HandleCondition{ Condition: systemProto.IsValidID, H: dispatch.Handle{ systemProto.ID_System_ConnectWorldServerS2C: connectWorldServerCallBack, }, }) //游戏消息处理 handle.Add(dispatch.HandleFuncCondition{ Condition: gameProto.IsValidID, H: func(session *link.Session, msg []byte) { //发送到用户客户端 msgIdentification := binary.GetUint64LE(msg[2:10]) userSession := global.GetSession(msgIdentification) if userSession == nil { return } protos.Send(userSession, msg) }, }) //创建消息分派 clientMsgDispatch = dispatch.NewDispatch(handle) }
//处理游戏逻辑 func dealGameMsg(msg []byte) { msgIdentification := binary.GetUint64LE(msg[2:10]) userSession := global.GetSession(msgIdentification) if userSession == nil { return } conn := userSession.Conn().(*proxys.DummyConn) conn.PutMsg(msg) }
//发送游戏消息到不同服务器 func sendGameMsg(serverName string, msg []byte) { if serverList, exists := servers[serverName]; exists { if serverName == "GameServer" { //游戏消息发送到用户对应的GameServer clientSessionID := binary.GetUint64LE(msg[2:10]) if nodeIndex, existsNodeID := gameUserSessions[clientSessionID]; existsNodeID { s := serverList[nodeIndex] s.session.Send(msg) } } else { for _, s := range serverList { s.session.Send(msg) } } } }
//发送消息到用户客户端 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) }