Exemplo n.º 1
0
        private async Task WriteInternalAsync(byte[] bytes, CancellationToken cancellationToken)
        {
            InactivityTimer?.Reset();

            var totalBytesWritten = 0;

            try
            {
                while (totalBytesWritten < bytes.Length)
                {
                    var bytesRemaining = bytes.Length - totalBytesWritten;
                    var bytesToWrite   = bytesRemaining > TcpClient.Client.SendBufferSize ? TcpClient.Client.SendBufferSize : bytesRemaining;

                    await Stream.WriteAsync(bytes, totalBytesWritten, bytesToWrite, cancellationToken).ConfigureAwait(false);

                    totalBytesWritten += bytesToWrite;

                    DataWritten?.Invoke(this, new ConnectionDataEventArgs(totalBytesWritten, bytes.Length));
                    InactivityTimer?.Reset();
                }
            }
            catch (Exception ex) when(!(ex is TimeoutException) && !(ex is OperationCanceledException))
            {
                Disconnect($"Write error: {ex.Message}");
                throw new ConnectionWriteException($"Failed to write {bytes.Length} bytes to {IPAddress}:{Port}: {ex.Message}", ex);
            }
        }
 private void OnDataWritten(object e, DownloaderEventArgs eventArgs)
 {
     DataWritten?.Invoke(e, eventArgs);
 }
Exemplo n.º 3
0
 protected virtual void OnDataWritten(object sender, DataWrittenEventArgs e)
 {
     DataWritten?.Invoke(sender, e);
 }