/// <summary> /// Reads the next incoming packet. /// </summary> /// <returns></returns> public Packet ReadPacket( ) { try { byte id = mReader.ReadByte(); Packet p = Packet.GetPacket(( PacketID )id); if (p is Packets.PacketInvalid) { throw new IOException("Received unknown packet"); } int len = PacketData.GetLength(remote.NetworkStream, remote.PacketOptions); byte[] data = new byte[len]; PacketData pData = new PacketData(data, remote.PacketOptions); p.ReadPacket(pData); return(p); } catch { return(null); } }
/// <summary> /// Starts the thread for writing. /// </summary> public void StartWrite() { new Thread(new ThreadStart(() => { while (Remote.CanProcessPackets) { if (_packetQueue.Count > 0) { var packet = _packetQueue.Dequeue(); byte[] data = packet.WritePacket().ReadAll(); byte[] lenData = PacketData.GetLength(data.Length, Remote.PacketOptions); Remote.NetworkStream.Write(lenData, 0, lenData.Length); Remote.NetworkStream.Write(data, 0, data.Length); } else { Thread.Sleep(5); } } })).Start(); }