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); } }
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); } }
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); }
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); } } }
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); }