示例#1
0
        public IAsyncResult BeginConnect(IPEndPoint remoteEP,
                                         AsyncCallback callback,
                                         Object state)
        {
            if (_physical == null)
            {
                AutomaticBind();
            }

            RUDPConnectIAsyncResult asyncResult = new RUDPConnectIAsyncResult(this, callback, state);

            Interlocked.Exchange <RUDPConnectIAsyncResult>(ref _asyncResultConnect, asyncResult);

            _remoteEndPoint = remoteEP;
            RUDPSocketError result = _physical.BeginConnect(this, DefaultConnectionTimeOut);

            if (result != RUDPSocketError.Success)
            {
                Interlocked.Exchange <RUDPConnectIAsyncResult>(ref _asyncResultConnect, null);
                _remoteEndPoint = null;
                throw new RUDPSocketException(result);
            }

            return(asyncResult);
        }
示例#2
0
 public int Send(byte[] buffer,
                 int offset,
                 int size,
                 out RUDPSocketError errorCode)
 {
     return(Send(buffer, offset, size, out errorCode, true));
 }
示例#3
0
        public int Send(byte[] buffer,
                        int offset,
                        int size)
        {
            RUDPSocketError errorCode = RUDPSocketError.Success;

            return(Send(buffer, offset, size, out errorCode, true));
        }
示例#4
0
 public IAsyncResult BeginSend(byte[] buffer,
                               int offset,
                               int size,
                               out RUDPSocketError errorCode,
                               AsyncCallback callback,
                               Object state)
 {
     return(BeginSend(buffer, offset, size, out errorCode, callback, state, true));
 }
示例#5
0
        internal void OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                return;
            }

            asyncResult.SetAsCompleted(error, false);
        }
示例#6
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);
            }
        }
示例#7
0
        internal void OnEndConnect(RUDPSocketError error)
        {
            RUDPConnectIAsyncResult result = null;

            Interlocked.Exchange <RUDPConnectIAsyncResult>(ref result, _asyncResultConnect);
            if (result == null)
            {
                return;
            }

            Interlocked.Exchange <RUDPConnectIAsyncResult>(ref _asyncResultConnect, null);

            result.Connected = (error == RUDPSocketError.Success);
            result.SetAsCompleted(error, false);
        }
示例#8
0
        public int Send(byte[] buffer,
                        int offset,
                        int size,
                        out RUDPSocketError errorCode,
                        bool reliable)
        {
            errorCode = RUDPStack.SendPayload(this, buffer, offset, size, reliable, null);

            if (errorCode != RUDPSocketError.Success)
            {
                return(-1);
            }

            return(size);
        }
示例#9
0
        public IAsyncResult BeginSend(byte[] buffer,
                                      int offset,
                                      int size,
                                      out RUDPSocketError errorCode,
                                      AsyncCallback callback,
                                      Object state,
                                      bool reliable)
        {
            RUDPSendIAsyncResult asyncResult = new RUDPSendIAsyncResult(this, callback, state, size);

            errorCode = RUDPStack.SendPayload(this, buffer, offset, size, reliable, asyncResult);

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

            return(asyncResult);
        }
示例#10
0
        internal void SetAsCompleted(RUDPSocketError socketError, Boolean completedSynchronously)
        {
            // Passing null for exception means no error occurred.
            // This is the common case
            SocketError = socketError;

            // The m_CompletedState field MUST be set prior calling the callback
            Int32 prevState = Interlocked.Exchange(ref m_CompletedState,
                                                   completedSynchronously ? c_StateCompletedSynchronously : c_StateCompletedAsynchronously);

            if (prevState != c_StatePending)
            {
                throw new InvalidOperationException("You can set a result only once");
            }

            // If the event exists, set it
            //Thread.MemoryBarrier(); // Joe Duffy
            if (_asyncWaitHandle != null)
            {
                _asyncWaitHandle.Set();
            }

            // If a callback method was set, call it
            if (_asyncCallback != null)
            {
                if (!ForceAsyncCall)
                {
                    try
                    {
                        _asyncCallback(this);
                    }
                    catch (Exception exception)
                    {
                        RUDPStack.HandleException(exception);
                    }
                }
                else
                {
                    ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(CompleteAsyncCall), null);
                }
            }
        }
示例#11
0
文件: RUDP.cs 项目: ACE-liu/finalJob
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="Remote">对方主机信息</param>
        /// <param name="Data">要发送的数据</param>
        public void Send(IPEndPoint Remote, byte[] Data)
        {
            RUDPSocketError err = RUDPSocketError.HostDown;

            try
            {
                if (IsAsync)
                {
                    rUDP.BeginSend(Data, 0, Data.Length, out err, new AsyncCallback(SendCallback), null);
                }
                else
                {
                    rUDP.Send(Data, 0, Data.Length);
                }
            }
            catch (Exception e)
            {
                if (Sock_Error != null)
                {
                    Sock_Error(this, new SockEventArgs(e.Source + "," + e.Message));
                }
            }
        }
示例#12
0
		internal void SetAsCompleted(RUDPSocketError socketError, Boolean completedSynchronously)
		{
			// Passing null for exception means no error occurred. 
			// This is the common case
			SocketError = socketError;

			// The m_CompletedState field MUST be set prior calling the callback
			Int32 prevState = Interlocked.Exchange(ref m_CompletedState,
			   completedSynchronously ? c_StateCompletedSynchronously : c_StateCompletedAsynchronously);

			if (prevState != c_StatePending)
				throw new InvalidOperationException("You can set a result only once");

			// If the event exists, set it
			//Thread.MemoryBarrier(); // Joe Duffy
			if (_asyncWaitHandle != null)
				_asyncWaitHandle.Set();

			// If a callback method was set, call it
			if (_asyncCallback != null)
			{
				if (!ForceAsyncCall)
				{
					try
					{
						_asyncCallback(this);
					}
					catch (Exception exception)
					{
						RUDPStack.HandleException(exception);
					}
				}
				else
					ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(CompleteAsyncCall), null);
			}
		}
示例#13
0
		internal void OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult)
		{
			if (asyncResult == null)
				return;

			asyncResult.SetAsCompleted(error, false);
		}
示例#14
0
		internal void OnEndConnect(RUDPSocketError error)
		{
			RUDPConnectIAsyncResult result = null;
			Interlocked.Exchange<RUDPConnectIAsyncResult>(ref result, _asyncResultConnect);
			if (result == null)
				return;

			Interlocked.Exchange<RUDPConnectIAsyncResult>(ref _asyncResultConnect, null);

			result.Connected = (error == RUDPSocketError.Success);
			result.SetAsCompleted(error, false);
		}
示例#15
0
 public RUDPSocketException(RUDPSocketError error)
 {
     Error = error;
 }
示例#16
0
		public RUDPSocketException(RUDPSocketError error)
		{
			Error = error;
		}
示例#17
0
		public IAsyncResult BeginSend(byte[] buffer,
										int offset,
										int size,
										out RUDPSocketError errorCode,
										AsyncCallback callback,
										Object state,
										bool reliable)
		{
			RUDPSendIAsyncResult asyncResult = new RUDPSendIAsyncResult(this, callback, state, size);

			errorCode = RUDPStack.SendPayload(this, buffer, offset, size, reliable, asyncResult);

			if (errorCode != RUDPSocketError.Success)
				return null;

			return asyncResult;
		}
示例#18
0
		public IAsyncResult BeginSend(byte[] buffer,
										int offset,
										int size,
										out RUDPSocketError errorCode,
										AsyncCallback callback,
										Object state)
		{
			return BeginSend(buffer, offset, size, out errorCode, callback, state, true);
		}
示例#19
0
		public int Send(byte[] buffer,
						int offset,
						int size,
						out RUDPSocketError errorCode,
						bool reliable)
		{
			errorCode = RUDPStack.SendPayload(this, buffer, offset, size, reliable, null);

			if (errorCode != RUDPSocketError.Success)
				return -1;

			return size;
		}
示例#20
0
		public int Send(byte[] buffer,
						int offset,
						int size,
						out RUDPSocketError errorCode)
		{
			return Send(buffer, offset, size, out errorCode, true);
		}
示例#21
0
		internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult)
		{
			asyncResult.Packet = packet;
			asyncResult.ForceAsyncCall = forceAsyncCall;
			asyncResult.SetAsCompleted(error, false);
		}
示例#22
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);
			}
		}
示例#23
0
		internal void OnDisconnected(RUDPSocket rudp, RUDPSocketError error)
		{
			UnregisterConnectedSocket(rudp);
			rudp.OnDisconnected(error);
		}
示例#24
0
 internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult)
 {
     asyncResult.Packet         = packet;
     asyncResult.ForceAsyncCall = forceAsyncCall;
     asyncResult.SetAsCompleted(error, false);
 }
示例#25
0
		/// <summary>
		/// Called when we have an error on a socket.
		/// </summary>
		static internal void OnSocketUnhandledError(RUDPSocket rudp, RUDPSocketError error, RUDPSendIAsyncResult sendAsyncResult)
		{
			//---- Disconnect the socket
			OnDisconnected(rudp, DisconnectionReason.SocketError);

			//---- Handle the error and forward it to the socket
			if (rudp._status == RUDPSocketStatus.Connecting)
				rudp.OnEndConnect(error);
			else
			{
				// On Send Error
				if (sendAsyncResult != null)
					rudp.OnEndSend(error, sendAsyncResult);

				// ELSE ... HOW TO GET sendAsyncResult when NULL ?????

				// On Receive Error
				RUDPReceiveIAsyncResult receiveAsyncResult = null;
				Interlocked.Exchange<RUDPReceiveIAsyncResult>(ref receiveAsyncResult, rudp._asyncResultReceive);
				if (receiveAsyncResult != null)
				{
					Interlocked.Exchange<RUDPReceiveIAsyncResult>(ref rudp._asyncResultReceive, null);

					rudp.OnEndReceive(error, null, true, receiveAsyncResult);
				}
			}
		}
示例#26
0
 internal void OnDisconnected(RUDPSocket rudp, RUDPSocketError error)
 {
     UnregisterConnectedSocket(rudp);
     rudp.OnDisconnected(error);
 }