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); }
public int Send(byte[] buffer, int offset, int size, out RUDPSocketError errorCode) { return(Send(buffer, offset, size, out errorCode, true)); }
public int Send(byte[] buffer, int offset, int size) { RUDPSocketError errorCode = RUDPSocketError.Success; return(Send(buffer, offset, size, out errorCode, true)); }
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)); }
internal void OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult) { if (asyncResult == null) { return; } asyncResult.SetAsCompleted(error, false); }
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); } }
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); }
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); }
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); }
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); } } }
/// <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)); } } }
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); } }
internal void OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult) { if (asyncResult == null) return; asyncResult.SetAsCompleted(error, false); }
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); }
public RUDPSocketException(RUDPSocketError error) { Error = error; }
public RUDPSocketException(RUDPSocketError error) { Error = error; }
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; }
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); }
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; }
public int Send(byte[] buffer, int offset, int size, out RUDPSocketError errorCode) { return Send(buffer, offset, size, out errorCode, true); }
internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult) { asyncResult.Packet = packet; asyncResult.ForceAsyncCall = forceAsyncCall; asyncResult.SetAsCompleted(error, false); }
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); } }
internal void OnDisconnected(RUDPSocket rudp, RUDPSocketError error) { UnregisterConnectedSocket(rudp); rudp.OnDisconnected(error); }
internal void OnEndReceive(RUDPSocketError error, RUDPIngoingPacket packet, bool forceAsyncCall, RUDPReceiveIAsyncResult asyncResult) { asyncResult.Packet = packet; asyncResult.ForceAsyncCall = forceAsyncCall; asyncResult.SetAsCompleted(error, false); }
/// <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); } } }
internal void OnDisconnected(RUDPSocket rudp, RUDPSocketError error) { UnregisterConnectedSocket(rudp); rudp.OnDisconnected(error); }