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 Task <int> ReceiveAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken) { if (buffer.Array == null) { throw new ArgumentNullException(nameof(buffer)); } cancellationToken.ThrowIfCancellationRequested(); using (cancellationToken.Register(() => _udpTransport.Close())) { try { var received = _dtlsTransport.Receive(buffer.Array, buffer.Offset, buffer.Count, 0); return(Task.FromResult(received)); } catch (ObjectDisposedException) { return(Task.FromResult(0)); } catch (SocketException) { return(Task.FromResult(0)); } } }
public Task <int> ReceiveAsync(ArraySegment <byte> buffer, CancellationToken cancellationToken) { int received; do { received = _dtlsTransport.Receive(buffer.Array, buffer.Offset, buffer.Count, 100); }while (received == 0 && !cancellationToken.IsCancellationRequested); return(Task.FromResult(received)); }
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); }
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); } }
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"); } }
public int Receive(byte[] buffer, int offset, int count, int waitMillis) { return(_dtlsTransport.Receive(buffer, offset, count, waitMillis)); }