Example #1
0
        public void Handle(IMessage message)
        {
            var type = message.GetType();

            Log.Trace("Message " + message.MsgId + " (Type " + type.Name + ") to be sent over TCP.");

            if (TcpConnection == null)
            {
                Log.Debug("TCP connection not yet established - Message " + message.MsgId + " (Type " + type.Name + ") will be discarded.");
                return;
            }

            foreach (var conn in TcpConnection)
            {
                try
                {
                    var data   = _messageSerializer.ToBytes(message);
                    var framed = Framer.FrameData(data);
                    conn.EnqueueSend(framed);
                }
                catch (Exception ex)
                {
                    Log.ErrorException(ex, "Exception caught while handling Message " + message.MsgId + " (Type " + type.Name + ")");
                }
            }
        }
Example #2
0
            public void DoWork()
            {
                WorkerLogger.Log("Worker started");

                while (!_cancellationToken.IsCancellationRequested)
                {
                    foreach (var id in _parent.ClientIds)
                    {
                        var client = _parent.GetDataClient(id);

                        IMessageFeed feed;
                        if (_messageFeeds.ContainsKey(id))
                        {
                            feed = _messageFeeds[id];
                        }
                        else
                        {
                            feed = _messageFeedFactory.Create();
                            _messageFeeds[id] = feed;
                        }

                        if (client.Available > 0)
                        {
                            WorkerLogger.Log("Received {0} bytes for pipe {1}", client.Available, id);

                            var buffer = new byte[client.Available];
                            client.Receive(buffer);

                            feed.Accept(buffer);
                        }

                        while (feed.Available > 0)
                        {
                            var content = feed.Pop();
                            var message = _messageSerializer.Deserialize(content);
                            _parent.OnReceivedMessage(id, message);
                        }
                    }

                    IdentifiedMessage identifiedMessage;
                    while (_parent.PopOutputMessage(out identifiedMessage))
                    {
                        var message = identifiedMessage.Message;

                        var id     = identifiedMessage.Id;
                        var client = _parent.GetDataClient(id);

                        var messageContent = _messageSerializer.Serialize(message);
                        var buffer         = _messageSerializer.ToBytes(messageContent);

                        WorkerLogger.Log("Sending message {0} from pipe {1}", message.GetType(), id);
                        client.Send(buffer);
                    }

                    Thread.Sleep(5);
                }

                Logger.Log("Logger stopped");
            }