protected override async void Run(Session session, C2G_LoginGate message, Action <G2C_LoginGate> reply) { G2C_LoginGate response = new G2C_LoginGate(); StartConfigComponent startComponent = Game.Scene.GetComponent <StartConfigComponent>(); GateSessionKeyComponent gateSessionKeyComponent = Game.Scene.GetComponent <GateSessionKeyComponent>(); try { long uid = gateSessionKeyComponent.Get(message.Key); if (uid <= 0) { response.Error = ErrorCode.ERR_ConnectGateKeyError; response.Message = "Gate key驗證失敗!"; reply(response); return; } // 登入成功,刪除Gate的Key gateSessionKeyComponent.Remove(message.Key); int lobbyAppId = 0; var player = CacheHelper.GetFromCache <Player>(uid); if (player != null && player.lobbyAppId != 0) { lobbyAppId = player.lobbyAppId; } else { lobbyAppId = SessionHelper.GetLobbyIdRandomly(); } // 隨機連接到Lobby伺服器,並創建PlayerUnit實體 G2L_LobbyUnitCreate g2L_LobbyUnitCreate = new G2L_LobbyUnitCreate(); g2L_LobbyUnitCreate.Uid = uid; g2L_LobbyUnitCreate.GateSessionId = session.InstanceId; g2L_LobbyUnitCreate.GateAppId = (int)IdGenerater.AppId; g2L_LobbyUnitCreate.LobbyAppId = lobbyAppId; // 等候LobbyUnit單元創建完成,並且也同步Player到了Gate Session lobbySession = SessionHelper.GetLobbySession(g2L_LobbyUnitCreate.LobbyAppId); L2G_LobbyUnitCreate l2G_LobbyUnitCreate = (L2G_LobbyUnitCreate)await lobbySession.Call(g2L_LobbyUnitCreate); if (l2G_LobbyUnitCreate.Error != ErrorCode.ERR_Success) { response.Error = l2G_LobbyUnitCreate.Error; reply(response); return; } player = BsonSerializer.Deserialize <Player>(l2G_LobbyUnitCreate.Json); CacheExHelper.WriteInCache(player, out player); session.AddComponent <SessionPlayerComponent, long>(player.gateSessionActorId).Player = player; session.AddComponent <MailBoxComponent, string>(MailboxType.GateSession); response.PlayerId = player.Id; reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async ETTask Run(Session session, C2G_LoginGate request, G2C_LoginGate response, Action reply) { GateSessionKeyComponent gateSessionKeyComponent = Game.Scene.GetComponent <GateSessionKeyComponent>(); //从已经分发的KEY里面寻找,如果没找到,说明非法用户,不给他连接gate服务器 long playerID = gateSessionKeyComponent.Get(request.Key); if (playerID == 0) { response.Error = ErrorCode.ERR_ConnectGateKeyError; response.Message = "Gate key验证失败!"; reply(); return; } //Key失效 gateSessionKeyComponent.Remove(request.Key); //专门给这个玩家创建一个Player对象 Player player = ComponentFactory.Create <Player, long>(playerID); player.AddComponent <UnitGateComponent, long>(session.InstanceId); //注册到PlayerComponent,方便管理 Game.Scene.GetComponent <PlayerComponent>().Add(player); //给这个session安排上Player session.AddComponent <SessionPlayerComponent>().Player = player; // 增加掉线组件 session.AddComponent <SessionOfflineComponent>(); // 增加心跳包 session.AddComponent <HeartBeatComponent>().CurrentTime = TimeHelper.ClientNowSeconds(); //添加邮箱组件表示该session是一个Actor,接收的消息将会队列处理 await session.AddComponent <MailBoxComponent, string>(MailboxType.GateSession).AddLocation(); Log.Info($"gate的actorid为{session.Id}"); //向登录服务器发送玩家上线消息 StartConfigComponent config = Game.Scene.GetComponent <StartConfigComponent>(); IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent <InnerConfig>().IPEndPoint; Session realmSession = Game.Scene.GetComponent <NetInnerComponent>().Get(realmIPEndPoint); await realmSession.Call(new G2R_PlayerOnline() { PlayerIDInPlayerComponent = player.Id, PlayerId = player.PlayerID, GateAppID = config.StartConfig.AppId }); //Log.Info("发送离线消息完毕"); //回复客户端的连接gate服务器请求 reply(); await ETTask.CompletedTask; }
protected override async void Run(Session session, C2G_LoginGate message, Action <G2C_LoginGate> reply) { G2C_LoginGate response = new G2C_LoginGate(); try { GateSessionKeyComponent gateSessionKeyComponent = Game.Scene.GetComponent <GateSessionKeyComponent>(); long userId = gateSessionKeyComponent.Get(message.Key); if (userId == 0) { response.Error = ErrorCode.ERR_ConnectGateKeyError; response.Message = "Gate key验证失败!"; reply(response); return; } //Key过期 gateSessionKeyComponent.Remove(message.Key); //创建User对象 User user = UserFactory.Create(userId, session.Id); await user.AddComponent <ActorComponent>().AddLocation(); //添加User对象关联到Session上 session.AddComponent <SessionUserComponent>().User = user; //添加消息转发组件 await session.AddComponent <ActorComponent, string>(ActorType.GateSession).AddLocation(); //向登录服务器发送玩家上线消息 StartConfigComponent config = Game.Scene.GetComponent <StartConfigComponent>(); IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent <InnerConfig>().IPEndPoint; Session realmSession = Game.Scene.GetComponent <NetInnerComponent>().Get(realmIPEndPoint); await realmSession.Call(new G2R_PlayerOnline() { UserId = userId, GateAppId = config.StartConfig.AppId }); response.PlayerId = user.Id; response.UserId = user.UserID; reply(response); session.Send(new G2C_TestHotfixMessage() { Info = "recv hotfix message success" }); } catch (Exception e) { ReplyError(response, e, reply); } }
protected override async void Run(Session session, C2G_RBLoginGate message, Action <G2C_RBLoginGate> reply) { G2C_RBLoginGate response = new G2C_RBLoginGate(); try { Log.Debug("[C2G_LoginGate]key:" + message.Key); GateSessionKeyComponent gateSessionKeyComponent = Game.Scene.GetComponent <GateSessionKeyComponent>(); long userId = gateSessionKeyComponent.Get(message.Key); if (userId == 0) { response.Error = ErrorCode.ERR_ConnectGateKeyError; reply(response); return; } //Key过期 gateSessionKeyComponent.Remove(message.Key); //创建User对象 User user = ComponentFactory.Create <User, long>(userId); user.AddComponent <UnitGateComponent, long>(session.Id); Game.Scene.GetComponent <UserComponent>().Add(user); await user.AddComponent <MailBoxComponent>().AddLocation(); //添加User对象关联到Session上 session.AddComponent <SessionUserComponent>().User = user; //添加消息转发组件 await session.AddComponent <MailBoxComponent, string>(ActorType.GateSession).AddLocation(); //向登录服务器发送玩家上线消息 StartConfigComponent config = Game.Scene.GetComponent <StartConfigComponent>(); IPEndPoint realmIPEndPoint = config.RealmConfig.GetComponent <InnerConfig>().IPEndPoint; Session realmSession = Game.Scene.GetComponent <NetInnerComponent>().Get(realmIPEndPoint); await realmSession.Call(new G2R_PlayerOnline() { UserID = userId, GateAppID = config.StartConfig.AppId }); response.PlayerId = user.Id; reply(response); } catch (Exception e) { ReplyError(response, e, reply); } }