summaryrefslogtreecommitdiff
path: root/include/net/conn.hpp
blob: 6569973655390f77f907e54d8fbfd12e041dac27 (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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once

#include <cstdint>
#include <functional>
#include <map>
#include <netinet/in.h>
#include <span>
#include <vector>

#include "net/packet.hpp"

namespace net {
class conn {
	int sock_fd;

	sockaddr_in conn_addr;
	socklen_t conn_addr_size;

	using handler_list = std::vector<std::function<bool(std::span<uint8_t>)>>;
	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;

		recv(packet_size);
		recv(id);

		return { id, packet_size - 3 };
	}

public:
	conn(int sock_fd, sockaddr_in conn_addr, socklen_t conn_addr_size)
	    : sock_fd(sock_fd)
	    , conn_addr(conn_addr)
	    , conn_addr_size(conn_addr_size)
	{
	}

	~conn()
	{
		close(sock_fd);
	}

	ssize_t send(std::span<uint8_t> data)
	{
		auto bytes = ::send(sock_fd, data.data(), data.size_bytes(), 0);
		if (bytes < 0) {
			throw std::runtime_error(strerror(errno));
		}
		return bytes;
	}

	template <std::integral T>
	ssize_t send(T t)
	{
		t = to_little(t);
		auto bytes = ::send(sock_fd, &t, sizeof(T), 0);
		if (bytes < 0) {
			throw std::runtime_error(strerror(errno));
		}
		return bytes;
	}

	ssize_t recv(std::span<uint8_t> data)
	{
		if (data.size_bytes() == 0) {
			return 0;
		}

		auto bytes = ::recv(sock_fd, data.data(), data.size_bytes(), 0);
		if (bytes <= 0) {
			throw std::runtime_error(strerror(errno));
		}
		return bytes;
	}

	template <std::integral T>
	ssize_t recv(T& t)
	{
		auto bytes = ::recv(sock_fd, &t, sizeof(T), 0);
		if (bytes <= 0) {
			throw std::runtime_error(strerror(errno));
		}
		t = to_little(t);
		if (bytes != sizeof(T)) {
			throw std::runtime_error(strerror(errno));
		}
		return bytes;
	}

	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);
			recv(payload);
			decode_packet(payload, t);
		}
	}

	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);
		});
	}

	template <packet::packet T>
	void send_packet(T p)
	{
		auto payload = encode_packet(p);

		send(int16_t(payload.size() + 3));
		send(T::packet_id);
		if (payload.size() > 0) {
			send(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()
	{
		auto [id, len] = read_header();
		if (id == 0) {
			return false;
		}

		std::vector<uint8_t> payload(len);
		recv(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;
	}
};
}