public async Task <CoapPacket> ReceiveAsync(CancellationToken token)
        {
            EnsureConnected();

            var bufLen = _datagramTransport.GetReceiveLimit();
            var buffer = new byte[bufLen];

            while (!token.IsCancellationRequested)
            {
                // we can't cancel waiting for a packet (BouncyCastle doesn't support this), so there will be a bit of delay between cancelling and actually stopping trying to receive.
                // there is a wait timeout of 5000ms to close the CoapEndPoint, this has to be less than that.
                // also, we use a long running task here so we don't block the calling thread till we're done waiting, but start a new one and yield instead
                int received = await Task.Factory.StartNew(() => _datagramTransport.Receive(buffer, 0, bufLen, 4000), TaskCreationOptions.LongRunning);

                if (received > 0)
                {
                    return(await Task.FromResult(new CoapPacket
                    {
                        Payload = new ArraySegment <byte>(buffer, 0, received).ToArray(),
                        Endpoint = this
                    }));
                }
            }

            throw new OperationCanceledException();
        }
        public void TestClientServer()
        {
            SecureRandom secureRandom = new SecureRandom();

            DtlsClientProtocol clientProtocol = new DtlsClientProtocol(secureRandom);
            DtlsServerProtocol serverProtocol = new DtlsServerProtocol(secureRandom);

            MockDatagramAssociation network = new MockDatagramAssociation(1500);

            Server server = new Server(serverProtocol, network.Server);

            Thread serverThread = new Thread(new ThreadStart(server.Run));

            serverThread.Start();

            DatagramTransport clientTransport = network.Client;

            clientTransport = new UnreliableDatagramTransport(clientTransport, secureRandom, 0, 0);

            clientTransport = new LoggingDatagramTransport(clientTransport, Console.Out);

            MockDtlsClient client = new MockDtlsClient(null);

            DtlsTransport dtlsClient = clientProtocol.Connect(client, clientTransport);

            for (int i = 1; i <= 10; ++i)
            {
                byte[] data = new byte[i];
                Arrays.Fill(data, (byte)i);
                dtlsClient.Send(data, 0, data.Length);
            }

            byte[] buf = new byte[dtlsClient.GetReceiveLimit()];
            while (dtlsClient.Receive(buf, 0, buf.Length, 100) >= 0)
            {
            }

            dtlsClient.Close();

            server.Shutdown(serverThread);
        }
示例#3
0
 public void Run()
 {
     try
     {
         DtlsTransport dtlsServer = mServerProtocol.Accept(mServerImpl, mServerTransport);
         byte[]        buf        = new byte[dtlsServer.GetReceiveLimit()];
         while (!isShutdown)
         {
             int length = dtlsServer.Receive(buf, 0, buf.Length, 100);
             if (length >= 0)
             {
                 dtlsServer.Send(buf, 0, length);
             }
         }
         dtlsServer.Close();
     }
     catch (Exception e)
     {
         mCaught = e;
         mOuter.LogException(mCaught);
     }
 }
 public void Run()
 {
     try
     {
         MockDtlsServer server     = new MockDtlsServer();
         DtlsTransport  dtlsServer = mServerProtocol.Accept(server, mServerTransport);
         byte[]         buf        = new byte[dtlsServer.GetReceiveLimit()];
         while (!isShutdown)
         {
             int length = dtlsServer.Receive(buf, 0, buf.Length, 1000);
             if (length >= 0)
             {
                 dtlsServer.Send(buf, 0, length);
             }
         }
         dtlsServer.Close();
     }
     catch (Exception e)
     {
         Console.Error.WriteLine(e.StackTrace);
     }
 }
示例#5
0
        public void RunTest(TlsTestConfig config)
        {
            CheckDtlsVersion(config.clientMinimumVersion);
            CheckDtlsVersion(config.clientOfferVersion);
            CheckDtlsVersion(config.serverMaximumVersion);
            CheckDtlsVersion(config.serverMinimumVersion);

            SecureRandom secureRandom = new SecureRandom();

            DtlsClientProtocol clientProtocol = new DtlsClientProtocol(secureRandom);
            DtlsServerProtocol serverProtocol = new DtlsServerProtocol(secureRandom);

            MockDatagramAssociation network = new MockDatagramAssociation(1500);

            TlsTestClientImpl clientImpl = new TlsTestClientImpl(config);
            TlsTestServerImpl serverImpl = new TlsTestServerImpl(config);

            Server server = new Server(this, serverProtocol, network.Server, serverImpl);

            Thread serverThread = new Thread(new ThreadStart(server.Run));

            serverThread.Start();

            Exception caught = null;

            try
            {
                DatagramTransport clientTransport = network.Client;

                if (TlsTestConfig.DEBUG)
                {
                    clientTransport = new LoggingDatagramTransport(clientTransport, Console.Out);
                }

                DtlsTransport dtlsClient = clientProtocol.Connect(clientImpl, clientTransport);

                for (int i = 1; i <= 10; ++i)
                {
                    byte[] data = new byte[i];
                    Arrays.Fill(data, (byte)i);
                    dtlsClient.Send(data, 0, data.Length);
                }

                byte[] buf = new byte[dtlsClient.GetReceiveLimit()];
                while (dtlsClient.Receive(buf, 0, buf.Length, 100) >= 0)
                {
                }

                dtlsClient.Close();
            }
            catch (Exception e)
            {
                caught = e;
                LogException(caught);
            }

            server.Shutdown(serverThread);

            // TODO Add checks that the various streams were closed

            Assert.AreEqual(config.expectFatalAlertConnectionEnd, clientImpl.FirstFatalAlertConnectionEnd, "Client fatal alert connection end");
            Assert.AreEqual(config.expectFatalAlertConnectionEnd, serverImpl.FirstFatalAlertConnectionEnd, "Server fatal alert connection end");

            Assert.AreEqual(config.expectFatalAlertDescription, clientImpl.FirstFatalAlertDescription, "Client fatal alert description");
            Assert.AreEqual(config.expectFatalAlertDescription, serverImpl.FirstFatalAlertDescription, "Server fatal alert description");

            if (config.expectFatalAlertConnectionEnd == -1)
            {
                Assert.IsNull(caught, "Unexpected client exception");
                Assert.IsNull(server.mCaught, "Unexpected server exception");
            }
        }