Exemple #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);
            }
        }
Exemple #2
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);
            }
        }
Exemple #3
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);
        }
Exemple #4
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);
                }
            }
        }
Exemple #5
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);
        }