public async Task SendPacketAsync(MqttBasePacket packet, TimeSpan timeout, CancellationToken cancellationToken) { ThrowIfDisposed(); cancellationToken.ThrowIfCancellationRequested(); try { await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); } catch (ObjectDisposedException) { throw new OperationCanceledException(); } try { var packetData = PacketFormatterAdapter.Encode(packet); if (timeout == TimeSpan.Zero) { await _channel.WriteAsync(packetData.Array, packetData.Offset, packetData.Count, cancellationToken).ConfigureAwait(false); } else { await MqttTaskTimeout.WaitAsync( t => _channel.WriteAsync(packetData.Array, packetData.Offset, packetData.Count, t), timeout, cancellationToken).ConfigureAwait(false); } Interlocked.Add(ref _bytesReceived, packetData.Count); _logger.Verbose("TX ({0} bytes) >>> {1}", packetData.Count, packet); } catch (Exception exception) { if (IsWrappedException(exception)) { throw; } WrapException(exception); } finally { PacketFormatterAdapter.FreeBuffer(); try { _writerSemaphore.Release(); } catch (ObjectDisposedException) { throw new OperationCanceledException(); } } }
public async Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken) { await _writerSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { var packetData = PacketSerializer.Serialize(packet); await _channel.WriteAsync(packetData.Array, packetData.Offset, packetData.Count, cancellationToken).ConfigureAwait(false); PacketSerializer.FreeBuffer(); _logger.Verbose("TX >>> {0}", packet); } catch (Exception exception) { if (IsWrappedException(exception)) { throw; } WrapException(exception); } finally { _writerSemaphore.Release(); } }
public async Task SendPacketAsync(MqttBasePacket packet, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); ThrowIfDisposed(); using (await _syncRoot.WaitAsync(cancellationToken).ConfigureAwait(false)) { // Check for cancellation here again because "WaitAsync" might take some time. cancellationToken.ThrowIfCancellationRequested(); try { var packetData = PacketFormatterAdapter.Encode(packet); await _channel.WriteAsync(packetData.Array, packetData.Offset, packetData.Count, cancellationToken).ConfigureAwait(false); Interlocked.Add(ref _bytesReceived, packetData.Count); _logger.Verbose("TX ({0} bytes) >>> {1}", packetData.Count, packet); } catch (Exception exception) { if (IsWrappedException(exception)) { throw; } WrapAndThrowException(exception); } finally { PacketFormatterAdapter.FreeBuffer(); } } }
async Task WriteAsync(int iterations, int size) { await Task.Yield(); for (var i = 0; i < iterations; i++) { await _serverChannel.WriteAsync(new byte[size], 0, size, CancellationToken.None).ConfigureAwait(false); } }
public async Task SendPacketAsync(MqttPacket packet, CancellationToken cancellationToken) { ThrowIfDisposed(); // This lock makes sure that multiple threads can send packets at the same time. // This is required when a disconnect is sent from another thread while the // worker thread is still sending publish packets etc. using (await _syncRoot.WaitAsync(cancellationToken).ConfigureAwait(false)) { // Check for cancellation here again because "WaitAsync" might take some time. cancellationToken.ThrowIfCancellationRequested(); try { var packetBuffer = PacketFormatterAdapter.Encode(packet); _packetInspector?.BeginSendPacket(packetBuffer); _logger.Verbose("TX ({0} bytes) >>> {1}", packetBuffer.Length, packet); await _channel.WriteAsync(packetBuffer.Packet.Array, packetBuffer.Packet.Offset, packetBuffer.Packet.Count, cancellationToken).ConfigureAwait(false); if (packetBuffer.Payload.Count > 0) { await _channel.WriteAsync(packetBuffer.Payload.Array, packetBuffer.Payload.Offset, packetBuffer.Payload.Count, cancellationToken).ConfigureAwait(false); } Interlocked.Add(ref _bytesReceived, packetBuffer.Length); } catch (Exception exception) { if (!WrapAndThrowException(exception)) { throw; } } finally { PacketFormatterAdapter.Cleanup(); } } }