client-cpp  0.10.0
ConnectMessage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 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 #ifdef _WIN32
25 #include <Winsock2.h>
26 #else
27 #include <arpa/inet.h>
28 #endif
29 
30 namespace kaa {
31 
33 {
34 public:
35  template<class T, class U, class V>
36  ConnectMessage(std::uint16_t timer,
37  std::uint32_t nextProtocolId,
38  const T& signature,
39  const U& sessionKey,
40  const V& payload) : message_(0)
41  {
42  char header[6];
43  std::uint8_t size = KaaTcpCommon::createBasicHeader(
45  payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size(), header);
46 
47  message_.resize(payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size() + size);
48 
49  std::copy(reinterpret_cast<const std::uint8_t *>(header),
50  reinterpret_cast<const std::uint8_t *>(header + size),
51  message_.begin());
52 
53  auto messageIt = message_.begin() + size;
54 
55  std::uint16_t nameLengthNetworkOrder = htons(KaaTcpCommon::KAA_TCP_NAME_LENGTH);
56  std::copy(reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder), reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder) + 2, messageIt);
57  messageIt += sizeof(std::uint16_t);
58 
59  std::copy((const std::uint8_t * const ) KaaTcpCommon::KAA_TCP_NAME,
60  (const std::uint8_t * const ) (KaaTcpCommon::KAA_TCP_NAME + KaaTcpCommon::KAA_TCP_NAME_LENGTH), messageIt);
62 
63  *(messageIt++) = KaaTcpCommon::PROTOCOL_VERSION;
64  *(messageIt++) = 0x02;
65 
66  std::uint32_t nextProtocolIdNetworkOrder = htonl(nextProtocolId);
67  std::copy(reinterpret_cast<std::uint8_t *>(&nextProtocolIdNetworkOrder), reinterpret_cast<std::uint8_t *>(&nextProtocolIdNetworkOrder) + 4, messageIt);
68  messageIt += sizeof(std::uint32_t);
69 
70  *(messageIt++) = sessionKey.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SESSION_KEY_FLAGS : 0;
71  *(messageIt++) = signature.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SIGNATURE_FLAGS : 0;
72 
73  std::uint16_t timerNetworkOrder = htons(timer);
74  std::copy(reinterpret_cast<std::uint8_t *>(&timerNetworkOrder), reinterpret_cast<std::uint8_t *>(&timerNetworkOrder) + 2, messageIt);
75  messageIt += sizeof(std::uint16_t);
76 
77  std::copy(sessionKey.begin(), sessionKey.end(), messageIt);
78  messageIt += sessionKey.size();
79 
80  std::copy(signature.begin(), signature.end(), messageIt);
81  messageIt += signature.size();
82 
83  std::copy(payload.begin(), payload.end(), messageIt);
84  }
86 
87  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
88 
89 private:
90  std::vector<std::uint8_t> message_;
91 };
92 
93 }
94 
95 
96 
97 #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)