// public methods /// <summary> /// Reads the message. /// </summary> /// <returns>A message.</returns> public CompressedMessage ReadMessage() { var reader = CreateJsonReader(); var context = BsonDeserializationContext.CreateRoot(reader); var messageDocument = BsonDocumentSerializer.Instance.Deserialize(context); var opcode = (Opcode)messageDocument["opcode"].ToInt32(); if (opcode != Opcode.Compressed) { throw new FormatException($"Command message invalid opcode: \"{opcode}\"."); } var compressorId = (CompressorType)messageDocument["compressorId"].ToInt32(); var compressedMessage = messageDocument["compressedMessage"].AsString; using (var originalTextReader = new StringReader(compressedMessage)) { var jsonEncoderFactory = new JsonMessageEncoderFactory(originalTextReader, _encoderSettings); var originalEncoder = _originalEncoderSelector.GetEncoder(jsonEncoderFactory); var originalMessage = originalEncoder.ReadMessage(); return(new CompressedMessage(originalMessage, null, compressorId)); } }
public ResponseMessage DecodeMessage(IByteBuffer buffer, IMessageEncoderSelector encoderSelector, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); _stopwatch.Stop(); _networkDuration = _stopwatch.Elapsed; ResponseMessage message; _stopwatch.Restart(); using (var stream = new ByteBufferStream(buffer, ownsBuffer: false)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, _messageEncoderSettings, _compressorSource); var opcode = PeekOpcode(stream); if (opcode == Opcode.Compressed) { var compresedMessageEncoder = encoderFactory.GetCompressedMessageEncoder(encoderSelector); var compressedMessage = (CompressedMessage)compresedMessageEncoder.ReadMessage(); message = (ResponseMessage)compressedMessage.OriginalMessage; } else { var encoder = encoderSelector.GetEncoder(encoderFactory); message = (ResponseMessage)encoder.ReadMessage(); } } _stopwatch.Stop(); _deserializationDuration = _stopwatch.Elapsed; return(message); }
public async Task <ResponseMessage> ReceiveMessageAsync( int responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) { Ensure.IsNotNull(encoderSelector, nameof(encoderSelector)); ThrowIfDisposedOrNotOpen(); try { if (_receivingMessageEventHandler != null) { _receivingMessageEventHandler(new ConnectionReceivingMessageEvent(_connectionId, responseTo)); } ResponseMessage reply; var stopwatch = Stopwatch.StartNew(); using (var buffer = await ReceiveBufferAsync(responseTo, cancellationToken).ConfigureAwait(false)) { stopwatch.Stop(); var networkDuration = stopwatch.Elapsed; cancellationToken.ThrowIfCancellationRequested(); stopwatch.Restart(); using (var stream = new ByteBufferStream(buffer)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, messageEncoderSettings); var encoder = encoderSelector.GetEncoder(encoderFactory); reply = (ResponseMessage)encoder.ReadMessage(); } stopwatch.Stop(); if (_receivedMessageEventHandler != null) { _receivedMessageEventHandler(new ConnectionReceivedMessageEvent(_connectionId, responseTo, buffer.Length, networkDuration, stopwatch.Elapsed)); } } return(reply); } catch (Exception ex) { if (_failedReceivingMessageEventHandler != null) { _failedReceivingMessageEventHandler(new ConnectionReceivingMessageFailedEvent(_connectionId, responseTo, ex)); } throw; } }
public async Task <ResponseMessage> ReceiveMessageAsync( int responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) { Ensure.IsNotNull(encoderSelector, "encoderSelector"); ThrowIfDisposedOrNotOpen(); try { if (_listener != null) { _listener.ConnectionBeforeReceivingMessage(new ConnectionBeforeReceivingMessageEvent(_connectionId, responseTo)); } var stopwatch = Stopwatch.StartNew(); ResponseMessage reply; int length; using (var buffer = await ReceiveBufferAsync(responseTo, cancellationToken).ConfigureAwait(false)) { cancellationToken.ThrowIfCancellationRequested(); length = buffer.Length; using (var stream = new ByteBufferStream(buffer)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, messageEncoderSettings); var encoder = encoderSelector.GetEncoder(encoderFactory); reply = (ResponseMessage)encoder.ReadMessage(); } } stopwatch.Stop(); if (_listener != null) { _listener.ConnectionAfterReceivingMessage(new ConnectionAfterReceivingMessageEvent(_connectionId, reply, length, stopwatch.Elapsed)); } return(reply); } catch (Exception ex) { if (_listener != null) { _listener.ConnectionErrorReceivingMessage(new ConnectionErrorReceivingMessageEvent(_connectionId, responseTo, ex)); } throw; } }
public ResponseMessage DecodeMessage(IByteBuffer buffer, IMessageEncoderSelector encoderSelector, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); _stopwatch.Stop(); _networkDuration = _stopwatch.Elapsed; ResponseMessage message; _stopwatch.Restart(); using (var stream = new ByteBufferStream(buffer, ownsBuffer: false)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, _messageEncoderSettings); var encoder = encoderSelector.GetEncoder(encoderFactory); message = (ResponseMessage)encoder.ReadMessage(); } _stopwatch.Stop(); _deserializationDuration = _stopwatch.Elapsed; return(message); }
/// <summary> /// Reads the message. /// </summary> /// <returns>A message.</returns> public CompressedMessage ReadMessage() { var reader = CreateBinaryReader(); var stream = reader.BsonStream; var messageStartPosition = stream.Position; var messageLength = stream.ReadInt32(); EnsureMessageLengthIsValid(messageLength); var requestId = stream.ReadInt32(); var responseTo = stream.ReadInt32(); var opcode = (Opcode)stream.ReadInt32(); EnsureOpcodeIsValid(opcode); var originalOpcode = (Opcode)stream.ReadInt32(); var uncompressedSize = stream.ReadInt32(); var compressorType = (CompressorType)stream.ReadByte(); var compressor = _compressorSource.Get(compressorType); using (var uncompressedBuffer = new MultiChunkBuffer(new OutputBufferChunkSource(BsonChunkPool.Default))) using (var uncompressedStream = new ByteBufferStream(uncompressedBuffer, ownsBuffer: false)) { uncompressedStream.WriteInt32(uncompressedSize + MessageHeaderLength); uncompressedStream.WriteInt32(requestId); uncompressedStream.WriteInt32(responseTo); uncompressedStream.WriteInt32((int)originalOpcode); compressor.Decompress(stream, uncompressedStream); uncompressedStream.Position = 0; uncompressedBuffer.MakeReadOnly(); var originalMessageEncoderFactory = new BinaryMessageEncoderFactory(uncompressedStream, _encoderSettings, _compressorSource); var originalMessageEncoder = _originalEncoderSelector.GetEncoder(originalMessageEncoderFactory); var originalMessage = originalMessageEncoder.ReadMessage(); return(new CompressedMessage(originalMessage, null, compressorType)); } }
public ResponseMessage DecodeMessage(IByteBuffer buffer, IMessageEncoderSelector encoderSelector, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); _stopwatch.Stop(); _networkDuration = _stopwatch.Elapsed; ResponseMessage message; _stopwatch.Restart(); using (var stream = new ByteBufferStream(buffer, ownsBuffer: false)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, _messageEncoderSettings); var encoder = encoderSelector.GetEncoder(encoderFactory); message = (ResponseMessage)encoder.ReadMessage(); } _stopwatch.Stop(); _deserializationDuration = _stopwatch.Elapsed; return message; }
public async Task<ResponseMessage> ReceiveMessageAsync( int responseTo, IMessageEncoderSelector encoderSelector, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken) { Ensure.IsNotNull(encoderSelector, nameof(encoderSelector)); ThrowIfDisposedOrNotOpen(); try { if (_receivingMessageEventHandler != null) { _receivingMessageEventHandler(new ConnectionReceivingMessageEvent(_connectionId, responseTo, EventContext.OperationId)); } ResponseMessage reply; var stopwatch = Stopwatch.StartNew(); using (var buffer = await ReceiveBufferAsync(responseTo, cancellationToken).ConfigureAwait(false)) { stopwatch.Stop(); var networkDuration = stopwatch.Elapsed; cancellationToken.ThrowIfCancellationRequested(); stopwatch.Restart(); using (var stream = new ByteBufferStream(buffer)) { var encoderFactory = new BinaryMessageEncoderFactory(stream, messageEncoderSettings); var encoder = encoderSelector.GetEncoder(encoderFactory); reply = (ResponseMessage)encoder.ReadMessage(); } stopwatch.Stop(); if (_commandEventHelper.ShouldCallAfterReceiving) { _commandEventHelper.AfterReceiving(reply, buffer, _connectionId, messageEncoderSettings); } if (_receivedMessageEventHandler != null) { _receivedMessageEventHandler(new ConnectionReceivedMessageEvent(_connectionId, responseTo, buffer.Length, networkDuration, stopwatch.Elapsed, EventContext.OperationId)); } } return reply; } catch (Exception ex) { if (_commandEventHelper.ShouldCallErrorReceiving) { _commandEventHelper.ErrorReceiving(responseTo, _connectionId, ex); } if (_failedReceivingMessageEventHandler != null) { _failedReceivingMessageEventHandler(new ConnectionReceivingMessageFailedEvent(_connectionId, responseTo, ex, EventContext.OperationId)); } throw; } }