Example #1
0
        /// <summary>
        /// We directly return the payload to avoid memory copy.
        /// </summary>
        public byte[] EndReceive(IAsyncResult asyncResult)
        {
            RUDPReceiveIAsyncResult result = asyncResult as RUDPReceiveIAsyncResult;

            result.EndInvoke(false);

            if (result.SocketError != RUDPSocketError.Success)
            {
                return(null);
            }

            return(result.Packet.Payload);
        }
Example #2
0
        internal void OnDisconnected(RUDPSocketError error)
        {
            OnEndConnect(error);

            RUDPReceiveIAsyncResult asyncResult = null;

            Interlocked.Exchange <RUDPReceiveIAsyncResult>(ref asyncResult, _asyncResultReceive);
            if (asyncResult != null)
            {
                Interlocked.Exchange <RUDPReceiveIAsyncResult>(ref _asyncResultReceive, null);
                OnEndReceive(error, null, false, asyncResult);
            }
        }
Example #3
0
        public IAsyncResult BeginReceive(out SocketError errorCode,
                                         AsyncCallback callback,
                                         Object state)
        {
            RUDPReceiveIAsyncResult asyncResult = new RUDPReceiveIAsyncResult(this, callback, state);

            Interlocked.Exchange <RUDPReceiveIAsyncResult>(ref _asyncResultReceive, asyncResult);

            errorCode = SocketError.Success;

            //---- Check if we do not already have packets
            HandleNextUserPacket(true);

            return(asyncResult);
        }
Example #4
0
        internal void HandleNextUserPacket(bool forceAsyncCall)
        {
            if (_asyncResultReceive == null)
            {
                return;
            }

            RUDPReceiveIAsyncResult asyncResult;
            RUDPIngoingPacket       packet = null;

            lock (_handleNextUserPacketLock)
            {
                if (_incomingPackets.Count < 1)
                {
                    return;
                }

                asyncResult = _asyncResultReceive;
                if (_asyncResultReceive == null)
                {
                    return;
                }

                //-- Clean the list
                while (_incomingPackets.Count > 0)
                {
                    // Get the current packet
                    packet = _incomingPackets.RemoveNextPacket();

                    // No more packet
                    if (packet == null)
                    {
                        return;
                    }

                    // Ordered packet
                    if (packet.Channel != RUDPPacketChannel.UserPacket)
                    {
                        _controlWindow.OnReceiveProcessed(packet);
                    }
                    else
                    {
                        break;                         // It is the next message to process.
                    }
                }

                //---- Check it is not null
                if (packet == null)
                {
                    return;
                }

                _asyncResultReceive = null;
            }

            //---- Receive
            if (asyncResult != null)
            {
                _controlWindow.OnReceiveProcessed(packet);
                OnEndReceive(RUDPSocketError.Success, packet, forceAsyncCall, asyncResult);
            }
        }
Example #5
0
 internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult)
 {
     asyncResult.Packet         = packet;
     asyncResult.ForceAsyncCall = forceAsyncCall;
     asyncResult.SetAsCompleted(error, false);
 }
Example #6
0
		internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult)
		{
			asyncResult.Packet = packet;
			asyncResult.ForceAsyncCall = forceAsyncCall;
			asyncResult.SetAsCompleted(error, false);
		}
Example #7
0
		public IAsyncResult BeginReceive(out SocketError errorCode,
										AsyncCallback callback,
										Object state)
		{
			RUDPReceiveIAsyncResult asyncResult = new RUDPReceiveIAsyncResult(this, callback, state);
			Interlocked.Exchange<RUDPReceiveIAsyncResult>(ref _asyncResultReceive, asyncResult);

			errorCode = SocketError.Success;

			//---- Check if we do not already have packets
			HandleNextUserPacket(true);

			return asyncResult;
		}
Example #8
0
		internal void HandleNextUserPacket(bool forceAsyncCall)
		{
			if (_asyncResultReceive == null)
				return;

			RUDPReceiveIAsyncResult asyncResult;
			RUDPIngoingPacket packet = null;
			lock (_handleNextUserPacketLock)
			{
				if (_incomingPackets.Count < 1)
					return;

				asyncResult = _asyncResultReceive;
				if (_asyncResultReceive == null)
					return;

				//-- Clean the list
				while (_incomingPackets.Count > 0)
				{
					// Get the current packet
					packet = _incomingPackets.RemoveNextPacket();

					// No more packet
					if (packet == null)
						return;

					// Ordered packet
					if (packet.Channel != RUDPPacketChannel.UserPacket)
						_controlWindow.OnReceiveProcessed(packet);
					else 
						break; // It is the next message to process.
				}

				//---- Check it is not null
				if (packet == null)
					return;

				_asyncResultReceive = null;
			}

			//---- Receive
			if (asyncResult != null)
			{
				_controlWindow.OnReceiveProcessed(packet);
				OnEndReceive(RUDPSocketError.Success, packet, forceAsyncCall, asyncResult);
			}
		}