|
| 1 | +using ProtoSocket; |
| 2 | +using System; |
| 3 | +using System.Buffers; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.IO.Pipelines; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace Example.Ssl |
| 10 | +{ |
| 11 | + public class SslCoder : IProtocolCoder<string> |
| 12 | + { |
| 13 | + private byte[] _bytes; |
| 14 | + private int _bytesLength; |
| 15 | + private int _bytesOffset; |
| 16 | + private State _state; |
| 17 | + |
| 18 | + public bool Read(PipeReader reader, CoderContext<string> ctx, out string frame) |
| 19 | + { |
| 20 | + if (reader.TryRead(out ReadResult result) && !result.IsCompleted) { |
| 21 | + // get the sequence buffer |
| 22 | + ReadOnlySequence<byte> buffer = result.Buffer; |
| 23 | + |
| 24 | + try { |
| 25 | + while (buffer.Length > 0) { |
| 26 | + if (_state == State.Size) { |
| 27 | + if (buffer.Length >= 2) { |
| 28 | + // copy length from buffer |
| 29 | + Span<byte> lengthBytes = stackalloc byte[2]; |
| 30 | + |
| 31 | + buffer.Slice(0, 2) |
| 32 | + .CopyTo(lengthBytes); |
| 33 | + |
| 34 | + int length = BitConverter.ToUInt16(lengthBytes); |
| 35 | + |
| 36 | + if (length > 32768) |
| 37 | + throw new ProtocolCoderException("The client sent an invalid frame length"); |
| 38 | + |
| 39 | + // increment the amount we were able to copy in |
| 40 | + buffer = buffer.Slice(2); |
| 41 | + |
| 42 | + // move state to content if we have a message with data |
| 43 | + if (length > 0) { |
| 44 | + _bytes = ArrayPool<byte>.Shared.Rent(length); |
| 45 | + _bytesLength = length; |
| 46 | + _bytesOffset = 0; |
| 47 | + _state = State.Content; |
| 48 | + } else { |
| 49 | + frame = string.Empty; |
| 50 | + return true; |
| 51 | + } |
| 52 | + } else { |
| 53 | + break; |
| 54 | + } |
| 55 | + } else if (_state == State.Content) { |
| 56 | + if (buffer.Length >= 1) { |
| 57 | + // figure out how much data we can read, and how much we actually have to read |
| 58 | + int remainingBytes = _bytesLength - _bytesOffset; |
| 59 | + int maxBytes = Math.Min((int)buffer.Length, remainingBytes); |
| 60 | + |
| 61 | + // copy into buffer |
| 62 | + buffer.Slice(0, maxBytes) |
| 63 | + .CopyTo(_bytes.AsSpan(_bytesOffset, maxBytes)); |
| 64 | + |
| 65 | + // increment offset by amount we copied |
| 66 | + _bytesOffset += maxBytes; |
| 67 | + buffer = buffer.Slice(maxBytes); |
| 68 | + |
| 69 | + // if we have filled the content array we can now produce a frame |
| 70 | + if (_bytesOffset == _bytesLength) { |
| 71 | + try { |
| 72 | + frame = Encoding.UTF8.GetString(_bytes); |
| 73 | + _state = State.Size; |
| 74 | + return true; |
| 75 | + } finally { |
| 76 | + ArrayPool<byte>.Shared.Return(_bytes); |
| 77 | + _bytes = null; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } finally { |
| 84 | + reader.AdvanceTo(buffer.GetPosition(0), buffer.End); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // we didn't find a frame |
| 89 | + frame = default; |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + public void Reset() |
| 94 | + { |
| 95 | + throw new NotSupportedException(); |
| 96 | + } |
| 97 | + |
| 98 | + public void Write(Stream stream, string frame, CoderContext<string> ctx) |
| 99 | + { |
| 100 | + // encode the frame into a UTF8 byte array |
| 101 | + byte[] frameBytes = Encoding.UTF8.GetBytes(frame); |
| 102 | + |
| 103 | + if (frameBytes.Length > 32768) |
| 104 | + throw new ProtocolCoderException("The frame is too large to write"); |
| 105 | + |
| 106 | + // encode the length |
| 107 | + byte[] lengthBytes = BitConverter.GetBytes((ushort)frame.Length); |
| 108 | + |
| 109 | + // write to stream |
| 110 | + stream.Write(lengthBytes); |
| 111 | + stream.Write(frameBytes); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Defines the states for this coder. |
| 116 | + /// </summary> |
| 117 | + enum State |
| 118 | + { |
| 119 | + Size, |
| 120 | + Content |
| 121 | + } |
| 122 | + |
| 123 | + ~SslCoder() |
| 124 | + { |
| 125 | + // return the array back to the pool if we deconstruct before finishing the entire frame |
| 126 | + if (_bytes != null) |
| 127 | + ArrayPool<byte>.Shared.Return(_bytes); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments