internal FragmentInformation(RUDPSocket rudpSocket, bool isReliable, byte[] payload, int offset, int size, RUDPSendIAsyncResult asyncResult) { rudp = rudpSocket; IsReliable = isReliable; Offset = offset; Size = size; Payload = payload; AsyncResult = asyncResult; }
internal void OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult) { if (asyncResult == null) { return; } asyncResult.SetAsCompleted(error, false); }
public int EndSend(IAsyncResult asyncResult) { RUDPSendIAsyncResult result = asyncResult as RUDPSendIAsyncResult; result.EndInvoke(false); if (result.SocketError != RUDPSocketError.Success) { return(-1); } return(result._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 static RUDPSocketError SendPayload(RUDPSocket rudp, byte[] payload, int offset, int payloadLength, bool reliable, RUDPSendIAsyncResult asyncResult) { // We are no longer active if (!_isStackRunning) return RUDPSocketError.SystemNotReady; //---- Only when connected if (rudp._status != RUDPSocketStatus.Connected) return RUDPSocketError.NotConnected; //---- Fragmentation asyncResult.ForceAsyncCall = true; FragmentInformation fragments = NewFragmentInformation(rudp, reliable, payload, offset, payloadLength, asyncResult); //----- Async send rudp._fragmentsLock.EnterWriteLock(); rudp._fragments.AddFirst(fragments); rudp._fragmentsLock.ExitWriteLock(); ForceFragmentsSending(rudp._controlThreadId); //---- If possible send Sync... otherwise relate to control thread /* if (!SendFragments(fragments)) { rudp._fragmentsLock.EnterWriteLock(); rudp._fragments.AddFirst(fragments); rudp._fragmentsLock.ExitWriteLock(); _protocolControlEvent.Set(); } */ //---- Synchrone send /* while (!SendFragments(fragments)) fragments.rudp._controlWindow.WaitObject.WaitOne(); */ return fragments.Error; }
internal static FragmentInformation NewFragmentInformation(RUDPSocket rudpSocket, bool isReliable, byte[] payload, int offset, int size, RUDPSendIAsyncResult asyncResult) { //return new FragmentInformation(rudpSocket, isReliable, payload, offset, size, asyncResult); FragmentInformation fragment; if (!_fragmentsPools.TryDequeue(out fragment)) { for (int index = 0; index < 100; index++) _fragmentsPools.Enqueue(new FragmentInformation(null, false, null, -1, -1, null)); return new FragmentInformation(rudpSocket, isReliable, payload, offset, size, asyncResult); } fragment.rudp = rudpSocket; fragment.IsReliable = isReliable; fragment.Offset = offset; fragment.Size = size; fragment.Payload = payload; fragment.AsyncResult = asyncResult; return fragment; }
/// <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 OnEndSend(RUDPSocket rudp, RUDPSendIAsyncResult asyncResult) { rudp.OnEndSend(RUDPSocketError.Success, asyncResult); }
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 OnEndSend(RUDPSocketError error, RUDPSendIAsyncResult asyncResult) { if (asyncResult == null) return; asyncResult.SetAsCompleted(error, false); }