diff options
author | Sergey Yershov <syershov@maps.me> | 2016-10-31 17:02:56 +0300 |
---|---|---|
committer | Sergey Yershov <syershov@maps.me> | 2016-11-01 15:30:27 +0300 |
commit | 35e1a79cc5e69457a78113baafc4ae0f2f102848 (patch) | |
tree | 6746d29262541810b7087a5063fe4f6b4453271a /tracking/protocol.cpp | |
parent | ba32a88ce2041928d61702fc38bc00c4a1a0d823 (diff) |
Bindings to Python for tracking protocol
Diffstat (limited to 'tracking/protocol.cpp')
-rw-r--r-- | tracking/protocol.cpp | 98 |
1 files changed, 86 insertions, 12 deletions
diff --git a/tracking/protocol.cpp b/tracking/protocol.cpp index ada9a49842..aea3402203 100644 --- a/tracking/protocol.cpp +++ b/tracking/protocol.cpp @@ -1,10 +1,30 @@ #include "tracking/protocol.hpp" #include "coding/endianness.hpp" +#include "coding/writer.hpp" #include "base/assert.hpp" #include "std/cstdint.hpp" +#include "std/sstream.hpp" + +namespace +{ +template <typename Container> +vector<uint8_t> CreateDataPacketImpl(Container const & points) +{ + vector<uint8_t> buffer; + MemWriter<decltype(buffer)> writer(buffer); + tracking::Protocol::Encoder::SerializeDataPoints(tracking::Protocol::Encoder::kLatestVersion, + writer, points); + + auto packet = tracking::Protocol::CreateHeader(tracking::Protocol::PacketType::CurrentData, + static_cast<uint32_t>(buffer.size())); + packet.insert(packet.end(), begin(buffer), end(buffer)); + + return packet; +} +} // namespace namespace tracking { @@ -13,6 +33,15 @@ 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; @@ -23,19 +52,62 @@ vector<uint8_t> Protocol::CreateAuthPacket(string const & clientId) return packet; } -vector<uint8_t> Protocol::CreateDataPacket(DataElements const & points) +// static +vector<uint8_t> Protocol::CreateDataPacket(DataElementsCirc const & points) { - vector<uint8_t> buffer; - MemWriter<decltype(buffer)> writer(buffer); - Encoder::SerializeDataPoints(Encoder::kLatestVersion, writer, points); + return CreateDataPacketImpl(points); +} - vector<uint8_t> packet; - InitHeader(packet, PacketType::CurrentData, static_cast<uint32_t>(buffer.size())); - packet.insert(packet.end(), begin(buffer), end(buffer)); +// static +vector<uint8_t> Protocol::CreateDataPacket(DataElementsVec const & points) +{ + return CreateDataPacketImpl(points); +} - return packet; +// 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 (!IsBigEndian()) + 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) +{ + ASSERT_GREATER_OR_EQUAL(data.size(), sizeof(uint32_t /* header */), ()); + switch (type) + { + case Protocol::PacketType::AuthV0: + return string(begin(data) + sizeof(uint32_t /* header */), end(data)); + case Protocol::PacketType::DataV0: break; + } + return string(); +} + +// static +Protocol::DataElementsVec Protocol::DecodeDataPacket(PacketType type, vector<uint8_t> const & data) +{ + ASSERT_GREATER_OR_EQUAL(data.size(), sizeof(uint32_t /* header */), ()); + DataElementsVec points; + MemReader memReader(data.data(), data.size()); + ReaderSource<MemReader> src(memReader); + src.Skip(sizeof(uint32_t /* header */)); + switch (type) + { + case Protocol::PacketType::DataV0: + Encoder::DeserializeDataPoints(Encoder::kLatestVersion, src, points); + break; + case Protocol::PacketType::AuthV0: break; + } + return points; +} + +// static void Protocol::InitHeader(vector<uint8_t> & packet, PacketType type, uint32_t payloadSize) { packet.resize(sizeof(uint32_t)); @@ -46,7 +118,7 @@ void Protocol::InitHeader(vector<uint8_t> & packet, PacketType type, uint32_t pa if (!IsBigEndian()) size = ReverseByteOrder(size); - + packet[0] = static_cast<uint8_t>(type); } @@ -54,9 +126,11 @@ string DebugPrint(Protocol::PacketType type) { switch (type) { - case Protocol::PacketType::AuthV0: return "AuthV0"; - case Protocol::PacketType::DataV0: return "DataV0"; - default: return "Unknown"; + case Protocol::PacketType::AuthV0: return "AuthV0"; + case Protocol::PacketType::DataV0: return "DataV0"; } + stringstream ss; + ss << "Unknown(" << static_cast<uint32_t>(type) << ")"; + return ss.str(); } } // namespace tracking |