private void RemoteRead(IAsyncResult ar) { NetworkStream stream = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item1; PacketBuffer buffer = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item2; bool isClient = stream == _clientStream; RC4Cipher cipher = isClient ? _clientReceiveState : _serverReceiveState; bool success = PluginUtils.ProtectedInvoke(() => { if (!stream.CanRead) { return; } int read; try { read = stream.EndRead(ar); } catch { return; } buffer.Advance(read); if (read == 0) { Dispose(); return; } else if (buffer.Index == 4) { // We have the first four bytes, resize the client buffer int len = IPAddress.NetworkToHostOrder( BitConverter.ToInt32(buffer.Bytes, 0)); if (len == 1014001516) //known issue { throw new Exception("Invalid Buffer Size Problem: Try joining a non-proxy server then switching to the proxy server again. More info: https://www.mpgh.net/forum/showthread.php?t=1196439"); } buffer.Resize(len); BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else if (buffer.BytesRemaining() > 0) { // Awaiting the rest of the packet BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else { // We have the full packet cipher.Cipher(buffer.Bytes); Packet packet = Packet.Create(buffer.Bytes); if (isClient) { _proxy.FireClientPacket(this, packet); } else { _proxy.FireServerPacket(this, packet); } if (packet.Send) { Send(packet, !isClient); } buffer.Reset(); BeginRead(0, 4, isClient); } }, "RemoteRead (isClient = " + isClient + ")", typeof(IOException)); if (!success) { Dispose(); } }
private void RemoteRead(IAsyncResult ar) { NetworkStream stream = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item1; PacketBuffer buffer = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item2; bool isClient = stream == _clientStream; RC4Cipher cipher = isClient ? _clientReceiveState : _serverReceiveState; bool success = PluginUtils.ProtectedInvoke(() => { if (!stream.CanRead) { return; } int read; try { read = stream.EndRead(ar); } catch { return; } buffer.Advance(read); if (read == 0) { Dispose(); return; } else if (buffer.Index == 4) { // We have the first four bytes, resize the client buffer buffer.Resize(IPAddress.NetworkToHostOrder( BitConverter.ToInt32(buffer.Bytes, 0))); BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else if (buffer.BytesRemaining() > 0) { // Awaiting the rest of the packet BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else { // We have the full packet cipher.Cipher(buffer.Bytes); byte[] temp = (byte[])buffer.Bytes.Clone(); if (_proxy.IsHooked((PacketType)buffer.Bytes[4])) { temp = null; //If a packet isn't hooked then just send the raw data (no packet processing). Helps with broken/missing packet types. } Packet packet = Packet.Create(buffer.Bytes); if (isClient) { _proxy.FireClientPacket(this, packet); } else { _proxy.FireServerPacket(this, packet); } if (packet.Send) { Send(packet, !isClient, temp); } buffer.Reset(); BeginRead(0, 4, isClient); } }, "RemoteRead (isClient = " + isClient + ")", typeof(IOException)); if (!success) { Dispose(); } }
private void RemoteRead(IAsyncResult ar) { NetworkStream stream = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item1; PacketBuffer buffer = (ar.AsyncState as Tuple <NetworkStream, PacketBuffer>).Item2; bool isClient = stream == _clientStream; RC4Cipher cipher = isClient ? _clientReceiveState : _serverReceiveState; bool success = PluginUtils.ProtectedInvoke(() => { if (!stream.CanRead) { return; } int read; try { read = stream.EndRead(ar); } catch { return; } buffer.Advance(read); if (read == 0) { Dispose(); return; } else if (buffer.Index == 4) { buffer.Resize(IPAddress.NetworkToHostOrder( BitConverter.ToInt32(buffer.Bytes, 0))); BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else if (buffer.BytesRemaining() > 0) { BeginRead(buffer.Index, buffer.BytesRemaining(), isClient); } else { cipher.Cipher(buffer.Bytes); Packet packet = Packet.Create(buffer.Bytes); if (isClient) { _proxy.FireClientPacket(this, packet); } else { _proxy.FireServerPacket(this, packet); } if (packet.Send) { Send(packet, !isClient); } buffer.Reset(); BeginRead(0, 4, isClient); } }, "RemoteRead (isClient = " + isClient + ")", typeof(IOException)); if (!success) { Dispose(); } }