public async ValueTask UnbindAsync(CancellationToken cancellationToken = default) { lock (Gate) { if (_state >= ConnectionListenerState.Disposing) { ThrowHelper.ThrowNewObjectDisposedException(ThrowHelper.ExceptionArgument.ConnectionListener); } if (_state != ConnectionListenerState.Bound) { ThrowHelper.ThrowNewInvalidOperationException(); } _state = ConnectionListenerState.Unbinding; } try { if (EndPoint is IPEndPoint ipEndPoint) { var threads = _transport.TransportThreads; foreach (var thread in threads) { await thread.Unbind(ipEndPoint); } } else { await _transport.AcceptThread.Unbind(EndPoint); } _acceptQueue.Writer.TryComplete(); lock (Gate) { if (_state != ConnectionListenerState.Unbinding) { ThrowHelper.ThrowNewInvalidOperationException(); } _state = ConnectionListenerState.Unbound; } } catch (Exception) { await DisposeAsync(); throw; } }
private async ValueTask BindAsync() { lock (Gate) { if (_state >= ConnectionListenerState.Disposing) { ThrowHelper.ThrowNewObjectDisposedException(ThrowHelper.ExceptionArgument.ConnectionListener); } if (_state != ConnectionListenerState.New) { ThrowHelper.ThrowNewInvalidOperationException(); } _state = ConnectionListenerState.Binding; } try { _transport.IncrementThreadRefCount(); var endpoint = EndPoint; switch (endpoint) { case IPEndPoint ipEndPoint when ipEndPoint.AddressFamily == AddressFamily.InterNetwork || ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6: var threads = _transport.TransportThreads; EndPoint boundEndPoint = endpoint; foreach (var thread in threads) { boundEndPoint = thread.Bind(ipEndPoint, _acceptQueue); } EndPoint = boundEndPoint; if (_options.ReceiveOnIncomingCpu) { threads[0].SetReceiveOnIncomingCpu((IPEndPoint)boundEndPoint); } break; case UnixDomainSocketEndPoint unixDomainSocketEndPoint: EndPoint = _transport.AcceptThread.Bind(unixDomainSocketEndPoint, _acceptQueue); break; case FileHandleEndPoint fileHandleEndPoint: EndPoint = _transport.AcceptThread.Bind(fileHandleEndPoint, _acceptQueue); break; default: ThrowHelper.ThrowNewNotSupportedException_EndPointNotSupported(); break; } lock (Gate) { if (_state != ConnectionListenerState.Binding) { ThrowHelper.ThrowNewInvalidOperationException(); } _state = ConnectionListenerState.Bound; } } catch (Exception) { await DisposeAsync(); throw; } }