예제 #1
0
        void cI_OnMessageReceived(short ver, short msg_type, int msg_id, byte[] msg_data)
        {
            if ((msg_type == 100) || (msg_type == this.msg_type && msg_id == this.msg_id))
            {
                Array.Copy(msg_data, this.data, msg_data.Length);
                this.msg_type = msg_type;
                this.msg_id   = msg_id;
                this.version  = ver;
                transactEvt.Set();
            }

            if (OnMessageReceived != null)
            {
                delegateMessageReceived dmr = new delegateMessageReceived(triggerMessageReceived);
                dmr.BeginInvoke(ver, msg_type, msg_id, msg_data, null, null);
            }

            lock (this)
            {
                if (b_enqueue)
                {
                    raw_message_queue.Enqueue(new RAW_Message(ver, msg_type, msg_id, msg_data));
                    b_enqueue = false;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Data process
        /// </summary>
        /// <param name="ar"></param>
        private void OnDataRead(IAsyncResult ar)
        {
            int offset = 0;
            AsynReadState ss = (AsynReadState)ar.AsyncState;

            //Detect client disconnection. then start anther acception
            if (ss.data[0] == 0)
            {
                server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTCPClientCallBack), server);
                return;
            }

            try
            {
            REPEAT:                             //if multiple messages exist in the result. repeat the process
                if (new_message)                //new_message is a flag to indicate if the data is part of unfinished message
                {
                    //Calculate message type, version, length and id
                    int header = (ss.data[offset] << 8) + ss.data[offset + 1];
                    msg_type = (Int16)(header & 0x03FF);
                    msg_ver = (Int16)((header >> 10) & 0x07);
                    msg_len = (ss.data[offset + 2] << 24) + (ss.data[offset + 3] << 16) + (ss.data[offset + 4] << 8) + ss.data[offset + 5];
                    msg_id = (ss.data[offset + 6] << 24) + (ss.data[offset + 7] << 16) + (ss.data[offset + 8] << 8) + ss.data[offset + 9];

                    //if the message length is 0. the rest of byte array are empty. restart non-block reading
                    if (msg_len == 0)
                    {
                        if (ns != null)
                        {
                            try
                            {
                                ns.Flush();
                                state = new AsynReadState(BUFFER_SIZE);
                                ns.BeginRead(state.data, 0, state.data.Length, new AsyncCallback(OnDataRead), state);
                            }
                            catch { }

                            return;
                        }
                        else
                            return;
                    }

                    msg_data = new byte[msg_len];
                    //if message length greater than the calcualted message length. copy message and trigger message event
                    if (ss.data.Length >= msg_len)
                    {
                        Array.Copy(ss.data, offset, msg_data, 0, msg_len);

                        delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                        msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                        offset += msg_len;
                        msg_len = 0;

                        goto REPEAT;
                    }
                    else//If the received data is shorter than the message length, keep reading for the next data 
                    {
                        new_message = false;
                        Array.Copy(ss.data, 0, msg_data, 0, ss.data.Length);
                        msg_cursor += ss.data.Length;
                    }
                }
                else
                {
                    //if data length larger than needed data for a complete message, 
                    //copy data into existing message and triggered message event 
                    if (ss.data.Length >= msg_len - msg_cursor)
                    {
                        Array.Copy(ss.data, 0, msg_data, msg_cursor, msg_len - msg_cursor);
                        delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                        msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                        offset += msg_len - msg_cursor;

                        

                        msg_len = 0;
                        msg_cursor = 0;

                        new_message = true;
                        goto REPEAT;
                    }
                    else //keep reading
                    {
                        new_message = false;
                        Array.Copy(ss.data, 0, msg_data, msg_cursor, ss.data.Length);
                        msg_cursor += ss.data.Length;
                    }
                }

                //if ns !=null, do next asyn-read, to ensure that read
                if (ns != null && ns.CanRead)
                {
                    try
                    {
                        ns.Flush();
                        state = new AsynReadState(BUFFER_SIZE);
                        ns.BeginRead(state.data, 0, state.data.Length, new AsyncCallback(OnDataRead), state);
                    }
                    catch { }
                }
            }
            catch
            {
            }
        }
예제 #3
0
        /// <summary>
        /// Asyn read result process
        /// </summary>
        /// <param name="ar"></param>
        private void OnDataRead(IAsyncResult ar)
        {
            int offset = 0;                     //used to keep the start position of a LLRP message in 
                                                //byte array returned from the read
            AsynReadState ss = (AsynReadState)ar.AsyncState;    //used to keep data

            lock (syn_msg)
            {
                try
                {
                REPEAT:
                    if (new_message)                //new_message is a flag to indicate if the data is part of unfinished message
                    {
                        msg_cursor = 0;
                        int reserved_date_len = ss.data.Length - offset;

                        if (reserved_date_len > 10)
                        {

                            //Calculate message type, version, length and id
                            int header = (ss.data[offset] << 8) + ss.data[offset + 1];
                            try
                            {
                                msg_type = (Int16)(header & 0x03FF);
                                msg_ver = (Int16)((header >> 10) & 0x07);
                                msg_len = (ss.data[offset + 2] << 24) + (ss.data[offset + 3] << 16) + (ss.data[offset + 4] << 8) + ss.data[offset + 5];
                                msg_id = (ss.data[offset + 6] << 24) + (ss.data[offset + 7] << 16) + (ss.data[offset + 8] << 8) + ss.data[offset + 9];
                            }
                            catch
                            {
                                msg_len = 0;
                            }

                            if (msg_len > 20000)
                            {
                                int i = 0;
                            }

                            //if data length larger than needed data for a complete message, 
                            //copy data into existing message and triggered message event 
                            if (msg_len > 0 && msg_ver == 1)
                            {

                                msg_data = new byte[msg_len];
                                //if message length greater than the calcualted message length. copy message and trigger message event

                                if (ss.data.Length >= (offset + msg_len))
                                {
                                    Array.Copy(ss.data, offset, msg_data, 0, msg_len);
                                    delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                                    msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                                    offset += msg_len;

                                    new_message = true;

                                    goto REPEAT;
                                }
                                else//If the received data is shorter than the message length, keep reading for the next data 
                                {
                                    new_message = false;

                                    Array.Copy(ss.data, offset, msg_data, 0, ss.data.Length - offset);
                                    msg_cursor = ss.data.Length - offset;

                                }
                            }
                        }
                        else
                        {
                            new_message = true;

                            //if ns !=null, do next asyn-read, to ensure that read
                            if (ns != null && ns.CanRead)
                            {
                                try
                                {
                                    ns.Flush();
                                    state = new AsynReadState(BUFFER_SIZE);

                                    Array.Copy(ss.data, offset, state.data, 0, reserved_date_len);

                                    if (!trying_to_close) ns.BeginRead(state.data, reserved_date_len, BUFFER_SIZE-reserved_date_len, new AsyncCallback(OnDataRead), state);
                                }
                                catch { }
                            }

                            return;
                        }
                    }
                    else
                    {
                        //if data length larger than needed data for a complete message, 
                        //copy data into existing message and triggered message event 
                        if (ss.data.Length >= msg_len - msg_cursor)
                        {
                            Array.Copy(ss.data, 0, msg_data, msg_cursor, msg_len - msg_cursor);
                            delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                            msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                            offset += msg_len - msg_cursor;

                            new_message = true;

                            goto REPEAT;
                        }
                        else //keep reading
                        {
                            new_message = false;

                            Array.Copy(ss.data, 0, msg_data, msg_cursor, ss.data.Length);
                            msg_cursor += ss.data.Length;
                        }
                    }

                    //if ns !=null, do next asyn-read, to ensure that read
                    if (ns != null && ns.CanRead)
                    {
                        try
                        {
                            ns.Flush();
                            state = new AsynReadState(BUFFER_SIZE);

                            if (!trying_to_close) ns.BeginRead(state.data, 0, BUFFER_SIZE, new AsyncCallback(OnDataRead), state);
                        }
                        catch { }
                    }
                }
                catch
                {
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Data process
        /// </summary>
        /// <param name="ar"></param>
        private void OnDataRead(IAsyncResult ar)
        {
            int           offset = 0;
            AsynReadState ss     = (AsynReadState)ar.AsyncState;

            //Detect client disconnection. then start anther acception
            if (ss.data[0] == 0)
            {
                server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTCPClientCallBack), server);
                return;
            }

            try
            {
REPEAT:                          //if multiple messages exist in the result. repeat the process
                if (new_message) //new_message is a flag to indicate if the data is part of unfinished message
                {
                    //Calculate message type, version, length and id
                    int header = (ss.data[offset] << 8) + ss.data[offset + 1];
                    msg_type = (Int16)(header & 0x03FF);
                    msg_ver  = (Int16)((header >> 10) & 0x07);
                    msg_len  = (ss.data[offset + 2] << 24) + (ss.data[offset + 3] << 16) + (ss.data[offset + 4] << 8) + ss.data[offset + 5];
                    msg_id   = (ss.data[offset + 6] << 24) + (ss.data[offset + 7] << 16) + (ss.data[offset + 8] << 8) + ss.data[offset + 9];

                    //if the message length is 0. the rest of byte array are empty. restart non-block reading
                    if (msg_len == 0)
                    {
                        if (ns != null)
                        {
                            try
                            {
                                ns.Flush();
                                state = new AsynReadState(BUFFER_SIZE);
                                ns.BeginRead(state.data, 0, state.data.Length, new AsyncCallback(OnDataRead), state);
                            }
                            catch { }

                            return;
                        }
                        else
                        {
                            return;
                        }
                    }

                    msg_data = new byte[msg_len];
                    //if message length greater than the calcualted message length. copy message and trigger message event
                    if (ss.data.Length >= msg_len)
                    {
                        Array.Copy(ss.data, offset, msg_data, 0, msg_len);

                        delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                        msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                        offset += msg_len;
                        msg_len = 0;

                        goto REPEAT;
                    }
                    else//If the received data is shorter than the message length, keep reading for the next data
                    {
                        new_message = false;
                        Array.Copy(ss.data, 0, msg_data, 0, ss.data.Length);
                        msg_cursor += ss.data.Length;
                    }
                }
                else
                {
                    //if data length larger than needed data for a complete message,
                    //copy data into existing message and triggered message event
                    if (ss.data.Length >= msg_len - msg_cursor)
                    {
                        Array.Copy(ss.data, 0, msg_data, msg_cursor, msg_len - msg_cursor);
                        delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                        msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                        offset += msg_len - msg_cursor;



                        msg_len    = 0;
                        msg_cursor = 0;

                        new_message = true;
                        goto REPEAT;
                    }
                    else //keep reading
                    {
                        new_message = false;
                        Array.Copy(ss.data, 0, msg_data, msg_cursor, ss.data.Length);
                        msg_cursor += ss.data.Length;
                    }
                }

                //if ns !=null, do next asyn-read, to ensure that read
                if (ns != null && ns.CanRead)
                {
                    try
                    {
                        ns.Flush();
                        state = new AsynReadState(BUFFER_SIZE);
                        ns.BeginRead(state.data, 0, state.data.Length, new AsyncCallback(OnDataRead), state);
                    }
                    catch { }
                }
            }
            catch
            {
            }
        }
예제 #5
0
        /// <summary>
        /// Asyn read result process
        /// </summary>
        /// <param name="ar"></param>
        private void OnDataRead(IAsyncResult ar)
        {
            int offset = 0;                                  //used to keep the start position of a LLRP message in
                                                             //byte array returned from the read
            AsynReadState ss = (AsynReadState)ar.AsyncState; //used to keep data

            lock (syn_msg)
            {
                try
                {
REPEAT:
                    if (new_message)                //new_message is a flag to indicate if the data is part of unfinished message
                    {
                        msg_cursor = 0;
                        int reserved_date_len = ss.data.Length - offset;

                        if (reserved_date_len > 10)
                        {
                            //Calculate message type, version, length and id
                            int header = (ss.data[offset] << 8) + ss.data[offset + 1];
                            try
                            {
                                msg_type = (Int16)(header & 0x03FF);
                                msg_ver  = (Int16)((header >> 10) & 0x07);
                                msg_len  = (ss.data[offset + 2] << 24) + (ss.data[offset + 3] << 16) + (ss.data[offset + 4] << 8) + ss.data[offset + 5];
                                msg_id   = (ss.data[offset + 6] << 24) + (ss.data[offset + 7] << 16) + (ss.data[offset + 8] << 8) + ss.data[offset + 9];
                            }
                            catch
                            {
                                msg_len = 0;
                            }

                            if (msg_len > 20000)
                            {
                                int i = 0;
                            }

                            //if data length larger than needed data for a complete message,
                            //copy data into existing message and triggered message event
                            if (msg_len > 0 && msg_ver == 1)
                            {
                                msg_data = new byte[msg_len];
                                //if message length greater than the calcualted message length. copy message and trigger message event

                                if (ss.data.Length >= (offset + msg_len))
                                {
                                    Array.Copy(ss.data, offset, msg_data, 0, msg_len);
                                    delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                                    msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                                    offset += msg_len;

                                    new_message = true;

                                    goto REPEAT;
                                }
                                else//If the received data is shorter than the message length, keep reading for the next data
                                {
                                    new_message = false;

                                    Array.Copy(ss.data, offset, msg_data, 0, ss.data.Length - offset);
                                    msg_cursor = ss.data.Length - offset;
                                }
                            }
                        }
                        else
                        {
                            new_message = true;

                            //if ns !=null, do next asyn-read, to ensure that read
                            if (ns != null && ns.CanRead)
                            {
                                try
                                {
                                    ns.Flush();
                                    state = new AsynReadState(BUFFER_SIZE);

                                    Array.Copy(ss.data, offset, state.data, 0, reserved_date_len);

                                    if (!trying_to_close)
                                    {
                                        ns.BeginRead(state.data, reserved_date_len, BUFFER_SIZE - reserved_date_len, new AsyncCallback(OnDataRead), state);
                                    }
                                }
                                catch { }
                            }

                            return;
                        }
                    }
                    else
                    {
                        //if data length larger than needed data for a complete message,
                        //copy data into existing message and triggered message event
                        if (ss.data.Length >= msg_len - msg_cursor)
                        {
                            Array.Copy(ss.data, 0, msg_data, msg_cursor, msg_len - msg_cursor);
                            delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                            msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                            offset += msg_len - msg_cursor;

                            new_message = true;

                            goto REPEAT;
                        }
                        else //keep reading
                        {
                            new_message = false;

                            Array.Copy(ss.data, 0, msg_data, msg_cursor, ss.data.Length);
                            msg_cursor += ss.data.Length;
                        }
                    }

                    //if ns !=null, do next asyn-read, to ensure that read
                    if (ns != null && ns.CanRead)
                    {
                        try
                        {
                            ns.Flush();
                            state = new AsynReadState(BUFFER_SIZE);

                            if (!trying_to_close)
                            {
                                ns.BeginRead(state.data, 0, BUFFER_SIZE, new AsyncCallback(OnDataRead), state);
                            }
                        }
                        catch { }
                    }
                }
                catch
                {
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Asyn read result process
        /// </summary>
        /// <param name="ar"></param>
        private void OnDataRead(IAsyncResult ar)
        {
            EMessageProcessingState state = (EMessageProcessingState)ar.AsyncState;

            // Here we get a new buffer from the socket
            try
            {
                buffer_bytes_available += ns.EndRead(ar);
                if (buffer_bytes_available == 0)
                {
                    Debug.WriteLine("Someone closed the socket on us");
                    return;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            lock (syn_msg)
            {
                while (buffer_bytes_available > 0)
                {
                    //Debug.WriteLine("Bytes Available " + buffer_bytes_available.ToString());
                    Int32 bytesToCopy;

                    switch (message_state)
                    {
                    case EMessageProcessingState.MESSAGE_UNKNOWN:
                        Debug.WriteLine("Unexpected message state");
                        break;

                    case EMessageProcessingState.MESSAGE_HEADER:

                        // In this state we are waiting for enough header bytes to arrive
                        // we should never have enough bytes when we enter this
                        Debug.Assert(msg_cursor < LLRP_HEADER_SIZE);
                        // copy as many as we can into the header storage
                        bytesToCopy = (Int32)Math.Min(LLRP_HEADER_SIZE - msg_cursor, buffer_bytes_available);
                        //Debug.WriteLine("Header Copy " + bytesToCopy.ToString());
                        Array.Copy(buffer, buffer_cursor, msg_header_storage, msg_cursor, bytesToCopy);
                        msg_cursor             += (UInt32)bytesToCopy;
                        buffer_cursor          += bytesToCopy;
                        buffer_bytes_available -= bytesToCopy;

                        Debug.Assert(msg_cursor <= LLRP_HEADER_SIZE);

                        if (msg_cursor == LLRP_HEADER_SIZE)
                        {
                            importAndQualifyHeader();
                            msg_data = new byte[msg_len];
                            Array.Copy(msg_header_storage, msg_data, LLRP_HEADER_SIZE);
                            message_state = EMessageProcessingState.MESSAGE_BODY;
                        }
                        break;

                    case EMessageProcessingState.MESSAGE_BODY:
                        // In this state, we are waiting for the body to arrive

                        // copy as many as we can into the header storage
                        bytesToCopy = (Int32)Math.Min(msg_len - msg_cursor, buffer_bytes_available);
                        //Debug.WriteLine("Message Copy " + bytesToCopy.ToString());
                        Array.Copy(buffer, buffer_cursor, msg_data, msg_cursor, bytesToCopy);
                        msg_cursor             += (UInt32)bytesToCopy;
                        buffer_cursor          += bytesToCopy;
                        buffer_bytes_available -= bytesToCopy;

                        Debug.Assert(msg_cursor <= msg_len);

                        if (msg_cursor == msg_len)
                        {
                            delegateMessageReceived msgRecv = new delegateMessageReceived(TriggerMessageEvent);
                            msgRecv.BeginInvoke(msg_ver, msg_type, msg_id, msg_data, null, null);

                            ReInitializeMessageProcessing();
                            message_state = EMessageProcessingState.MESSAGE_HEADER;
                        }
                        break;
                    }
                }
            }

            try
            {
                // we are out of bytes, receive the next message
                StartNewBufferReceive();
            }
            catch
            {
                // do nothing here
            }
        }
예제 #7
0
        void cI_OnMessageReceived(short ver, short msg_type, int msg_id, byte[] msg_data)
        {
            if ((msg_type == 100) || (msg_type == this.msg_type && msg_id == this.msg_id))
            {
                Array.Copy(msg_data, this.data, msg_data.Length);
                this.msg_type = msg_type;
                this.msg_id = msg_id;
                this.version = ver;
                transactEvt.Set();
            }

            if (OnMessageReceived != null)
            {
                delegateMessageReceived dmr = new delegateMessageReceived(triggerMessageReceived);
                dmr.BeginInvoke(ver, msg_type, msg_id, msg_data, null, null);
            }

            lock (this)
            {
                if (b_enqueue)
                {
                    raw_message_queue.Enqueue(new RAW_Message(ver, msg_type, msg_id, msg_data));
                    b_enqueue = false;
                }
            }

        }