protected async Task WriteMessageAsync <T>(int methodId, T message) { ThrowIfDisposed(); #if NON_UNITY var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue); #else var rent = MessagePack.Internal.BufferPool.Default.Rent(); #endif var buffer = rent; byte[] v; try { var offset = 0; offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 2); offset += MessagePackBinary.WriteInt32(ref buffer, offset, methodId); offset += LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, message, resolver); v = MessagePackBinary.FastCloneWithResize(buffer, offset); } finally { #if NON_UNITY System.Buffers.ArrayPool <byte> .Shared.Return(rent); #else MessagePack.Internal.BufferPool.Default.Return(rent); #endif } using (await asyncLock.LockAsync()) { await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false); } }
byte[] BuildMessage <T>(int methodId, T value, Guid[] connectionIds, bool isExcept) { var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue); var buffer = rent; try { var offset = 0; // redis-format: [isExcept, [connectionIds], [raw-bloadcast-format]] offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 3); offset += MessagePackBinary.WriteBoolean(ref buffer, offset, isExcept); offset += NativeGuidArrayFormatter.Serialize(ref buffer, offset, connectionIds); offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 2); offset += MessagePackBinary.WriteInt32(ref buffer, offset, methodId); offset += LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, value, resolver); var result = MessagePackBinary.FastCloneWithResize(buffer, offset); return(result); } finally { System.Buffers.ArrayPool <byte> .Shared.Return(rent); } }
public void SerializeToBlock() { var originalData = Enumerable.Range(1, 1000).Select(x => x).ToArray(); var idx = 0; var writer = new MessagePackWriter(16); #pragma warning disable CS0618 // 类型或成员已过时 LZ4MessagePackSerializer.SerializeToBlock(ref writer, ref idx, originalData, MessagePackSerializer.DefaultResolver); #pragma warning restore CS0618 // 类型或成员已过时 var lz4Data = LZ4MessagePackSerializer.Serialize(originalData); writer.ToArray(idx).AsSpan().SequenceEqual(lz4Data).Is(true); }
public void SerializeToBlock() { var originalData = Enumerable.Range(1, 1000).Select(x => x).ToArray(); byte[] bytes = null; var len = LZ4MessagePackSerializer.SerializeToBlock(ref bytes, 0, originalData, MessagePackSerializer.DefaultResolver); var lz4Data = LZ4MessagePackSerializer.Serialize(originalData); len.Is(lz4Data.Length); for (int i = 0; i < len; i++) { if (bytes[i] != lz4Data[i]) { throw new AssertException("not same"); } } }
byte[] BuildMessage <T>(int methodId, T value) { var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue); var buffer = rent; try { var offset = 0; offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 2); offset += MessagePackBinary.WriteInt32(ref buffer, offset, methodId); offset += LZ4MessagePackSerializer.SerializeToBlock <T>(ref buffer, offset, value, resolver); var result = MessagePackBinary.FastCloneWithResize(buffer, offset); return(result); } finally { System.Buffers.ArrayPool <byte> .Shared.Return(rent); } }
protected async Task <TResponse> WriteMessageWithResponseAsync <TRequest, TResponse>(int methodId, TRequest message) { ThrowIfDisposed(); var mid = Interlocked.Increment(ref messageId); var tcs = new TaskCompletionSourceEx <TResponse>(); // use Ex responseFutures[mid] = (object)tcs; #if NON_UNITY var rent = System.Buffers.ArrayPool <byte> .Shared.Rent(ushort.MaxValue); #else var rent = MessagePack.Internal.BufferPool.Default.Rent(); #endif var buffer = rent; byte[] v; try { var offset = 0; offset += MessagePackBinary.WriteArrayHeader(ref buffer, offset, 3); offset += MessagePackBinary.WriteInt32(ref buffer, offset, mid); offset += MessagePackBinary.WriteInt32(ref buffer, offset, methodId); offset += LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, message, resolver); v = MessagePackBinary.FastCloneWithResize(buffer, offset); } finally { #if NON_UNITY System.Buffers.ArrayPool <byte> .Shared.Return(rent); #else MessagePack.Internal.BufferPool.Default.Return(rent); #endif } using (await asyncLock.LockAsync().ConfigureAwait(false)) { await connection.RawStreamingCall.RequestStream.WriteAsync(v).ConfigureAwait(false); } return(await tcs.Task); // wait until server return response(or error). if connection was closed, throws cancellation from DisposeAsyncCore. }
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; }
// 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); }
public int Serialize(ref byte[] bytes, int offset, IFormatterResolver resolver) { return(LZ4MessagePackSerializer.SerializeToBlock(ref bytes, offset, orderedData, resolver)); }