Exemplo n.º 1
0
        private async Task CreateBufferWriteSingleAsync(MessageFrame messageFrame,
                                                        CancellationToken cancellationToken)
        {
            var sendBufferSize = MessageFrame.HeaderSize + messageFrame.RawMessage.Length;

            WeakReference <MemoryStream> reference;
            var mem = GetOrCreateMemoryStream(sendBufferSize, out reference);

            try
            {
                WriteTypeId(mem, messageFrame.MessageTypeId);
                WriteLength(mem, messageFrame.RawMessage.Length);
                WriteBody(mem, messageFrame.RawMessage);

                var toSend = mem.GetBuffer();
                await _streamSocket.WriteAsync(toSend, 0, sendBufferSize, cancellationToken);
            }
            finally
            {
                mem.SetLength(0);
                RecycledMemoryStreams.Enqueue(reference);
            }
        }
Exemplo n.º 2
0
 public async Task WriteGreetingAsync(NodeGreetingMessage greetingMessage, CancellationToken cancellationToken)
 {
     var serialized = greetingMessage.Serialize();
     await _streamSocket.WriteAsync(serialized, 0, serialized.Length, cancellationToken);
 }