Home › Forums › Support › TCP Packet ordering… › Reply To: TCP Packet ordering…
Thank 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.