-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxybase.hpp
More file actions
90 lines (78 loc) · 2.69 KB
/
proxybase.hpp
File metadata and controls
90 lines (78 loc) · 2.69 KB
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
#pragma once
#include <string>
#include <span>
#include <atomic>
#include <thread>
#include <memory>
#include <vector>
#include "logging.hpp"
#include "types.hpp"
#define CROW_ENABLE_COMPRESSION 1
#include "crow_all.h"
class ProxyBase : public Loggable {
public:
ProxyBase(unsigned short port);
virtual ~ProxyBase();
void run(); // blocks
void stop();
virtual std::string udp_packet_to_json(std::span<const char> data) = 0;
unsigned short get_proxy_port() const { return port; }
// Non-copyable, non-movable
ProxyBase(const ProxyBase&) = delete;
ProxyBase& operator=(const ProxyBase&) = delete;
ProxyBase(ProxyBase&&) = delete;
ProxyBase& operator=(ProxyBase&&) = delete;
protected:
template<typename T>
bool get_panel_state(std::span<const char> data, T& panel_state)
{
// Check minimum size for header
if (data.size() < sizeof(panel_packet_header))
{
log_error("Packet too small for header: %zu bytes (need %zu)",
data.size(), sizeof(panel_packet_header));
return false;
}
// Parse the header
panel_packet_header header;
memcpy(&header, data.data(), sizeof(panel_packet_header));
// Check if we have enough data for the complete packet
size_t expected_total_size = sizeof(panel_packet_header) + header.pp_byte_count;
if (data.size() < expected_total_size)
{
log_error("Packet too small: %zu bytes (need %zu total, header=%zu + payload=%u)",
data.size(), expected_total_size, sizeof(panel_packet_header), header.pp_byte_count);
return false;
}
// Check if the payload size matches the PDP panel state size
if (header.pp_byte_count != sizeof(T))
{
log_error("Invalid payload size: %u bytes (expected %zu for panel state)",
header.pp_byte_count, sizeof(T));
return false;
}
memcpy(&panel_state, data.data() + sizeof(panel_packet_header), sizeof(T));
return true;
}
template<typename T, size_t N = (sizeof(T) * 2)>
std::string to_hex(T value)
{
static const char* digits = "0123456789ABCDEF";
std::string result(N, '0');
for (size_t i = N - 1; i >= 0 && value; --i)
{
result[i] = digits[value & 0xF];
value >>= 4;
}
return result;
}
unsigned short port;
private:
void udp_loop();
std::thread udp_thread;
std::atomic<bool> stop_requested{false};
crow::SimpleApp ws_server;
std::future<void> server_future;
std::set<crow::websocket::connection*> ws_clients;
std::mutex ws_clients_mutex;
};