public async Task Relay_LoginHandler(RelayServer server, RelaySession session, CRequestLoginMessage message) { var ip = session.RemoteEndPoint; Logger.ForAccount(message.AccountId, "") .Information("RelayServer login from {remoteAddress}", ip); var plr = GameServer.Instance.PlayerManager[message.AccountId]; if (plr == null) { Logger.ForAccount(message.AccountId, "") .Error("Login failed"); await session.SendAsync(new SNotifyLoginResultMessage(1)); return; } if (plr.RelaySession != null && plr.RelaySession.IsConnected) { Logger.ForAccount(session) .Error("Already online"); await session.SendAsync(new SNotifyLoginResultMessage(2)); return; } var gameIp = plr.Session.RemoteEndPoint; if (!gameIp.Address.Equals(ip.Address)) { Logger.ForAccount(message.AccountId, "") .Error("Suspicious login"); await session.SendAsync(new SNotifyLoginResultMessage(3)); return; } if (plr.Room == null || plr.Room?.Id != message.RoomLocation.RoomId) { Logger.ForAccount(message.AccountId, "") .Error($"Suspicious login(Not in a room/Invalid room id) (given id:{message.RoomLocation.RoomId})"); await session.SendAsync(new SNotifyLoginResultMessage(4)); return; } session.GameSession = plr.Session; plr.RelaySession = session; Logger.ForAccount(session) .Information("Login success"); await session.SendAsync(new SEnterLoginPlayerMessage(plr.RelaySession.HostId, plr.Account.Id, plr.Account.Nickname)); foreach (var p in plr.Room.Players.Values.Where( p => p.RelaySession?.P2PGroup != null /*&& p.Account.Id != plr.Account.Id*/)) { await p.RelaySession.SendAsync(new SEnterLoginPlayerMessage(plr.RelaySession.HostId, plr.Account.Id, plr.Account.Nickname)); await session.SendAsync(new SEnterLoginPlayerMessage(p.RelaySession.HostId, p.Account.Id, p.Account.Nickname)); } plr.Room.Group.Join(session.HostId); await session.SendAsync(new SNotifyLoginResultMessage(0)); plr.RoomInfo.IsConnecting = false; plr.Room.OnPlayerJoined(new RoomPlayerEventArgs(plr)); }
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)); }