public void EnsureReliability(OutboundDisruptorEntry disruptorEntry, IMessage message, IEnumerable<MessageSubscription> concernedSubscriptions, MessageWireData messageData)
        {
            var messageOptions = _messageOptions[message.GetType().FullName];

            if (messageOptions.ReliabilityLevel != ReliabilityLevel.FireAndForget)
                foreach (var wireMessage in disruptorEntry.NetworkSenderData.WireMessages)
                {
                    int seqNum;
                    if (!_endpointToSequenceNumber.TryGetValue(wireMessage.Endpoint, out seqNum))
                    {
                        _endpointToSequenceNumber.Add(wireMessage.Endpoint, 0);
                        seqNum = 0;
                    }
                    wireMessage.MessageData.SequenceNumber = seqNum;
                    _endpointToSequenceNumber[wireMessage.Endpoint] = seqNum + 1;
                }

            if (disruptorEntry.MessageTargetHandlerData.IsAcknowledgement)
            {
                SendAcknowledgementShadowMessages(message, concernedSubscriptions, disruptorEntry, messageData);
            }
            else
            {
                if (messageOptions.ReliabilityLevel == ReliabilityLevel.Persisted)
                {
                    SendShadowMessages(concernedSubscriptions, messageData, disruptorEntry);

                }
            }
        }
 public ShadowMessageCommand(MessageWireData message, PeerId primaryRecipient, bool primaryWasOnline, IEndpoint targetEndpoint)
 {
     Message = message;
     PrimaryRecipient = primaryRecipient;
     PrimaryWasOnline = primaryWasOnline;
     TargetEndpoint = targetEndpoint;
 }
        public byte[] Serialize(MessageWireData data)
        {
            var guidLength = 16;
            var length = data.Data.Length + guidLength + 4 + 4 + 4 + 4;
            var finalArray = new byte[length];

            var idByteArray = data.MessageIdentity.ToByteArray();
            idByteArray.CopyTo(finalArray, 0);

            ByteUtils.WriteInt(finalArray, guidLength, _serializationHelper.GetMessageTypeId(data.MessageType));
            ByteUtils.WriteInt(finalArray, guidLength + 4, data.SendingPeerId.Id);
            var sendingPeerOffset = guidLength + 4 + 4;

            ByteUtils.WriteInt(finalArray, sendingPeerOffset, data.Data.Length);
            data.Data.CopyTo(finalArray, sendingPeerOffset + 4);

            if (data.SequenceNumber != null)
                ByteUtils.WriteInt(finalArray, sendingPeerOffset + 4 + data.Data.Length, data.SequenceNumber.Value);
            else
            {
                ByteUtils.WriteInt(finalArray, sendingPeerOffset + 4 + data.Data.Length, -1);
            }

            return finalArray;
        }
        public void serializationTestsCustom()
        {
            var serializer = new MessageWireDataSerializer(new SerializationHelper(new AssemblyScanner()));
            using (var perfMeasure = new PerformanceMeasure(() =>
                                                                {
                                                                    var instance = new MessageWireData();
                                                                   var ser = serializer.Serialize(_testData);
                                                                   using (var stream = new MemoryStream(ser))
                                                                       serializer.Deserialize(stream, instance);

                                                               }, 1000000)) ;
        }
        public void should_serialize_deserialize()
        {
            var message = new MessageWireData(typeof(TestData.FakeCommand).FullName, Guid.NewGuid(), new PeerId(3), Encoding.ASCII.GetBytes("data"))
                              {SequenceNumber = 1234567};
            var buffer = _serializer.Serialize(message);
            var stream = new MemoryStream(buffer,0, buffer.Length);
            var deserializedMessage = new MessageWireData();
            _serializer.Deserialize(stream, deserializedMessage);

            Assert.AreEqual(message.Data, deserializedMessage.Data);
            Assert.AreEqual(message.MessageIdentity, deserializedMessage.MessageIdentity);
            Assert.AreEqual(message.MessageType, deserializedMessage.MessageType);
            Assert.AreEqual(message.SendingPeerId, deserializedMessage.SendingPeerId);
            Assert.AreEqual(message.SequenceNumber, deserializedMessage.SequenceNumber);
        }
        public void Deserialize(Stream data, MessageWireData instance)
        {
            data.Read(_guidArray, 0, 16);
               instance.MessageIdentity = new Guid(_guidArray);
               var messageTypeId = ByteUtils.ReadIntFromStream(data);

            instance.SendingPeerId = new PeerId(ByteUtils.ReadIntFromStream(data));
            var dataLength = ByteUtils.ReadIntFromStream(data);
            var binaryData = new byte[dataLength];
            data.Read(binaryData, 0, dataLength);
            instance.Data = binaryData;
            int? sequenceNumber = null;
                sequenceNumber = ByteUtils.ReadIntFromStream(data);
            instance.SequenceNumber = sequenceNumber;

            instance.MessageType = _serializationHelper.GetMessageTypeFromId(messageTypeId);
               // return new MessageWireData(messageTypeFromId, id, new PeerId(sendingPeer), binaryData) { SequenceNumber = sequenceNumber == -1 ? null : sequenceNumber };
        }
        public void SendHeartbeat(IEndpoint endpoint)
        {
            var sequence = _ringBuffer.Next();
            var data = _ringBuffer[sequence];
            data.MessageTargetHandlerData = new MessageTargetHandlerData();
            var heartbeatRequest = new HeartbeatRequest(DateTime.UtcNow, endpoint);
            var serializedMessage = BusSerializer.Serialize(heartbeatRequest);
            var messageWireData = new MessageWireData(typeof(HeartbeatRequest).FullName, Guid.NewGuid(), _peerConfiguration.PeerId, serializedMessage);
            data.NetworkSenderData.WireMessages = new List<WireSendingMessage>();
            data.NetworkSenderData.WireMessages.Add(new WireSendingMessage(messageWireData, endpoint));

            _ringBuffer.Publish(sequence);
        }
 private MessageWireData CreateMessageWireData(IMessage message)
 {
     var serializedMessage = BusSerializer.Serialize(message);
     var messageId = Guid.NewGuid();
     var messageType = message.GetType().FullName;
     var messageData = new MessageWireData(messageType, messageId, _peerConfiguration.PeerId, serializedMessage);
     return messageData;
 }
        private void SendShadowMessages(IEnumerable<MessageSubscription> concernedSubscriptions, MessageWireData messageData, OutboundDisruptorEntry disruptorData)
        {
            foreach (var subscription in concernedSubscriptions)
            {
                HashSet<ServicePeer> targetShadows;
                if (_peersToShadows.TryGetValue(subscription.Peer, out targetShadows))
                {
                    var endpoints = targetShadows.Select(x => x.HandledMessages.Single(y => y.MessageType == typeof(ShadowMessageCommand)).Endpoint).Distinct();

                    foreach (var endpoint in endpoints)
                    {
                        var shadowMessage = new ShadowMessageCommand(messageData, subscription.Peer, true, subscription.Endpoint);
                        var shadowMessageData = CreateMessageWireData(shadowMessage);
                        var wireMessage = new WireSendingMessage(shadowMessageData, endpoint);
                        disruptorData.NetworkSenderData.WireMessages.Add(wireMessage);
                    }
                }
            }
        }
        private void SendAcknowledgementShadowMessages(IMessage message, IEnumerable<MessageSubscription> concernedSubscriptions, OutboundDisruptorEntry disruptorData, MessageWireData messageData)
        {
            var completionAcknowledgementMessage = (CompletionAcknowledgementMessage)message;
            if (_messageOptions[completionAcknowledgementMessage.MessageType].ReliabilityLevel == ReliabilityLevel.Persisted)
            {
                SendToSelfShadows(completionAcknowledgementMessage.MessageId,
                                  completionAcknowledgementMessage.ProcessingSuccessful,
                                  disruptorData.MessageTargetHandlerData.TargetPeer,
                                  completionAcknowledgementMessage.Endpoint,
                                  completionAcknowledgementMessage.MessageType,
                                  disruptorData);

                SendShadowMessages(concernedSubscriptions, messageData, disruptorData);
            }
        }
 public WireSendingMessage(MessageWireData messageData, IEndpoint endpoint)
     : this()
 {
     MessageData = messageData;
     Endpoint = endpoint;
 }