|
| 1 | +using System.Buffers.Text; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | + |
| 4 | +namespace NetCord.Rest; |
| 5 | + |
| 6 | +[GenerateMethodsForProperties] |
| 7 | +public partial class InviteTargetUsersProperties : IHttpSerializable |
| 8 | +{ |
| 9 | + private readonly Stream _stream; |
| 10 | + private byte _read; |
| 11 | + |
| 12 | + private InviteTargetUsersProperties(Stream stream) |
| 13 | + { |
| 14 | + _stream = stream; |
| 15 | + } |
| 16 | + |
| 17 | + public static InviteTargetUsersProperties FromStream(Stream stream) => new(stream); |
| 18 | + |
| 19 | + public static InviteTargetUsersProperties FromEnumerable(IEnumerable<ulong> userIds) => new(new UserIdsStream(userIds)); |
| 20 | + |
| 21 | + HttpContent IHttpSerializable.Serialize() => Serialize(); |
| 22 | + |
| 23 | + internal HttpContent Serialize() |
| 24 | + { |
| 25 | + if (Interlocked.Exchange(ref _read, 1) is 1) |
| 26 | + ThrowAlreadySent(); |
| 27 | + |
| 28 | + return new StreamContent(_stream); |
| 29 | + } |
| 30 | + |
| 31 | + [DoesNotReturn] |
| 32 | + private static void ThrowAlreadySent() |
| 33 | + { |
| 34 | + throw new InvalidOperationException("The invite target users have already been sent."); |
| 35 | + } |
| 36 | + |
| 37 | + private sealed class UserIdsStream(IEnumerable<ulong> userIds) : Stream |
| 38 | + { |
| 39 | + private readonly IEnumerator<ulong> _enumerator = userIds.GetEnumerator(); |
| 40 | + private readonly byte[] _buffer = new byte[22]; |
| 41 | + private int _startPosition; |
| 42 | + private int _endPosition; |
| 43 | + |
| 44 | + public override bool CanRead => true; |
| 45 | + |
| 46 | + public override bool CanSeek => false; |
| 47 | + |
| 48 | + public override bool CanWrite => false; |
| 49 | + |
| 50 | + public override long Length => throw new NotSupportedException(); |
| 51 | + |
| 52 | + public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } |
| 53 | + |
| 54 | + public override void Flush() => throw new NotSupportedException(); |
| 55 | + |
| 56 | + public override int Read(byte[] buffer, int offset, int count) => Read(new Span<byte>(buffer, offset, count)); |
| 57 | + |
| 58 | + public override int Read(Span<byte> buffer) |
| 59 | + { |
| 60 | + int totalWritten = 0; |
| 61 | + |
| 62 | + if (_startPosition != _endPosition) |
| 63 | + { |
| 64 | + var bufferedLength = _endPosition - _startPosition; |
| 65 | + |
| 66 | + var length = Math.Min(bufferedLength, buffer.Length); |
| 67 | + |
| 68 | + _buffer.AsSpan(_startPosition, length).CopyTo(buffer); |
| 69 | + _startPosition += length; |
| 70 | + |
| 71 | + if (length != bufferedLength) |
| 72 | + return length; |
| 73 | + |
| 74 | + totalWritten += length; |
| 75 | + } |
| 76 | + |
| 77 | + var newLine = "\r\n"u8; |
| 78 | + |
| 79 | + while (_enumerator.MoveNext()) |
| 80 | + { |
| 81 | + var userId = _enumerator.Current; |
| 82 | + if (!Utf8Formatter.TryFormat(userId, buffer[totalWritten..], out var written)) |
| 83 | + { |
| 84 | + _ = Utf8Formatter.TryFormat(userId, _buffer, out var writtenToBuffer); |
| 85 | + |
| 86 | + _buffer.AsSpan(0, _startPosition = written = buffer.Length - totalWritten).CopyTo(buffer[totalWritten..]); |
| 87 | + |
| 88 | + totalWritten += written; |
| 89 | + |
| 90 | + newLine.CopyTo(_buffer.AsSpan(writtenToBuffer)); |
| 91 | + _endPosition = writtenToBuffer + newLine.Length; |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + totalWritten += written; |
| 96 | + |
| 97 | + if (!newLine.TryCopyTo(buffer[totalWritten..])) |
| 98 | + { |
| 99 | + var remaining = buffer.Length - totalWritten; |
| 100 | + if (remaining > 0) |
| 101 | + { |
| 102 | + newLine[..remaining].CopyTo(buffer[totalWritten..]); |
| 103 | + totalWritten += remaining; |
| 104 | + } |
| 105 | + |
| 106 | + newLine[remaining..].CopyTo(_buffer); |
| 107 | + _startPosition = 0; |
| 108 | + _endPosition = newLine.Length - remaining; |
| 109 | + |
| 110 | + break; |
| 111 | + } |
| 112 | + |
| 113 | + totalWritten += newLine.Length; |
| 114 | + } |
| 115 | + |
| 116 | + return totalWritten; |
| 117 | + } |
| 118 | + |
| 119 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 120 | + { |
| 121 | + return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask(); |
| 122 | + } |
| 123 | + |
| 124 | + public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 125 | + { |
| 126 | + return new(Read(buffer.Span)); |
| 127 | + } |
| 128 | + |
| 129 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 130 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 131 | + public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 132 | + |
| 133 | + protected override void Dispose(bool disposing) |
| 134 | + { |
| 135 | + if (disposing) |
| 136 | + _enumerator.Dispose(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
0 commit comments