client-cpp  0.6.3
ConnectMessage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 CyberVision, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CONNECTMESSAGE_HPP_
18 #define CONNECTMESSAGE_HPP_
19 
23 #include <botan/botan.h>
24 #include <arpa/inet.h>
25 
26 namespace kaa {
27 
29 {
30 public:
31  template<class T, class U, class V>
32  ConnectMessage(std::uint16_t timer,
33  std::uint32_t nextProtocolId,
34  const T& signature,
35  const U& sessionKey,
36  const V& payload) : message_(0)
37  {
38  char header[6];
39  std::uint8_t size = KaaTcpCommon::createBasicHeader(
41  payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size(), header);
42 
43  message_.resize(payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size() + size);
44 
45  std::copy(reinterpret_cast<const std::uint8_t *>(header),
46  reinterpret_cast<const std::uint8_t *>(header + size),
47  message_.begin());
48 
49  auto messageIt = message_.begin() + size;
50 
51  std::uint16_t nameLengthNetworkOrder = htons(KaaTcpCommon::KAA_TCP_NAME_LENGTH);
52  std::copy(reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder), reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder) + 2, messageIt);
53  messageIt += sizeof(std::uint16_t);
54 
55  std::copy((const std::uint8_t * const ) KaaTcpCommon::KAA_TCP_NAME,
56  (const std::uint8_t * const ) (KaaTcpCommon::KAA_TCP_NAME + KaaTcpCommon::KAA_TCP_NAME_LENGTH), messageIt);
58 
59  *(messageIt++) = KaaTcpCommon::PROTOCOL_VERSION;
60  *(messageIt++) = 0x02;
61 
62  std::uint32_t nextProtocolIdNetworkOrder = htonl(nextProtocolId);
63  std::copy(reinterpret_cast<std::uint8_t *>(&nextProtocolIdNetworkOrder), reinterpret_cast<std::uint8_t *>(&nextProtocolIdNetworkOrder) + 4, messageIt);
64  messageIt += sizeof(std::uint32_t);
65 
66  *(messageIt++) = sessionKey.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SESSION_KEY_FLAGS : 0;
67  *(messageIt++) = signature.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SIGNATURE_FLAGS : 0;
68 
69  std::uint16_t timerNetworkOrder = htons(timer);
70  std::copy(reinterpret_cast<std::uint8_t *>(&timerNetworkOrder), reinterpret_cast<std::uint8_t *>(&timerNetworkOrder) + 2, messageIt);
71  messageIt += sizeof(std::uint16_t);
72 
73  std::copy(sessionKey.begin(), sessionKey.end(), messageIt);
74  messageIt += sessionKey.size();
75 
76  std::copy(signature.begin(), signature.end(), messageIt);
77  messageIt += signature.size();
78 
79  std::copy(payload.begin(), payload.end(), messageIt);
80  }
82 
83  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
84 
85 private:
86  std::vector<std::uint8_t> message_;
87 };
88 
89 }
90 
91 
92 
93 #endif /* CONNECTMESSAGE_HPP_ */
static const std::uint8_t KAA_CONNECT_SESSION_KEY_FLAGS
static const std::uint8_t KAA_CONNECT_HEADER_LENGTH
const std::vector< std::uint8_t > & getRawMessage() const
static const std::uint8_t KAA_CONNECT_SIGNATURE_FLAGS
ConnectMessage(std::uint16_t timer, std::uint32_t nextProtocolId, const T &signature, const U &sessionKey, const V &payload)
static const std::uint8_t PROTOCOL_VERSION
static const std::uint16_t KAA_TCP_NAME_LENGTH
static const char *const KAA_TCP_NAME
static std::uint8_t createBasicHeader(std::uint8_t messageType, std::uint32_t length, char *message)