client-cpp  0.10.0
KaaSyncRequest.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 KAASYNCREQUEST_HPP_
18 #define KAASYNCREQUEST_HPP_
19 
22 #ifdef _WIN32
23 #include <Winsock2.h>
24 #else
25 #include <arpa/inet.h>
26 #endif
27 
28 
29 namespace kaa {
30 
31 enum class KaaSyncMessageType : std::uint8_t {
32  UNUSED = 0x0,
33  SYNC = 0x1,
34  BOOTSTRAP = 0x2
35 };
36 
38 {
39 public:
40  template<class T>
41  KaaSyncRequest(bool zipped, bool encrypted, std::uint16_t messageId, const T& payload, KaaSyncMessageType messageType) : message_(0)
42  {
43  char header[6];
44  std::uint8_t size = KaaTcpCommon::createBasicHeader(
46  payload.size() + KaaTcpCommon::KAA_SYNC_HEADER_LENGTH, header);
47 
48  message_.resize(payload.size() + KaaTcpCommon::KAA_SYNC_HEADER_LENGTH + size);
49 
50  std::copy(reinterpret_cast<const std::uint8_t *>(header),
51  reinterpret_cast<const std::uint8_t *>(header + size),
52  message_.begin());
53 
54  auto messageIt = message_.begin() + size;
55 
56  std::uint16_t nameLengthNetworkOrder = htons(KaaTcpCommon::KAA_TCP_NAME_LENGTH);
57  std::copy(reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder), reinterpret_cast<std::uint8_t *>(&nameLengthNetworkOrder) + 2, messageIt);
58  messageIt += 2;
59 
60  std::copy((const std::uint8_t * const ) KaaTcpCommon::KAA_TCP_NAME,
61  (const std::uint8_t * const ) (KaaTcpCommon::KAA_TCP_NAME + KaaTcpCommon::KAA_TCP_NAME_LENGTH), messageIt);
63 
64  *(messageIt++) = KaaTcpCommon::PROTOCOL_VERSION;
65 
66  std::uint16_t messageIdNetworkOrder = htons(messageId);
67  std::copy(reinterpret_cast<std::uint8_t *>(&messageIdNetworkOrder), reinterpret_cast<std::uint8_t *>(&messageIdNetworkOrder) + 2, messageIt);
68  messageIt += 2;
69 
70  *messageIt |= (((std::uint8_t)messageType) << 4);
71 
73  if (zipped) {
75  }
76  if (encrypted) {
78  }
79  ++messageIt;
80 
81  std::copy(payload.begin(), payload.end(), messageIt);
82  }
83 
85 
86  const std::vector<std::uint8_t>& getRawMessage() const { return message_; }
87 
88 private:
89  std::vector<std::uint8_t> message_;
90 
91  static const std::uint8_t KAA_SYNC_MESSAGE_TYPE_SYNC = 0x01;
92  static const std::uint8_t KAA_SYNC_MESSAGE_TYPE_BOOTSTRAP = 0x02;
93 };
94 
95 }
96 
97 
98 #endif /* KAASYNCREQUEST_HPP_ */
static const std::uint8_t KAA_SYNC_ZIPPED_BIT
KaaSyncRequest(bool zipped, bool encrypted, std::uint16_t messageId, const T &payload, KaaSyncMessageType messageType)
const std::vector< std::uint8_t > & getRawMessage() const
static const std::uint8_t KAA_SYNC_REQUEST_BIT
static const std::uint8_t KAA_SYNC_HEADER_LENGTH
static const std::uint8_t KAA_SYNC_ENCRYPTED_BIT
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)
KaaSyncMessageType