private void ProcessQueue() { Console.Out.WriteLine("Starting processing thread!"); while (isRunning) { if (buggedPackets.Count != 0 && buggedPackets.Count % 100 == 0) { Console.Out.WriteLine("Bugged packets is now " + buggedPackets.Count + ". Enjoy your exception"); int crash = Int32.Parse("LOL"); } while (receivedPackets.Count > 0 && receivedPackets.First != null) { if (onProcessPacket == null) { Console.Out.WriteLine("There are packets in the queue, but noone is listening to them!!"); } else { //if (receivedPackets.First == null) continue; try { // Process the packets Packet[] receivedPacketArray = this.ConstructPacket(receivedPackets.First.Value.packet); for (int i = 0; i < receivedPacketArray.Length; i++) { Packet receivedPacket = receivedPacketArray[i]; // Console.Out.WriteLine("Processing packet " + receivedPacket.GetHeader()); onProcessPacket(receivedPacket); if (receivedPackets.First.Value.client.confirmPackets) { if (receivedPacket.GetHeader() != Headers.PACKET_RECEIVED) { //Console.Out.WriteLine("Confirming packet with header " + receivedPacket.GetHeader() + // " ID " + receivedPacket.GetPacketID()); // Send a confirmation packet, that the packet is processed and received Packet confirmPacket = new Packet(Headers.PACKET_RECEIVED); confirmPacket.SetPacketID(receivedPacket.GetPacketID()); receivedPackets.First.Value.client.SendPacket(confirmPacket); } else { ConfirmPacket(receivedPacket.GetPacketID()); } } } receivedPackets.RemoveFirst(); } catch (Exception e) { Console.WriteLine("Nullpointer in PacketProcessor D=. Removing first to buggedPackets"); if (receivedPackets.First == null) { Console.WriteLine("First is null"); } else { buggedPackets.AddLast(receivedPackets.First.Value); receivedPackets.RemoveFirst(); } // Stop for now, try again later break; } } } lock (syncSentPackets) { for (int i = 0; i < this.sentPackets.Count; i++) { PacketSenderPair pair = this.sentPackets.ElementAt(i); if (pair.client.confirmPackets) { double now = (new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds); if (now - pair.packet.timeSent > packetResendTimeoutMS) { Console.Out.WriteLine("Resending packet with ID " + pair.packet.GetPacketID() + " and header " + ((int)pair.packet.GetHeader()).ToString("x") /* + // ".. ( " + now + " - " + pair.packet.timeSent + " )"*/); this.sentPackets.Remove(pair); pair.client.SendPacket(pair.packet); i--; } } } } Thread.Sleep(10); } Console.Out.WriteLine("Ending processing thread!"); isRunning = false; }
/// <summary> /// Constructs a packet from a full byte array. /// </summary> /// <param name="fullData">The data as received by the socket.</param> /// <returns>The packet, including header, packet ID, and the data.</returns> private Packet[] ConstructPacket(byte[] fullData) { String stringData = this.BytesToString(fullData); String[] split = stringData.Split((char)SocketClient.END_OF_PACKET); Packet[] packets = new Packet[split.Length - 1]; if (split.Length > 2) Console.Out.WriteLine("Found a double packet! (full data: \n'" + stringData + "')"); for (int i = 0; i < split.Length - 1; i++) { if (split[i].Contains((char)SocketClient.END_OF_PACKET)) { Console.Out.WriteLine("Packet contained end of packet char!"); } // Extra catch for empty packets if (split[i].Length < 2) continue; byte[] currentPacketData = this.StringToBytes(split[i]); byte[] packetID = new byte[4]; for (int j = 1; j < 5; j++) { packetID[j - 1] = currentPacketData[j]; } byte[] headerlessData = new byte[currentPacketData.Length - 5]; for (int j = 5; j < currentPacketData.Length; j++) { headerlessData[j - 5] = currentPacketData[j]; } Packet p = new Packet(currentPacketData[0], headerlessData); p.SetPacketID(packetID); packets[i] = p; } return packets; }