Пример #1
0
        public async Task RelayLoginHandler(RelaySession session, CRequestLoginMessage message)
        {
            var plr = GameServer.Instance.PlayerManager[message.AccountId];

            if (plr == null)
            {
                await session.SendAsync(new SNotifyLoginResultMessage(1));

                await session.CloseAsync();

                return;
            }

            var ip = session.RemoteEndPoint;

            Logger.ForAccount(plr).Information("RelayServer login from {remoteAddress}", ip);

            var gameIp = plr.Session.RemoteEndPoint;

            if (!gameIp.Address.Equals(ip.Address))
            {
                Logger.ForAccount(plr).Error("Suspicious login");
                await plr.SendAsync(new SNotifyLoginResultMessage(1));

                await session.CloseAsync();

                return;
            }

            var inChannel = plr.Channel != null && plr.Channel.Id > 0;
            var inRoom    = plr.Room != null && plr.Room.Id > 0;

            if (!inChannel || !inRoom)
            {
                if (!inChannel)
                {
                    Logger.ForAccount(plr).Error("Suspicious login (Not inside a channel)");
                }
                else
                {
                    Logger.ForAccount(plr).Error("Suspicious login (Not inside a room)");
                }

                plr?.Room?.Leave(plr);
                await plr.SendAsync(new SNotifyLoginResultMessage(1));

                await session.CloseAsync();

                return;
            }

            var roomExists  = plr.Channel.RoomManager.Any(x => x.Id == message.RoomLocation.RoomId);
            var unequalRoom = plr.Room?.Id != message.RoomLocation.RoomId;

            if (unequalRoom || !roomExists)
            {
                Logger.ForAccount(plr).Error($"Suspicious login (Invalid roomId: {message.RoomLocation.RoomId})");

                plr?.Room.Leave(plr);
                await plr.SendAsync(new SNotifyLoginResultMessage(1));

                await session.CloseAsync();

                return;
            }

            if (plr.RelaySession != null && plr.RelaySession != session)
            {
                await plr.RelaySession.CloseAsync();

                plr.RelaySession = null;
            }

            session.GameSession = plr.Session;
            plr.RelaySession    = session;

            Logger.ForAccount(plr)
            .Information("Login success");

            await plr.SendAsync(new SEnterLoginPlayerMessage(plr.RelaySession.HostId, plr.Account.Id, plr.Account.Nickname));

            foreach (var p in plr.Room.Players.Values)
            {
                if (p?.RelaySession == null)
                {
                    continue;
                }

                await p.SendAsync(new SEnterLoginPlayerMessage(plr.RelaySession.HostId, plr.Account.Id, plr.Account.Nickname));

                await plr.SendAsync(new SEnterLoginPlayerMessage(p.RelaySession.HostId, p.Account?.Id ?? 0, p.Account?.Nickname ?? "n/A"));
            }

            plr.Room.Group?.Join(session.HostId);
            plr.RoomInfo.IsConnecting = false;
            plr.Room.OnPlayerJoined(new RoomPlayerEventArgs(plr));
            await plr.SendAsync(new SNotifyLoginResultMessage(0));
        }