public void Bind(IPEndPoint endpoint, ChannelWriter <ConnectionContext> acceptQueue) { var domain = endpoint.AddressFamily == AddressFamily.InterNetwork ? AF_INET : AF_INET6; LinuxSocket s = socket(domain, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP); s.SetOption(SOL_SOCKET, SO_REUSEADDR, 1); s.SetOption(SOL_SOCKET, SO_REUSEPORT, 1); s.Bind(endpoint); s.Listen(ListenBacklog); var context = new AcceptSocketContext(s, endpoint, acceptQueue); _acceptSocketQueue.Enqueue(context); _threadContext.Notify(); }
public ValueTask <ConnectionContext> Connect(IPEndPoint endpoint) { var domain = endpoint.AddressFamily == AddressFamily.InterNetwork ? AF_INET : AF_INET6; LinuxSocket s = socket(domain, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP); if (_threadContext.Options.TcpNoDelay) { s.SetOption(SOL_TCP, TCP_NODELAY, 1); } var context = new OutboundConnectionContext(s, endpoint, _threadContext); var tcs = new TaskCompletionSource <ConnectionContext>(TaskCreationOptions.RunContinuationsAsynchronously); // Ensure the transport thread doesn't run continuations context.ConnectCompletion = tcs; endpoint.ToSockAddr(context.Addr, out var addrLength); context.AddrLen = addrLength; _connections[s] = context; Connect(context); _threadContext.Notify(); return(new ValueTask <ConnectionContext>(tcs.Task)); }
private void CompleteAccept(AcceptSocketContext acceptContext, int result) { if (result < 0) { var err = -result; if (err == EAGAIN || err == EINTR || err == EMFILE) { Debug.WriteLine($"accept completed with EAGAIN on {(int)acceptContext.Socket}"); Accept(acceptContext); return; } throw new ErrnoException(err); } LinuxSocket socket = result; Debug.WriteLine($"Accepted {(int)socket} from {(int)acceptContext.Socket}"); if (_threadContext.Options.TcpNoDelay) { socket.SetOption(SOL_TCP, TCP_NODELAY, 1); } var remoteEndpoint = IPEndPointFormatter.AddrToIpEndPoint(acceptContext.Addr); var context = new InboundConnectionContext(socket, acceptContext.EndPoint, remoteEndpoint, _threadContext); _connections[socket] = context; acceptContext.AcceptQueue.TryWrite(context); Accept(acceptContext); Read(context); ReadFromApp(context); }
public ValueTask <ConnectionContext> Connect(IPEndPoint endpoint) { var domain = endpoint.AddressFamily == AddressFamily.InterNetwork ? AF_INET : AF_INET6; LinuxSocket s = socket(domain, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_TCP); if (_threadContext.Options.TcpNoDelay) { s.SetOption(SOL_TCP, TCP_NODELAY, 1); } var context = new OutboundConnectionContext(s, endpoint, _threadContext); bool connectedSynchronously; try { connectedSynchronously = s.TryConnectNonBlocking(endpoint); } catch (ErrnoException ex) { return(new ValueTask <ConnectionContext>(Task.FromException <ConnectionContext>(ex))); } if (connectedSynchronously) { context.LocalEndPoint = s.GetLocalAddress(); return(new ValueTask <ConnectionContext>(context)); } var tcs = new TaskCompletionSource <ConnectionContext>(); context.ConnectCompletion = tcs; // Socket will become writable, once connect completed _clientSocketQueue.Enqueue(context); _threadContext.Notify(); return(new ValueTask <ConnectionContext>(tcs.Task)); }