/// <summary>
 /// Used to establish the connection used by this server and begin listening for incoming messages.
 /// </summary>
 /// <returns>A <see cref="Task"/> to handle the server listen operation. This task will not resolve as long as the server is running.</returns>
 public Task StartAsync()
 {
     _closedSignal = new TaskCompletionSource <string>();
     _sender.Connect(_websocketTransport);
     _receiver.Connect(_websocketTransport);
     return(_closedSignal.Task);
 }
        /// <summary>
        /// Establish a connection with optional custom headers.
        /// </summary>
        /// <param name="requestHeaders">An optional <see cref="IDictionary{TKey, TValue}"/> of string header names and string header values to include when sending the
        /// initial request to establish this connection.
        /// </param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
        public async Task ConnectAsync(IDictionary <string, string> requestHeaders = null)
        {
            if (IsConnected)
            {
                return;
            }

            var clientWebSocket = new ClientWebSocket();

            if (requestHeaders != null)
            {
                foreach (var key in requestHeaders.Keys)
                {
                    clientWebSocket.Options.SetRequestHeader(key, requestHeaders[key]);
                }
            }

            await clientWebSocket.ConnectAsync(new Uri(_url), CancellationToken.None).ConfigureAwait(false);

            var socketTransport = new WebSocketTransport(clientWebSocket);

            // Listen for disconnected events.
            _sender.Disconnected   += OnConnectionDisconnected;
            _receiver.Disconnected += OnConnectionDisconnected;

            _sender.Connect(socketTransport);
            _receiver.Connect(socketTransport);

            IsConnected = true;
        }
Beispiel #3
0
        /// <summary>
        /// Used to establish the connection used by this server and begin listening for incoming messages.
        /// </summary>
        /// <returns>A <see cref="Task"/> to handle the server listen operation. This task will not resolve as long as the server is running.</returns>
        public async Task StartAsync()
        {
            var incomingPipeName = _baseName + NamedPipeTransport.ServerIncomingPath;
            var incomingServer   = new NamedPipeServerStream(incomingPipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await incomingServer.WaitForConnectionAsync().ConfigureAwait(false);

            var outgoingPipeName = _baseName + NamedPipeTransport.ServerOutgoingPath;
            var outgoingServer   = new NamedPipeServerStream(outgoingPipeName, PipeDirection.Out, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await outgoingServer.WaitForConnectionAsync().ConfigureAwait(false);

            _sender.Connect(new NamedPipeTransport(outgoingServer));
            _receiver.Connect(new NamedPipeTransport(incomingServer));
        }
        /// <summary>
        /// Establish a connection with optional custom headers.
        /// </summary>
        /// <param name="requestHeaders">An optional <see cref="IDictionary{TKey, TValue}"/> of string header names and string header values to include when sending the
        /// initial request to establish this connection.
        /// </param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
        public async Task ConnectAsync(IDictionary <string, string> requestHeaders)
        {
            var outgoingPipeName = _baseName + NamedPipeTransport.ServerIncomingPath;
            var outgoing         = new NamedPipeClientStream(".", outgoingPipeName, PipeDirection.Out, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await outgoing.ConnectAsync().ConfigureAwait(false);

            var incomingPipeName = _baseName + NamedPipeTransport.ServerOutgoingPath;
            var incoming         = new NamedPipeClientStream(".", incomingPipeName, PipeDirection.In, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await incoming.ConnectAsync().ConfigureAwait(false);

            _sender.Connect(new NamedPipeTransport(outgoing));
            _receiver.Connect(new NamedPipeTransport(incoming));
        }
        /// <summary>
        /// Establish a connection with optional custom headers.
        /// </summary>
        /// <param name="requestHeaders">An optional <see cref="IDictionary{TKey, TValue}"/> of string header names and string header values to include when sending the
        /// initial request to establish this connection.
        /// </param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> used to signal this operation should be cancelled.</param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods (can't change this without breaking binary compat)
        public async Task ConnectAsyncEx(IDictionary <string, string> requestHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods
        {
            if (IsConnected)
            {
                return;
            }

            var clientWebSocket = new ClientWebSocket();

            if (requestHeaders != null)
            {
                foreach (var key in requestHeaders.Keys)
                {
                    clientWebSocket.Options.SetRequestHeader(key, requestHeaders[key]);
                }
            }

            await clientWebSocket.ConnectAsync(new Uri(_url), cancellationToken).ConfigureAwait(false);

#pragma warning disable CA2000 // Dispose objects before losing scope

            // We don't dispose the websocket, since WebSocketTransport is now
            // the owner of the web socket.
            var socketTransport = new WebSocketTransport(clientWebSocket);
#pragma warning restore CA2000 // Dispose objects before losing scope

            // Listen for disconnected events.
            _sender.Disconnected   += OnConnectionDisconnected;
            _receiver.Disconnected += OnConnectionDisconnected;

            _sender.Connect(socketTransport);
            _receiver.Connect(socketTransport);

            IsConnected = true;
        }
        /// <summary>
        /// Establish a connection with optional custom headers.
        /// </summary>
        /// <param name="requestHeaders">An optional <see cref="IDictionary{TKey, TValue}"/> of string header names and string header values to include when sending the
        /// initial request to establish this connection.
        /// </param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
        public async Task ConnectAsync(IDictionary <string, string> requestHeaders)
        {
            var outgoingPipeName = _baseName + NamedPipeTransport.ServerIncomingPath;
            var outgoing         = new NamedPipeClientStream(".", outgoingPipeName, PipeDirection.Out, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await outgoing.ConnectAsync().ConfigureAwait(false);

            var incomingPipeName = _baseName + NamedPipeTransport.ServerOutgoingPath;
            var incoming         = new NamedPipeClientStream(".", incomingPipeName, PipeDirection.In, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await incoming.ConnectAsync().ConfigureAwait(false);

#pragma warning disable CA2000 // Dispose objects before losing scope

            // We don't dispose the websocket, since NamedPipeTransport is now
            // the owner of the web socket.
            _sender.Connect(new NamedPipeTransport(outgoing));
            _receiver.Connect(new NamedPipeTransport(incoming));

#pragma warning restore CA2000 // Dispose objects before losing scope
        }
Beispiel #7
0
        /// <summary>
        /// Used to establish the connection used by this server and begin listening for incoming messages.
        /// </summary>
        /// <returns>A <see cref="Task"/> to handle the server listen operation. This task will not resolve as long as the server is running.</returns>
        public async Task StartAsync()
        {
            var incomingPipeName = _baseName + NamedPipeTransport.ServerIncomingPath;
            var incomingServer   = new NamedPipeServerStream(incomingPipeName, PipeDirection.In, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await incomingServer.WaitForConnectionAsync().ConfigureAwait(false);

            var outgoingPipeName = _baseName + NamedPipeTransport.ServerOutgoingPath;
            var outgoingServer   = new NamedPipeServerStream(outgoingPipeName, PipeDirection.Out, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.WriteThrough | PipeOptions.Asynchronous);
            await outgoingServer.WaitForConnectionAsync().ConfigureAwait(false);

#pragma warning disable CA2000 // Dispose objects before losing scope

            // Ownership of the disposable transports is passed to the
            // sender and receiver, which now need to take care of
            // disposing them when the time is right.
            _sender.Connect(new NamedPipeTransport(outgoingServer));
            _receiver.Connect(new NamedPipeTransport(incomingServer));

#pragma warning restore CA2000 // Dispose objects before losing scope
        }