Beispiel #1
0
 // ############# 当有数据需要读取时,这里 自动 被调用
 private void _OnRead()
 {
     /*
      *  socket.Available 是从已经从网络接收的、可供读取的数据的字节数。
      *  这个值是指 缓冲区 中已接收数据的字节数,不是实际的数据大小。
      *  而且如果网络有延迟,Send之后马上读取Available属性不一定能读到正确的值,
      *  所以不能利用socket.Available来判断总共要接受的字节数。
      */
     if (m_Socket.Available > 0)
     {
         try
         {
             byte[] buffer = new byte[13];
             m_Socket.Receive(buffer, 13, SocketFlags.Peek);       // 开始读取 Peek 快速查看传入消息
             Message_Head head = MessageParse.UnParseHead(buffer); // 解析消息头
             if (head == null)
             {
                 _OnError(new SocketException((int)SocketError.ProtocolOption));
                 Close();
             }
             else
             {
                 // 13 = 5(协议头)+4(版本号)+4(消息体长度Length)
                 // 消息体长度 = command(int型4位)+msg(byte[])
                 int iLength = head.Length + 13;    // 总长度
                 if (iLength <= m_Socket.Available) // 消息全部到达
                 {
                     buffer = new byte[iLength];
                     m_Socket.Receive(buffer, 0, buffer.Length, SocketFlags.None); // 读取消息到buffer中
                     MessageData data = MessageParse.UnParse(buffer);              // 解析成Data
                     if (data != null)
                     {
                         lock (_lock)
                         {                          // 上锁,其他线程会被阻塞在外面直到锁被解开
                             m_Datas.Enqueue(data); // 数据入队列
                         }
                     }
                     else
                     {
                         _OnError(new SocketException((int)SocketError.ProtocolOption));  // 10042 Option unknown, or unsupported.
                         Close();
                     }
                 }
             }
         }
         catch (ObjectDisposedException)
         {
             Close();
         }
         catch (SocketException ex)
         {
             _OnError(ex);
             Close();
         }
     }
 }
Beispiel #2
0
 private void _OnRead()
 {
     if (m_Socket.Available > 0)
     {
         try
         {
             byte[] buffer = new byte[13];
             m_Socket.Receive(buffer, 13, SocketFlags.Peek);
             Message_Head head = MessageParse.UnParseHead(buffer);
             if (head == null)
             {
                 _OnError(new SocketException(10042));
                 Close();
             }
             else
             {
                 int iLength = head.Length + 13;
                 if (iLength <= m_Socket.Available)
                 {
                     buffer = new byte[iLength];
                     m_Socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                     MessageData data = MessageParse.UnParse(buffer);
                     if (data != null)
                     {
                         lock (_lock)
                         {
                             m_Datas.Enqueue(data);
                         }
                     }
                     else
                     {
                         _OnError(new SocketException(10042));
                         Close();
                     }
                 }
             }
         }
         catch (ObjectDisposedException)
         {
             Close();
         }
         catch (SocketException ex)
         {
             _OnError(ex);
             Close();
         }
     }
 }
Beispiel #3
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();
            }
        }
    }