| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | namespace System.Net |
| | | 11 | | { |
| | | 12 | | internal sealed class StreamFramer |
| | | 13 | | { |
| | 0 | 14 | | private readonly FrameHeader _writeHeader = new FrameHeader(); |
| | 0 | 15 | | private readonly FrameHeader _curReadHeader = new FrameHeader(); |
| | | 16 | | |
| | 0 | 17 | | private readonly byte[] _readHeaderBuffer = new byte[FrameHeader.Size]; |
| | 0 | 18 | | private readonly byte[] _writeHeaderBuffer = new byte[FrameHeader.Size]; |
| | | 19 | | private bool _eof; |
| | | 20 | | |
| | 0 | 21 | | public FrameHeader ReadHeader => _curReadHeader; |
| | 0 | 22 | | public FrameHeader WriteHeader => _writeHeader; |
| | | 23 | | |
| | | 24 | | public async ValueTask<byte[]?> ReadMessageAsync<TAdapter>(Stream stream, CancellationToken cancellationToken) |
| | | 25 | | where TAdapter : IReadWriteAdapter |
| | 0 | 26 | | { |
| | 0 | 27 | | if (_eof) |
| | 0 | 28 | | { |
| | 0 | 29 | | return null; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | byte[] buffer = _readHeaderBuffer; |
| | | 33 | | |
| | 0 | 34 | | int bytesRead = await TAdapter.ReadAtLeastAsync( |
| | 0 | 35 | | stream, buffer, buffer.Length, throwOnEndOfStream: false, cancellationToken).ConfigureAwait(false); |
| | 0 | 36 | | if (bytesRead < buffer.Length) |
| | 0 | 37 | | { |
| | 0 | 38 | | if (bytesRead == 0) |
| | 0 | 39 | | { |
| | | 40 | | // m_Eof, return null |
| | 0 | 41 | | _eof = true; |
| | 0 | 42 | | return null; |
| | | 43 | | } |
| | 0 | 44 | | throw new IOException(SR.Format(SR.net_io_readfailure, SR.net_io_connectionclosed)); |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | _curReadHeader.CopyFrom(buffer, 0); |
| | 0 | 48 | | if (_curReadHeader.PayloadSize > FrameHeader.MaxMessageSize) |
| | 0 | 49 | | { |
| | 0 | 50 | | throw new InvalidOperationException(SR.Format(SR.net_frame_size, |
| | 0 | 51 | | FrameHeader.MaxMessageSize, |
| | 0 | 52 | | _curReadHeader.PayloadSize.ToString(NumberFormatInfo.Inva |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | buffer = new byte[_curReadHeader.PayloadSize]; |
| | | 56 | | |
| | 0 | 57 | | if (buffer.Length > 0) |
| | 0 | 58 | | { |
| | 0 | 59 | | bytesRead = await TAdapter.ReadAtLeastAsync( |
| | 0 | 60 | | stream, buffer, buffer.Length, throwOnEndOfStream: false, cancellationToken).ConfigureAwait(false); |
| | 0 | 61 | | if (bytesRead < buffer.Length) |
| | 0 | 62 | | { |
| | 0 | 63 | | throw new IOException(SR.Format(SR.net_io_readfailure, SR.net_io_connectionclosed)); |
| | | 64 | | } |
| | 0 | 65 | | } |
| | 0 | 66 | | return buffer; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task WriteMessageAsync<TAdapter>(Stream stream, byte[] message, CancellationToken cancellationToken |
| | | 70 | | where TAdapter : IReadWriteAdapter |
| | 0 | 71 | | { |
| | 0 | 72 | | _writeHeader.PayloadSize = message.Length; |
| | 0 | 73 | | _writeHeader.CopyTo(_writeHeaderBuffer, 0); |
| | | 74 | | |
| | 0 | 75 | | await TAdapter.WriteAsync(stream, _writeHeaderBuffer, cancellationToken).ConfigureAwait(false); |
| | 0 | 76 | | if (message.Length != 0) |
| | 0 | 77 | | { |
| | 0 | 78 | | await TAdapter.WriteAsync(stream, message, cancellationToken).ConfigureAwait(false); |
| | 0 | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Describes the header used in framing of the stream data. |
| | | 84 | | internal sealed class FrameHeader |
| | | 85 | | { |
| | | 86 | | public const int IgnoreValue = -1; |
| | | 87 | | public const int HandshakeDoneId = 20; |
| | | 88 | | public const int HandshakeErrId = 21; |
| | | 89 | | public const int HandshakeId = 22; |
| | | 90 | | public const int DefaultMajorV = 1; |
| | | 91 | | public const int DefaultMinorV = 0; |
| | | 92 | | public const int Size = 5; |
| | | 93 | | public const int MaxMessageSize = 0xFFFF; |
| | | 94 | | |
| | | 95 | | private int _payloadSize = -1; |
| | | 96 | | |
| | | 97 | | public int MessageId { get; set; } = HandshakeId; |
| | | 98 | | public int MajorV { get; private set; } = DefaultMajorV; |
| | | 99 | | public int MinorV { get; private set; } = DefaultMinorV; |
| | | 100 | | |
| | | 101 | | public int PayloadSize |
| | | 102 | | { |
| | | 103 | | get => _payloadSize; |
| | | 104 | | set |
| | | 105 | | { |
| | | 106 | | if (value > MaxMessageSize) |
| | | 107 | | { |
| | | 108 | | throw new ArgumentException(SR.Format(SR.net_frame_max_size, MaxMessageSize, value), nameof(PayloadS |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | _payloadSize = value; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public void CopyTo(byte[] dest, int start) |
| | | 116 | | { |
| | | 117 | | dest[start++] = (byte)MessageId; |
| | | 118 | | dest[start++] = (byte)MajorV; |
| | | 119 | | dest[start++] = (byte)MinorV; |
| | | 120 | | dest[start++] = (byte)((_payloadSize >> 8) & 0xFF); |
| | | 121 | | dest[start] = (byte)(_payloadSize & 0xFF); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public void CopyFrom(byte[] bytes, int start) |
| | | 125 | | { |
| | | 126 | | MessageId = bytes[start++]; |
| | | 127 | | MajorV = bytes[start++]; |
| | | 128 | | MinorV = bytes[start++]; |
| | | 129 | | _payloadSize = (bytes[start++] << 8) | bytes[start]; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |