private void ProcessIncomingPackets() { while (this._isRunning) { var packetCount = this._transport.ReceiveBuffer.Count; while (packetCount-- > 0) { if (this._transport.ReceiveBuffer.BeginRead(out byte[] data, out int offset, out int count)) { using (var stream = new MemoryStream(data, offset, count)) { ClientPacketEnvelope packetEnvelope = default; if (!packetEnvelope.Deserialize(stream, this._packetEncryption)) { this._logger.Verbose("Failed to deserialize packet."); } // Process player packet / input switch (packetEnvelope.Type) { case ClientPacketType.ControlPlane: if (this._transport.ReceiveBuffer.GetEndPoint(out IPEndPoint endPoint)) { this._controlPacketController.Process( packetEnvelope.PlayerId, endPoint, packetEnvelope.ControlPacket); } break; case ClientPacketType.PlayerInput: this._simulationPacketController.Process( packetEnvelope.PlayerId, packetEnvelope.PlayerInputPacket); break; default: #pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation this._logger.Error($"Unknown packet type {packetEnvelope.Type}"); #pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation break; } } this._transport.ReceiveBuffer.EndRead(); } } } }
public void BeginHandshakeSyn(uint sequenceKey) { this._connection.Handshake.SequenceKey = sequenceKey; var synPacket = new ClientPacketEnvelope { Type = ClientPacketType.ControlPlane, PlayerId = this._connection.PlayerId, ControlPacket = new ControlPacket { ControlMessage = ControlMessageEnum.ConnectSyn, ControlSynPacketData = new ControlSynPacketData { SequenceKey = sequenceKey } } }; this._transport.SendPacket(synPacket); this._logger.Info($"Begin SYN handshake: sequenceKey={sequenceKey}"); }