コード例 #1
0
ファイル: Server_Tests.cs プロジェクト: xljiulang/MQTTnet
        public async Task Send_Garbage()
        {
            using (var testEnvironment = new TestEnvironment(TestContext))
            {
                await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));

                // Send an invalid packet and ensure that the server will close the connection and stay in a waiting state
                // forever. This is security related.
                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await PlatformAbstractionLayer.ConnectAsync(client, "localhost", testEnvironment.ServerPort);

                var buffer = Encoding.UTF8.GetBytes("Garbage");
                client.Send(buffer, buffer.Length, SocketFlags.None);

                await Task.Delay(TimeSpan.FromSeconds(3));

                try
                {
                    var receivedBytes = await PlatformAbstractionLayer.ReceiveAsync(client, new ArraySegment <byte>(new byte[10]), SocketFlags.Partial);

                    if (receivedBytes == 0)
                    {
                        return;
                    }

                    Assert.Fail("Receive should throw an exception.");
                }
                catch (SocketException)
                {
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        protected IrbisProvider()
        {
            Log.Trace(nameof(IrbisProvider) + "::Constructor");

            Services            = new ServiceContainer();
            PlatformAbstraction = new PlatformAbstractionLayer();
        }
コード例 #3
0
ファイル: Server_Tests.cs プロジェクト: xljiulang/MQTTnet
        public async Task Close_Idle_Connection()
        {
            using (var testEnvironment = new TestEnvironment(TestContext))
            {
                await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(1)));

                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await PlatformAbstractionLayer.ConnectAsync(client, "localhost", testEnvironment.ServerPort);

                // Don't send anything. The server should close the connection.
                await Task.Delay(TimeSpan.FromSeconds(3));

                try
                {
                    var receivedBytes = await PlatformAbstractionLayer.ReceiveAsync(client, new ArraySegment <byte>(new byte[10]), SocketFlags.Partial);

                    if (receivedBytes == 0)
                    {
                        return;
                    }

                    Assert.Fail("Receive should throw an exception.");
                }
                catch (SocketException)
                {
                }
            }
        }
コード例 #4
0
        public async Task Dispose_Channel_While_Used()
        {
            var ct           = new CancellationTokenSource();
            var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                serverSocket.Bind(new IPEndPoint(IPAddress.Any, 50001));
                serverSocket.Listen(0);

#pragma warning disable 4014
                Task.Run(async() =>
#pragma warning restore 4014
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var client = await PlatformAbstractionLayer.AcceptAsync(serverSocket);
                        var data   = new byte[] { 128 };
                        await PlatformAbstractionLayer.SendAsync(client, new ArraySegment <byte>(data), SocketFlags.None);
                    }
                }, ct.Token);

                var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await PlatformAbstractionLayer.ConnectAsync(clientSocket, IPAddress.Loopback, 50001);

                await Task.Delay(100, ct.Token);

                var tcpChannel = new MqttTcpChannel(new NetworkStream(clientSocket, true), "test", null);

                var buffer = new byte[1];
                await tcpChannel.ReadAsync(buffer, 0, 1, ct.Token);

                Assert.AreEqual(128, buffer[0]);

                // This block should fail after dispose.
#pragma warning disable 4014
                Task.Run(() =>
#pragma warning restore 4014
                {
                    Task.Delay(200, ct.Token);
                    tcpChannel.Dispose();
                }, ct.Token);

                try
                {
                    await tcpChannel.ReadAsync(buffer, 0, 1, CancellationToken.None);
                }
                catch (Exception exception)
                {
                    Assert.IsInstanceOfType(exception, typeof(SocketException));
                    Assert.AreEqual(SocketError.OperationAborted, ((SocketException)exception).SocketErrorCode);
                }
            }
            finally
            {
                ct.Cancel(false);
                serverSocket.Dispose();
            }
        }
コード例 #5
0
        void DoWork(CancellationToken cancellationToken)
        {
            try
            {
                _logger.Info("Starting keep alive monitor.");

                while (!cancellationToken.IsCancellationRequested)
                {
                    TryMaintainConnections();
                    PlatformAbstractionLayer.Sleep(_options.KeepAliveMonitorInterval);
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception exception)
            {
                _logger.Error(exception, "Unhandled exception while checking keep alive timeouts.");
            }
            finally
            {
                _logger.Verbose("Stopped checking keep alive timeout.");
            }
        }
 // Getters & Setters ---------------------------------------------------------------------------
 public void setPlatformAbstractionLayer(PlatformAbstractionLayer backtoryPlatform)
 {
     platformAbstractionLayer = backtoryPlatform;
 }
コード例 #7
0
ファイル: NetCore20Runtime.cs プロジェクト: sergey-msu/azos
 public NetCore20Runtime() : this(false)
 {
     PlatformAbstractionLayer.____SetImplementation(this);
 }
コード例 #8
0
 public DotNetFrameworkRuntime() : this(false)
 {
     PlatformAbstractionLayer.____SetImplementation(this);
 }