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); } }
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); } }
public static byte[] Serialize(Dictionary <string, object> obj) { var buffer = InternalMemoryPool.GetBuffer(); var len = Serialize(ref buffer, 0, obj); // do not return MemoryPool.Buffer. return(MessagePackBinary.FastCloneWithResize(buffer, len)); }
/// <summary> /// Serialize to binary with specified resolver. /// </summary> public static byte[] Serialize <T>(T obj, IFormatterResolver resolver) { if (resolver == null) { resolver = MessagePackSerializer.DefaultResolver; } var buffer = SerializeCore(obj, resolver); return(MessagePackBinary.FastCloneWithResize(buffer.Array, buffer.Count)); }
public static byte[] Serialize(RootUst rootUst, bool isLineColumn, bool compress, ILogger logger) { byte[] result = Serialize(rootUst, isLineColumn, compress, logger, out int writeSize); if (compress) { return(result); } return(MessagePackBinary.FastCloneWithResize(result, writeSize)); }
/// <summary> /// Serialize to binary with specified resolver. /// </summary> public static byte[] Serialize <T>(T obj, IFormatterResolver resolver) { if (resolver == null) { resolver = DefaultResolver; } var formatter = resolver.GetFormatterWithVerify <T>(); var buffer = InternalMemoryPool.GetBuffer(); var len = formatter.Serialize(ref buffer, 0, obj, resolver); // do not return MemoryPool.Buffer. return(MessagePackBinary.FastCloneWithResize(buffer, len)); }
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 static byte[] ToLZ4Binary(ArraySegment <byte> messagePackBinary) { var buffer = ToLZ4BinaryCore(messagePackBinary); return(MessagePackBinary.FastCloneWithResize(buffer.Array, buffer.Count)); }