| 372 | } |
| 373 | |
| 374 | std::unique_ptr<ITcpSocket> Accept() override |
| 375 | { |
| 376 | if (_status != SocketStatus::listening) |
| 377 | { |
| 378 | throw std::runtime_error("Socket not listening."); |
| 379 | } |
| 380 | struct sockaddr_storage client_addr{}; |
| 381 | socklen_t client_len = sizeof(struct sockaddr_storage); |
| 382 | |
| 383 | std::unique_ptr<ITcpSocket> tcpSocket; |
| 384 | SOCKET socket = accept(_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_len); |
| 385 | if (socket == INVALID_SOCKET) |
| 386 | { |
| 387 | if (LAST_SOCKET_ERROR() != EWOULDBLOCK) |
| 388 | { |
| 389 | LOG_ERROR("Failed to accept client."); |
| 390 | } |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | if (!SetNonBlocking(socket, true)) |
| 395 | { |
| 396 | closesocket(socket); |
| 397 | LOG_ERROR("Failed to set non-blocking mode."); |
| 398 | } |
| 399 | else |
| 400 | { |
| 401 | auto ipAddress = GetIpAddressFromSocket(reinterpret_cast<sockaddr_in*>(&client_addr)); |
| 402 | |
| 403 | char hostName[NI_MAXHOST]; |
| 404 | int32_t rc = getnameinfo( |
| 405 | reinterpret_cast<struct sockaddr*>(&client_addr), client_len, hostName, sizeof(hostName), nullptr, 0, |
| 406 | NI_NUMERICHOST | NI_NUMERICSERV); |
| 407 | SetNoDelay(true); |
| 408 | |
| 409 | if (rc == 0) |
| 410 | { |
| 411 | tcpSocket = std::make_unique<TcpSocket>(socket, hostName, ipAddress); |
| 412 | } |
| 413 | else |
| 414 | { |
| 415 | tcpSocket = std::make_unique<TcpSocket>(socket, "", ipAddress); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | return tcpSocket; |
| 420 | } |
| 421 | |
| 422 | void Connect(const std::string& address, uint16_t port) override |
| 423 | { |
no outgoing calls
no test coverage detected