public void TrySend(SocketAsyncEventArgs args) { if (!m_Sending.TryEnter()) { return; } if (args.GetSendContextData().IsEmpty) { m_Sending.TryExit(); return; } try { bool willRaiseEventLater; while (!(willRaiseEventLater = SendAsync(args))) { var canSendAvailableBufferList = CompleteSend(args); if (!canSendAvailableBufferList) { m_Sending.TryExit(); break; } } } catch (Exception e) { CloseSocketWhileSending(args, e); } }
private void TryReceive() { if (!m_Receiving.TryEnter()) { return; } var receiveAsyncEventArgs = new SocketAsyncEventArgs { UserToken = new ReceiveContextData(2 * INITIAL_READ_BYTES) }; receiveAsyncEventArgs.Completed += (sender, e) => OnReceiveCompleted(e); try { receiveAsyncEventArgs.PrepareReceiveBuffer(INITIAL_READ_BYTES); bool willRaiseEventLater; while (!(willRaiseEventLater = ReceiveAsync(receiveAsyncEventArgs))) { var canReceive = CompleteReceive(receiveAsyncEventArgs); if (!canReceive) { break; } } } catch (Exception e) { CloseSocketWhileReceiving(receiveAsyncEventArgs, e); } }