/
packet_recorder.cpp
47 lines (40 loc) · 1.28 KB
/
packet_recorder.cpp
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
#include <framework/global.h>
#include <framework/core/clock.h>
#include <framework/core/resourcemanager.h>
#include "packet_recorder.h"
PacketRecorder::PacketRecorder(const std::string& file)
{
m_start = g_clock.millis();
#ifdef ANDROID
g_resources.makeDir("records");
m_stream = std::ofstream(std::string("records/") + file);
#else
std::error_code ec;
std::filesystem::create_directory("records", ec);
m_stream = std::ofstream(std::filesystem::path("records") / file);
#endif
}
PacketRecorder::~PacketRecorder()
{
}
void PacketRecorder::addInputPacket(const InputMessagePtr& packet)
{
m_stream << "< " << (g_clock.millis() - m_start) << " ";
for (auto& buffer : packet->getBodyBuffer()) {
m_stream << std::setfill('0') << std::setw(2) << std::hex << (uint16_t)(uint8_t)buffer;
}
m_stream << std::dec << "\n";
}
void PacketRecorder::addOutputPacket(const OutputMessagePtr& packet)
{
if (m_firstOutput) {
// skip packet with login and password
m_firstOutput = false;
return;
}
m_stream << "> " << (g_clock.millis() - m_start) << " ";
for (auto& buffer : packet->getBuffer()) {
m_stream << std::setfill('0') << std::setw(2) << std::hex << (uint16_t)(uint8_t)buffer;
}
m_stream << std::dec << "\n";
}