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