Exemplo n.º 1
0
        public override void Handle(IMessage message)
        {
            var type = message.GetType();

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

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

            try
            {
                if (TcpConnections.IsEmpty())
                {
                    Log.Error("Cannot send a message without a connection.");
                    return;
                }
                var connection = TcpConnections.First();
                if (!MessageSerializers.TryGetValue(type, out var serializer))
                {
                    serializer = new SimpleJsonSerializer();
                }
                var json   = serializer.SerializeMessage(message);
                var data   = Encoder.ToBytes(json, type);
                var framed = Framer.FrameData(data);
                connection.EnqueueSend(framed);
            }
            catch (Exception ex)
            {
                Log.ErrorException(ex, $"Exception caught while handling Message {message.MsgId} (Type {type.Name})");
            }
        }
        public override void Handle(IMessage message)
        {
            // The server side does not initiate communication. The only messages it will ever send are
            // CommandResponses back to a client who sent a Command.
            var type = message.GetType();

            if (!(message is CommandResponse cmdResponse))
            {
                Log.Debug($"Cannot send a message of type {type.Name} from a server-side TCP bus.");
                return;
            }
            Log.Trace($"Message {message.MsgId} (Type {type.Name}) to be sent over TCP.");

            if (TcpConnections.IsEmpty())
            {
                Log.Debug($"TCP connection not yet established - Message {message.MsgId} (Type {type.Name}) will be discarded.");
                return;
            }

            try
            {
                // Send the CommandResponse back to the endpoint where the Command originated.
                var connectionId = _messageRouting[cmdResponse.SourceCommand.MsgId];
                var connection   = TcpConnections.FirstOrDefault(x => x.ConnectionId == connectionId);
                if (connection is null)
                {
                    throw new Exception("Could not find a TCP connection to use for sending the message.");
                }

                if (!MessageSerializers.TryGetValue(type, out var serializer))
                {
                    serializer = new SimpleJsonSerializer();
                }
                var json   = serializer.SerializeMessage(message);
                var data   = Encoder.ToBytes(json, type);
                var framed = Framer.FrameData(data);
                connection.EnqueueSend(framed);
            }
            catch (Exception ex)
            {
                Log.ErrorException(ex, $"An error occurred while handling Message {message.MsgId} (Type {type.Name})");
            }
        }