Skip to content

Commit

Permalink
Merge pull request #1175 from paullouisageneau/fix-getpeername-failure
Browse files Browse the repository at this point in the history
Prevent TcpTransport initialization errors from stopping TcpServer
  • Loading branch information
paullouisageneau authored May 11, 2024
2 parents 8116593 + c1985b6 commit f57d9e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/impl/tcpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ shared_ptr<TcpTransport> TcpServer::accept() {
socket_t incomingSock = ::accept(mSock, (struct sockaddr *)&addr, &addrlen);

if (incomingSock != INVALID_SOCKET) {
return std::make_shared<TcpTransport>(incomingSock, nullptr); // no state callback

try {
return std::make_shared<TcpTransport>(incomingSock, nullptr); // no state callback
} catch(const std::exception &e) {
PLOG_WARNING << e.what();
::closesocket(incomingSock);
}
} else if (sockerrno != SEAGAIN && sockerrno != SEWOULDBLOCK) {
PLOG_ERROR << "TCP server failed, errno=" << sockerrno;
throw std::runtime_error("TCP server failed");
Expand Down
2 changes: 1 addition & 1 deletion src/impl/tcptransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ TcpTransport::TcpTransport(socket_t sock, state_callback callback)
struct sockaddr_storage addr;
socklen_t addrlen = sizeof(addr);
if (::getpeername(mSock, reinterpret_cast<struct sockaddr *>(&addr), &addrlen) < 0)
throw std::runtime_error("getsockname failed");
throw std::runtime_error("getpeername failed");

unmap_inet6_v4mapped(reinterpret_cast<struct sockaddr *>(&addr), &addrlen);

Expand Down

0 comments on commit f57d9e2

Please sign in to comment.