Example #1
0
        public async Task SendAsync_AfterServerDisconnect_ClientAutomaticallyReconnects()
        {
            ITransport         clientTransport   = null;
            ITransport         serverTransport   = null;
            ITransportListener transportListener = null;

            try
            {
                // Arrange
                transportListener = CreateTransportListener(ListenerUri, EnvelopeSerializer);
                await transportListener.StartAsync(CancellationToken);

                clientTransport = CreateClientTransport(EnvelopeSerializer);
                var serverTransportTask = transportListener.AcceptTransportAsync(CancellationToken);
                await clientTransport.OpenAsync(ListenerUri, CancellationToken);

                serverTransport = await serverTransportTask;
                await serverTransport.OpenAsync(ListenerUri, CancellationToken);

                var message = Dummy.CreateMessage(Dummy.CreateTextContent());

                // Act
                await transportListener.StopAsync(CancellationToken);

                await serverTransport.CloseAsync(CancellationToken);

                transportListener = CreateTransportListener(ListenerUri, EnvelopeSerializer);
                await transportListener.StartAsync(CancellationToken);

                serverTransport = await transportListener.AcceptTransportAsync(CancellationToken);

                await serverTransport.OpenAsync(ListenerUri, CancellationToken);

                await clientTransport.SendAsync(message, CancellationToken);

                var actual = await serverTransport.ReceiveAsync(CancellationToken);

                // Assert
                var actualMessage = actual.ShouldBeOfType <Message>();
                CompareMessages(message, actualMessage);
            }
            finally
            {
                (clientTransport as IDisposable)?.Dispose();
                (serverTransport as IDisposable)?.Dispose();
                (transportListener as IDisposable)?.Dispose();
            }
        }
Example #2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (_listenerTask != null)
                {
                    throw new InvalidOperationException("The server is already started");
                }

                // Initialize a block for holding the channel consumer tasks.
                _consumerBlock = new ActionBlock <IServerChannel>(
                    ConsumeAsync,
                    new ExecutionDataflowBlockOptions
                {
                    BoundedCapacity        = _maxActiveChannels,
                    MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded,
                    EnsureOrdered          = false
                });

                await _transportListener.StartAsync(cancellationToken).ConfigureAwait(false);

                // Initialize a background task for listening for new transport connections
                _listenerCts  = new CancellationTokenSource();
                _listenerTask = Task.Run(() => AcceptTransportsAsync(_listenerCts.Token));
            }
            finally
            {
                _semaphore.Release();
            }
        }
Example #3
0
        public async Task StartServerAsync()
        {
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            //var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "f864d23e92894c56df566b7ab7a9c6411d50d14d", false);
            var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "3fd0b5f28dc32f5f0bb9f44cf1f6816846e44cbe", false);
            
            if (certificates.Count == 0)
            {
                throw new InvalidOperationException("Server certificate not found");
            }

            store.Close();
#if DEBUG
            ITraceWriter traceWriter = new DebugTraceWriter("Server"); 
#else
            ITraceWriter traceWriter = new FileTraceWriter("server.log"); 
#endif

            _listener = new TcpTransportListener(
                _listenerUri,
                certificates[0],
                new EnvelopeSerializer(),
                traceWriter
                );


            await _listener.StartAsync();
        }
Example #4
0
        public async Task StartServerAsync()
        {
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            store.Open(OpenFlags.ReadOnly);

            //var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "f864d23e92894c56df566b7ab7a9c6411d50d14d", false);
            var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, "3fd0b5f28dc32f5f0bb9f44cf1f6816846e44cbe", false);

            if (certificates.Count == 0)
            {
                throw new InvalidOperationException("Server certificate not found");
            }

            store.Close();
#if DEBUG
            ITraceWriter traceWriter = new DebugTraceWriter("Server");
#else
            ITraceWriter traceWriter = new FileTraceWriter("server.log");
#endif

            _listener = new TcpTransportListener(
                _listenerUri,
                certificates[0],
                new EnvelopeSerializer(),
                traceWriter
                );


            await _listener.StartAsync();
        }
Example #5
0
        public TcpTransportTests()
        {
            _uri = new Uri("net.tcp://localhost:55321");
            _cancellationToken  = TimeSpan.FromSeconds(30).ToCancellationToken();
            _envelopeSerializer = new FakeEnvelopeSerializer(10);
            _transportListener  = new TcpTransportListener(_uri, null, _envelopeSerializer);
            _transportListener.StartAsync(_cancellationToken).Wait();
            var serverTcpTransportTask = _transportListener.AcceptTransportAsync(_cancellationToken);

            _clientTransport = new TcpTransport(_envelopeSerializer);
            _clientTransport.OpenAsync(_uri, _cancellationToken).Wait();
            _serverTransport = (TcpTransport)serverTcpTransportTask.Result;
            _serverTransport.OpenAsync(_uri, _cancellationToken).Wait();
        }
        public async Task SetupAsync()
        {
            await _transportListener.StartAsync(_cancellationToken);

            var serverTcpTransportTask = _transportListener.AcceptTransportAsync(_cancellationToken);

            _clientTransport = new TcpTransport(_envelopeSerializer, null);
            await _clientTransport.OpenAsync(_uri, _cancellationToken);

            _serverTransport = (TcpTransport)await serverTcpTransportTask;
            await _serverTransport.OpenAsync(_uri, _cancellationToken);

            _message = Dummy.CreateMessage(Dummy.CreateTextContent());
        }
        public async Task SetUp()
        {
            _uri = CreateUri();
            _cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            _cancellationToken  = _cts.Token;
            _envelopeSerializer = new EnvelopeSerializer(new DocumentTypeResolver().WithMessagingDocuments());
            _transportListener  = CreateTransportListener(_uri, _envelopeSerializer);
            await _transportListener.StartAsync(_cancellationToken);

            var serverTcpTransportTask = _transportListener.AcceptTransportAsync(_cancellationToken);

            _clientTransport = new SynchronizedTransportDecorator(CreateClientTransport(_envelopeSerializer));
            await _clientTransport.OpenAsync(_uri, _cancellationToken);

            _serverTransport = new SynchronizedTransportDecorator(await serverTcpTransportTask);
            await _serverTransport.OpenAsync(_uri, _cancellationToken);
        }
        public WebSocketTransportBinaryTests()
        {
            var trace = new CustomTraceWriter();

            _uri = new Uri("ws://localhost:8081");
            _cancellationToken  = TimeSpan.FromSeconds(30).ToCancellationToken();
            _envelopeSerializer = new EnvelopeSerializer(new DocumentTypeResolver().WithMessagingDocuments());
            _transportListener  = new WebSocketTransportListener(_uri, null, _envelopeSerializer, trace, webSocketMessageType: System.Net.WebSockets.WebSocketMessageType.Binary);
            _transportListener.StartAsync(_cancellationToken).Wait();

            var serverTcpTransportTask = _transportListener.AcceptTransportAsync(_cancellationToken);

            _clientTransport = new ClientWebSocketTransport(_envelopeSerializer, trace, webSocketMessageType: System.Net.WebSockets.WebSocketMessageType.Binary);
            _clientTransport.OpenAsync(_uri, _cancellationToken).Wait();

            _serverTransport = (WebSocketTransport)serverTcpTransportTask.Result;
            _serverTransport.OpenAsync(_uri, _cancellationToken).Wait();
        }