public ISubscription SubscribeAsync <T>(Func <T, Task> subscribeMethod, SubscriptionConfiguration config)
        {
            var routingKey = config.RoutingKey;

            var topologyTask = _topologyProvider.BindQueueAsync(config.Queue, config.Exchange, routingKey);
            var channelTask  = _channelFactory.CreateChannelAsync();

            var subscriberTask = Task
                                 .WhenAll(topologyTask, channelTask)
                                 .ContinueWith(t =>
            {
                if (topologyTask.IsFaulted)
                {
                    throw topologyTask.Exception ?? new Exception("Topology Task Faulted");
                }
                var consumer            = _consumerFactory.CreateConsumer(config, channelTask.Result);
                consumer.OnMessageAsync = async(o, args) =>
                {
                    var body = _serializer.Deserialize <T>(args.Body);
                    await subscribeMethod(body);
                };
                consumer.Model.BasicConsume(config.Queue.FullQueueName, config.NoAck, consumer);
                _logger.LogDebug($"Setting up a consumer on channel '{channelTask.Result.ChannelNumber}' for queue {config.Queue.QueueName} with NoAck set to {config.NoAck}.");
                return(new Subscription(consumer, config.Queue.FullQueueName));
            });

            Task.WaitAll(subscriberTask);
            _subscriptions.Add(subscriberTask.Result);
            return(subscriberTask.Result);
        }
        public async Task <TMessage> Get(TimeSpan?visibilityTimeout = null)
        {
            visibilityTimeout = visibilityTimeout ?? TimeSpan.FromMinutes(5);

            var message = await _cloudQueue.Value.GetMessageAsync(visibilityTimeout, null, null);

            return(await _messageSerializer.Deserialize <TMessage>(
                       message.AsBytes,
                       new MessageInfo(
                           message.Id,
                           message.InsertionTime.Value.UtcDateTime,
                           message.ExpirationTime.Value.UtcDateTime,
                           message.NextVisibleTime.Value.UtcDateTime,
                           message.DequeueCount,
                           message.PopReceipt)));
        }
        public Message DeserializeMessage(string body)
        {
            foreach (var pair in _map)
            {
                TypeSerializer typeSerializer = pair.Value;
                string         messageSubject = typeSerializer.Serializer.GetMessageSubject(body);

                if (string.IsNullOrWhiteSpace(messageSubject))
                {
                    continue;
                }

                Type matchedType = typeSerializer.Type;

                if (!string.Equals(_messageSubjectProvider.GetSubjectForType(matchedType), messageSubject, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                IMessageSerializer messageSerializer = typeSerializer.Serializer;
                return(messageSerializer.Deserialize(body, matchedType));
            }

            // TODO Maybe we should log the body separately (at debug/trace?), rather than include it in the exception message. Then they're easier to filter.
            throw new MessageFormatNotSupportedException(
                      $"Message can not be handled - type undetermined. Message body: '{body}'");
        }
예제 #4
0
        private void Time(object message, IMessageSerializer serializer)
        {
            var watch = new Stopwatch();

            watch.Start();

            for (var i = 0; i < numberOfIterations; i++)
            {
                using (var stream = new MemoryStream())
                    serializer.Serialize(message, stream);
            }

            watch.Stop();
            Debug.WriteLine("Serializing: " + watch.Elapsed);

            watch.Reset();

            var s = new MemoryStream();

            serializer.Serialize(message, s);
            var buffer = s.GetBuffer();

            s.Dispose();

            watch.Start();

            for (var i = 0; i < numberOfIterations; i++)
            {
                using (var forDeserializing = new MemoryStream(buffer))
                    serializer.Deserialize(forDeserializing);
            }

            watch.Stop();
            Debug.WriteLine("Deserializing: " + watch.Elapsed);
        }
예제 #5
0
파일: CommandBus.cs 프로젝트: dsomok/Rattle
        public Task <IMessage> Send <TCommand>(string service, TCommand command)
            where TCommand : ICommand
        {
            var taskCompletionSource = new TaskCompletionSource <IMessage>();

            var commandId = Guid.NewGuid().ToString();

            var responseQueue = m_channel.QueueDeclare().QueueName;

            m_consumer.Consume(responseQueue, true, true, deliveryArgs =>
            {
                if (deliveryArgs.BasicProperties != null &&
                    deliveryArgs.BasicProperties.CorrelationId == commandId &&
                    !string.IsNullOrEmpty(deliveryArgs.BasicProperties.Type))
                {
                    var response = m_serializer.Deserialize(deliveryArgs.BasicProperties.Type, deliveryArgs.Body);
                    taskCompletionSource.SetResult(response);
                }
                else
                {
                    taskCompletionSource.SetException(new DeliveryFailedException(deliveryArgs));
                }
            });

            var commandTopic = $"command.{service}.{command.GetType().Name}";

            m_publisher.Publish(EXCHANGE_NAME, commandTopic, command, commandId, responseQueue);

            return(taskCompletionSource.Task);
        }
예제 #6
0
 public static MailMessage DeserializeMessage(this IMessageSerializer messageSerializer, MessageContext context)
 {
     using (var stream = new MemoryStream(context.Body))
     {
         return((MailMessage)messageSerializer.Deserialize(stream, new[] { typeof(MailMessage) }).Single());
     }
 }
 public static MailMessage DeserializeMessage(this IMessageSerializer messageSerializer, TransportMessage message)
 {
     using (var stream = new MemoryStream(message.Body))
     {
         return((MailMessage)messageSerializer.Deserialize(stream, new[] { typeof(MailMessage) }).Single());
     }
 }
예제 #8
0
        public static void ShouldContain <TMessage>(this IInboundTransport transport, IMessageSerializer serializer)
            where TMessage : class
        {
            var future = new Future <TMessage>();

            transport.Receive(context =>
            {
                context.ShouldNotBeNull();
                context.ShouldBeAnInstanceOf <IReceiveContext>();

                serializer.Deserialize(context);

                IConsumeContext <TMessage> messageContext;
                if (context.TryGetContext(out messageContext))
                {
                    if (!future.IsCompleted)
                    {
                        future.Complete(messageContext.Message);
                    }
                }

                return(null);
            }, TimeSpan.FromSeconds(3));

            future.IsCompleted.ShouldBeTrue(transport.Address + " should contain a message of type " + typeof(TMessage).Name);
        }
예제 #9
0
        private async Task <Envelope> DeserialzeEnvelope(EventData eventData)
        {
            object messageId;

            eventData.Properties.TryGetValue(
                "Khala.Envelope.MessageId", out messageId);

            object correlationId;

            eventData.Properties.TryGetValue(
                "Khala.Envelope.CorrelationId", out correlationId);

            using (Stream stream = eventData.GetBodyStream())
                using (var reader = new StreamReader(stream, Encoding.UTF8))
                {
                    string value = await reader.ReadToEndAsync().ConfigureAwait(false);

                    object message = _messageSerializer.Deserialize(value);

                    return(new Envelope(
                               ParseGuid(messageId) ?? Guid.NewGuid(),
                               ParseGuid(correlationId),
                               message));
                }
        }
        private void SocketListener_DataRecieved(object sender, SocketEventArgs e)
        {
            var connection = (Connection)e.Socket.UserToken;
            var message    = messageSerializer.Deserialize(e.Data);

            connection.ReceiveMessage(message);
        }
        public static void ShouldContain(this IInboundTransport transport, IMessageSerializer serializer,
                                         Type messageType)
        {
            var future = new Future <bool>();

            transport.Receive(context =>
            {
                context.ShouldNotBeNull();
                context.ShouldBeAnInstanceOf <IReceiveContext>();

                serializer.Deserialize(context);

                if (context.IsContextAvailable(messageType))
                {
                    if (!future.IsCompleted)
                    {
                        future.Complete(true);
                    }
                }

                return(null);
            }, TimeSpan.FromSeconds(8));

            future.IsCompleted.ShouldBeTrue(transport.Address + " should contain a message of type " +
                                            messageType.ToShortTypeName());
        }
예제 #12
0
        private void MessageReadTS()
        {
            _log.Debug(nameof(MessageReadTS));

            while (!_readCancel.IsCancellationRequested)
            {
                Read(out Byte[] data);

                if (data.Length > 0)
                {
                    IMessage message = _serializer.Deserialize(data);

                    if (message.Command == Commands.Finished)
                    {
                        _readCancel.Cancel();
                    }

                    RaiseMessageReceived(message);
                }

                Thread.Sleep(100);
            }

            try {
                _inReader.Close();
            } catch (Exception ex) { _log.Error("Failed to disconnect.", ex); }

            _log.Debug($"Thread {nameof(MessageReadTS)} stopped.");
        }
        /// <summary>
        /// This method processes records, performing retries as needed.
        /// </summary>
        /// <param name="records">The records to be processed.</param>
        protected void ProcessReceivedRecords(List <Record> records)
        {
            foreach (Record rec in records)
            {
                bool     processedSuccessfully = false;
                TMessage data = null;

                try
                {
                    // Use the passed in serializer to interpret the message
                    data = serializer.Deserialize <TMessage>(rec.Data, rec.Data.Length);

                    // Your own logic to process a record goes here.
                    HandleMessage(data);

                    processedSuccessfully = true;
                }
                catch (Exception e)
                {
                    //Console.Error.WriteLine("Exception processing record data: " + data, e);

                    HandleProcessingFailure(rec, e);
                }

                if (!processedSuccessfully)
                {
                    //TODO: Decide what to do when message processing fails
                    //The answer is probably explode and die
                    // However the best way to do this is probably to just throw and exception from HandleProcessingFailure in the derived classes.
                }
            }
        }
예제 #14
0
        public async Task <Tuple <long, T> > Get <T>(string bucket, Id streamId, Id[] parents) where T : class
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Poco, bucket, streamId, parents);

            Logger.Write(LogLevel.Debug, () => $"Getting poco stream [{streamName}]");

            if (_shouldCache)
            {
                var cached = _cache.Retreive(streamName) as Tuple <long, T>;
                if (cached != null)
                {
                    Logger.Write(LogLevel.Debug, () => $"Found poco [{streamId}] bucket [{bucket}] version {cached.Item1} in cache");
                    return(new Tuple <long, T>(cached.Item1,
                                               // An easy way to make a deep copy
                                               _serializer.Deserialize <T>(_serializer.Serialize(cached.Item2))));
                }
            }

            var read = await _store.GetEventsBackwards(streamName, StreamPosition.End, 1).ConfigureAwait(false);

            if (read == null || !read.Any())
            {
                return(null);
            }

            var @event = read.Single();

            if (_shouldCache)
            {
                _cache.Cache(streamName, @event.Event);
            }
            return(new Tuple <long, T>(@event.Descriptor.Version, @event.Event as T));
        }
        public object FromTransport(string serializedMessage, string serializedType)
        {
            var type    = _messageTypeResolver.GetMessageType(serializedType);
            var message = _messageSerializer.Deserialize(type, serializedMessage);

            return(message);
        }
예제 #16
0
        private void WireUpConsumer(IRawConsumer consumer, IConsumerConfiguration cfg)
        {
            consumer.OnMessageAsync = (o, args) =>
            {
                ResponseCompletionSource responseTcs = null;
                return(_errorStrategy.ExecuteAsync(() =>
                {
                    if (_responseDictionary.TryRemove(args.BasicProperties.CorrelationId, out responseTcs))
                    {
                        _logger.LogDebug($"Recived response with correlationId {args.BasicProperties.CorrelationId}.");
                        responseTcs.RequestTimer.Dispose();

                        _errorStrategy.OnResponseRecievedAsync(args, responseTcs);
                        if (responseTcs.Task.IsFaulted)
                        {
                            return _completed;
                        }
                        var response = _serializer.Deserialize(args);
                        responseTcs.TrySetResult(response);
                        return _completed;
                    }
                    _logger.LogWarning($"Unable to find callback for {args.BasicProperties.CorrelationId}.");
                    return _completed;
                }, exception => _errorStrategy.OnResponseRecievedException(consumer, cfg, args, responseTcs, exception)));
            };
        }
예제 #17
0
        private void Time(IMessage[] messages, IMessageSerializer serializer) {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            for (int i = 0; i < numberOfIterations; i++)
                using (MemoryStream stream = new MemoryStream())
                    serializer.Serialize(messages, stream);

            watch.Stop();
            Debug.WriteLine("Serializing: " + watch.Elapsed);

            watch.Reset();

            MemoryStream s = new MemoryStream();
            serializer.Serialize(messages, s);
            byte[] buffer = s.GetBuffer();
            s.Dispose();
            Console.WriteLine(Encoding.ASCII.GetString(buffer));

            watch.Start();

            object[] result = null;

            for (int i = 0; i < numberOfIterations; i++)
                using (var forDeserializing = new MemoryStream(buffer))
                    result = serializer.Deserialize(forDeserializing);

            watch.Stop();
            Debug.WriteLine("Deserializing: " + watch.Elapsed);
        }
        public async Task <object> SendRequest(Uri uri, object requestData, CancellationToken cancellationToken = default)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri));
            }
            if (requestData == null)
            {
                throw new ArgumentNullException(nameof(requestData));
            }

            var payload = await _serializer.Serialize(requestData);

            var content = new StreamContent(payload);

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);

            requestMessage.Content = content;

            var handle = BuildHandler(_httpClient, _httpMessageHandlers, requestMessage, cancellationToken);

            var responseMessage = await handle();

            responseMessage.EnsureSuccessStatusCode();

            var resultStream = await responseMessage.Content.ReadAsStreamAsync();

            return(await _serializer.Deserialize(resultStream));
        }
예제 #19
0
        private void TriggerMessageReceived(HttpMessage message)
        {
            var request = message as HttpRequest;

            if (_messageSerializer != null && request != null)
            {
                if (message.Body != null && message.Body.Length > 0)
                {
                    var result = _messageSerializer.Deserialize(message.Headers["Content-Type"], message.Body);
                    if (result == null)
                    {
                        throw new BadRequestException("Unsupported content-type: " + message.ContentType);
                    }

                    var formAndFiles = result as FormAndFilesResult;
                    if (formAndFiles != null)
                    {
                        request.Form  = formAndFiles.Form;
                        request.Files = formAndFiles.Files;
                    }
                    else
                    {
                        throw new HttpException(500, "Unknown decoder result: " + result);
                    }
                }
                var cookies = request.Headers["Cookie"];
                if (cookies != null)
                {
                    request.Cookies = _cookieParser.Parse(cookies);
                }
            }

            _messageReceived(message);
        }
예제 #20
0
        public static UnpackedMessage ReadDataMessage(byte[] buffer, IMessageSerializer serializer)
        {
            var header = ReadHeader(buffer);

            if (header.MessageFormatVersion != MessageHeader.DataMessageFormatVersion)
            {
                throw new InvalidOperationException("Unexpected message format");
            }

            var    attributes = ReadAttributes(buffer, header);
            string contract   = attributes
                                .GetAttributeString(MessageAttributeTypeContract.ContractName)
                                .ExposeException("Protocol violation: message should have contract name");
            var type = serializer
                       .GetTypeByContractName(contract)
                       .ExposeException("Unsupported contract name: '{0}'", contract);

            var index = MessageHeader.FixedSize + (int)header.AttributesLength;
            var count = (int)header.ContentLength;

            using (var stream = new MemoryStream(buffer, index, count))
            {
                var instance = serializer.Deserialize(stream, type);
                return(new UnpackedMessage(header, attributes, instance, type));
            }
        }
예제 #21
0
        public async Task Handle(TNotification notification, CancellationToken cancellationToken = default)
        {
            if (notification == null)
            {
                throw new ArgumentNullException(nameof(notification));
            }

            var requestMessagePayloadStream = (await _messageSerializer.Serialize(notification, _messageMetadata.RequestMetadata))
                                              ?? throw new InvalidOperationException("Request message payload was serialized to a null value");

            var responseMessagePayloadStream = await _messageTransport.SendRequest(_uri, requestMessagePayloadStream, cancellationToken);

            var messageSerializerResult = (await _messageSerializer.Deserialize(responseMessagePayloadStream))
                                          ?? throw new InvalidOperationException("Response message payload was deserialized to a null value");

            foreach (var metaData in messageSerializerResult.MessageMetadata)
            {
                _messageMetadata.ResponseMetadata.Add(metaData);
            }

            if (messageSerializerResult.Message != null)
            {
                throw new InvalidOperationException($"Response message contained a message payload, but was not expected to");
            }
        }
        public static IMessage Deserialize(BinaryReader reader, MavMessageType mavType)
        {
            IMessageSerializer serializer = CreateSerializer(mavType);
            IMessage           message    = serializer?.Deserialize(reader);

            return(message);
        }
예제 #23
0
        public ISubscription SubscribeAsync <T>(Func <T, TMessageContext, Task> subscribeMethod, SubscriptionConfiguration config)
        {
            var routingKey = _config.RouteWithGlobalId
                                ? $"{config.RoutingKey}.#"
                                : config.RoutingKey;

            var topologyTask = _topologyProvider.BindQueueAsync(config.Queue, config.Exchange, routingKey);
            var channelTask  = _channelFactory.CreateChannelAsync();

            var subscriberTask = Task
                                 .WhenAll(topologyTask, channelTask)
                                 .ContinueWith(t =>
            {
                var consumer            = _consumerFactory.CreateConsumer(config, channelTask.Result);
                consumer.OnMessageAsync = (o, args) => _errorHandling.ExecuteAsync(() =>
                {
                    var body    = _serializer.Deserialize <T>(args.Body);
                    var context = _contextProvider.ExtractContext(args.BasicProperties.Headers[PropertyHeaders.Context]);
                    _contextEnhancer.WireUpContextFeatures(context, consumer, args);
                    return(subscribeMethod(body, context));
                }, exception => _errorHandling.OnSubscriberExceptionAsync(consumer, config, args, exception));
                consumer.Model.BasicConsume(config.Queue.FullQueueName, config.NoAck, consumer);
                _logger.LogDebug($"Setting up a consumer on channel '{channelTask.Result.ChannelNumber}' for queue {config.Queue.QueueName} with NoAck set to {config.NoAck}.");
                return(new Subscription(consumer, config.Queue.QueueName));
            });

            Task.WaitAll(subscriberTask);
            _subscriptions.Add(subscriberTask.Result);
            return(subscriberTask.Result);
        }
    public async ValueTask <object> ReceiveAsync(CancellationToken cancellationToken)
    {
        var message = await _connection.ReceiveAsync(cancellationToken)
                      .ConfigureAwait(false);

        if (message is not MessageData messageData)
        {
            throw new InvalidOperationException($"Received invalid message: {message.GetType().Name} is not a {typeof(MessageData).Name} type.");
        }

        var data        = messageData.Data;
        var messageType = _messageTypeCache.GetTypeById(messageData.TypeId);

        var deserialized = _serializer.Deserialize(data, messageType);

        // TODO: Consider using MetadataFactory here (but set messageId to null, because it wasn't passed).
        // Probably best to implement CreateEmpty() method on MetadataFactory.
        if (messageData.Metadata == null)
        {
            messageData.Metadata = MessageMetadata.CreateEmpty();
        }

        return(new MessageWithMetadata
        {
            Message = deserialized,
            Metadata = messageData.Metadata
        });
    }
예제 #25
0
        /// <summary>
        /// Fetches a weather report from the API for the given coordinates.
        /// </summary>
        public async Task <WeatherReport> GetWeatherReport(double latitude, double longitude)
        {
            var responseString = await _httpClient.GetStringAsync(
                $"data/2.5/weather?lat={latitude}&lon={longitude}&APPID={config.Key}&units=metric");

            return(_messageSerializer.Deserialize <WeatherReport>(responseString));
        }
예제 #26
0
        protected object[] DeserializeMessages(OpenedQueue messageQueue, Message transportMessage, Action <CurrentMessageInformation, Exception> messageSerializationException)
        {
            try
            {
                return(messageSerializer.Deserialize(transportMessage.BodyStream));
            }
            catch (Exception e)
            {
                try
                {
                    logger.Error("Error when serializing message", e);
                    if (messageSerializationException != null)
                    {
                        var information = new MsmqCurrentMessageInformation
                        {
                            MsmqMessage = transportMessage,
                            Queue       = messageQueue,
                            Message     = transportMessage,
                            Source      = messageQueue.RootUri,
                            MessageId   = transportMessage.GetMessageId(),
                            Headers     = transportMessage.Extension.DeserializeHeaders(),
                        };

                        messageSerializationException(information, e);
                    }
                }
                catch (Exception moduleEx)
                {
                    logger.Error("Error when notifying about serialization exception", moduleEx);
                }
                throw;
            }
        }
예제 #27
0
        protected T SerializeAndReturn <T>(T obj)
            where T : class
        {
            byte[] serializedMessageData;

            using (var output = new MemoryStream())
            {
                _serializer.Serialize(output, obj.ToSendContext());

                serializedMessageData = output.ToArray();

                //		Trace.WriteLine(Encoding.UTF8.GetString(serializedMessageData));
            }

            using (var input = new MemoryStream(serializedMessageData))
            {
                IReceiveContext receiveContext = ReceiveContext.FromBodyStream(input);
                _serializer.Deserialize(receiveContext);

                IConsumeContext <T> context;
                receiveContext.TryGetContext(out context).ShouldBeTrue();

                context.ShouldNotBeNull();

                return(context.Message);
            }
        }
예제 #28
0
 internal SolaceMessage(IMessage mbMessage, IMessageSerializer serializer, Action acknowledgeAction, Action abortAction)
 {
     this.CorrelationId = mbMessage.CorrelationId;
     this.Content       = serializer.Deserialize <TContent>((byte[])(Array)mbMessage.GetBinaryAttachment());
     this.ackAction     = acknowledgeAction;
     this.abortAction   = abortAction;
 }
예제 #29
0
 private Envelope RestoreEnvelope(PendingEvent pendingEvent) =>
 new Envelope(
     pendingEvent.MessageId,
     _serializer.Deserialize(pendingEvent.EventJson),
     pendingEvent.OperationId,
     pendingEvent.CorrelationId,
     pendingEvent.Contributor);
예제 #30
0
        public async Task <T> GetSaga <T>(string partitionKey, string id)
        {
            using (var connection = await GetConnection().ConfigureAwait(false))
            {
                var sql     = $@"SELECT Json FROM {_schema}.{_tableName} WHERE PartitionKey = @PartitionKey AND Id = @Id AND Expiration > @UtcNow";
                var command = GetCommandWithParameters(sql, connection, partitionKey, id);
                try
                {
                    var result = await command.ExecuteReaderAsync(CommandBehavior.SingleResult).ConfigureAwait(false);

                    if (!result.HasRows)
                    {
                        throw new SagaNotFoundException(partitionKey, id);
                    }

                    await result.ReadAsync().ConfigureAwait(false);

                    var json = result.GetString(0);
                    return(_serializer.Deserialize <T>(json));
                }
                catch (SqlException e) when(e.Number == 208)
                {
                    await CreateSagaTable(connection);

                    return(await GetSaga <T>(partitionKey, id).ConfigureAwait(false));
                }
            }
        }
예제 #31
0
 private object[] DeserializeMessages(Message message)
 {
     try
     {
         return(messageSerializer.Deserialize(new MemoryStream(message.Data)));
     }
     catch (Exception e)
     {
         try
         {
             logger.Error("Error when serializing message", e);
             var serializationError = MessageSerializationException;
             if (serializationError != null)
             {
                 currentMessageInformation = new RhinoQueueCurrentMessageInformation
                 {
                     Message            = message,
                     Source             = new Uri(message.Headers["source"]),
                     MessageId          = new Guid(message.Headers["id"]),
                     TransportMessageId = message.Id.ToString(),
                     TransportMessage   = message,
                     Queue = queue,
                 };
                 serializationError(currentMessageInformation, e);
             }
         }
         catch (Exception moduleEx)
         {
             logger.Error("Error when notifying about serialization exception", moduleEx);
         }
         throw;
     }
 }
예제 #32
0
        internal static object Reserialize(IMessageSerializer serializer, object message)
        {
            if (serializer == null)
                return message;

            var writer = new BinaryTokenStreamWriter();
            serializer.Serialize(message, writer);

            var bytes = writer.ToByteArray();
            var reader = new BinaryTokenStreamReader(bytes);

            return serializer.Deserialize(reader);
        }
예제 #33
0
        private IMessage InvokeMutators(IMessage message, IMessageSerializer serializer)
        {
            var messageToSerialize = messageMutator.MutateOutgoing(message);
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new[] { messageToSerialize }, stream);

                stream.Position = 0;

                var result = serializer.Deserialize(stream)[0];

                return messageMutator.MutateIncoming(result);
            }
        }
예제 #34
0
        IObservable<object> ListenForMessages(ZmqContext context, string address, IMessageSerializer serializer)
        {
            return
                Observable.Create<object>
                (
                    o =>
                    {
                        _socket.Connect(address);

                        var poller = _scheduler.SchedulePeriodic
                        (
                            _pollInterval,
                            () =>
                            {
                                try
                                {
                                    var msg = _socket.ReceiveMessage(TimeSpan.Zero);
                                    while (msg != null && msg.FrameCount == 3)
                                    {
                                        var typeName = ZmqContext.DefaultEncoding.GetString(msg[1].Buffer);
                                        var type     = MessageTypeRegistry.Resolve(typeName);
                                        var data     = msg[2].Buffer;

                                        if (type != null)
                                        {
                                            var poco = serializer.Deserialize(data, type);
                                            o.OnNext(poco);
                                        }

                                        msg = _socket.ReceiveMessage(TimeSpan.Zero);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    o.OnError(ex);
                                }
                            }
                        );

                        return new CompositeDisposable(poller, Disposable.Create(() => _socket.Disconnect(address)));
                    }
                )
                .SubscribeOn(_scheduler)
                .ObserveOn(_scheduler)
                .Publish()
                .RefCount();
        }