private void ReadPacket() { if (BytesToSkip > 0) { int skipped = Math.Min(BytesToSkip, (int)Memory.Length); Memory.Position += skipped; BytesToSkip -= skipped; } while (Memory.Length - Memory.Position > 2) { // Commands are always the first two bytes // Followed by either the packet data in case the packet // has its size fixed, or the packet length var tmp = new byte[2]; Memory.Read(tmp, 0, 2); ushort cmd = BitConverter.ToUInt16(tmp, 0); if (!RegisteredPackets.ContainsKey(cmd)) { // We gotta break because we don't know the size of the packet Debug.LogWarning($"Received Unknown Command: {string.Format("0x{0:x4}", cmd)}\nProbably: {(PacketHeader)cmd}"); Memory.Position -= 2; break; } else { int size = RegisteredPackets[cmd].Size; bool isFixed = true; if (size <= 0) { isFixed = false; // Do we have more than two bytes left? if (Memory.Length - Memory.Position > 2) { Memory.Read(tmp, 0, 2); size = BitConverter.ToUInt16(tmp, 0); } else { Memory.Position -= 4; break; } } // Read skipping command and length byte[] data = new byte[size]; Memory.Read(data, 0, size - (isFixed ? 2 : 4)); ConstructorInfo ci = RegisteredPackets[cmd].Type.GetConstructor(new Type[] { }); InPacket packet = (InPacket)ci.Invoke(null); using (var br = new BinaryReader(data)) { var shouldContinue = packet.Read(br); ThreadManager.ExecuteOnMainThread(() => { if (PacketHooks.ContainsKey(cmd)) { PacketHooks[cmd].DynamicInvoke(cmd, size, packet); } else { Debug.LogWarning($"Received Unhadled Command {(PacketHeader)cmd}"); } PacketReceived?.Invoke(cmd, size, packet); }); if (!shouldContinue) { break; } } } } if (Memory.Length - Memory.Position > 0) { MemoryStream ms = new MemoryStream(); ms.Write(Memory.GetBuffer(), (int)Memory.Position, (int)Memory.Length - (int)Memory.Position); Memory.Dispose(); Memory = ms; } }
private void TryReadPackets() { if (bytesToSkip > 0) { int skipped = Math.Min(bytesToSkip, (int)memory.Length); memory.Position += skipped; bytesToSkip -= skipped; } while (memory.Length - memory.Position > 2) { byte[] tmp = new byte[2]; memory.Read(tmp, 0, 2); ushort cmd = BitConverter.ToUInt16(tmp, 0); if (!packetSize.ContainsKey(cmd)) { if (InvalidPacket != null) { InvalidPacket(); } memory.Position -= 2; break; } else { int size = packetSize[cmd].Size; bool isFixed = true; if (size <= 0) { isFixed = false; if (memory.Length - memory.Position > 2) { memory.Read(tmp, 0, 2); size = BitConverter.ToUInt16(tmp, 0); } else { memory.Position -= 4; break; } } byte[] data = new byte[size]; memory.Read(data, 0, size - (isFixed ? 2 : 4)); ConstructorInfo ci = packetSize[cmd].Type.GetConstructor(new Type[] { }); InPacket p = (InPacket)ci.Invoke(null); Debug.Print("Packet " + p.ToString()); if (!p.Read(data)) { if (InvalidPacket != null) { InvalidPacket(); } break; } if (packetHooks.ContainsKey(cmd)) { packetHooks[cmd].DynamicInvoke(cmd, size, p); } if (PacketReceived != null) { PacketReceived(cmd, size, p); } } } if (memory.Length - memory.Position > 0) { MemoryStream ms = new MemoryStream(); ms.Write(memory.GetBuffer(), (int)memory.Position, (int)memory.Length - (int)memory.Position); memory.Dispose(); memory = ms; } }
private void ReadPacket() { if (BytesToSkip > 0) { int skipped = Math.Min(BytesToSkip, (int)Memory.Length); Memory.Position += skipped; BytesToSkip -= skipped; } while (Memory.Length - Memory.Position > 2) { var tmp = new byte[2]; Memory.Read(tmp, 0, 2); ushort cmd = BitConverter.ToUInt16(tmp, 0); if (!PacketSize.ContainsKey(cmd)) { Debug.LogWarning($"Received Unknown Command: {cmd}"); Memory.Position -= 2; break; } else { int size = PacketSize[cmd].Size; bool isFixed = true; if (size <= 0) { isFixed = false; if (Memory.Length - Memory.Position > 2) { Memory.Read(tmp, 0, 2); size = BitConverter.ToUInt16(tmp, 0); } else { Memory.Position -= 4; break; } } byte[] data = new byte[size]; Memory.Read(data, 0, size - (isFixed ? 2 : 4)); ConstructorInfo ci = PacketSize[cmd].Type.GetConstructor(new Type[] { }); InPacket packet = (InPacket)ci.Invoke(null); if (!packet.Read(data)) { break; } ThreadManager.ExecuteOnMainThread(() => { if (PacketHooks.ContainsKey(cmd)) { PacketHooks[cmd].DynamicInvoke(cmd, size, packet); } else { Debug.LogWarning($"Received Unhadled Command {(PacketHeader) cmd}"); } PacketReceived?.Invoke(cmd, size, packet); }); } } if (Memory.Length - Memory.Position > 0) { MemoryStream ms = new MemoryStream(); ms.Write(Memory.GetBuffer(), (int)Memory.Position, (int)Memory.Length - (int)Memory.Position); Memory.Dispose(); Memory = ms; } }