示例#1
0
        private void WriteTransportMessageAndSendToPeers(TransportMessage transportMessage, List <Peer> peers, SendContext context, ProtoBufferWriter bufferWriter)
        {
            bufferWriter.Reset();
            bufferWriter.WriteTransportMessage(transportMessage, _environment);

            if (context.PersistencePeer == null && transportMessage.IsPersistTransportMessage)
            {
                bufferWriter.WritePersistentPeerIds(transportMessage, transportMessage.PersistentPeerIds);
            }

            foreach (var target in peers)
            {
                var isPersistent = context.WasPersisted(target.Id);
                bufferWriter.SetWasPersisted(isPersistent);

                SendToPeer(transportMessage, bufferWriter, target);
            }

            if (context.PersistencePeer != null)
            {
                bufferWriter.WritePersistentPeerIds(transportMessage, context.PersistentPeerIds);

                SendToPeer(transportMessage, bufferWriter, context.PersistencePeer);
            }
        }
示例#2
0
        public void MeasureWritePerformance()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();
            var writer           = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);

            const int count = 10 * 1000 * 1000;

            using (Measure.Throughput(count))
            {
                for (var i = 0; i < count; i++)
                {
                    writer.Reset();
                    writer.WriteTransportMessage(transportMessage);
                }
            }
        }
示例#3
0
        public void should_serialize_transport_message_twice()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            var writer = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);

            var deserialized1 = Serializer.Deserialize <TransportMessage_1_5_0>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserialized1.WasPersisted.ShouldEqual(true);

            writer.Reset();
            transportMessage.WasPersisted = false;
            writer.WriteTransportMessage(transportMessage);

            var deserialized2 = Serializer.Deserialize <TransportMessage_1_5_0>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserialized2.WasPersisted.ShouldEqual(false);
        }
示例#4
0
        private void SendEndOfStreamMessages(List <ZmqOutboundSocket> connectedOutboundSockets, ProtoBufferWriter bufferWriter)
        {
            foreach (var outboundSocket in connectedOutboundSockets)
            {
                _logger.InfoFormat("Sending EndOfStream to {0}", outboundSocket.EndPoint);

                var endOfStreamMessage = new TransportMessage(MessageTypeId.EndOfStream, new MemoryStream(), PeerId, InboundEndPoint)
                {
                    WasPersisted = false
                };
                bufferWriter.Reset();
                bufferWriter.WriteTransportMessage(endOfStreamMessage, _environment);
                outboundSocket.Send(bufferWriter.Buffer, bufferWriter.Position, endOfStreamMessage);
            }
        }
示例#5
0
        public byte[] Serialize(TransportMessage transportMessage)
        {
            _bufferWriter.Reset();
            _bufferWriter.WriteTransportMessage(transportMessage);

            var bytes = new byte[_bufferWriter.Position];

            Buffer.BlockCopy(_bufferWriter.Buffer, 0, bytes, 0, _bufferWriter.Position);

            // prevent service from leaking after fat transport message serializations
            if (_bufferWriter.Position > _maximumCapacity)
            {
                _bufferWriter = new ProtoBufferWriter(new byte[_maximumCapacity]);
            }

            return(bytes);
        }
示例#6
0
        public void should_serialize_transport_message_and_set_WasPersisted(bool?previousWasPersistedValue, bool newWasPersistedValue)
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            transportMessage.WasPersisted = previousWasPersistedValue;

            var writer = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);
            writer.SetWasPersisted(newWasPersistedValue);

            var deserializedTransportMessage1 = Serializer.Deserialize <TransportMessage>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserializedTransportMessage1.Id.ShouldEqual(transportMessage.Id);
            deserializedTransportMessage1.Environment.ShouldEqual(transportMessage.Environment);
            deserializedTransportMessage1.WasPersisted.ShouldEqual(newWasPersistedValue);
        }
示例#7
0
        public void should_serialize_transport_message_and_read_from_protobuf()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            var writer = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);

            var deserialized = Serializer.Deserialize <TransportMessage>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserialized.Id.ShouldEqual(transportMessage.Id);
            deserialized.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserialized.GetContentBytes().ShouldEqual(transportMessage.GetContentBytes());
            deserialized.Originator.ShouldEqualDeeply(transportMessage.Originator);
            deserialized.Originator.SenderMachineName.ShouldEqual(transportMessage.Originator.SenderMachineName);
            deserialized.Environment.ShouldEqual(transportMessage.Environment);
            deserialized.WasPersisted.ShouldEqual(true);
        }
        public void should_read_message()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            var bufferWriter = new ProtoBufferWriter();

            bufferWriter.WriteTransportMessage(transportMessage);

            var bufferReader = new ProtoBufferReader(bufferWriter.Buffer, bufferWriter.Position);
            var deserialized = bufferReader.ReadTransportMessage();

            deserialized.Id.ShouldEqual(transportMessage.Id);
            deserialized.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserialized.GetContentBytes().ShouldEqual(transportMessage.GetContentBytes());
            deserialized.Originator.ShouldEqualDeeply(transportMessage.Originator);
            deserialized.Environment.ShouldEqual(transportMessage.Environment);
            deserialized.WasPersisted.ShouldEqual(transportMessage.WasPersisted);
        }
示例#9
0
        public void should_serialize_transport_message_with_empty_strings()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            transportMessage.Environment = null;
            transportMessage.Originator  = new OriginatorInfo(new PeerId(null), null, null, null);

            var writer = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);

            var deserializedTransportMessage1 = Serializer.Deserialize <TransportMessage>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserializedTransportMessage1.Id.ShouldEqual(transportMessage.Id);
            deserializedTransportMessage1.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserializedTransportMessage1.GetContentBytes().ShouldEqual(transportMessage.GetContentBytes());
            deserializedTransportMessage1.Originator.InitiatorUserName.ShouldEqual(transportMessage.Originator.InitiatorUserName);
            deserializedTransportMessage1.Environment.ShouldEqual(transportMessage.Environment);
            deserializedTransportMessage1.WasPersisted.ShouldEqual(true);
        }
示例#10
0
        public void should_serialize_transport_message_with_persistent_peer_ids_and_read_from_protobuf()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            transportMessage.PersistentPeerIds = new List <PeerId>
            {
                new PeerId("Abc.Testing.A"),
                new PeerId("Abc.Testing.B"),
            };

            var writer = new ProtoBufferWriter();

            writer.WriteTransportMessage(transportMessage);
            writer.WritePersistentPeerIds(transportMessage, transportMessage.PersistentPeerIds);

            var deserialized = Serializer.Deserialize <TransportMessage>(new MemoryStream(writer.Buffer, 0, writer.Position));

            deserialized.Id.ShouldEqual(transportMessage.Id);
            deserialized.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserialized.PersistentPeerIds.ShouldBeEquivalentTo(transportMessage.PersistentPeerIds);
        }
        public void should_read_message_with_persistent_peer_ids()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            transportMessage.PersistentPeerIds = new List <PeerId>
            {
                new PeerId("Abc.Testing.A"),
                new PeerId("Abc.Testing.B"),
            };

            var bufferWriter = new ProtoBufferWriter();

            bufferWriter.WriteTransportMessage(transportMessage);
            bufferWriter.WritePersistentPeerIds(transportMessage, transportMessage.PersistentPeerIds);

            var bufferReader = new ProtoBufferReader(bufferWriter.Buffer, bufferWriter.Position);
            var deserialized = bufferReader.ReadTransportMessage();

            deserialized.Id.ShouldEqual(transportMessage.Id);
            deserialized.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserialized.PersistentPeerIds.ShouldEqual(transportMessage.PersistentPeerIds);
        }
        public void should_read_empty_message()
        {
            var transportMessage = new EmptyCommand().ToTransportMessage();

            var bufferWriter = new ProtoBufferWriter();

            bufferWriter.WriteTransportMessage(transportMessage);

            var bufferReader = new ProtoBufferReader(bufferWriter.Buffer, bufferWriter.Position);
            var deserialized = bufferReader.ReadTransportMessage();

            deserialized.Id.ShouldEqual(transportMessage.Id);
            deserialized.MessageTypeId.ShouldEqual(transportMessage.MessageTypeId);
            deserialized.Content.ShouldEqual(Stream.Null);
            deserialized.Originator.ShouldEqualDeeply(transportMessage.Originator);
            deserialized.Environment.ShouldEqual(transportMessage.Environment);
            deserialized.WasPersisted.ShouldEqual(transportMessage.WasPersisted);

            var deserializedMessage = deserialized.ToMessage() as EmptyCommand;

            deserializedMessage.ShouldNotBeNull();
        }
        public void MeasureReadPerformance()
        {
            var transportMessage = TestDataBuilder.CreateTransportMessage <FakeCommand>();

            var bufferWriter = new ProtoBufferWriter();

            bufferWriter.WriteTransportMessage(transportMessage);

            var bufferReader = new ProtoBufferReader(bufferWriter.Buffer, bufferWriter.Position);

            bufferReader.ReadTransportMessage();

            const int count = 100_000_000;

            using (Measure.Throughput(count))
            {
                for (var i = 0; i < count; i++)
                {
                    bufferReader.Reset();
                    bufferReader.ReadTransportMessage();
                }
            }
        }
        public void should_serialize_empty_messages_like_1_5_0()
        {
            var oldTransportMessage = new TransportMessage_1_5_0
            {
                Id         = new MessageId(Guid.NewGuid()),
                Originator = new OriginatorInfo_1_5_0(),
                Content    = new byte[0],
            };
            var oldOutput = new MemoryStream();

            ProtoBuf.Serializer.Serialize(oldOutput, oldTransportMessage);

            var newTransportMessage = new TransportMessage()
            {
                Id         = oldTransportMessage.Id,
                Originator = new OriginatorInfo(),
                Content    = new MemoryStream(),
            };
            var bufferWriter = new ProtoBufferWriter();

            bufferWriter.WriteTransportMessage(newTransportMessage);

            bufferWriter.ToArray().SequenceEqual(oldOutput.ToArray()).ShouldBeTrue();
        }