private byte[] AssembleSessionMessage(SessionMessageBase message,
                                              uint sequenceNumber,
                                              uint sourceParticipantId,
                                              uint targetParticipantId = 0)
        {
            var fragment = new SessionFragmentMessage();

            fragment.Header.IsFragment         = message.Header.IsFragment;
            fragment.Header.ChannelId          = message.Header.ChannelId;
            fragment.Header.RequestAcknowledge = message.Header.RequestAcknowledge;
            fragment.Header.SessionMessageType = message.Header.SessionMessageType;
            fragment.Header.Version            = message.Header.Version;

            fragment.Header.SequenceNumber      = sequenceNumber;
            fragment.Header.SourceParticipantId = sourceParticipantId;
            fragment.Header.TargetParticipantId = targetParticipantId;

            var writer = new EndianWriter();

            message.Serialize(writer);
            fragment.Fragment = writer.ToBytes();

            ((ICryptoMessage)fragment).Crypto = _crypto;

            var finalWriter = new EndianWriter();

            fragment.Serialize(finalWriter);
            return(finalWriter.ToBytes());
        }
예제 #2
0
        public MessageInfo ReadMessage(byte[] encryptedMessage)
        {
            var message = new SessionFragmentMessage();

            message.Crypto = _crypto;

            message.Deserialize(new BEReader(encryptedMessage));

            if (message.InvalidSignature)
            {
                throw new Exception("Invalid message signature.");
            }

            var info = new MessageInfo()
            {
                RequestAcknowledge = message.Header.RequestAcknowledge,
                Version            = message.Header.Version,
                ChannelId          = message.Header.ChannelId,
                MessageType        = message.Header.SessionMessageType.ToString(),
                Data = message.Fragment
            };

            if (message.Header.SessionMessageType == SessionMessageType.Json)
            {
                var jsonMessage = new JsonMessage();
                jsonMessage.Deserialize(new BEReader(message.Fragment));

                info.Json = jsonMessage.Json;
            }

            return(info);
        }