internal static Packet Instantiate(byte[] source, PacketDefinitionRegistry packetRegistry = null) { packetRegistry = packetRegistry ?? PacketDefinitionRegistryFactory.CreateClassicClient(); var processingStream = new MemoryStream(source); var received = new byte[source.Length]; var packetReader = new StreamPacketReader(processingStream, received); int packetId = packetReader.ReadByte(); if ((packetId < 0) || (packetId > 255)) { throw new EndOfStreamException(); } var packetDefinition = packetRegistry.Find(packetId); var packetSize = packetDefinition.GetSize(packetReader); packetReader.ReadBytes(packetSize - packetReader.Position); var payload = new byte[packetSize]; Array.Copy(received, 0, payload, 0, packetSize); var packet = new Packet(packetId, payload); return(packet); }
public Packet Receive(Stream processingStream) { var packetReader = new StreamPacketReader(processingStream, receiveBuffer); int packetId = packetReader.ReadByte(); if ((packetId < 0) || (packetId > 255)) { throw new EndOfStreamException(); } diagnosticPullStream.StartPacket(); var packetDefinition = packetRegistry.Find(packetId); var packetSize = packetDefinition.GetSize(packetReader); packetReader.ReadBytes(packetSize - packetReader.Position); var payload = new byte[packetSize]; Array.Copy(receiveBuffer, 0, payload, 0, packetSize); var packet = new Packet(packetId, payload); if (packetId == PacketDefinitions.ConnectToGameServer.Id) { Status = ServerConnectionStatus.PreGame; } diagnosticPullStream.FinishPacket(packet); return(packet); }
private static int GetPacketLength(StreamPacketReader reader, int packetId) { PacketDefinition packetDefinition; if (PacketDefinitionRegistry.TryFind(packetId, out packetDefinition)) { return(packetDefinition.GetSize(reader)); } throw new NotImplementedException($"Unknown packet type {packetId:X2}"); }
public static Packet ParsePacket(IPullStream inputStream) { byte[] buffer = new byte[65535]; var reader = new StreamPacketReader(new PullStreamToStreamAdapter(inputStream), buffer); int packetId = reader.ReadByte(); int packetLength = GetPacketLength(reader, packetId); reader.ReadBytes(packetLength - reader.Position); var payload = new byte[packetLength]; Array.Copy(buffer, 0, payload, 0, packetLength); return(new Packet(packetId, payload)); }
public LoginEncryptionDetectionResult Detect(uint seed, IPullStream inputStream, Version defaultVersion) { var reader = new StreamPacketReader(new PullStreamToStreamAdapter(inputStream), rawBuffer); var length = 0; while (inputStream.DataAvailable && length < 62) { rawBuffer[length++] = reader.ReadByte(); } rawBuffer.CopyTo(decryptedBuffer, 0); var versionsToTest = GetVersionsToTest(defaultVersion).GetEnumerator(); LoginEncryptionKey?currentKey = null; LoginCrypt loginCrypt = null; while (!IsGameServerPacket(decryptedBuffer)) { if (!versionsToTest.MoveNext()) { break; } var version = versionsToTest.Current; currentKey = LoginEncryptionKey.FromVersion(version); loginCrypt = new LoginCrypt(seed, currentKey.Value); loginCrypt.Decrypt(rawBuffer, decryptedBuffer, 62); } if (currentKey.HasValue) { return(new LoginEncryptionDetectionResult(decryptedBuffer, loginCrypt, currentKey.Value)); } else { return(new LoginEncryptionDetectionResult(decryptedBuffer)); } }
public Packet ParsePacket(IPullStream inputStream) { byte[] buffer = new byte[65535]; var reader = new StreamPacketReader(new PullStreamToStreamAdapter(inputStream), buffer); int packetId = reader.ReadByte(); try { int packetLength = GetPacketLength(reader, packetId); reader.ReadBytes(packetLength - reader.Position); var payload = new byte[packetLength]; Array.Copy(buffer, 0, payload, 0, packetLength); return(new Packet(packetId, payload)); } catch (Exception ex) { throw new EndOfStreamException($"EndOfStreamException occured when reading packet {packetId:X2}", ex); } }