GCC Code Coverage Report


Directory: src/gate/
File: src/gate/interfaces/socop_impl.hpp
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 150 173 86.7%
Functions: 44 52 84.6%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef SOCOP_IMPL_HPP_INCLUDED
2 #define SOCOP_IMPL_HPP_INCLUDED
3
4 #include "socop.hpp"
5 #include <string.h>
6
7
8
9 100 Socop::Result Socop::translate_result(int result)
10 {
11 100 return Result(result);
12 }
13
14 1325 int Socop::translate_result(Socop::Result result)
15 {
16 1325 return static_cast<int>(result);
17 }
18
19
20
21 ✗ SocopDriver::SocopDriver(Bytereader reader_function, void* reader_param,
22 ✗ Bytewriter writer_function, void* writer_param)
23 {
24 ✗ this->read_byte = reader_function;
25 ✗ this->read_param = reader_param;
26 ✗ this->write_byte = writer_function;
27 ✗ this->write_param = writer_param;
28 ✗ }
29
30 3 SocopDriver::SocopDriver()
31 {
32 3 this->read_byte = &SocopDriver::do_read_dispatcher;
33 3 this->read_param = this;
34 3 this->write_byte = &SocopDriver::do_write_dispatcher;
35 3 this->write_param = this;
36 3 }
37
38 6 SocopDriver::~SocopDriver()
39 {
40 6 }
41
42 ✗ Socop::Result SocopDriver::do_read(unsigned char& received_byte, bool want_more)
43 {
44 // Method needs to be overwritten by derived implementation
45 ✗ return Socop::Error_NotImpl;
46 }
47 ✗ Socop::Result SocopDriver::do_write(unsigned char byte_to_send, bool send_more)
48 {
49 // Method needs to be overwritten by derived implementation
50 ✗ return Socop::Error_NotImpl;
51 }
52
53 650 int SocopDriver::do_read_dispatcher(unsigned char* num, unsigned want_more, void* user_param)
54 {
55 650 SocopDriver* self = static_cast<SocopDriver*>(user_param);
56 650 Socop::Result result = self->do_read(*num, want_more ? true : false);
57 650 return Socop::translate_result(result);
58 }
59 650 int SocopDriver::do_write_dispatcher(unsigned char num, unsigned send_more, void* user_param)
60 {
61 650 SocopDriver* self = static_cast<SocopDriver*>(user_param);
62 650 Socop::Result result = self->do_write(num, send_more ? true : false);
63 650 return Socop::translate_result(result);
64 }
65
66
67 class SocopClientDispatcher
68 {
69 public:
70 50 static void progress_callback(socop_client_t* session, int message_type, int result_code)
71 {
72 50 SocopClient* client = static_cast<SocopClient*>(session->client_param);
73 50 client->onProgress(message_type, Socop::translate_result(result_code));
74 50 }
75 };
76
77 3 SocopClient::SocopClient()
78 {
79 3 memset(&this->impl, 0, sizeof(this->impl));
80 3 }
81
82 3 void SocopClient::init(SocopDriver& driver)
83 {
84 3 socop_client_init(&this->impl, driver.read_byte, driver.read_param, driver.write_byte, driver.write_param);
85 3 this->impl.client_param = this;
86 3 this->impl.callback = &SocopClientDispatcher::progress_callback;
87 3 }
88
89 ✗ SocopClient::SocopClient(SocopDriver& driver)
90 {
91 ✗ this->init(driver);
92 ✗ }
93 6 SocopClient::~SocopClient()
94 {
95 6 }
96
97 3 SocopClient::Result SocopClient::reset()
98 {
99 3 return translate_result(socop_client_reset(&this->impl));
100 }
101 3 SocopClient::Result SocopClient::net_status(unsigned& net_status)
102 {
103 3 return translate_result(socop_client_netstatus(&this->impl, &net_status));
104 }
105 3 SocopClient::Result SocopClient::net_start(char const* parameters, unsigned parameters_length)
106 {
107 3 return translate_result(socop_client_netstart(&this->impl, parameters, parameters_length));
108 }
109 3 SocopClient::Result SocopClient::net_stop()
110 {
111 3 return translate_result(socop_client_netstop(&this->impl));
112 }
113
114 1 SocopClient::Result SocopClient::connect(Address const& addr, Socket& new_tcp_socket)
115 {
116 1 return translate_result(socop_client_connect(&this->impl, &addr, &new_tcp_socket));
117 }
118 1 SocopClient::Result SocopClient::bind(Address const& addr, Socket& new_udp_socket)
119 {
120 1 return translate_result(socop_client_bind(&this->impl, &addr, &new_udp_socket));
121 }
122 1 SocopClient::Result SocopClient::listen(Address const& addr, Socket& new_listen_socket)
123 {
124 1 return translate_result(socop_client_listen(&this->impl, &addr, &new_listen_socket));
125 }
126 1 SocopClient::Result SocopClient::accept(Socket listen_socket, Socket& new_connection_socket)
127 {
128 1 return translate_result(socop_client_accept(&this->impl, listen_socket, &new_connection_socket));
129 }
130 3 SocopClient::Result SocopClient::close(Socket sock)
131 {
132 3 return translate_result(socop_client_close(&this->impl, sock));
133 }
134
135 1 SocopClient::Result SocopClient::receive(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used)
136 {
137 1 return translate_result(socop_client_receive(&this->impl, sock, buffer, buffer_length, &buffer_used));
138 }
139 1 SocopClient::Result SocopClient::receive_from(Socket sock, void* buffer, unsigned buffer_length, unsigned& buffer_used, Address& addr)
140 {
141 1 return translate_result(socop_client_recv_from(&this->impl, sock, buffer, buffer_length, &buffer_used, &addr));
142 }
143 1 SocopClient::Result SocopClient::send(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent)
144 {
145 1 return translate_result(socop_client_send(&this->impl, sock, buffer, buffer_length, &buffer_sent));
146 }
147 1 SocopClient::Result SocopClient::send_to(Socket sock, void const* buffer, unsigned buffer_length, unsigned& buffer_sent, Address const& addr)
148 {
149 1 return translate_result(socop_client_sendto(&this->impl, sock, buffer, buffer_length, &buffer_sent, &addr));
150 }
151
152 1 SocopClient::Result SocopClient::shutdown(Socket sock, unsigned flags)
153 {
154 1 return translate_result(socop_client_shutdown(&this->impl, sock, flags));
155 }
156 1 SocopClient::Result SocopClient::select(Socket sock, unsigned& flags, unsigned timeout_ms)
157 {
158 1 return translate_result(socop_client_select(&this->impl, sock, &flags, timeout_ms));
159 }
160 ✗ SocopClient::Result SocopClient::peer(Socket sock, Address& addr)
161 {
162 ✗ return translate_result(socop_client_peer(&this->impl, sock, &addr));
163 }
164 ✗ SocopClient::Result SocopClient::iocontrol(Socket sock, unsigned option, void* buffer, unsigned buffer_len)
165 {
166 ✗ return translate_result(socop_client_ioctl(&this->impl, sock, option, buffer, buffer_len));
167 }
168 ✗ void SocopClient::onProgress(int message_type, Socop::Result result)
169 {
170 // do nothing
171 ✗ }
172
173
174
175 class SocopServerDispatcher : public Socop
176 {
177 public:
178 3 static int on_reset(socop_server_t* svr, void* param, unsigned param_len)
179 {
180 3 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
181 3 Socop::Result result = obj->on_reset();
182 3 return Socop::translate_result(result);
183 }
184
185 3 static int on_netstatus(socop_server_t* svr, void* param, unsigned param_len)
186 {
187 3 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
188 3 Socop::Result result = obj->on_net_status();
189 3 return Socop::translate_result(result);
190 }
191 3 static int on_netstart(socop_server_t* svr, void* param, unsigned param_len)
192 {
193 3 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
194 3 Socop::Result result = obj->on_net_start(static_cast<char const*>(param), param_len);
195 3 return Socop::translate_result(result);
196 }
197
198 3 static int on_netstop(socop_server_t* svr, void* param, unsigned param_len)
199 {
200 3 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
201 3 Socop::Result result = obj->on_net_stop();
202 3 return Socop::translate_result(result);
203 }
204
205
206 1 static int on_connect(socop_server_t* svr, socop_addr_t const* addr, socop_socket_t* new_sock)
207 {
208 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
209 1 Socop::Result result = obj->on_connect(*addr, *new_sock);
210 1 return Socop::translate_result(result);
211 }
212
213 1 static int on_bind(socop_server_t* svr, socop_addr_t const* addr, socop_socket_t* new_sock)
214 {
215 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
216 1 Socop::Result result = obj->on_bind(*addr, *new_sock);
217 1 return Socop::translate_result(result);
218 }
219
220 1 static int on_listen(socop_server_t* svr, socop_addr_t const* addr, socop_socket_t* new_sock)
221 {
222 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
223 1 Socop::Result result = obj->on_listen(*addr, *new_sock);
224 1 return Socop::translate_result(result);
225 }
226
227 1 static int on_accept(socop_server_t* svr, socop_socket_t listen_sock, socop_socket_t* new_sock)
228 {
229 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
230 1 Socop::Result result = obj->on_accept(listen_sock, *new_sock);
231 1 return Socop::translate_result(result);
232 }
233
234 3 static int on_close(socop_server_t* svr, socop_socket_t sock, unsigned param)
235 {
236 3 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
237 3 Socop::Result result = obj->on_close(sock);
238 3 return Socop::translate_result(result);
239 }
240
241
242 1 static int on_recv(socop_server_t* svr, socop_socket_t sock, void* buffer, unsigned buffer_capacity, unsigned* buffer_used)
243 {
244 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
245 1 Socop::Result result = obj->on_receive(sock, buffer, buffer_capacity, *buffer_used);
246 1 return Socop::translate_result(result);
247 }
248
249 1 static int on_recvfrom(socop_server_t* svr, socop_socket_t sock, void* buffer, unsigned buffer_capacity, unsigned* buffer_used, socop_addr_t* addr)
250 {
251 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
252 1 Socop::Result result = obj->on_receive_from(sock, buffer, buffer_capacity, *buffer_used, *addr);
253 1 return Socop::translate_result(result);
254 }
255
256 1 static int on_send(socop_server_t* svr, socop_socket_t sock, void* buffer, unsigned buffer_length, unsigned* buffer_sent)
257 {
258 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
259 1 Socop::Result result = obj->on_send(sock, buffer, buffer_length, *buffer_sent);
260 1 return Socop::translate_result(result);
261 }
262
263 1 static int on_sendto(socop_server_t* svr, socop_socket_t sock, void* buffer, unsigned buffer_length, unsigned* buffer_sent, socop_addr_t* addr)
264 {
265 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
266 1 Socop::Result result = obj->on_send_to(sock, buffer, buffer_length, *buffer_sent, *addr);
267 1 return Socop::translate_result(result);
268 }
269
270 1 static int on_shutdown(socop_server_t* svr, socop_socket_t sock, unsigned flags)
271 {
272 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
273 1 Socop::Result result = obj->on_shutdown(sock, flags);
274 1 return Socop::translate_result(result);
275 }
276
277 1 static int on_select(socop_server_t* svr, socop_socket_t sock, unsigned* in_out_flags)
278 {
279 1 SocopServer* obj = static_cast<SocopServer*>(svr->server_param);
280 1 Socop::Result result = obj->on_select(sock, *in_out_flags);
281 1 return Socop::translate_result(result);
282 }
283
284 };
285
286
287 3 SocopServer::SocopServer()
288 {
289 3 memset(&this->impl, 0, sizeof(this->impl));
290 3 }
291
292 ✗ SocopServer::SocopServer(SocopDriver const& driver)
293 {
294 ✗ this->init(driver);
295 ✗ }
296
297 3 void SocopServer::init(SocopDriver const& driver)
298 {
299 3 memset(&this->impl, 0, sizeof(this->impl));
300
301 3 socop_server_init(&this->impl, this, driver.read_byte, driver.read_param, driver.write_byte, driver.write_param);
302
303 3 this->impl.on_reset = &SocopServerDispatcher::on_reset;
304 3 this->impl.on_netstatus = &SocopServerDispatcher::on_netstatus;
305 3 this->impl.on_netstart = &SocopServerDispatcher::on_netstart;
306 3 this->impl.on_netstop = &SocopServerDispatcher::on_netstop;
307
308 3 this->impl.on_connect = &SocopServerDispatcher::on_connect;
309 3 this->impl.on_bind = &SocopServerDispatcher::on_bind;
310 3 this->impl.on_listen = &SocopServerDispatcher::on_listen;
311 3 this->impl.on_accept = &SocopServerDispatcher::on_accept;
312 3 this->impl.on_close = &SocopServerDispatcher::on_close;
313
314 3 this->impl.on_recv = &SocopServerDispatcher::on_recv;
315 3 this->impl.on_recvfrom = &SocopServerDispatcher::on_recvfrom;
316 3 this->impl.on_send = &SocopServerDispatcher::on_send;
317 3 this->impl.on_sendto = &SocopServerDispatcher::on_sendto;
318 3 this->impl.on_shutdown = &SocopServerDispatcher::on_shutdown;
319 3 this->impl.on_select = &SocopServerDispatcher::on_select;
320 3 }
321
322
323 6 SocopServer::~SocopServer()
324 {
325 6 }
326
327
328 25 SocopServer::Result SocopServer::chat()
329 {
330 25 return translate_result(socop_server_chat_once(&this->impl));
331 }
332
333
334 #endif
335