GCC Code Coverage Report


Directory: src/gate/
File: src/gate/net/cxx_sockettools.cpp
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 137 156 87.8%
Functions: 38 44 86.4%
Branches: 37 100 37.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/net/sockettools.hpp"
30 #include "gate/exceptions.hpp"
31
32 namespace gate
33 {
34 namespace net
35 {
36
37 /////////////////////////////////////
38 // SocketSelector implementation //
39 /////////////////////////////////////
40
41 1 SocketSelector::SocketSelector()
42 {
43 1 result_t const result = gate_socketselector_create(&this->impl);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_ERROR(result);
45 1 }
46 2 SocketSelector::~SocketSelector() noexcept
47 {
48 1 gate_socketselector_destroy(&this->impl);
49 1 }
50 1 bool_t SocketSelector::select(gate_socket_t const* sockets, size_t socketsCount, uint8_t* statusFlags, uint32_t timeoutMS)
51 {
52 1 result_t const result = gate_socketselector_select(&this->impl, sockets, socketsCount, statusFlags, timeoutMS);
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_RESULT_TIMEOUT == result)
54 {
55 return false;
56 }
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
58 1 return true;
59 }
60 1 void SocketSelector::intercept()
61 {
62 1 result_t const result = gate_socketselector_interrupt(&this->impl);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
64 1 }
65
66
67 //////////////////////////////////////
68 // SocketGroupSink implementation //
69 //////////////////////////////////////
70
71 2 SocketGroupSink::~SocketGroupSink() noexcept
72 {
73 2 }
74
75 3 void SocketGroupSink::onPrepare(gate_socket_t sock, result_t result, void* userParam)
76 {
77 GATE_UNUSED_ARG(sock);
78 GATE_UNUSED_ARG(result);
79 GATE_UNUSED_ARG(userParam);
80 3 }
81 1 void SocketGroupSink::onConnect(gate_socket_t sock, result_t result, void* userParam)
82 {
83 GATE_UNUSED_ARG(sock);
84 GATE_UNUSED_ARG(result);
85 GATE_UNUSED_ARG(userParam);
86 1 }
87 1 void SocketGroupSink::onAccept(gate_socket_t sock, result_t result, void* userParam)
88 {
89 GATE_UNUSED_ARG(sock);
90 GATE_UNUSED_ARG(result);
91 GATE_UNUSED_ARG(userParam);
92 1 }
93 void SocketGroupSink::onRead(gate_socket_t sock, result_t result, char const* data, size_t dataLength, void* userParam)
94 {
95 GATE_UNUSED_ARG(sock);
96 GATE_UNUSED_ARG(result);
97 GATE_UNUSED_ARG(data);
98 GATE_UNUSED_ARG(dataLength);
99 GATE_UNUSED_ARG(userParam);
100 }
101 1 void SocketGroupSink::onWrite(gate_socket_t sock, result_t result, char const* data, size_t dataLength, void* userParam)
102 {
103 GATE_UNUSED_ARG(sock);
104 GATE_UNUSED_ARG(result);
105 GATE_UNUSED_ARG(data);
106 GATE_UNUSED_ARG(dataLength);
107 GATE_UNUSED_ARG(userParam);
108 1 }
109 1 void SocketGroupSink::onShutdown(gate_socket_t sock, result_t result, void* userParam)
110 {
111 GATE_UNUSED_ARG(sock);
112 GATE_UNUSED_ARG(result);
113 GATE_UNUSED_ARG(userParam);
114 1 }
115 void SocketGroupSink::onError(gate_socket_t sock, result_t result, void* userParam)
116 {
117 GATE_UNUSED_ARG(sock);
118 GATE_UNUSED_ARG(result);
119 GATE_UNUSED_ARG(userParam);
120 }
121
122
123 //////////////////////////////////
124 // SocketGroup implementation //
125 //////////////////////////////////
126
127 1 SocketGroup::SocketGroup()
128 1 : eventSink(NULL)
129 {
130 1 result_t result = gate_socketgroup_create(&this->impl, this);
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_ERROR(result);
132 1 }
133 2 SocketGroup::~SocketGroup()
134 {
135 1 gate_socketgroup_destroy(&this->impl);
136 1 }
137
138 1 gate_socketgroup_t* SocketGroup::c_impl()
139 {
140 1 return &this->impl;
141 }
142
143
144 1 void SocketGroup::remove(gate_socket_t sock)
145 {
146 1 result_t result = gate_socketgroup_remove(&this->impl, sock);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_ERROR(result);
148 1 }
149 1 void SocketGroup::remove(Socket& sock)
150 {
151 1 this->remove(*sock.c_impl());
152 1 }
153 1 void SocketGroup::clear()
154 {
155 1 result_t result = gate_socketgroup_clear(&this->impl);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_ERROR(result);
157 1 }
158 1 void SocketGroup::connect(gate_socket_t sock, Socket::Endpoint const& endpoint, void* userParam)
159 {
160 1 result_t result = gate_socketgroup_connect(&this->impl, sock, &endpoint, userParam);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
162 1 }
163 1 void SocketGroup::connect(Socket& sock, Socket::Endpoint const& endpoint, void* userParam)
164 {
165 1 this->connect(*sock.c_impl(), endpoint, userParam);
166 1 }
167 1 void SocketGroup::accept(gate_socket_t sock, void* userParam)
168 {
169 1 result_t result = gate_socketgroup_accept(&this->impl, sock, userParam);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
171 1 }
172 1 void SocketGroup::accept(Socket& sock, void* userParam)
173 {
174 1 this->accept(*sock.c_impl(), userParam);
175 1 }
176 2 void SocketGroup::read(gate_socket_t sock, size_t length, void* userParam)
177 {
178 2 result_t result = gate_socketgroup_read(&this->impl, sock, length, userParam);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
180 2 }
181 2 void SocketGroup::read(Socket& sock, size_t length, void* userParam)
182 {
183 2 this->read(*sock.c_impl(), length, userParam);
184 2 }
185 2 void SocketGroup::write(gate_socket_t sock, char const* data, size_t length, void* userParam)
186 {
187 2 result_t result = gate_socketgroup_write(&this->impl, sock, data, length, userParam);
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
189 2 }
190 2 void SocketGroup::write(Socket& sock, char const* data, size_t length, void* userParam)
191 {
192 2 this->write(*sock.c_impl(), data, length, userParam);
193 2 }
194 2 void SocketGroup::shutdown(gate_socket_t sock, void* userParam)
195 {
196 2 result_t result = gate_socketgroup_shutdown_write(&this->impl, sock, userParam);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
198 2 }
199 2 void SocketGroup::shutdown(Socket& sock, void* userParam)
200 {
201 2 this->shutdown(*sock.c_impl(), userParam);
202 2 }
203
204
205 7 void SocketGroup::eventDispatcher(gate_socketgroup_t* group, gate_uint32_t operation,
206 gate_socket_t sock, gate_result_t result, void* userParam,
207 char const* data, gate_size_t dataLength)
208 {
209 7 SocketGroup* parent = static_cast<SocketGroup*>(group->user_tag);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!parent) return;
211
212 7 SocketGroupSink* sink = parent->eventSink;
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!sink) return;
214
215
10/22
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
✓ Branch 15 taken 1 times.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
7 GATEXX_TRY_IGNORE({
216 switch (operation)
217 {
218 case GATE_SOCKETGROUP_OPERATION_PREPARE:
219 sink->onPrepare(sock, result, userParam);
220 break;
221 case GATE_SOCKETGROUP_OPERATION_CONNECT:
222 sink->onConnect(sock, result, userParam);
223 break;
224 case GATE_SOCKETGROUP_OPERATION_ACCEPT:
225 sink->onAccept(sock, result, userParam);
226 break;
227 case GATE_SOCKETGROUP_OPERATION_READ:
228 sink->onRead(sock, result, data, dataLength, userParam);
229 break;
230 case GATE_SOCKETGROUP_OPERATION_WRITE:
231 sink->onWrite(sock, result, data, dataLength, userParam);
232 break;
233 case GATE_SOCKETGROUP_OPERATION_SHUTDOWN_WRITE:
234 sink->onShutdown(sock, result, userParam);
235 break;
236 case GATE_SOCKETGROUP_OPERATION_ERROR:
237 sink->onError(sock, result, userParam);
238 break;
239 }
240 });
241 }
242
243 1 void SocketGroup::run(SocketGroupSink* eventHandler)
244 {
245 1 SocketGroupSink* sink = this->eventSink;
246 1 this->eventSink = eventHandler;
247 1 result_t result = gate_socketgroup_run(&this->impl, &SocketGroup::eventDispatcher);
248 1 this->eventSink = sink;
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
250 1 }
251 1 void SocketGroup::quit()
252 {
253 1 result_t result = gate_socketgroup_quit(&this->impl);
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
255 1 }
256
257
258 //////////////////////////////////
259 // SocketQueue implementation //
260 //////////////////////////////////
261
262 enumint_t const SocketQueue::Flag_OpenClient = GATE_SOCKETQUEUE_OPEN_CLIENT;
263 enumint_t const SocketQueue::Flag_OpenServer = GATE_SOCKETQUEUE_OPEN_SERVER;
264
265
266 2 static gate_socketqueue_t* createSocketQueue(uint32_t idleIntervalMs)
267 {
268 2 gate_socketqueue_t* ptr_queue = NULL;
269
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 result_t result = gate_socketqueue_create(&ptr_queue, idleIntervalMs);
270
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 GATEXX_CHECK_EXCEPTION(result);
271 2 return ptr_queue;
272 }
273
274 2 SocketQueue SocketQueue::create(uint32_t idleIntervalMs)
275 {
276 2 return SocketQueue(createSocketQueue(idleIntervalMs));
277 }
278
279
280 2 SocketQueue::SocketQueue(gate_socketqueue_t* queue)
281 : DataQueue((gate_dataqueue_t*)queue),
282 2 ptr_socketqueue(queue)
283 {
284 2 }
285 1 SocketQueue::SocketQueue(SocketQueue const& src)
286 : DataQueue(src),
287 1 ptr_socketqueue(src.ptr_socketqueue)
288 {
289 1 }
290 1 SocketQueue& SocketQueue::operator=(SocketQueue const& src)
291 {
292
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (this != &src)
293 {
294 1 DataQueue::operator=(src);
295 1 this->ptr_socketqueue = src.ptr_socketqueue;
296 }
297 1 return *this;
298 }
299 9 SocketQueue::~SocketQueue()
300 {
301 3 ptr_socketqueue = NULL;
302 3 }
303
304 1 void SocketQueue::closeAll()
305 {
306 1 gate_socketqueue_close_all(this->ptr_socketqueue);
307 1 }
308
309 gate_socketqueue_t* SocketQueue::c_impl()
310 {
311 return this->ptr_socketqueue;
312 }
313
314 gate_socketqueue_t const* SocketQueue::c_impl() const
315 {
316 return this->ptr_socketqueue;
317 }
318
319
320
321 ControlStream SocketStream::createClient(Socket::Endpoint const& ep, bool_t delayConnect)
322 {
323 gate_controlstream_t* ptr_strm = NULL;
324 gate_result_t result = gate_socketstream_create_endpoint(&ep, &ptr_strm, delayConnect);
325 GATEXX_CHECK_EXCEPTION(result);
326 return ControlStream(ptr_strm);
327 }
328
329
330 2 ControlStream SocketStream::createClient(String const& address, bool_t delayConnect)
331 {
332 2 gate_controlstream_t* ptr_strm = NULL;
333
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 gate_result_t result = gate_socketstream_create_address(address.c_impl(), &ptr_strm, delayConnect);
334
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 GATEXX_CHECK_EXCEPTION(result);
335
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return ControlStream(ptr_strm);
336 }
337
338 ControlStream SocketStream::createServer(Socket::Endpoint const& ep)
339 {
340 gate_controlstream_t* ptr_strm = NULL;
341 gate_result_t result = gate_socketstream_create_server(&ep, &ptr_strm);
342 GATEXX_CHECK_EXCEPTION(result);
343 return ControlStream(ptr_strm);
344 }
345
346 1 ControlStream SocketStream::createServer(String const& address)
347 {
348 gate_socket_endpoint_t ep;
349
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_socket_parse_endpoint(address.c_impl(), &ep);
350
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
351
352 1 gate_controlstream_t* ptr_strm = NULL;
353
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 result = gate_socketstream_create_server(&ep, &ptr_strm);
354
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
355
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return ControlStream(ptr_strm);
356 }
357
358
359 } // end of namespace net
360 } // end of namespace gate
361