Ejemplo n.º 1
0
    private void _OnRead()
    {
        if (m_Socket.Available >= MessageConst.HEADER_LENGTH)
        {
            try {
                byte[] buffer = new byte[MessageConst.HEADER_LENGTH];
                //试读取消息头数据
                m_Socket.Receive(buffer, MessageConst.HEADER_LENGTH, SocketFlags.Peek);

                if (buffer.Length >= MessageConst.HEADER_LENGTH)//首先保证读取满足消息头的字节数据
                {
                    byte[] headTemp = MessageParse.Decrypt(buffer, 4, MessageParse.CopyByteArray(RECEIVE_KEY));
                    System.Array.Reverse(headTemp, 0, 2);
                    System.Array.Reverse(headTemp, 2, 4);

                    short flag = System.BitConverter.ToInt16(headTemp, 0);
                    short len  = System.BitConverter.ToInt16(headTemp, 2);
                    if (flag == MessageConst.HEADER)   //验证标志符
                    {
                        if (len <= m_Socket.Available) //满足完整消息的字节长度
                        {
                            //解密头消息
                            headTemp = MessageParse.Decrypt(buffer, MessageConst.HEADER_LENGTH, MessageParse.CopyByteArray(RECEIVE_KEY));
                            //解析头信息数据
                            Message_Head head = MessageParse.UnParseHead(headTemp);
                            if (head == null)//验证头信息有效性
                            {
                                _OnError(new SocketException(10042));
                                Close();
                            }
                            else
                            {
                                int iLength = head.Length;
                                buffer = new byte[iLength];
                                m_Socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                                //解密字节数据
                                byte[] buffer1 = MessageParse.Decrypt(buffer, iLength, MessageParse.CopyByteArray(RECEIVE_KEY));
                                //正式解析整个消息信息数据
                                MessageData data = MessageParse.UnParse(buffer1);
                                if (data != null)
                                {
                                    //计算数据校验和
                                    int checksum = MessageParse.CalculateCheckSum(buffer1);
                                    //验证校验和有效性
                                    if (checksum == data.head.CHECK_SUM)
                                    {
                                        lock (_lock)
                                        {
                                            m_Datas.Enqueue(data);
                                        }
                                    }
                                }
                                else
                                {
                                    _OnError(new SocketException(10042));
                                    Close();
                                }
                            }
                        }
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                Close();
            }
            catch (SocketException ex) {
                _OnError(ex);
                Close();
            }
        }
    }