| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, 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 | ✗ | SocketSelector::SocketSelector() | |
| 42 | { | ||
| 43 | ✗ | result_t result = gate_socketselector_create(&this->impl); | |
| 44 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 45 | ✗ | } | |
| 46 | ✗ | SocketSelector::~SocketSelector() noexcept | |
| 47 | { | ||
| 48 | ✗ | gate_socketselector_destroy(&this->impl); | |
| 49 | ✗ | } | |
| 50 | ✗ | bool SocketSelector::select(gate_socket_t const* sockets, size_t socketsCount, uint8_t* statusFlags, uint32_t timeoutMS) | |
| 51 | { | ||
| 52 | ✗ | result_t result = gate_socketselector_select(&this->impl, sockets, socketsCount, statusFlags, timeoutMS); | |
| 53 | ✗ | if (GATE_RESULT_TIMEOUT == result) | |
| 54 | { | ||
| 55 | ✗ | return false; | |
| 56 | } | ||
| 57 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 58 | ✗ | return true; | |
| 59 | } | ||
| 60 | ✗ | void SocketSelector::intercept() | |
| 61 | { | ||
| 62 | ✗ | result_t result = gate_socketselector_interrupt(&this->impl); | |
| 63 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 64 | ✗ | } | |
| 65 | |||
| 66 | |||
| 67 | ////////////////////////////////////// | ||
| 68 | // SocketGroupSink implementation // | ||
| 69 | ////////////////////////////////////// | ||
| 70 | |||
| 71 | ✗ | SocketGroupSink::~SocketGroupSink() noexcept | |
| 72 | { | ||
| 73 | ✗ | } | |
| 74 | |||
| 75 | ✗ | 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 | ✗ | } | |
| 81 | ✗ | 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 | ✗ | } | |
| 87 | ✗ | 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 | ✗ | } | |
| 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 | ✗ | 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 | ✗ | } | |
| 109 | ✗ | 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 | ✗ | } | |
| 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 | ✗ | SocketGroup::SocketGroup() | |
| 128 | ✗ | : eventSink(NULL) | |
| 129 | { | ||
| 130 | ✗ | result_t result = gate_socketgroup_create(&this->impl, this); | |
| 131 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 132 | ✗ | } | |
| 133 | ✗ | SocketGroup::~SocketGroup() | |
| 134 | { | ||
| 135 | ✗ | gate_socketgroup_destroy(&this->impl); | |
| 136 | ✗ | } | |
| 137 | |||
| 138 | ✗ | void SocketGroup::remove(gate_socket_t sock) | |
| 139 | { | ||
| 140 | ✗ | result_t result = gate_socketgroup_remove(&this->impl, sock); | |
| 141 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 142 | ✗ | } | |
| 143 | ✗ | void SocketGroup::remove(Socket& sock) | |
| 144 | { | ||
| 145 | ✗ | this->remove(*sock.c_impl()); | |
| 146 | ✗ | } | |
| 147 | ✗ | void SocketGroup::clear() | |
| 148 | { | ||
| 149 | ✗ | result_t result = gate_socketgroup_clear(&this->impl); | |
| 150 | ✗ | GATEXX_CHECK_ERROR(result); | |
| 151 | ✗ | } | |
| 152 | ✗ | void SocketGroup::connect(gate_socket_t sock, Socket::Endpoint const& endpoint, void* userParam) | |
| 153 | { | ||
| 154 | ✗ | result_t result = gate_socketgroup_connect(&this->impl, sock, &endpoint, userParam); | |
| 155 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 156 | ✗ | } | |
| 157 | ✗ | void SocketGroup::connect(Socket& sock, Socket::Endpoint const& endpoint, void* userParam) | |
| 158 | { | ||
| 159 | ✗ | this->connect(*sock.c_impl(), endpoint, userParam); | |
| 160 | ✗ | } | |
| 161 | ✗ | void SocketGroup::accept(gate_socket_t sock, void* userParam) | |
| 162 | { | ||
| 163 | ✗ | result_t result = gate_socketgroup_accept(&this->impl, sock, userParam); | |
| 164 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 165 | ✗ | } | |
| 166 | ✗ | void SocketGroup::accept(Socket& sock, void* userParam) | |
| 167 | { | ||
| 168 | ✗ | this->accept(*sock.c_impl(), userParam); | |
| 169 | ✗ | } | |
| 170 | ✗ | void SocketGroup::read(gate_socket_t sock, size_t length, void* userParam) | |
| 171 | { | ||
| 172 | ✗ | result_t result = gate_socketgroup_read(&this->impl, sock, length, userParam); | |
| 173 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 174 | ✗ | } | |
| 175 | ✗ | void SocketGroup::read(Socket& sock, size_t length, void* userParam) | |
| 176 | { | ||
| 177 | ✗ | this->read(*sock.c_impl(), length, userParam); | |
| 178 | ✗ | } | |
| 179 | ✗ | void SocketGroup::write(gate_socket_t sock, char const* data, size_t length, void* userParam) | |
| 180 | { | ||
| 181 | ✗ | result_t result = gate_socketgroup_write(&this->impl, sock, data, length, userParam); | |
| 182 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 183 | ✗ | } | |
| 184 | ✗ | void SocketGroup::write(Socket& sock, char const* data, size_t length, void* userParam) | |
| 185 | { | ||
| 186 | ✗ | this->write(*sock.c_impl(), data, length, userParam); | |
| 187 | ✗ | } | |
| 188 | ✗ | void SocketGroup::shutdown(gate_socket_t sock, void* userParam) | |
| 189 | { | ||
| 190 | ✗ | result_t result = gate_socketgroup_shutdown_write(&this->impl, sock, userParam); | |
| 191 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 192 | ✗ | } | |
| 193 | ✗ | void SocketGroup::shutdown(Socket& sock, void* userParam) | |
| 194 | { | ||
| 195 | ✗ | this->shutdown(*sock.c_impl(), userParam); | |
| 196 | ✗ | } | |
| 197 | |||
| 198 | |||
| 199 | ✗ | void SocketGroup::eventDispatcher(gate_socketgroup_t* group, gate_uint32_t operation, | |
| 200 | gate_socket_t sock, gate_result_t result, void* userParam, | ||
| 201 | char const* data, gate_size_t dataLength) | ||
| 202 | { | ||
| 203 | ✗ | SocketGroup* parent = static_cast<SocketGroup*>(group->user_tag); | |
| 204 | ✗ | if (!parent) return; | |
| 205 | |||
| 206 | ✗ | SocketGroupSink* sink = parent->eventSink; | |
| 207 | ✗ | if (!sink) return; | |
| 208 | |||
| 209 | try | ||
| 210 | { | ||
| 211 | ✗ | switch (operation) | |
| 212 | { | ||
| 213 | ✗ | case GATE_SOCKETGROUP_OPERATION_PREPARE: | |
| 214 | ✗ | sink->onPrepare(sock, result, userParam); | |
| 215 | ✗ | break; | |
| 216 | ✗ | case GATE_SOCKETGROUP_OPERATION_CONNECT: | |
| 217 | ✗ | sink->onConnect(sock, result, userParam); | |
| 218 | ✗ | break; | |
| 219 | ✗ | case GATE_SOCKETGROUP_OPERATION_ACCEPT: | |
| 220 | ✗ | sink->onAccept(sock, result, userParam); | |
| 221 | ✗ | break; | |
| 222 | ✗ | case GATE_SOCKETGROUP_OPERATION_READ: | |
| 223 | ✗ | sink->onRead(sock, result, data, dataLength, userParam); | |
| 224 | ✗ | break; | |
| 225 | ✗ | case GATE_SOCKETGROUP_OPERATION_WRITE: | |
| 226 | ✗ | sink->onWrite(sock, result, data, dataLength, userParam); | |
| 227 | ✗ | break; | |
| 228 | ✗ | case GATE_SOCKETGROUP_OPERATION_SHUTDOWN_WRITE: | |
| 229 | ✗ | sink->onShutdown(sock, result, userParam); | |
| 230 | ✗ | break; | |
| 231 | ✗ | case GATE_SOCKETGROUP_OPERATION_ERROR: | |
| 232 | ✗ | sink->onError(sock, result, userParam); | |
| 233 | ✗ | break; | |
| 234 | } | ||
| 235 | } | ||
| 236 | ✗ | catch (...) {} | |
| 237 | } | ||
| 238 | |||
| 239 | ✗ | void SocketGroup::run(SocketGroupSink* eventHandler) | |
| 240 | { | ||
| 241 | ✗ | SocketGroupSink* sink = this->eventSink; | |
| 242 | ✗ | this->eventSink = eventHandler; | |
| 243 | ✗ | result_t result = gate_socketgroup_run(&this->impl, &SocketGroup::eventDispatcher); | |
| 244 | ✗ | this->eventSink = sink; | |
| 245 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 246 | ✗ | } | |
| 247 | ✗ | void SocketGroup::quit() | |
| 248 | { | ||
| 249 | ✗ | result_t result = gate_socketgroup_quit(&this->impl); | |
| 250 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 251 | ✗ | } | |
| 252 | |||
| 253 | |||
| 254 | ////////////////////////////////// | ||
| 255 | // SocketQueue implementation // | ||
| 256 | ////////////////////////////////// | ||
| 257 | |||
| 258 | enumint_t const SocketQueue::Flag_OpenClient = GATE_SOCKETQUEUE_OPEN_CLIENT; | ||
| 259 | enumint_t const SocketQueue::Flag_OpenServer = GATE_SOCKETQUEUE_OPEN_SERVER; | ||
| 260 | |||
| 261 | |||
| 262 | 2 | static gate_socketqueue_t* createSocketQueue(uint32_t idleIntervalMs) | |
| 263 | { | ||
| 264 | 2 | gate_socketqueue_t* ptr_queue = NULL; | |
| 265 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | result_t result = gate_socketqueue_create(&ptr_queue, idleIntervalMs); |
| 266 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2 | GATEXX_CHECK_EXCEPTION(result); |
| 267 | 2 | return ptr_queue; | |
| 268 | } | ||
| 269 | |||
| 270 | 2 | SocketQueue SocketQueue::create(uint32_t idleIntervalMs) | |
| 271 | { | ||
| 272 | 2 | return SocketQueue(createSocketQueue(idleIntervalMs)); | |
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | 2 | SocketQueue::SocketQueue(gate_socketqueue_t* queue) | |
| 277 | : DataQueue((gate_dataqueue_t*)queue), | ||
| 278 | 2 | ptr_socketqueue(queue) | |
| 279 | { | ||
| 280 | 2 | } | |
| 281 | 1 | SocketQueue::SocketQueue(SocketQueue const& src) | |
| 282 | : DataQueue(src), | ||
| 283 | 1 | ptr_socketqueue(src.ptr_socketqueue) | |
| 284 | { | ||
| 285 | 1 | } | |
| 286 | 1 | SocketQueue& SocketQueue::operator=(SocketQueue const& src) | |
| 287 | { | ||
| 288 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 289 | { | ||
| 290 | 1 | DataQueue::operator=(src); | |
| 291 | 1 | this->ptr_socketqueue = src.ptr_socketqueue; | |
| 292 | } | ||
| 293 | 1 | return *this; | |
| 294 | } | ||
| 295 | 9 | SocketQueue::~SocketQueue() | |
| 296 | { | ||
| 297 | 3 | ptr_socketqueue = NULL; | |
| 298 | 3 | } | |
| 299 | |||
| 300 | 1 | void SocketQueue::closeAll() | |
| 301 | { | ||
| 302 | 1 | gate_socketqueue_close_all(this->ptr_socketqueue); | |
| 303 | 1 | } | |
| 304 | |||
| 305 | ✗ | gate_socketqueue_t* SocketQueue::c_impl() | |
| 306 | { | ||
| 307 | ✗ | return this->ptr_socketqueue; | |
| 308 | } | ||
| 309 | |||
| 310 | ✗ | gate_socketqueue_t const* SocketQueue::c_impl() const | |
| 311 | { | ||
| 312 | ✗ | return this->ptr_socketqueue; | |
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | ✗ | ControlStream SocketStream::createClient(Socket::Endpoint const& ep, bool_t delayConnect) | |
| 318 | { | ||
| 319 | ✗ | gate_controlstream_t* ptr_strm = NULL; | |
| 320 | ✗ | gate_result_t result = gate_socketstream_create_endpoint(&ep, &ptr_strm, delayConnect); | |
| 321 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 322 | ✗ | return ControlStream(ptr_strm); | |
| 323 | } | ||
| 324 | |||
| 325 | |||
| 326 | 1 | ControlStream SocketStream::createClient(String const& address, bool_t delayConnect) | |
| 327 | { | ||
| 328 | 1 | gate_controlstream_t* ptr_strm = NULL; | |
| 329 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_result_t result = gate_socketstream_create_address(address.c_impl(), &ptr_strm, delayConnect); |
| 330 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 331 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return ControlStream(ptr_strm); |
| 332 | } | ||
| 333 | |||
| 334 | ✗ | ControlStream SocketStream::createServer(Socket::Endpoint const& ep) | |
| 335 | { | ||
| 336 | ✗ | gate_controlstream_t* ptr_strm = NULL; | |
| 337 | ✗ | gate_result_t result = gate_socketstream_create_server(&ep, &ptr_strm); | |
| 338 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
| 339 | ✗ | return ControlStream(ptr_strm); | |
| 340 | } | ||
| 341 | |||
| 342 | 1 | ControlStream SocketStream::createServer(String const& address) | |
| 343 | { | ||
| 344 | gate_socket_endpoint_t ep; | ||
| 345 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_socket_parse_endpoint(address.c_impl(), &ep); |
| 346 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
| 347 | |||
| 348 | 1 | gate_controlstream_t* ptr_strm = NULL; | |
| 349 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result = gate_socketstream_create_server(&ep, &ptr_strm); |
| 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 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return ControlStream(ptr_strm); |
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | } // end of namespace net | ||
| 356 | } // end of namespace gate | ||
| 357 |