コード例 #1
0
        // helper for reflection
        internal async ValueTask WriteResponseMessageNil(Task value)
        {
            if (MessageId == -1) // don't write.
            {
                return;
            }

            // MessageFormat:
            // response:  [messageId, methodId, response]
            byte[] BuildMessage()
            {
                using (var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter())
                {
                    var writer = new MessagePackWriter(buffer);

                    writer.WriteArrayHeader(3);
                    writer.Write(MessageId);
                    writer.Write(MethodId);
                    writer.WriteNil();
                    writer.Flush();
                    return(buffer.WrittenSpan.ToArray());
                }
            }

            await value.ConfigureAwait(false);

            var result = BuildMessage();

            using (await AsyncWriterLock.LockAsync().ConfigureAwait(false))
            {
                await ServiceContext.ResponseStream !.WriteAsync(result).ConfigureAwait(false);
            }
            responseSize = result.Length;
            responseType = typeof(Nil);
        }
コード例 #2
0
        internal async ValueTask WriteErrorMessage(int statusCode, string detail, Exception ex, bool isReturnExceptionStackTraceInErrorDetail)
        {
            // MessageFormat:
            // error-response:  [messageId, statusCode, detail, StringMessage]
            var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue);

            var buffer = rent;

            byte[] result;
            try
            {
                var offset = 0;
                offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 4);
                offset += MessagePackBinary.WriteInt32(ref buffer, offset, MessageId);
                offset += MessagePackBinary.WriteInt32(ref buffer, offset, statusCode);
                offset += MessagePackBinary.WriteString(ref buffer, offset, detail);

                var msg = (isReturnExceptionStackTraceInErrorDetail)
                    ? ex.ToString()
                    : null;
                if (msg != null)
                {
                    offset += LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, msg, FormatterResolver);
                }
                else
                {
                    offset += MessagePackBinary.WriteNil(ref buffer, offset);
                }
                result = MessagePackBinary.FastCloneWithResize(buffer, offset);
            }
            finally
            {
                System.Buffers.ArrayPool <byte> .Shared.Return(rent);
            }
            using (await AsyncWriterLock.LockAsync().ConfigureAwait(false))
            {
                await ServiceContext.ResponseStream.WriteAsync(result).ConfigureAwait(false);
            }
            responseSize = result.Length;
        }
コード例 #3
0
        internal async ValueTask WriteErrorMessage(int statusCode, string detail, Exception?ex, bool isReturnExceptionStackTraceInErrorDetail)
        {
            // MessageFormat:
            // error-response:  [messageId, statusCode, detail, StringMessage]
            byte[] BuildMessage()
            {
                using (var buffer = ArrayPoolBufferWriter.RentThreadStaticWriter())
                {
                    var writer = new MessagePackWriter(buffer);
                    writer.WriteArrayHeader(4);
                    writer.Write(MessageId);
                    writer.Write(statusCode);
                    writer.Write(detail);

                    var msg = (isReturnExceptionStackTraceInErrorDetail && ex != null)
                        ? ex.ToString()
                        : null;

                    if (msg != null)
                    {
                        MessagePackSerializer.Serialize(ref writer, msg, SerializerOptions);
                    }
                    else
                    {
                        writer.WriteNil();
                    }
                    writer.Flush();
                    return(buffer.WrittenSpan.ToArray());
                }
            }

            var result = BuildMessage();

            using (await AsyncWriterLock.LockAsync().ConfigureAwait(false))
            {
                await ServiceContext.ResponseStream !.WriteAsync(result).ConfigureAwait(false);
            }
            responseSize = result.Length;
        }
コード例 #4
0
        // helper for reflection
        internal async ValueTask WriteResponseMessageNil(Task value)
        {
            if (MessageId == -1) // don't write.
            {
                return;
            }

            // MessageFormat:
            // response:  [messageId, methodId, response]
            var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue);

            var buffer = rent;

            byte[] result;
            try
            {
                var offset = 0;
                offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 3);
                offset += MessagePackBinary.WriteInt32(ref buffer, offset, MessageId);
                offset += MessagePackBinary.WriteInt32(ref buffer, offset, MethodId);

                await value.ConfigureAwait(false);

                offset += LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, Nil.Default, FormatterResolver);

                result = MessagePackBinary.FastCloneWithResize(buffer, offset);
            }
            finally
            {
                System.Buffers.ArrayPool <byte> .Shared.Return(rent);
            }
            using (await AsyncWriterLock.LockAsync().ConfigureAwait(false))
            {
                await ServiceContext.ResponseStream.WriteAsync(result).ConfigureAwait(false);
            }
            responseSize = result.Length;
            responseType = typeof(Nil);
        }