blob: bc242f3d3bbe497791720516d05a45c520b28f92 (
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
|
#include "tracking/connection.hpp"
#include "tracking/protocol.hpp"
#include "platform/platform.hpp"
#include "platform/socket.hpp"
namespace
{
uint32_t constexpr kSocketTimeoutMs = 10000;
} // namespace
namespace tracking
{
Connection::Connection(unique_ptr<platform::Socket> socket, string const & host, uint16_t port,
bool isHistorical)
: m_socket(move(socket)), m_host(host), m_port(port)
{
if (!m_socket)
return;
m_socket->SetTimeout(kSocketTimeoutMs);
}
// TODO: implement handshake
bool Connection::Reconnect()
{
if (!m_socket)
return false;
m_socket->Close();
if (!m_socket->Open(m_host, m_port))
return false;
auto packet = Protocol::CreateAuthPacket(GetPlatform().UniqueClientId());
if (!m_socket->Write(packet.data(), static_cast<uint32_t>(packet.size())))
return false;
std::string check(std::begin(Protocol::kFail), std::end(Protocol::kFail));
bool const isSuccess =
m_socket->Read(reinterpret_cast<uint8_t *>(&check[0]), static_cast<uint32_t>(check.size()));
if (!isSuccess || check != std::string(std::begin(Protocol::kOk), std::end(Protocol::kOk)))
return false;
return true;
}
void Connection::Shutdown()
{
if (!m_socket)
return;
m_socket->Close();
}
// TODO: implement historical
bool Connection::Send(boost::circular_buffer<DataPoint> const & points)
{
if (!m_socket)
return false;
auto packet = Protocol::CreateDataPacket(points, tracking::Protocol::PacketType::CurrentData);
return m_socket->Write(packet.data(), static_cast<uint32_t>(packet.size()));
}
} // namespace tracking
|