Beispiel #1
0
 /// <summary>
 /// 接受指定大小的网络包
 /// </summary>
 /// <param name="_arg">要接收的网络包参数数据</param>
 private void _receivePacket(ReceivePacketArg _arg)
 {
     try
     {
         _arg.BytesReceived = 0;
         _arg.AsyncArgs.SetBuffer(0, _arg.BytesWanted);
         bool will_raise_event = false;
         lock ( m_connect_socket_lock )
         {
             will_raise_event = m_connect_socket.ReceiveAsync(_arg.AsyncArgs);
         }
         if (!will_raise_event)
         {
             _onReceivePacket(null, _arg.AsyncArgs);
         }
     }
     catch (Exception e)
     {
         Log.Error("Failed to receive packet: {0}", e.Message);
     }
 }
Beispiel #2
0
        /// <summary>
        /// 网络包接收到的处理函数
        /// </summary>
        /// <param name="_sender">not used</param>
        /// <param name="_args">这次操作相关的网络异步事件参数</param>
        private void _onReceivePacket(Object _sender, SocketAsyncEventArgs _args)
        {
            try
            {
                if (_args.BytesTransferred > 0 && SocketError.Success == _args.SocketError)
                {
                    ReceivePacketArg arg = _args.UserToken as ReceivePacketArg;
                    arg.BytesReceived += _args.BytesTransferred;
                    if (arg.BytesReceived == arg.BytesWanted)
                    {
                        if (m_receiving_body)
                        {                         // 正在接受消息体
                            if (null != eventPacketArrived)
                            {
                                NetPacket packet = new NetPacket();
                                packet.PacketType = PacketType.Message;
                                packet.Data       = _args.Buffer;
                                packet.DataSize   = arg.BytesWanted;
                                eventPacketArrived(StreamId, packet);
                            }
                            m_receiving_body = false;
                            _args.SetBuffer(null, 0, 0);
                            _receiveNextMessage();
                        }
                        else
                        {                         // 正在接受消息头
                            UInt16 message_length_u16 = BitConverter.ToUInt16(_args.Buffer, 2);
                            //Int32 message_length = IPAddress.NetworkToHostOrder( Convert.ToInt32( message_length_u16 ) );

                            //Int32 message_length = BitConverter.ToInt32( _args.Buffer, 0 );
                            //message_length = IPAddress.NetworkToHostOrder( message_length );

                            Byte[] buffer = new Byte[message_length_u16];
                            m_recv_body_args.AsyncArgs.SetBuffer(buffer, 0, message_length_u16);
                            m_recv_body_args.BytesWanted   = message_length_u16;
                            m_recv_body_args.BytesReceived = 0;
                            m_receiving_body = true;
                            _receivePacket(m_recv_body_args);
                        }
                    }
                    else if (arg.BytesReceived > arg.BytesWanted)
                    {
                        throw new Exception("异常的网络数据接收:已接收字节数大于需求接收的字节数。");
                    }
                    else
                    {
                        _args.SetBuffer(arg.BytesReceived, arg.BytesWanted - arg.BytesReceived);
                        bool will_raise_event = false;
                        lock ( m_connect_socket_lock )
                        {
                            will_raise_event = m_connect_socket.ReceiveAsync(_args);
                        }
                        if (!will_raise_event)
                        {
                            _onReceivePacket(null, _args);
                        }
                    }
                }
                else
                {
                    if (0 == _args.BytesTransferred)
                    {
                        _closeSocket();
                        if (null != eventPacketArrived)
                        {
                            NetPacket packet = new NetPacket();
                            packet.PacketType = PacketType.Disconnected;
                            packet.Data       = null;
                            packet.DataSize   = 0;
                            eventPacketArrived(StreamId, packet);
                        }
                    }
                    else
                    {
                        Disconnect();
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Failed to receive net packet: {0}", e.Message);
            }
        }