Welcome to mirror list, hosted at ThFree Co, Russian Federation.

protocol.cpp « tracking - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e17584fb5f3e555c8e97fbdb05bd53f226b3215 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "tracking/protocol.hpp"

#include "coding/endianness.hpp"
#include "coding/writer.hpp"

#include "base/assert.hpp"

#include "std/cstdint.hpp"
#include "std/sstream.hpp"
#include "std/utility.hpp"

namespace
{
template <typename Container>
vector<uint8_t> CreateDataPacketImpl(Container const & points,
                                     tracking::Protocol::PacketType const type)
{
  vector<uint8_t> buffer;
  MemWriter<decltype(buffer)> writer(buffer);

  uint32_t version = tracking::Protocol::Encoder::kLatestVersion;
  switch (type)
  {
  case tracking::Protocol::PacketType::DataV0: version = 0; break;
  case tracking::Protocol::PacketType::DataV1: version = 1; break;
  case tracking::Protocol::PacketType::AuthV0: ASSERT(false, ("Not a DATA packet.")); break;
  }

  tracking::Protocol::Encoder::SerializeDataPoints(version, writer, points);

  auto packet = tracking::Protocol::CreateHeader(type, static_cast<uint32_t>(buffer.size()));
  packet.insert(packet.end(), begin(buffer), end(buffer));

  return packet;
}
}  // namespace

namespace tracking
{
uint8_t const Protocol::kOk[4] = {'O', 'K', '\n', '\n'};
uint8_t const Protocol::kFail[4] = {'F', 'A', 'I', 'L'};

static_assert(sizeof(Protocol::kFail) >= sizeof(Protocol::kOk), "");

//  static
vector<uint8_t> Protocol::CreateHeader(PacketType type, uint32_t payloadSize)
{
  vector<uint8_t> header;
  InitHeader(header, type, payloadSize);
  return header;
}

//  static
vector<uint8_t> Protocol::CreateAuthPacket(string const & clientId)
{
  vector<uint8_t> packet;

  InitHeader(packet, PacketType::CurrentAuth, static_cast<uint32_t>(clientId.size()));
  packet.insert(packet.end(), begin(clientId), end(clientId));

  return packet;
}

//  static
vector<uint8_t> Protocol::CreateDataPacket(DataElementsCirc const & points, PacketType type)
{
  return CreateDataPacketImpl(points, type);
}

//  static
vector<uint8_t> Protocol::CreateDataPacket(DataElementsVec const & points, PacketType type)
{
  return CreateDataPacketImpl(points, type);
}

//  static
pair<Protocol::PacketType, size_t> Protocol::DecodeHeader(vector<uint8_t> const & data)
{
  ASSERT_GREATER_OR_EQUAL(data.size(), sizeof(uint32_t /* header */), ());

  uint32_t size = (*reinterpret_cast<uint32_t const *>(data.data())) & 0xFFFFFF00;
  if (!IsBigEndianMacroBased())
    size = ReverseByteOrder(size);

  return make_pair(PacketType(static_cast<uint8_t>(data[0])), size);
}

//  static
string Protocol::DecodeAuthPacket(Protocol::PacketType type, vector<uint8_t> const & data)
{
  switch (type)
  {
  case Protocol::PacketType::AuthV0: return string(begin(data), end(data));
  case Protocol::PacketType::DataV0:
  case Protocol::PacketType::DataV1: ASSERT(false, ("Not an AUTH packet.")); break;
  }
  return string();
}

//  static
Protocol::DataElementsVec Protocol::DecodeDataPacket(PacketType type, vector<uint8_t> const & data)
{
  DataElementsVec points;
  MemReader memReader(data.data(), data.size());
  ReaderSource<MemReader> src(memReader);
  switch (type)
  {
  case Protocol::PacketType::DataV0:
    Encoder::DeserializeDataPoints(0 /* version */, src, points);
    break;
  case Protocol::PacketType::DataV1:
    Encoder::DeserializeDataPoints(1 /* version */, src, points);
    break;
  case Protocol::PacketType::AuthV0: ASSERT(false, ("Not a DATA packet.")); break;
  }
  return points;
}

//  static
void Protocol::InitHeader(vector<uint8_t> & packet, PacketType type, uint32_t payloadSize)
{
  packet.resize(sizeof(uint32_t));
  uint32_t & size = *reinterpret_cast<uint32_t *>(packet.data());
  size = payloadSize;

  ASSERT_LESS(size, 0x00FFFFFF, ());

  if (!IsBigEndianMacroBased())
    size = ReverseByteOrder(size);

  packet[0] = static_cast<uint8_t>(type);
}

string DebugPrint(Protocol::PacketType type)
{
  switch (type)
  {
  case Protocol::PacketType::AuthV0: return "AuthV0";
  case Protocol::PacketType::DataV0: return "DataV0";
  case Protocol::PacketType::DataV1: return "DataV1";
  }
  stringstream ss;
  ss << "Unknown(" << static_cast<uint32_t>(type) << ")";
  return ss.str();
}
}  // namespace tracking