client-cpp  0.0.1-SNAPSHOT
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(boost::uint16_t timer,
33  const T& signature,
34  const U& sessionKey,
35  const V& payload) : message_(0)
36  {
37  char header[6];
38  boost::uint8_t size = KaaTcpCommon::createBasicHeader(
39  (boost::uint8_t) KaaTcpMessageType::MESSAGE_CONNECT,
40  payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size(), header);
41 
42  message_.resize(payload.size() + KaaTcpCommon::KAA_CONNECT_HEADER_LENGTH + sessionKey.size() + signature.size() + size);
43 
44  std::copy(reinterpret_cast<const boost::uint8_t *>(header),
45  reinterpret_cast<const boost::uint8_t *>(header + size),
46  message_.begin());
47 
48  auto messageIt = message_.begin() + size;
49 
50  boost::uint16_t nameLengthNetworkOrder = htons(KaaTcpCommon::KAA_TCP_NAME_LENGTH);
51  std::copy(reinterpret_cast<boost::uint8_t *>(&nameLengthNetworkOrder), reinterpret_cast<boost::uint8_t *>(&nameLengthNetworkOrder) + 2, messageIt);
52  messageIt += 2;
53 
54  std::copy((const boost::uint8_t * const ) KaaTcpCommon::KAA_TCP_NAME,
55  (const boost::uint8_t * const ) (KaaTcpCommon::KAA_TCP_NAME + KaaTcpCommon::KAA_TCP_NAME_LENGTH), messageIt);
57 
58  *(messageIt++) = KaaTcpCommon::PROTOCOL_VERSION;
59  *(messageIt++) = 0x02;
60  *(messageIt++) = sessionKey.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SESSION_KEY_FLAGS : 0;
61  *(messageIt++) = signature.size() > 0 ? KaaTcpCommon::KAA_CONNECT_SIGNATURE_FLAGS : 0;
62 
63  boost::uint16_t timerNetworkOrder = htons(timer);
64  std::copy(reinterpret_cast<boost::uint8_t *>(&timerNetworkOrder), reinterpret_cast<boost::uint8_t *>(&timerNetworkOrder) + 2, messageIt);
65  messageIt += 2;
66 
67  std::copy(sessionKey.begin(), sessionKey.end(), messageIt);
68  messageIt += sessionKey.size();
69 
70  std::copy(signature.begin(), signature.end(), messageIt);
71  messageIt += signature.size();
72 
73  std::copy(payload.begin(), payload.end(), messageIt);
74  }
76 
77  const std::vector<boost::uint8_t>& getRawMessage() const { return message_; }
78 
79 private:
80  std::vector<boost::uint8_t> message_;
81 };
82 
83 }
84 
85 
86 
87 #endif /* CONNECTMESSAGE_HPP_ */
static boost::uint8_t createBasicHeader(boost::uint8_t messageType, boost::uint32_t length, char *message)
static const boost::uint8_t PROTOCOL_VERSION
static const boost::uint16_t KAA_TCP_NAME_LENGTH
static const boost::uint8_t KAA_CONNECT_HEADER_LENGTH
ConnectMessage(boost::uint16_t timer, const T &signature, const U &sessionKey, const V &payload)
static const char *const KAA_TCP_NAME
const std::vector< boost::uint8_t > & getRawMessage() const
static const boost::uint8_t KAA_CONNECT_SESSION_KEY_FLAGS
static const boost::uint8_t KAA_CONNECT_SIGNATURE_FLAGS