Beispiel #1
0
        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);

            lock (connectionsLock)
            {
                connections.Add(connection);
            }

            logger.Site().Debug("Setup server-side connection for {0}. Starting Epoxy handshake.", connection.RemoteEndPoint);
            await connection.StartAsync();
        }
Beispiel #2
0
        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);

                // TODO: keep these in some master collection for shutdown
                var connection = EpoxyConnection.MakeClientConnection(this, epoxyStream, logger, metrics);
                await connection.StartAsync();

                return(connection);
            }
            catch (Exception ex)
            {
                logger.Site().Error(ex, "Failed to start Epoxy client connection to '{0}'", endpoint);
                throw;
            }
        }
        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;
            }
        }