public EpoxyListener MakeListener(IPEndPoint address) { var listener = new EpoxyListener(this, address, serverTlsConfig, timeoutConfig, logger, metrics); try { listeners.Add(listener); } catch (InvalidOperationException) { throw new InvalidOperationException("This EpoxyTransport has been stopped already."); } return(listener); }
async Task StartClientConnectionAsync(Task <Socket> socketTask) { // If this throws, it will have cleaned up before getting here, so we don't need to // handle cleanup. EpoxyNetworkStream epoxyStream = await EpoxyNetworkStream.MakeAsync( socketFunc : () => socketTask, streamFunc : socket => EpoxyNetworkStream.MakeServerStreamAsync(socket, tlsConfig, logger), timeoutConfig : timeoutConfig, logger : logger); // We now have a completely established EpoxyNetworkStream that will eventually need to // be shutdown. None of the code between here and adding the connection to our collection // will throw (for network I/O reasons), so we'll safely save the connection. (The // intervening code may throw for things like OOM. If it does, we've got bigger problems // that we likely won't be able to recover from anyway. Ignoring that contingency for // now.) var connection = EpoxyConnection.MakeServerConnection( parentTransport, this, serviceHost, epoxyStream, logger, metrics); try { connections.Add(connection); } catch (InvalidOperationException) { logger.Site().Debug("Listener was shutdown while accepting connection from {0}. Connection has been abandoned.", connection.RemoteEndPoint); await connection.StopAsync(); throw; } logger.Site().Debug("Setup server-side connection for {0}. Starting Epoxy handshake.", connection.RemoteEndPoint); await connection.StartAsync(); }
public async Task <EpoxyConnection> ConnectToAsync(Endpoint endpoint, CancellationToken ct) { logger.Site().Information("Connecting to {0}.", endpoint); EpoxyClientTlsConfig tlsConfig = endpoint.UseTls ? clientTlsConfig : null; try { EpoxyNetworkStream epoxyStream = await EpoxyNetworkStream.MakeAsync( socketFunc : () => ConnectClientSocketAsync(endpoint), streamFunc : socket => EpoxyNetworkStream.MakeClientStreamAsync(endpoint.Host, socket, tlsConfig, logger), timeoutConfig : timeoutConfig, logger : logger); var connection = EpoxyConnection.MakeClientConnection(this, epoxyStream, logger, metrics); try { connections.Add(connection); } catch (InvalidOperationException) { await connection.StopAsync(); throw new InvalidOperationException("This EpoxyTransport has been stopped already."); } await connection.StartAsync(); return(connection); } catch (Exception ex) { logger.Site().Error(ex, "Failed to start Epoxy client connection to '{0}'", endpoint); throw; } }