Skip to content

Commit c73d273

Browse files
committed
Check return value of setsockopt and guard by accepted=true
1 parent 95c24ac commit c73d273

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

include/ur_client_library/comm/socket_t.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ typedef int socket_t;
7171
# define MSG_NOSIGNAL 0
7272
#endif
7373

74+
#include "ur_client_library/log.h"
75+
7476
/*!
7577
* \brief Get the last socket error as an std::error_code
7678
*
@@ -92,3 +94,14 @@ inline std::system_error makeSocketError(const std::string& message)
9294
{
9395
return std::system_error(getLastSocketErrorCode(), message);
9496
}
97+
98+
inline void setSocketOptionAndWarnOnError(socket_t socket, int level, int optname, const void* optval,
99+
unsigned int optlen, const std::string& option_name)
100+
{
101+
int result = ur_setsockopt(socket, level, optname, optval, optlen);
102+
if (result != 0)
103+
{
104+
std::error_code error_code = getLastSocketErrorCode();
105+
URCL_LOG_WARN("Failed to set socket option %s: %s", option_name.c_str(), error_code.message().c_str());
106+
}
107+
}

src/comm/tcp_server.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ void TCPServer::handleConnect()
261261
}
262262
}
263263

264-
int flag = 1;
265-
ur_setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
264+
if (accepted)
265+
{
266+
constexpr int flag = 1;
267+
setSocketOptionAndWarnOnError(client_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag), "TCP_NODELAY");
268+
}
266269

267270
{
268271
std::lock_guard<std::mutex> lk(callback_mutex_);

0 commit comments

Comments
 (0)