예제 #1
0
        /// <summary>
        /// Reinjects the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="header">The header.</param>
        public void Reinject(IMessage message, MessageHeader header)
        {
            if (header.ReinjectionNumber > _maxReijections)
            {
                return;
            }

            header.ReinjectionNumber++;

            var messageBus = MessageBusFactory.Create(header.IdentificationService, message, DataContractSerializer);

            messageBus.Header = header;
            var serializedMessage = DataContractSerializer.Serialize(messageBus);

            _receiverReceiverEndPoint.Reinject(serializedMessage, header.Priority);
        }
예제 #2
0
        /// <summary>
        /// Creates the specified identification.
        /// </summary>
        /// <param name="identification">The identification.</param>
        /// <param name="message">The message.</param>
        /// <param name="dataContractSerializer">The data contract serializer.</param>
        /// <returns></returns>
        public static MessageBus Create(Identification identification, IMessage message, IDataContractSerializer dataContractSerializer)
        {
            var messageBus = new MessageBus
            {
                Header =
                {
                    CreatedAt             = DateTime.UtcNow,
                    IdentificationService =
                    {
                        Id   = identification.Id,
                        Type = identification.Type
                    },
                    BodyType         = GetKey(message.GetType()),
                    Type             = MessageBusType.Generic,
                    EncodingCodePage = dataContractSerializer.Encoding.CodePage
                },
                Body = dataContractSerializer.Serialize(message)
            };

            return(messageBus);
        }
예제 #3
0
        /// <summary>
        /// Creates the specified identification.
        /// </summary>
        /// <param name="identification">The identification.</param>
        /// <param name="message">The message.</param>
        /// <param name="dataContractSerializer">The data contract serializer.</param>
        /// <returns></returns>
        public static MessageBus Create(Identification identification, IMessage message, IDataContractSerializer dataContractSerializer)
        {
            var messageBus = new MessageBus
                                 {
                                     Header =
                                         {
                                             CreatedAt = DateTime.UtcNow,
                                             IdentificationService =
                                                 {
                                                     Id = identification.Id,
                                                     Type = identification.Type
                                                 },
                                             BodyType = GetKey(message.GetType()),
                                             Type = MessageBusType.Generic,
                                             EncodingCodePage = dataContractSerializer.Encoding.CodePage
                                         },
                                     Body = dataContractSerializer.Serialize(message)
                                 };

            return messageBus;
        }
예제 #4
0
 /// <summary>
 ///     Sends the specified message bus.
 /// </summary>
 /// <param name="messageBus">The message bus.</param>
 public void Publish(MessageBus messageBus)
 {
     byte[] serializedMessage = Encoding.UTF8.GetBytes(_dataContractSerializer.Serialize(messageBus));
     //TODO: Crear la serializacion del message bus
     Publish(messageBus.Header.BodyType, messageBus.Header.Priority, serializedMessage);
 }
예제 #5
0
        public static byte[] ToBytes(MessageBus messageBus)
        {
            var serializedMessage = Serializer.Serialize(messageBus);

            var bytesMessage  = GzipCompression.Zip(serializedMessage);
            var bytesBodyType = Encoding.UTF8.GetBytes(messageBus.Header.BodyType);

            CallerContext callerContext = null;

            if (messageBus.Header.CallStack.Count > 0)
            {
                callerContext = messageBus.Header.CallStack.Peek();
            }
            byte[] idBytes      = new byte[0];
            byte[] typeBytes    = new byte[0];
            int    bufferLength = 0;

            if (callerContext != null)
            {
                idBytes      = Encoding.UTF8.GetBytes(callerContext.Identification.Id);
                typeBytes    = Encoding.UTF8.GetBytes(callerContext.Identification.Type);
                bufferLength = 15 + bytesBodyType.Length + 4 + bytesMessage.Length + 4 + idBytes.Length + 4 +
                               typeBytes.Length;
            }
            else
            {
                bufferLength = 15 + bytesBodyType.Length + 4 + bytesMessage.Length + 4;
            }


            var bytes = new byte[bufferLength];

            bytes[0] = (byte)messageBus.Header.Type;
            bytes[1] = (byte)messageBus.Header.Priority;

            var ticks = messageBus.Header.CreatedAt.Ticks;

            bytes[3]  = (byte)(ticks);
            bytes[4]  = (byte)(ticks >> 8);
            bytes[5]  = (byte)(ticks >> 16);
            bytes[6]  = (byte)(ticks >> 24);
            bytes[7]  = (byte)(ticks >> 32);
            bytes[8]  = (byte)(ticks >> 40);
            bytes[9]  = (byte)(ticks >> 48);
            bytes[10] = (byte)(ticks >> 56);


            bytes[11] = (byte)(bytesBodyType.Length);
            bytes[12] = (byte)(bytesBodyType.Length >> 8);
            bytes[13] = (byte)(bytesBodyType.Length >> 16);
            bytes[14] = (byte)(bytesBodyType.Length >> 24);

            Array.Copy(bytesBodyType, 0, bytes, 15, bytesBodyType.Length);

            var messagePosition = 15 + bytesBodyType.Length;

            bytes[messagePosition]     = (byte)(bytesMessage.Length);
            bytes[messagePosition + 1] = (byte)(bytesMessage.Length >> 8);
            bytes[messagePosition + 2] = (byte)(bytesMessage.Length >> 16);
            bytes[messagePosition + 3] = (byte)(bytesMessage.Length >> 24);

            Array.Copy(bytesMessage, 0, bytes, messagePosition + 4, bytesMessage.Length);

            var idPosition = messagePosition + 4 + bytesMessage.Length;

            bytes[idPosition]     = (byte)(idBytes.Length);
            bytes[idPosition + 1] = (byte)(idBytes.Length >> 8);
            bytes[idPosition + 2] = (byte)(idBytes.Length >> 16);
            bytes[idPosition + 3] = (byte)(idBytes.Length >> 24);

            if (callerContext != null)
            {
                Array.Copy(idBytes, 0, bytes, idPosition + 4, idBytes.Length);

                var typePosition = idPosition + 4 + idBytes.Length;
                bytes[typePosition]     = (byte)(typeBytes.Length);
                bytes[typePosition + 1] = (byte)(typeBytes.Length >> 8);
                bytes[typePosition + 2] = (byte)(typeBytes.Length >> 16);
                bytes[typePosition + 3] = (byte)(typeBytes.Length >> 24);

                Array.Copy(typeBytes, 0, bytes, typePosition + 4, typeBytes.Length);
            }
            return(bytes);
        }