- This topic has 2 replies, 2 voices, and was last updated 10 years, 10 months ago by Anonymous.
-
AuthorPosts
-
December 21, 2013 at 18:11 #1349AnonymousInactive
Hello, All. Sorry for my bad english in advance.
I’m writing a network card game using NetworkComms.net and I have one big problem. Is there any way to execute a callback (when a packet is received) in the order, corresponding to the packet order?
Example:
1. Server sends three packets: eg. packet1 packet2 packet3
2. Client receives and handle thoose packets, but the order is eg. packet3 packet1 packet2Is there any way to handle thoose packets in the correct order?
Thank you.December 23, 2013 at 11:38 #1350AnonymousInactiveHeya JustOzz,
Many thanks for your interest in our library and welcome to our forums. What you want to achieve is possible. By default incoming packets are added to NetworkComms.CommsThreadPool and then executed as soon as possible.
If packets are sent in the correct order, using a single connection, by the client, any reordering in the execution can only occur in NetworkComms.CommsThreadPool. So your first option, and also the simplest is to only allow a single thread in the thread pool:
NetworkComms.CommsThreadPool = new CommsThreadPool(1, 1, 1, new TimeSpan(0, 0, 15));
Your second option is to set the receive handle priority for specific packet types to QueueItemPriority.Highest, please see the Data Prioritisation section of this tutorial for more, which will execute the incoming packet handler immediately as it is received., which guarantees correct ordering. This is a little trickier to setup but is the best option.
If you have any further problem please post back.
Regards,
MarcDecember 23, 2013 at 16:30 #1351AnonymousInactiveThank you so much for your answer. I necessarily use your answer in my next projects. But for now, I wrote packet queuing byt myself. There is my solution if your interested:
1. When receive handler executing, I’m using
PacketHeader.GetOption(PacketHeaderLongItems.PacketSequenceNumber)
2. If this packet number is greater then the last number more than 1 (first packet number is 2, because, I think, that the real first packet is “NULL” packet), this packet is queuing and executing later.Packet queuing:
private Dictionary<long, int[]> queuedData; lock (queuedData) { queuedData.Add(packetNo, data); WriteLog(String.Format("Received packet No {0}. Data: {1} {2} {3}. Queued.", packetNo, data[0].ToString("X8"), data[1].ToString("X8"), data[2].ToString("X8"))); }
3. Queued data is processing by the special thread, running in background all the time, while connection is alive. Queued data is processing 1 time in 100 millisecond. Here the code:
void FindAndProcessQueuedPackets() { try { while (true) { bool isDone = false; while (!isDone) { bool isFound = false; for (int i = 0; i < queuedData.Count; i++) { lock (queuedData) { if (queuedData.Keys.ElementAt(i) == lastPacketNo + 1) { isFound = true; int[] queuedPacketData = queuedData.Values.ElementAt(i); WriteLog(String.Format("Processing queued packet No {0}. Data: {1} {2} {3}.", queuedData.Keys.ElementAt(i), queuedPacketData[0].ToString("X8"), queuedPacketData[1].ToString("X8"), queuedPacketData[2].ToString("X8"))); NETWORK_MESSAGE queuedMessage = (NETWORK_MESSAGE)queuedPacketData[0]; int queuedParameter1 = queuedPacketData[1]; int queuedParameter2 = queuedPacketData[2]; switch (queuedMessage) { case NETWORK_MESSAGE.HELLO: { if (isServer) SendMessage(NETWORK_MESSAGE.HELLO); break; } } queuedData.Remove(queuedData.Keys.ElementAt(i)); lastPacketNo++; MessageReceivedEvent(this, new NetworkMessageEventArgs(queuedMessage, queuedParameter1, queuedParameter2)); } } } if (!isFound) isDone = true; } Thread.Sleep(100); } } catch (ThreadAbortException ex) { ; } }
And, thank you so much for this library. It’s awesome! ) It’s very useful and easy to include in the projects. Please, don’t stop in development of this library. Thank you, and sorry for my bad english.
-
AuthorPosts
- You must be logged in to reply to this topic.