Example #1
0
        protected void ReceivedDataSplit(object state)
        {
            while (this.isConnected)
            {
                byte[] receivedBytes = null;
                lock (SyncObject)
                {
                    try
                    {
                        if (receivedRingBuffer.DataCount < 3)
                        {
                            Monitor.Wait(SyncObject);
                            continue;
                        }
                        if (receivedRingBuffer[0] != 0xAA)
                        {
                            receivedRingBuffer.Clear(1);
                            continue;
                        }

                        int len = DataConvert.Bytes_To_Ushort(new byte[2] {
                            receivedRingBuffer[1], receivedRingBuffer[2]
                        }) + 5;
                        if (receivedRingBuffer.DataCount < len)
                        {
                            Monitor.Wait(SyncObject);
                            continue;
                        }
                        if (receivedRingBuffer[len - 1] != 0xBB)
                        {
                            receivedRingBuffer.Clear(1);
                            continue;
                        }
                        receivedBytes = new byte[len];
                        receivedRingBuffer.ReadFromRingBuffer(receivedBytes, 0, len);
                        receivedRingBuffer.Clear(len);
                        Monitor.Pulse(SyncObject);
                    }
                    catch { }
                }
                if (receivedBytes != null)
                {
                    Msg.MessageObject msg = new Msg.MessageObject(receivedBytes);
                    if (msg.CheckData())
                    {
                        CallDelegateReceived(msg);
                    }
                }
            }
        }