Sending Order

Jun 7, 2013 at 10:40 PM
Edited Jun 7, 2013 at 10:43 PM
Hi kerryjiang

Congratulations on an amazing framework! I have a lot of respect for your work.

I tried to understand the code for sending, but the queuing code is complex and I am not confident.
public class MySession : AppSession<MySession, BinaryRequestInfo>
{
    private readonly _sendLock = new object();
    
    internal void MySend(ArraySegment<byte> data)
    {
        lock (_sendLock)
        {
            Send(data);
        }
    }
}
If MySend is the only way I call Send, is it guaranteed that the segments will be sent in the order that Send is called?

If not, do I need to set Config.SendSync = true?

Thank you very much.
Coordinator
Jun 8, 2013 at 4:37 AM
Needn't do it. The queue is thread safe, and first queued data must be sent first.
Jun 8, 2013 at 5:01 AM
Edited Jun 8, 2013 at 5:06 AM
1) But are the items always successfully queued in the order that Send(ArraySegment<byte>) is called? I ask this because I have noticed that there is a spin wait which may occur while the data is attempting to queue.
private void InternalSend(ArraySegment<byte> segment)
{
    if (!m_Connected)
        return;

    //data queued in here
    if (InternalTrySend(segment))
        return;

    var spinWait = new SpinWait();

    while (m_Connected)
    {
        spinWait.SpinOnce();

        if (InternalTrySend(segment))
            return;
    }
}
Isn't it possible that different requesting threads will enter this context, and maybe succeed in queuing in a different order?

2) Also, if the segment does not send immediately, and more requests come, does the queue ever change order? I have noticed that somtimes the queue itself is replaced.
Coordinator
Jun 19, 2013 at 4:51 PM
Edited Jun 19, 2013 at 4:54 PM
If there are two pieces of data coming from two threads, anyway you cannot ensure the order because you cannot control the CPU scheduler.

If you want to keep the original order of many pieces of data, put your ordered data into a list and then send the list directly:
session.Send(list);