Beispiel #1
0
        public async ValueTask <ITransmissionConnection> ConnectAsync(string brokerWorkingDir, CancellationToken cancellationToken = default)
        {
            var webSocketAddress = _secure
                ? EnvironmentHelper.GetWebSocketSecureAddress()
                : EnvironmentHelper.GetWebSocketAddress();

            if (string.IsNullOrEmpty(webSocketAddress))
            {
                Log.Trace("Waiting initialization of server {0}", _serverName);
                var serverStateReader = new ServerStateReader(_serverName, brokerWorkingDir);
                if (!await serverStateReader
                    .WaitInitializationAsync(MaxServerInitializationTime, cancellationToken)
                    .ConfigureAwait(false))
                {
                    throw new TimeoutException(
                              $"Timeout ({MaxServerInitializationTime.TotalSeconds}sec) while waiting for server \"{_serverName}\" availability");
                }

                webSocketAddress = serverStateReader.ReadSetting("address");
            }

            if (string.IsNullOrEmpty(webSocketAddress))
            {
                throw new InvalidOperationException("Cannot find url to connect");
            }

            Log.Trace("Creating new connection to url {0}", webSocketAddress);
            var connection = new WebSocketClientTransmissionConnection(webSocketAddress);
            await connection.ConnectAsync(cancellationToken).ConfigureAwait(false);

            Log.Trace("Created new connection {0} to url {1}", connection.Id, webSocketAddress);
            return(connection);
        }
Beispiel #2
0
        private async ValueTask <Stream> ConnectStreamAsync(string brokerWorkingDir, CancellationToken cancellationToken)
        {
            var serverStateReader = new ServerStateReader(ServerName, brokerWorkingDir);

            if (!await serverStateReader
                .WaitInitializationAsync(MaxServerInitializationTime, cancellationToken)
                .ConfigureAwait(false))
            {
                throw new TimeoutException(
                          $"Timeout ({MaxServerInitializationTime.TotalSeconds}sec) while waiting for server \"{ServerName}\" availability");
            }
            var pipeName = serverStateReader.ReadSetting("address");

            if (string.IsNullOrEmpty(pipeName))
            {
                throw new InvalidOperationException("Cannot find pipe name to connect");
            }
            Log.Trace("Connecting to pipe");
            var pipeClientStream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

            try
            {
                await ConnectAsync(pipeClientStream, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.Trace("Connection to pipe terminated: {0}", ex.FormatTypeAndMessage());
                pipeClientStream.Dispose();
                cancellationToken.ThrowIfCancellationRequested();
                throw;
            }
            Log.Trace("Connected to pipe");
            return(pipeClientStream);
        }