/// <summary> /// Registers the specified application and notification types and allows for additional request data. /// </summary> /// <param name="application">The <see cref="Application"/> to register.</param> /// <param name="notificationTypes">The <see cref="NotificationType"/>s to register.</param> /// <param name="requestData">The <see cref="RequestData"/> containing the additional information.</param> /// <param name="state">An optional state object that will be passed into the response events associated with this request</param> public virtual void Register(Application application, NotificationType[] notificationTypes, RequestData requestData, object state) { HeaderCollection appHeaders = application.ToHeaders(); List <HeaderCollection> notifications = new List <HeaderCollection>(); foreach (NotificationType notificationType in notificationTypes) { HeaderCollection notificationHeaders = notificationType.ToHeaders(); notifications.Add(notificationHeaders); } MessageBuilder mb = new MessageBuilder(RequestType.REGISTER, this.GetKey()); foreach (Header header in appHeaders) { mb.AddHeader(header); } mb.AddHeader(new Header(Header.NOTIFICATIONS_COUNT, notificationTypes.Length.ToString())); // handle any additional request data if (requestData != null) { HeaderCollection requestDataHeaders = requestData.ToHeaders(); foreach (Header header in requestDataHeaders) { mb.AddHeader(header); } } foreach (HeaderCollection headers in notifications) { MessageSection ms = new MessageSection(); foreach (Header header in headers) { ms.AddHeader(header); } mb.AddMessageSection(ms); } Send(mb, OnResponseReceived, false, state); }
/// <summary> /// Registers the specified application and notification types and allows for additional request data. /// </summary> /// <param name="application">The <see cref="Application"/> to register.</param> /// <param name="notificationTypes">The <see cref="NotificationType"/>s to register.</param> /// <param name="requestData">The <see cref="RequestData"/> containing the additional information.</param> /// <param name="state">An optional state object that will be passed into the response events associated with this request</param> public virtual void Register(Application application, NotificationType[] notificationTypes, RequestData requestData, object state) { HeaderCollection appHeaders = application.ToHeaders(); List<HeaderCollection> notifications = new List<HeaderCollection>(); foreach (NotificationType notificationType in notificationTypes) { HeaderCollection notificationHeaders = notificationType.ToHeaders(); notifications.Add(notificationHeaders); } MessageBuilder mb = new MessageBuilder(RequestType.REGISTER, this.GetKey()); foreach(Header header in appHeaders) { mb.AddHeader(header); } mb.AddHeader(new Header(Header.NOTIFICATIONS_COUNT, notificationTypes.Length.ToString())); // handle any additional request data if (requestData != null) { HeaderCollection requestDataHeaders = requestData.ToHeaders(); foreach (Header header in requestDataHeaders) { mb.AddHeader(header); } } foreach(HeaderCollection headers in notifications) { MessageSection ms = new MessageSection(); foreach(Header header in headers) { ms.AddHeader(header); } mb.AddMessageSection(ms); } Send(mb, OnResponseReceived, false, state); }
/// <summary> /// Converts the contents of the message into an array of bytes /// </summary> /// <returns>Array of bytes</returns> public override byte[] GetBytes() { List<byte> allBytes = new List<byte>(); List<byte> messageBytes = new List<byte>(); List<BinaryData> allBinaryData = new List<BinaryData>(); allBinaryData.AddRange(this.binaryData); // additional sections messageBytes.AddRange(this.bytes); foreach (MessageSection section in this.sections) { messageBytes.AddRange(blankLineBytes); messageBytes.AddRange(section.GetBytes()); allBinaryData.AddRange(section.BinaryData); } // encrypt message byte[] bytesToEncrypt = messageBytes.ToArray(); EncryptionResult result = key.Encrypt(bytesToEncrypt); string encryptionInfo = DisplayName.Fetch(key.EncryptionAlgorithm); if (key.EncryptionAlgorithm != Cryptography.SymmetricAlgorithmType.PlainText) { string iv = Cryptography.HexEncode(result.IV); encryptionInfo = String.Format("{0}:{1}", encryptionInfo, iv); } string hashInfo = ""; if (includeKeyHash) { string keyHash = key.KeyHash; string salt = key.Salt; hashInfo = String.Format(" {0}:{1}.{2}", DisplayName.Fetch(key.HashAlgorithm), keyHash, salt); } // start building message string s = String.Format("{0} {1}{2}", messageType, encryptionInfo, hashInfo); allBytes.AddRange(protocolHeaderBytes); allBytes.AddRange(GetStringBytes(s.ToUpper())); allBytes.AddRange(blankLineBytes); if (key.EncryptionAlgorithm != Cryptography.SymmetricAlgorithmType.PlainText) { /* use this to add the Length: header to the main section MessageSection lengthSection = new MessageSection(); lengthSection.AddHeader(new Header(Header.RESOURCE_LENGTH, result.EncryptedBytes.Length.ToString())); lengthSection.AddBlankLine(); allBytes.AddRange(lengthSection.GetBytes()); * */ allBytes.AddRange(result.EncryptedBytes); allBytes.AddRange(blankLineBytes); } else { allBytes.AddRange(result.EncryptedBytes); } // handle binary resources foreach (BinaryData data in allBinaryData) { if (data != null && data.Data != null) { // encrypt each resource, making sure to use the same IV EncryptionResult er = key.Encrypt(data.Data, ref result.IV); MessageSection section = new MessageSection(); section.AddBlankLine(); section.AddHeader(new Header(Header.RESOURCE_IDENTIFIER, data.ID)); section.AddHeader(new Header(Header.RESOURCE_LENGTH, er.EncryptedBytes.Length.ToString())); section.AddBlankLine(); allBytes.AddRange(section.GetBytes()); allBytes.AddRange(er.EncryptedBytes); allBytes.AddRange(blankLineBytes); } } allBytes.AddRange(blankLineBytes); return allBytes.ToArray(); }
/// <summary> /// Adds a <see cref="MessageSection"/> to the message /// </summary> /// <param name="section"><see cref="MessageSection"/></param> public void AddMessageSection(MessageSection section) { this.sections.Add(section); }
/// <summary> /// Converts the contents of the message into an array of bytes /// </summary> /// <returns>Array of bytes</returns> public override byte[] GetBytes() { List <byte> allBytes = new List <byte>(); List <byte> messageBytes = new List <byte>(); List <BinaryData> allBinaryData = new List <BinaryData>(); allBinaryData.AddRange(this.binaryData); // additional sections messageBytes.AddRange(this.bytes); foreach (MessageSection section in this.sections) { messageBytes.AddRange(blankLineBytes); messageBytes.AddRange(section.GetBytes()); allBinaryData.AddRange(section.BinaryData); } // encrypt message byte[] bytesToEncrypt = messageBytes.ToArray(); EncryptionResult result = key.Encrypt(bytesToEncrypt); string encryptionInfo = DisplayName.Fetch(key.EncryptionAlgorithm); if (key.EncryptionAlgorithm != Cryptography.SymmetricAlgorithmType.PlainText) { string iv = Cryptography.HexEncode(result.IV); encryptionInfo = String.Format("{0}:{1}", encryptionInfo, iv); } string hashInfo = ""; if (includeKeyHash) { string keyHash = key.KeyHash; string salt = key.Salt; hashInfo = String.Format(" {0}:{1}.{2}", DisplayName.Fetch(key.HashAlgorithm), keyHash, salt); } // start building message string s = String.Format("{0} {1}{2}", messageType, encryptionInfo, hashInfo); allBytes.AddRange(protocolHeaderBytes); allBytes.AddRange(GetStringBytes(s.ToUpper())); allBytes.AddRange(blankLineBytes); if (key.EncryptionAlgorithm != Cryptography.SymmetricAlgorithmType.PlainText) { /* use this to add the Length: header to the main section * * MessageSection lengthSection = new MessageSection(); * * lengthSection.AddHeader(new Header(Header.RESOURCE_LENGTH, result.EncryptedBytes.Length.ToString())); * * lengthSection.AddBlankLine(); * * allBytes.AddRange(lengthSection.GetBytes()); * * */ allBytes.AddRange(result.EncryptedBytes); allBytes.AddRange(blankLineBytes); } else { allBytes.AddRange(result.EncryptedBytes); } // handle binary resources foreach (BinaryData data in allBinaryData) { if (data != null && data.Data != null) { // encrypt each resource, making sure to use the same IV EncryptionResult er = key.Encrypt(data.Data, ref result.IV); MessageSection section = new MessageSection(); section.AddBlankLine(); section.AddHeader(new Header(Header.RESOURCE_IDENTIFIER, data.ID)); section.AddHeader(new Header(Header.RESOURCE_LENGTH, er.EncryptedBytes.Length.ToString())); section.AddBlankLine(); allBytes.AddRange(section.GetBytes()); allBytes.AddRange(er.EncryptedBytes); allBytes.AddRange(blankLineBytes); } } allBytes.AddRange(blankLineBytes); return(allBytes.ToArray()); }