Exemplo n.º 1
0
        /// <summary>
        /// Message received event.
        /// </summary>

        public TCPIPClient()
        {
            state = new AsynReadState(BUFFER_SIZE);
        }
Exemplo n.º 2
0
 public TCPIPServer()
 {
     state = new AsynReadState(BUFFER_SIZE);
 }
Exemplo n.º 3
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
            {
            }
        }
Exemplo n.º 4
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
                {
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Message received event.
        /// </summary>

        public TCPIPClient()
        {
            state = new AsynReadState(BUFFER_SIZE);
        }
Exemplo n.º 6
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
            {
            }
        }
Exemplo n.º 7
0
 public TCPIPServer()
 {
     state = new AsynReadState(BUFFER_SIZE);
 }
Exemplo n.º 8
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
                {
                }
            }
        }