示例#1
0
        public async Task SetUp()
        {
            _stream    = new Mock <Stream>();
            _tcpClient = new Mock <ITcpClient>();
            _tcpClient
            .Setup(c => c.GetStream())
            .Returns(() => _stream.Object);

            _envelopeSerializer = new Mock <IEnvelopeSerializer>();
            _traceWriter        = new Mock <ITraceWriter>();

            _cancellationToken = TimeSpan.FromSeconds(10).ToCancellationToken();
            _serverUri         = new Uri($"net.tcp://*****:*****@fakedomain.local");
            _serverCertificate = GetDomainCertificate(_serverIdentity);
            _tcpListener       = new TcpTransportListener(
                _serverUri,
                _serverCertificate,
                _envelopeSerializer.Object,
                clientCertificateValidationCallback:
                (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) =>
            {
                return(true);
            });
            await _tcpListener.StartAsync(_cancellationToken);
        }
示例#2
0
        public async Task StartAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await ListenerSemaphore.WaitAsync(cancellationToken);

            await _transportListener.StartAsync();

#pragma warning disable 4014
            ProducerConsumer.CreateAsync(

                c => _transportListener.AcceptTransportAsync(c),
                async t =>
            {
                await t.OpenAsync(null, _cts.Token);

                var serverChannel = new ServerChannel(
                    Guid.NewGuid().ToString(),
                    new Node("postmaster", "msging.net", "instance"),
                    t,
                    TimeSpan.FromSeconds(60),
                    autoReplyPings: true);

                var clientNode = new Node("client", "msging.net", "instance");

                await serverChannel.EstablishSessionAsync(
                    new[] { SessionCompression.None },
                    new[] { SessionEncryption.None },
                    new[]
                {
                    AuthenticationScheme.Guest,
                    AuthenticationScheme.Key,
                    AuthenticationScheme.Plain,
                    AuthenticationScheme.Transport,
                },
                    (n, a) => new AuthenticationResult(null, clientNode).AsCompletedTask(), _cts.Token);

                var channelListener = new ChannelListener(
                    m => TaskUtil.TrueCompletedTask,
                    n => TaskUtil.TrueCompletedTask,
                    async c =>
                {
                    if (c.Status == CommandStatus.Pending)
                    {
                        await serverChannel.SendCommandAsync(
                            new Command(c.Id)
                        {
                            Status = CommandStatus.Success,
                            Method = c.Method
                        },
                            _cts.Token);
                    }
                    return(true);
                });

                channelListener.Start(serverChannel);

                return(true);
            },
                _cts.Token);
        }
示例#3
0
        public async Task SetupAsync()
        {
            _uri = new Uri("net.tcp://localhost:55321");
            _cancellationToken    = TimeSpan.FromSeconds(30).ToCancellationToken();
            _envelopeSerializer   = new FakeEnvelopeSerializer(10);
            _tcpTransportListener = new TcpTransportListener(_uri, null, _envelopeSerializer);
            await _tcpTransportListener.StartAsync(_cancellationToken);

            var serverTcpTransportTask = _tcpTransportListener.AcceptTransportAsync(_cancellationToken);

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

            _serverTcpTransport = (TcpTransport)await serverTcpTransportTask;
            await _serverTcpTransport.OpenAsync(_uri, _cancellationToken);
        }
        public async Task StartAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await ListenerSemaphore.WaitAsync(cancellationToken);

            await _transportListener.StartAsync();

            ProducerConsumer.CreateAsync(
                c => _transportListener.AcceptTransportAsync(c),
                async(transport, _) =>
            {
                await transport.OpenAsync(null, _cts.Token);

                var serverChannel = new ServerChannel(
                    Guid.NewGuid().ToString(),
                    new Node("postmaster", "msging.net", "instance"),
                    transport,
                    TimeSpan.FromSeconds(60),
                    autoReplyPings: true);

                await serverChannel.EstablishSessionAsync(
                    new[] { SessionCompression.None },
                    new[] { SessionEncryption.None },
                    new[]
                {
                    AuthenticationScheme.Guest,
                    AuthenticationScheme.Key,
                    AuthenticationScheme.Plain,
                    AuthenticationScheme.Transport,
                    AuthenticationScheme.External
                },
                    (n, a, _) =>
                {
                    Authentications.Enqueue(a);
                    return(new AuthenticationResult(DomainRole.RootAuthority, a).AsCompletedTask());
                },
                    (n, s, c) =>
                {
                    return(n.AsCompletedTask());
                }, _cts.Token);

                var channelListener = new ChannelListener(
                    m =>
                {
                    Messages.Enqueue(m);
                    return(TaskUtil.TrueCompletedTask);
                },
                    n =>
                {
                    Notifications.Enqueue(n);
                    return(TaskUtil.TrueCompletedTask);
                },
                    async c =>
                {
                    Commands.Enqueue(c);
                    if (c.Status == CommandStatus.Pending)
                    {
                        await serverChannel.SendCommandAsync(
                            new Command(c.Id)
                        {
                            Status = CommandStatus.Success,
                            Method = c.Method
                        },
                            _cts.Token);
                    }
                    return(true);
                });

                channelListener.Start(serverChannel);
                Channels.Enqueue(serverChannel);

                return(true);
            },
                _cts.Token);
        }