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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#pragma once
#include <cstdint>
#include <functional>
#include <map>
#include <span>
#include <vector>
#include <netinet/in.h>
#include "net/packet.hpp"
#include "util/io.hpp"
namespace net {
class conn {
io::serialized_io<io::file_io> rw;
sockaddr_in conn_addr;
socklen_t conn_addr_size;
using handler = std::function<bool(std::span<uint8_t>)>;
using handler_list = std::vector<handler>;
std::array<handler_list, 256> handlers;
std::map<uint16_t, handler_list> netmodule_handlers;
private:
packet::packet_header read_header()
{
uint16_t packet_size;
uint8_t id;
rw.read(packet_size);
rw.read(id);
return { id, packet_size - 3 };
}
public:
conn(int sock_fd, sockaddr_in conn_addr, socklen_t conn_addr_size)
: rw(io::file_io(sock_fd))
, conn_addr(conn_addr)
, conn_addr_size(conn_addr_size)
{
}
template <packet::packet T>
void expect_packet(T& t)
{
auto [id, len] = read_header();
assert(id == T::packet_id);
if (len > 0) {
std::vector<uint8_t> payload(len);
rw.read(payload);
decode_packet(payload, t);
}
}
template <class H>
void reg_handler(int id, H h)
{
handlers[id].push_back(h);
}
template <packet::packet P, typename H>
void reg_handler(H h)
{
handlers[P::packet_id].push_back([this, h](std::span<uint8_t> payload) {
P t;
if (payload.size() > 0) {
packet::decode_packet(payload, t);
}
return h(t);
});
}
template <packet::netmodule P, typename H>
void reg_handler(H h)
{
netmodule_handlers[P::module_id].push_back([this, h](std::span<uint8_t> payload) {
P t;
if (payload.size() > 0) {
packet::decode_packet(payload, t);
}
return h(t);
});
}
void send_packet(uint8_t id, std::span<uint8_t> payload)
{
std::vector<uint8_t> packet_data;
auto buffer = io::serialized_io(io::buffered_io(packet_data));
buffer.write(int16_t(payload.size() + 3));
buffer.write(id);
if (payload.size() > 0) {
buffer.write(payload);
}
rw.write(packet_data);
}
template <packet::packet T>
void send_packet(T p)
{
auto payload = encode_packet(p);
send_packet(T::packet_id, payload);
}
bool handle_netmodule(std::span<uint8_t> payload)
{
uint16_t id;
std::memcpy(&id, payload.data(), sizeof(id));
id = to_little(id);
if (netmodule_handlers[id].size() <= 0) {
// currently i won't bother implementing netmodules
// FIXME: return false
return true;
}
for (auto& h : handlers[id]) {
if (h(payload.subspan(2)) == true) {
return false;
}
}
return true;
}
bool handle()
{
try {
auto [id, len] = read_header();
if (id == 0) {
return false;
}
std::vector<uint8_t> payload(len);
rw.read(payload);
// got netmodule
if (id == 82) {
return handle_netmodule(payload);
}
if (handlers[id].size() <= 0) {
return false;
}
for (auto& h : handlers[id]) {
if (h(payload) == true) {
break;
}
}
return true;
} catch (io::file_io::eof e) {
return false;
}
}
};
}
|