public Task SendAsync(IRelayMessage message) { if (Disposed) { return(Task.CompletedTask); } return(RelaySession?.SendAsync(message)); }
public async Task Relay_LoginHandler(RelayServer server, RelaySession session, Message.Relay.CRequestLoginMessage message) { var ip = session.RemoteEndPoint; Logger.Info() .Account(message.AccountId, "") .Message($"Login from {ip}") .Write(); var plr = GameServer.Instance.PlayerManager[message.AccountId]; if (plr == null) { Logger.Error() .Account(message.AccountId, "") .Message("Login failed") .Write(); session.SendAsync(new Message.Relay.SNotifyLoginResultMessage(1)); return; } if (plr.RelaySession != null && plr.RelaySession.IsConnected) { Logger.Error() .Account(session) .Message("Already online") .Write(); session.SendAsync(new Message.Relay.SNotifyLoginResultMessage(2)); return; } var gameIp = plr.Session.RemoteEndPoint; if (!gameIp.Address.Equals(ip.Address)) { Logger.Error() .Account(message.AccountId, "") .Message("Suspicious login") .Write(); session.SendAsync(new Message.Relay.SNotifyLoginResultMessage(3)); return; } if (plr.Room == null || plr.Room?.Id != message.RoomLocation.RoomId) { Logger.Error() .Account(message.AccountId, "") .Message("Suspicious login(Not in a room/Invalid room id)") .Write(); session.SendAsync(new Message.Relay.SNotifyLoginResultMessage(4)); return; } session.GameSession = plr.Session; plr.RelaySession = session; Logger.Info() .Account(session) .Message("Login success") .Write(); await session.SendAsync(new Message.Relay.SEnterLoginPlayerMessage(plr.RoomInfo.Slot, 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 Message.Relay.SEnterLoginPlayerMessage(plr.RoomInfo.Slot, plr.Account.Id, plr.Account.Nickname)); await session.SendAsync(new Message.Relay.SEnterLoginPlayerMessage(p.RoomInfo.Slot, p.Account.Id, p.Account.Nickname)); } plr.Room.Group.Join(session.HostId); await session.SendAsync(new Message.Relay.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)); }