/// <summary> /// Writes a byte array to the underlying client stream. /// </summary> /// <param name="client">The stream to write the line to.</param> /// <param name="buffer">The byte array buffer to write to the client stream.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A task which asynchronously performs the operation.</returns> public static Task WriteAsync(this INetworkClient client, byte[] buffer, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } return(client.WriteAsync(new [] { new ArraySegment <byte>(buffer) }, cancellationToken)); }
/// <summary> /// Read a line from the byte stream. /// </summary> /// <param name="client">The stream to write the line to.</param> /// <param name="text">The text to write to the client stream.</param> /// <param name="encoding">The encoding to use when converting the bytes to a text representation.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A task which asynchronously performs the operation.</returns> public static Task WriteLineAsync(this INetworkClient client, string text, Encoding encoding, CancellationToken cancellationToken = default(CancellationToken)) { if (client == null) { throw new ArgumentNullException(nameof(client)); } return(client.WriteAsync(encoding.GetBytes(text + Environment.NewLine), cancellationToken)); }
/// <summary> /// Read a line from the byte stream. /// </summary> /// <param name="client">The stream to write the line to.</param> /// <param name="text">The text to write to the client stream.</param> /// <param name="encoding">The encoding to use when converting the bytes to a text representation.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>A task which asynchronously performs the operation.</returns> public static Task WriteLineAsync(this INetworkClient client, string text, Encoding encoding, CancellationToken cancellationToken = default) { if (client == null) { throw new ArgumentNullException(nameof(client)); } var newLine = new string(new[] { (char)13, (char)10 }); return(client.WriteAsync(encoding.GetBytes(text + newLine), cancellationToken)); }