//发送连接DB服务器 func sendConnectDBServer() { INFO(global.ServerName + " Connect DBServer ...") send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectDBServerC2S{ ServerName: protos.String(global.ServerName), }) protos.Send(dbClient, send_msg) }
//发送连接LogServer func sendConnectLogServer() { INFO(global.ServerName + " Connect LogServer ...") send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectLogServerC2S{ ServerName: protos.String(global.ServerName), }) sendSystemMsgToServer(send_msg) }
//发送连接WorldServer func sendConnectWorldServer() { INFO(global.ServerName + " Connect WorldServer ...") send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectWorldServerC2S{ ServerName: protos.String(global.ServerName), ServerID: protos.Uint32(global.ServerID), }) SendSystemMsgToServer(send_msg) }
func sendCommonLog(dir string, logType uint32, content string) { send_msg := protos.MarshalProtoMsg(&logProto.Log_CommonLogC2S{ Dir: protos.String(dir), Type: protos.Uint32(logType), Content: protos.String(content), Time: protos.Int64(time.Now().Unix()), }) sendLogMsgToServer(send_msg) }
//在游戏服务端删除虚拟用户 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)) }
//LoginServer用户上线 func SetClientSessionOnline(userSession *link.Session) { //发送用户上线消息到serverName protoMsg := &systemProto.System_ClientSessionOnlineC2S{ SessionID: protos.Uint64(userSession.Id()), Network: protos.String(userSession.Conn().RemoteAddr().Network()), Addr: protos.String(userSession.Conn().RemoteAddr().String()), } send_msg := protos.MarshalProtoMsg(protoMsg) sendSystemMsg2("LoginServer", 0, send_msg) }
//通知GameServer用户登录成功 func SetClientLoginSuccess(userName string, userID uint64, session *link.Session) { send_msg := protos.MarshalProtoMsg(&systemProto.System_ClientLoginSuccessC2S{ UserID: protos.Uint64(userID), UserName: protos.String(userName), SessionID: protos.Uint64(session.Id()), GameServerID: protos.Uint32(0), Network: protos.String(session.Conn().RemoteAddr().Network()), Addr: protos.String(session.Conn().RemoteAddr().String()), }) sendSystemMsgToServer(send_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) }
//重新连接 func againConnect(session *link.Session, msg protos.ProtoMsg) { rev_msg := msg.Body.(*gameProto.AgainConnectC2S) newSessionID := module.User.AgainConnect(rev_msg.GetSessionID(), session) if newSessionID != 0 { send_msg := protos.MarshalProtoMsg(&gameProto.AgainConnectS2C{ SessionID: protos.Uint64(newSessionID), }) protos.Send(session, send_msg) } }
//其他客户端连接LogServer处理 func connectLogServer(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ConnectLogServerC2S) serverName := rev_msg.GetServerName() servers[serverName] = session session.AddCloseCallback(session, func() { delete(servers, serverName) ERR(serverName + " Disconnect At " + global.ServerName) }) send_msg := protos.MarshalProtoMsg(&systemProto.System_ConnectLogServerS2C{}) protos.Send(session, send_msg) }
//发送获取用户数据结果 func SendGetUserInfoResult(session *link.Session, errorCode int32, u *UserModel) { if errorCode != 0 { SendErrorMsg(session, errorCode) } else { send_msg := protos.MarshalProtoMsg(&gameProto.GetUserInfoS2C{ UserInfo: &gameProto.Person{ ID: protos.Uint64(u.DBUser.ID), Name: protos.String(u.DBUser.Name), Money: protos.Int32(u.DBUser.Money), SessionID: protos.Uint64(session.Id()), }, }) protos.Send(session, send_msg) } }
//通知GameServer、LoginServer用户下线, 网关调用 func SetClientSessionOffline(sessionID uint64) { //给该用户所分配的GameServerID gameServerID := getUserGameServerID(sessionID) //发送消息到GameServer和LoginServer protoMsg := &systemProto.System_ClientSessionOfflineC2S{ SessionID: protos.Uint64(sessionID), } send_msg := protos.MarshalProtoMsg(protoMsg) sendSystemMsg2("GameServer", gameServerID, send_msg) sendSystemMsg2("LoginServer", 0, send_msg) //给该用户不再分配游戏服务器 noAllotGameServer(sessionID) }
//在GameServer设置用户登录成功 func setClientLoginSuccess(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientLoginSuccessC2S) userConn := proxys.NewDummyConn(rev_msg.GetSessionID(), rev_msg.GetNetwork(), rev_msg.GetAddr(), transferClient) userSession := link.NewSessionByID(userConn, codecType.DummyCodecType{}, rev_msg.GetSessionID()) global.AddSession(userSession) //接收游戏消息 go func() { var msg []byte for { if err := userSession.Receive(&msg); err != nil { break } gameProxy.MsgDispatch.Process(userSession, msg) } }() module.User.LoginSuccess(userSession, rev_msg.GetUserName(), rev_msg.GetUserID(), rev_msg.GetGameServerID()) //通知WorldServer用户登录成功 worldProxy.SendSystemMsgToServer(protos.MarshalProtoMsg(rev_msg)) }
//处理玩家登录成功,分配服务器 func clientLoginSuccess(session *link.Session, protoMsg protos.ProtoMsg) { rev_msg := protoMsg.Body.(*systemProto.System_ClientLoginSuccessC2S) //用户SessionID userSessionID := rev_msg.GetSessionID() //给该用户分配GameServer allotGameServer(userSessionID) //给该用户所分配的GameServerID gameServerID := getUserGameServerID(userSessionID) if gameServerID == 0 { return } //给该消息填充上所分配的GameServerID rev_msg.GameServerID = protos.Uint32(gameServerID) //通知GameServer用户登录成功 send_msg := protos.MarshalProtoMsg(rev_msg) sendSystemMsg2("GameServer", gameServerID, send_msg) }
//发送登录结果 func SendLoginResult(session *link.Session, userID uint64) { send_msg := protos.MarshalProtoMsg(&gameProto.UserLoginS2C{ UserID: protos.Uint64(userID), }) protos.Send(session, send_msg) }
//发送通用错误消息 func SendErrorMsg(session *link.Session, errID int32) { send_msg := protos.MarshalProtoMsg(&gameProto.ErrorMsgS2C{ ErrorID: protos.Int32(errID), }) protos.Send(session, send_msg) }
//发送其他客户端登录 func SendOtherLogin(session *link.Session) { protos.Send(session, protos.MarshalProtoMsg(&gameProto.OtherLoginS2C{})) }
//发送服务器连接成功 func SendConnectSuccess(session *link.Session) { protos.Send(session, protos.MarshalProtoMsg(&gameProto.ConnectSuccessS2C{})) }