public bool MulticastMessage(int messageId, byte[] messageBody)
 {
     if (!IsStarted)
     {
         return(false);
     }
     byte[] dataBytes = messageEncoder.Encode(messageId, messageBody, IsMessageNeedEncrypt(messageId), IsMessageNeedCompress(messageId));
     return(server.MulticastMessage(dataBytes));
 }
        public void Should_encode_length_in_message()
        {
            var binaryContent = Encoding.UTF8.GetBytes("somebytes");
            var expectedBytes = binaryContent.Length;
            var data          = Unpooled.Buffer(expectedBytes).WriteBytes(binaryContent);

            List <IByteBuf> encodedMessages;

            Encoder.Encode(TestConnection, data, out encodedMessages);

            var encodedData = encodedMessages[0];

            Assert.Equal(expectedBytes + 4, encodedData.ReadableBytes);
            var decodedLength = encodedData.ReadInt();

            Assert.Equal(expectedBytes, decodedLength);
        }
Exemple #3
0
        public void ShouldDecodeSingleMessage()
        {
            var binaryContent = Encoding.ASCII.GetBytes("somebytes");
            var expectedBytes = binaryContent.Length;

            var data = ByteBuffer.AllocateDirect(expectedBytes).WriteBytes(binaryContent);

            List <IByteBuf> encodedMessages;

            Encoder.Encode(TestConnection, data, out encodedMessages);

            List <IByteBuf> decodedMessages;

            Decoder.Decode(TestConnection, encodedMessages[0], out decodedMessages);

            Assert.IsTrue(binaryContent.SequenceEqual(decodedMessages[0].ToArray()));
        }
        public virtual Task WriteAsync(IMessage message)
        {
            if (writer == null)
            {
                throw new IOException("The channel is not connected.");
            }

            return(encoder.Encode(message, writer));
        }
Exemple #5
0
        public virtual async Task WriteAsync(IMessage message)
        {
            if (writer == null)
            {
                throw new IOException("The channel is not connected.");
            }

            await encoder.Encode(message, writer).ConfigureAwait(false);
        }
Exemple #6
0
        public bool SendMessage(int messageId, byte[] messageBody, bool needEncrypt = false, bool needCompress = false)
        {
            if (IsConnected)
            {
                byte[] dataBytes = messageEncoder.Encode(messageId, messageBody, needEncrypt, needCompress);
                return(client.SendMessage(dataBytes));
            }

            return(false);
        }
            public void Encode(object message, IProtocolEncoderOutput output)
            {
                Type            type    = message.GetType();
                IMessageEncoder encoder = FindEncoder(type);

                if (encoder == null)
                {
                    throw new ProtocolEncoderException("Unexpected message type: " + type);
                }

                encoder.Encode(message, output);
            }
Exemple #8
0
        public void Encode(IoSession session, Object message, IProtocolEncoderOutput output)
        {
            State           state   = GetState(session);
            IMessageEncoder encoder = FindEncoder(state, message.GetType());

            if (encoder == null)
            {
                throw new UnknownMessageTypeException("No message encoder found for message: " + message);
            }
            else
            {
                encoder.Encode(session, message, output);
            }
        }
        private Message BuildBrokeredMessage(IDomainEvent domainEvent)
        {
            var json    = _serializer.SerializeObject(domainEvent);
            var message = new Message(_encoder.Encode(json))
            {
                ContentType = domainEvent.GetType().Name
            };

            var userProperties = _userPropertyStrategies
                                 .SelectMany(s => s.GetUserProperties(domainEvent))
                                 .ToList();

            userProperties.ForEach(kvp => message.UserProperties.TryAdd(kvp));
            message.UserProperties.Add("Encoding", _encoder.Name);

            return(message);
        }
Exemple #10
0
        public void Send(OutgoingTransportMessage transportMessage, IEnumerable <string> addresses)
        {
            var messageBuffer = messageEncoder.Encode(transportMessage.Message);

            var message = new Message(messageBuffer);

            message.Header                  = new Header();
            message.Header.Durable          = true;
            message.Properties              = new Properties();
            message.Properties.CreationTime = DateTime.UtcNow;
            message.Properties.MessageId    = Guid.NewGuid().ToString();
            message.Properties.ReplyTo      = "TODO";
            message.ApplicationProperties   = new ApplicationProperties();
            message.ApplicationProperties["LightRail.ContentType"]          = messageEncoder.ContentType;
            message.ApplicationProperties["LightRail.EnclosedMessageTypes"] = string.Join(",", messageMapper.GetEnclosedMessageTypes(transportMessage.Message.GetType()).Distinct());
            foreach (var pair in transportMessage.Headers)
            {
                message.ApplicationProperties[pair.Key] = pair.Value;
            }

            var connection = new Connection(amqpAddress);
            var session    = new Session(connection);

            // Azure does not support Amqp transactions "The server was unable to process the request; please retry the operation. If the problem persists, please contact your Service Bus administrator and provide the tracking id..TrackingId:583da4f8d58d4fa59dc9521c6f799cb8_GWIN-AN5B307EEHM,TimeStamp:11.7.2014. 7:44:17"
            try
            {
                foreach (var address in addresses)
                {
                    logger.Info("Sending Message {0} to {1}", message.Properties.MessageId, address);
                    var senderLink = new SenderLink(session, Guid.NewGuid().ToString(), address);
                    try
                    {
                        senderLink.Send(message);
                    }
                    finally
                    {
                        senderLink.Close();
                    }
                }
            }
            finally
            {
                session.Close();
                connection.Close();
            }
        }
        public async Task Send(MicroMessage message, bool flush = true)
        {
            if (!message.IsResult)
            {
                return;
            }
            var data          = _encoder.Encode(message);
            var contentLength = data.Length;

            _response.Headers.Add("Content-Type", "application/json");
            _response.Headers.Add("Content-Length", contentLength.ToString());
            await _response.Body.WriteAsync(data, 0, data.Length);

            if (flush)
            {
                await _response.Body.FlushAsync();
            }
        }
Exemple #12
0
        public void TestEncoding()
        {
            var binaryContent = Encoding.ASCII.GetBytes("somebytes");
            var expectedBytes = binaryContent.Length;
            var data          = ByteBuffer.AllocateDirect(expectedBytes).WriteBytes(binaryContent);

            List <IByteBuf> encodedMessages;

            Encoder.Encode(TestConnection, data, out encodedMessages);

            var encodedData = encodedMessages[0];

            // check lenght
            Assert.AreEqual(expectedBytes + 3, encodedData.ReadableBytes);

            // check frame
            Assert.AreEqual(encodedData.GetByte(0), Convert.ToByte((char)11));
            Assert.AreEqual(encodedData.GetByte(encodedData.ReadableBytes - 2), Convert.ToByte((char)28));
            Assert.AreEqual(encodedData.GetByte(encodedData.ReadableBytes - 1), Convert.ToByte((char)13));
        }
Exemple #13
0
        protected IByteBuffer GetByteBuffer(IMicroMessage message)
        {
            var data = _messageEncoder.Encode(message);

            return(Unpooled.WrappedBuffer(data));
        }
Exemple #14
0
        public async Task WriteAsync(NetworkStream stream, string responseText)
        {
            var response = await _encoder.Encode(responseText);

            await stream.WriteAsync(response, 0, response.Length);
        }
Exemple #15
0
 public virtual Task WriteAsync(IMessage message)
 {
     return(encoder.Encode(message, writer));
 }