Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi,
I am facing issue while consuming data in event in window application. Data received is not in same sequence as sent from class where event is raised. Here is complete scenario.

There are 2 threads. In one thread, I receive data packets over TCP continuously and put it in array. In other thread, I pick that data from array then process that data and then send it to front end. I am raising event to send data to front end.
Received packet contains numeric sequence number. When event is raised, same sequence number is sent but in front end numbers received are not in same sequence.

Here is complete code. I have replaced Socket related code.

C#
namespace MyConnectionManager
{
    public delegate void DataReceivedHandler(int seqNum);

    public class MyConnection
    {
        public event DataReceivedHandler DataReceived;

        private Thread receiver = null;
        private Thread processor = null;
        private delegate void RecieveDelegate(int seqNum);
        private bool isSocketConnected = false;
        public MyConnection()
        {
        }

        public bool StartServer()
        {
            bool isStarted = false;
            try
            {
                receiver = new Thread(new ThreadStart(EnqueData));
                processor = new Thread(new ThreadStart(ProcessData));

                isSocketConnected = true;

                receiver.Start();
                processor.Start();

                isStarted = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isStarted;
        }

        public bool StopServer()
        {
            bool isStopped = false;
            try
            {
                isSocketConnected = false;

                receiver = null;
                processor = null;

                isStopped = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return isStopped;
        }

        int dummyData = 1;
        private void EnqueData()
        {
            try
            {
                while (dummyData <= 100000)
                {
                    MyEnque(dummyData);
                    dummyData++; 
                }
            }
            catch (Exception ex) 
            { 
                Console.WriteLine("Exception :=> " + ex.Message); 
            }
        }

        private void ProcessData()
        {
            try
            {
                RecieveDelegatercvrdy = new RecieveDelegate(ReceiveReady);
                while (isSocketConnected)
                {
                    int recvd = MyDequeue();
                    if (recvd > 0)
                    {
                        rcvrdy.BeginInvoke(recvd, null, null);
                    }
                }
            }
            catch (Exception ex) 
            { 
                Console.WriteLine("Exception :=> " + ex.Message); 
            }
        }

        private void ReceiveReady(int seqNum)
        {
            try
            {
                if (DataReceived != null)
                    DataReceived(seqNum);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region Circular Array
        private int[] seqQueue = new int[1000000];
        private int readSeq = 0;
        private int writerSeq = 0;
        private int restart = 0;

        private void MyEnque(int Data)
        {
            try
            {
                if (writerSeq >= 999999)
                {
                    writerSeq = 0;
                    restart  = 1;
                }

                seqQueue[writerSeq] = Data;
                writerSeq++;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private int MyDequeue()
        {
            int myOutput = null;
            try
            {
                if (readSeq < writerSeq && lPacketQueue[readSeq] != null)
                {
                    myOutput = seqQueue[readSeq];
                    readSeq++;
                }

                if (readSeq > writerSeq && lRestartArray == 1)
                {
                    if (readSeq >= 999999)
                    {
                        readSeq = 0;
                        restart = 0;
                    }

                    if (lPacketQueue[readSeq] != null)
                    {
                        myOutput = seqQueue[readSeq];
                        readSeq++;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return myOutput;
        }
        #endregion
    }
}

namespace MyMain
{
    class Program
    {
        static MyConnection conn = new MyConnection();

        static void Main(string[] args)
        {
            try
            {
                conn.DataReceived += conn_DataReceived;
                System.Threading.Thread.Sleep(5000);

                conn.StartServer();
                Console.WriteLine("Waiting to finish....");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception :=> " + ex.Message);
            }
        }

        static void conn_DataReceived(int seqNum)
        {
            try
            {
                Console.WriteLine("Received Seq Num :=> {0}", seqNum);

                if(seqNum >= 100000)
                    conn.StopServer();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception :=> " + ex.Message);
            }
        }
    }
}


OUTPUT
Received Seq Num :=> 1
Received Seq Num :=> 2
Received Seq Num :=> 4
Received Seq Num :=> 6
Received Seq Num :=> 3
Received Seq Num :=> 7
Received Seq Num :=> 5
.....
Received Seq Num :=> 100000
Received Seq Num :=> 99956
Received Seq Num :=> 99999
Received Seq Num :=> 99998


Can anybody help me in receiving this data in same sequence as sent by server inside event?

Thanks and Regards,

What I have tried:

Please check the complete code that I have added in main section in question.
Posted
Updated 4-Oct-16 11:11am
v2

As _duDE_ said, if you send data packets asynchronously there is no way to know the order in which they will be received. What you have to do on the receiving end is to reassemble the packets into the correct order. To do this they must be sent with some field that indicates the correct order. This does mean that the receiving end may have to wait for packet 1 after already getting packets 2 and 3. Once you have a section of data in the correct order you can then send that section on to whatever processing you need to do.
 
Share this answer
 
v2
If you send and receive the data asynchronous, there is no guarantee to get this data in the same sequence as sent.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900