func newFrontLink(session *link.Session, linkID uint64) *frontendLink { flink := &frontendLink{ session: session, linkId: linkID, clients: make(map[uint64]*link.Session), clientWaits: make(map[uint64]*link.Session), } session.AddCloseCallback(flink, func() { flink.Close() }) go func() { var msg = gatewayMsg{} for { if err := session.Receive(&msg); err != nil { break } switch msg.Command { case CMD_MSG: flink.dispathMsg(msg.ClientId, msg.Data) case CMD_BRD: flink.broadcast(msg.ClientIds, msg.Data) case CMD_NEW_2: flink.newClient(msg.ClientId, msg.ClientIds[0]) case CMD_DEL: flink.delClient(msg.ClientId, false) } } flink.Close() }() return flink }
func AddSession(session *link.Session) { sessionMutex.Lock() defer sessionMutex.Unlock() sessions[session.Id()] = session session.AddCloseCallback(session, func() { RemoveSession(session.Id()) }) }
//其他客户端连接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 (this UserModule) dealLoginSuccess(session *link.Session, userName string, userID uint64) { //通知GameServer登录成功 transferProxy.SetClientLoginSuccess(userName, userID, session) //发送登录成功消息 gameProxy.SendLoginResult(session, userID) //用户下线时处理 session.AddCloseCallback(session, func() { //记录用户下线Log logProxy.UserOffLine(userID) }) //记录用户登录Log logProxy.UserLogin(userID) }
//用户登录成功处理 func (this UserModule) LoginSuccess(session *link.Session, userName string, userID uint64, gameServerID uint32) bool { cacheSuccess := module.Cache.AddOnlineUser(userName, userID, session, gameServerID) if cacheSuccess { session.AddCloseCallback(session, func() { module.Cache.RemoveOnlineUser(session.Id()) DEBUG("用户下线:当前在线人数", module.Cache.GetOnlineUsersNum()) }) DEBUG("用户上线:当前在线人数", module.Cache.GetOnlineUsersNum()) return true } else { ERR("what????", userName) return false } }
func newBackendLink(id uint64, listener *BackendListener, session *link.Session) *backendLink { session.EnableAsyncSend(10000) this := &backendLink{ id: id, listener: listener, session: session, conns: make(map[uint64]*BackendConn), } session.AddCloseCallback(this, func() { this.Close(false) }) go this.loop() return this }
//其他客户端连接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 loginSuccess(session *link.Session, userName string, userID uint64) bool { cacheSuccess := module.Cache.AddOnlineUser(userName, userID, session) if cacheSuccess { session.AddCloseCallback(session, func() { module.Cache.RemoveOnlineUser(session.Id()) DEBUG("下线:在线人数", module.Cache.GetOnlineUsersNum()) }) DEBUG("上线:在线人数", module.Cache.GetOnlineUsersNum()) //通知游戏服务器登录成功 transferProxy.SendClientLoginSuccess(userName, userID, session.Id()) return true } return false }