Beispiel #1
0
            public NatsMemoryOwner(NatsMemoryPool owner, int length)
            {
                _owner  = owner;
                _buffer = owner._pool.Rent(length);

                Memory = _buffer.AsMemory(0, length <= 0 ? _owner.MaxBufferSize : length);
            }
Beispiel #2
0
 public static INatsServerMessage?ParseMessage(NatsMemoryPool pool, in ReadOnlySpan <byte> line, ref SequenceReader <byte> reader)
Beispiel #3
0
        public static IMemoryOwner <byte> RentedSerialize(NatsMemoryPool pool, string subscriptionId, int?maxMessages)
        {
            var hint = _command.Length;

            hint += subscriptionId.Length;
            if (maxMessages != null)
            {
                if (maxMessages < 10)
                {
                    hint += 1;
                }
                else if (maxMessages < 100)
                {
                    hint += 2;
                }
                else if (maxMessages < 1_000)
                {
                    hint += 3;
                }
                else if (maxMessages < 10_000)
                {
                    hint += 4;
                }
                else if (maxMessages < 100_000)
                {
                    hint += 5;
                }
                else if (maxMessages < 1_000_000)
                {
                    hint += 6;
                }
                else if (maxMessages < 10_000_000)
                {
                    hint += 7;
                }
                else
                {
                    throw new ArgumentOutOfRangeException(nameof(maxMessages));
                }
                hint += _del.Length;
            }

            hint += _end.Length;

            var rented = pool.Rent(hint);
            var buffer = rented.Memory;

            _command.CopyTo(buffer);
            var consumed = _command.Length;

            consumed += Encoding.UTF8.GetBytes(subscriptionId, buffer.Slice(consumed).Span);
            if (maxMessages != null)
            {
                _del.CopyTo(buffer.Slice(consumed));
                consumed += _del.Length;
                Utf8Formatter.TryFormat(maxMessages.Value, buffer.Slice(consumed).Span, out var written);
                consumed += written;
            }

            _end.CopyTo(buffer.Slice(consumed));
            return(rented);
        }
Beispiel #4
0
        public static IMemoryOwner <byte> RentedSerialize(NatsMemoryPool pool, string subject, string?replyTo, ReadOnlyMemory <byte> payload)
        {
            var hint = _command.Length;       // PUB

            hint += subject.Length + 1;       // Subject + space
            hint += replyTo?.Length + 1 ?? 0; // ReplyTo
            if (payload.Length < 10)
            {
                hint += 1;
            }
            else if (payload.Length < 100)
            {
                hint += 2;
            }
            else if (payload.Length < 1_000)
            {
                hint += 3;
            }
            else if (payload.Length < 10_000)
            {
                hint += 4;
            }
            else if (payload.Length < 100_000)
            {
                hint += 5;
            }
            else if (payload.Length < 1_000_000)
            {
                hint += 6;
            }
            else if (payload.Length < 10_000_000)
            {
                hint += 7;
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(payload));
            }

            hint += _end.Length; // Ending
            hint += payload.Length;
            hint += _end.Length; // Ending Payload

            var rented = pool.Rent(hint);
            var buffer = rented.Memory;

            _command.CopyTo(buffer);
            var consumed = _command.Length;

            consumed += Encoding.ASCII.GetBytes(subject, buffer.Slice(consumed).Span);
            _del.CopyTo(buffer.Slice(consumed));
            consumed++;
            if (!string.IsNullOrEmpty(replyTo))
            {
                consumed += Encoding.UTF8.GetBytes(replyTo, buffer.Slice(consumed).Span);
                _del.CopyTo(buffer.Slice(consumed));
                consumed++;
            }

            Utf8Formatter.TryFormat(payload.Length, buffer.Slice(consumed).Span, out var written);
            consumed += written;
            _end.CopyTo(buffer.Slice(consumed));
            consumed += _end.Length;
            if (!payload.IsEmpty)
            {
                payload.CopyTo(buffer.Slice(consumed));
                consumed += payload.Length;
            }

            _end.CopyTo(buffer.Slice(consumed));
            return(rented);
        }
Beispiel #5
0
 public NatsMessageParser(NatsMemoryPool memoryPool)
 {
     _memoryPool = memoryPool;
 }