/// <summary> /// This is not a perfect implementation for a udp buffer given /// that it relies on the packets being transferred in order. /// Very wrong, but at the time of writing and looking at the cod /// protocol I cannot see a way of application level ordering /// or I lack the experience with UDP (none - first time using it) /// to know of another way to reorder the packets without /// any sequence ids or timestamps. /// </summary> /// <param name="packet"></param> protected override void OnPacketReceived(Packet packet) { CallOfDutyPacket callOfDutyPacket = packet as CallOfDutyPacket; if (callOfDutyPacket != null) { if (callOfDutyPacket.IsEOP == true && this.recvPacketBuffer == null) { this.packetQueue.Dequeue(); base.OnPacketReceived(packet); } else if (callOfDutyPacket.IsEOP == true && this.recvPacketBuffer != null) { if (this.packetQueue.Count > 0) { this.packetQueue.Dequeue(); } base.OnPacketReceived(recvPacketBuffer.Combine(callOfDutyPacket)); this.recvPacketBuffer = null; } else if (callOfDutyPacket.IsEOP == false && this.recvPacketBuffer == null) { this.recvPacketBuffer = callOfDutyPacket; } else if (callOfDutyPacket.IsEOP == false && this.recvPacketBuffer != null) { this.recvPacketBuffer = this.recvPacketBuffer.Combine(callOfDutyPacket); } // else - the hell happened? } }
public CallOfDutyPacket Combine(CallOfDutyPacket b) { if (String.Compare(this.Command, b.Command, true) == 0) { this.Message = this.Message + b.Message; this.IsEOP = b.IsEOP; } return this; }
public CallOfDutyClient(string hostname, ushort port) : base(hostname, port) { this.PacketSerializer = new CallOfDutyPacketSerializer(); this.packetQueue = new Queue<CallOfDutyPacket>(); this.recvPacketBuffer = null; this.packetRecheck.Elapsed += new ElapsedEventHandler(packetRecheck_Elapsed); }
/// <summary> /// Deserializes an array of bytes into a Packet of type P /// </summary> /// <param name="packetData">The array to deserialize to a packet. Must be exact length of bytes.</param> /// <returns>A new packet with data extracted from packetDate</returns> public Packet Deserialize(byte[] packetData) { CallOfDutyPacket packet = new CallOfDutyPacket() { Type = PacketType.Request, Origin = PacketOrigin.Client }; string[] packetStrings = Encoding.ASCII.GetString(packetData, 5, packetData.Length - 5).Split(new char[] { '\n' }, 2); if (packetStrings.Length == 2) { packet.Command = packetStrings[0]; packet.Message = packetStrings[1]; packet.IsEOP = (packet.Message.Length == 0 || (packet.Message.Length >= 2 && packet.Message[packet.Message.Length - 1] == '\n' && packet.Message[packet.Message.Length - 2] == '\n')); } return packet; }