internal void Send(Packet.PacketTypeC2S type, object payloadData) { var packetC2S = new PacketC2S(type, payloadData); _logger.LogTrace($"Sending packet: {packetC2S}"); var payload = Encoding.Unicode.GetBytes(JsonNet.Serialize(packetC2S)); using (var memStream = new MemoryStream()) { var writer = new BinaryWriter(memStream); writer.Write(payload.Length); writer.Write(payload); _easySocket.SendAsync(memStream.ToArray()); } }
private void HandlePacket(PacketC2S packet) { if (!Authorized && packet.Type != Packet.PacketTypeC2S.Authorize) { _logger.LogDebug($"{this} tried to send unauthorized packets!"); IllegalAction("Please authorize first!"); return; } _logger.LogDebug($"{this} sent packet: {packet.Type}"); switch ((Packet.PacketTypeC2S)packet.TypeNum) { case Packet.PacketTypeC2S.Authorize: lock (_authLock) { if (Authorized) { IllegalAction("Already authorized"); return; } var authorize = packet.GetPayload <Authorize>(); Login = authorize.Identifier; Username = authorize.Name; FullGameState = authorize.FullGameState; if (Username.Length > _rules.MaxUsernameLength) { IllegalAction("Are you Daenerys Targaryen? No? Then you can't possibly have a name that long. (Exceeded name length limitation)"); return; } if (Login == null || Login.ToByteArray().Distinct().Count() <= 1) { IllegalAction("Invalid login. The login is supposed to be a GUID of your choice (choose any, but keep that one!). It needs to be in a notation that can be parsed by this: https://msdn.microsoft.com/en-us/library/system.guid.parse(v=vs.110).aspx"); return; } if (string.IsNullOrWhiteSpace(Username)) { IllegalAction("Now, don't get me wrong, I really like your name. Reminds me of 'No Game No Life'. However, the spectator client will be really sad if he can't render anything, so please be a little more creative and come back with something not-empty!"); return; } Identifier = _server.GenerateUniqueUserIdentifier(); Authorized = true; IServerUser existingConnection; if (_server.WouldAuthCollide(Login, this, out existingConnection)) { _logger.LogInformation($"User relogging: {this} -> {existingConnection}"); if (existingConnection.Authorized && existingConnection.Connected && !Address.Equals(existingConnection.Address)) { _logger.LogWarning($"Possible account sharing: {this} and {existingConnection}"); } existingConnection.Inherit(this, _socket.Socket); Authorized = false; _server.Kick(this); _server.HandleUserReconnect(existingConnection); return; } Send(Packet.PacketTypeS2C.AuthResult, new AuthCompleted(_server.GetMotd(), _server.GetGameModes())); } break; case Packet.PacketTypeC2S.GameAction: _server.HandleUserAction(this, packet.GetPayload <LiveGameAction>()); break; case Packet.PacketTypeC2S.SwitchQueueMode: IsBot = !IsBot; _logger.LogInformation($"{this} is now a passive bot: {IsBot}"); break; case Packet.PacketTypeC2S.EnterQueue: _server.HandleUserEnterQueue(this, packet.GetPayload <QueueAction>()); break; case Packet.PacketTypeC2S.LeaveQueue: _server.HandleUserLeaveQueue(this, packet.GetPayload <QueueAction>()); break; case Packet.PacketTypeC2S.TurnEnd: _server.HandleUserEndTurn(this, packet.GetPayload <long>()); break; case Packet.PacketTypeC2S.ForceResync: _server.HandleUserResync(this, packet.GetPayload <long>()); break; default: _logger.LogWarning($"Authorized user sent invalid packet type: {packet.TypeNum} from: {this}"); IllegalAction("Unknown packet type"); break; } }