/// <summary> /// Receives <paramref name="count" /> bytes, then shuffles them to host order. /// </summary> /// <param name="count">The number of bytes to receive</param> /// <param name="token">The token on which this receive operation can be cancelled.</param> /// <returns>The receiving buffer (in host order).</returns> private byte[] ReceiveNetworkOrder(int count, CancellationToken token) { var buf = _reader.ReadBytes(count); if (buf.Length != count) { // Stream has ended, which generally means we're about to be cancelled. token.WaitHandle.WaitOne(); } token.ThrowIfCancellationRequested(); BitManipulation.ShuffleForNetworkOrder(buf, count); return(buf); }
/// <summary> /// Sends all given data to the stream synchronously, in /// network (big-endian) order. /// <para> /// If the host architecture is little-endian, the contents /// of <paramref name="bytes" /> will be reversed in-place. /// </para> /// </summary> /// <param name="bytes">The data to send (in its entirety).</param> private void SendNetworkOrder(byte[] bytes) { BitManipulation.ShuffleForNetworkOrder(bytes, bytes.Length); _writer.Write(bytes); }