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 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) }
//在World服务端删除虚拟用户 func setSessionOffline(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientSessionOfflineC2S) userSession := global.GetSession(rev_msg.GetSessionID()) if userSession != nil { userSession.Close() } }
//在游戏服务端删除虚拟用户 func setSessionOffline(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientSessionOfflineC2S) userSession := global.GetSession(rev_msg.GetSessionID()) if userSession != nil { userSession.Close() } //通知WorldServer用户下线 worldProxy.SendSystemMsgToServer(protos.MarshalProtoMsg(rev_msg)) }
//处理游戏逻辑 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) }
//其他客户端连接TransferServer处理 func connectTransferServer(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ConnectTransferServerC2S) serverName := rev_msg.GetServerName() serverID := rev_msg.GetServerID() useServerName := strings.Split(serverName, "[")[0] serverList, exists := servers[useServerName] if !exists { serverList = make([]Server, 0, 10) } server := Server{ session: session, serverID: serverID, serverIndex: len(serverList), } serverList = append(serverList, server) servers[useServerName] = serverList //服务器断开连接处理 session.AddCloseCallback(session, func() { serverList = append(serverList[:server.serverIndex], serverList[server.serverIndex+1:]...) servers[useServerName] = serverList ERR(serverName + " Disconnect At " + global.ServerName) }) //GameServer可以有多个 if useServerName == "GameServer" { addr := strings.Split(session.Conn().RemoteAddr().String(), ":") addrIp := addr[0] addrPort, _ := strconv.Atoi(addr[1]) gameNode := hashs.NewNode(server.serverIndex, addrIp, addrPort, serverName, 1) gameConsistent.Add(gameNode) //GameServer断开连接处理 session.AddCloseCallback(session, func() { //移除此Node gameConsistent.Remove(gameNode) //将此Node的所有用户断开连接 for clientSessionID, gameNodeIndex := range gameUserSessions { if server.serverIndex == gameNodeIndex { clientSession := global.GetSession(clientSessionID) if clientSession != nil { clientSession.Close() } } } }) } //发送连接成功消息 send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectTransferServerS2C{}) protos.Send(session, send_msg) }
//处理接收到纯DB的消息 func dealReceiveDBMsgS2C(msg packet.RAW) { protoMsg := dbProto.UnmarshalProtoMsg(msg) if protoMsg == dbProto.NullProtoMsg { return } var session *link.Session = global.GetSession(protoMsg.Identification) if session == nil { return } switch protoMsg.ID { case dbProto.ID_DB_User_LoginS2C: userLoginCallBack(session, protoMsg) } }
//发送消息到用户客户端 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) }