Skip to content

Commit 26d0553

Browse files
committed
Fix potential problems caused by socket type mismatch between Linux and Windows
1 parent 9204a39 commit 26d0553

19 files changed

+71
-70
lines changed

include/ur_client_library/comm/tcp_server.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class TCPServer
7979
* \param func Function handling the event information. The file descriptor created by the
8080
* connection event will be passed to the function.
8181
*/
82-
void setConnectCallback(std::function<void(const int)> func)
82+
void setConnectCallback(std::function<void(const socket_t)> func)
8383
{
8484
new_connection_callback_ = func;
8585
}
@@ -90,7 +90,7 @@ class TCPServer
9090
* \param func Function handling the event information. The file descriptor created by the
9191
* connection event will be passed to the function.
9292
*/
93-
void setDisconnectCallback(std::function<void(const int)> func)
93+
void setDisconnectCallback(std::function<void(const socket_t)> func)
9494
{
9595
disconnect_callback_ = func;
9696
}
@@ -101,7 +101,7 @@ class TCPServer
101101
* \param func Function handling the event information. The file client's file_descriptor will be
102102
* passed to the function as well as the actual message received from the client.
103103
*/
104-
void setMessageCallback(std::function<void(const int, char*, int)> func)
104+
void setMessageCallback(std::function<void(const socket_t, char*, int)> func)
105105
{
106106
message_callback_ = func;
107107
}
@@ -132,7 +132,7 @@ class TCPServer
132132
*
133133
* \returns True on success, false otherwise
134134
*/
135-
bool write(const int fd, const uint8_t* buf, const size_t buf_len, size_t& written);
135+
bool write(const socket_t fd, const uint8_t* buf, const size_t buf_len, size_t& written);
136136

137137
/*!
138138
* \brief Get the maximum number of clients allowed to connect to this server
@@ -163,10 +163,10 @@ class TCPServer
163163
//! Handles connection events
164164
void handleConnect();
165165

166-
void handleDisconnect(const int fd);
166+
void handleDisconnect(const socket_t fd);
167167

168168
//! read data from socket
169-
void readData(const int fd);
169+
void readData(const socket_t fd);
170170

171171
//! Event handler. Blocks until activity on any client or connection attempt
172172
void spin();

include/ur_client_library/comm/tcp_socket.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class TCPSocket
5757
void setupOptions();
5858

5959
protected:
60-
static bool open(int socket_fd, struct sockaddr* address, size_t address_len)
60+
static bool open(socket_t socket_fd, struct sockaddr* address, size_t address_len)
6161
{
62-
return ::connect(socket_fd, address, address_len) == 0;
62+
return ::connect(socket_fd, address, static_cast<socklen_t>(address_len)) == 0;
6363
}
6464

6565
bool setup(const std::string& host, const int port, const size_t max_num_tries = 0,
@@ -90,7 +90,7 @@ class TCPSocket
9090
*
9191
* \returns The file descriptor of the socket
9292
*/
93-
int getSocketFD()
93+
socket_t getSocketFD()
9494
{
9595
return socket_fd_;
9696
}

include/ur_client_library/control/reverse_interface.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ class ReverseInterface
142142
setKeepaliveCount(const uint32_t count);
143143

144144
protected:
145-
virtual void connectionCallback(const int filedescriptor);
145+
virtual void connectionCallback(const socket_t filedescriptor);
146146

147-
virtual void disconnectionCallback(const int filedescriptor);
147+
virtual void disconnectionCallback(const socket_t filedescriptor);
148148

149-
virtual void messageCallback(const int filedescriptor, char* buffer, int nbytesrecv);
149+
virtual void messageCallback(const socket_t filedescriptor, char* buffer, int nbytesrecv);
150150

151-
int client_fd_;
151+
socket_t client_fd_;
152152
comm::TCPServer server_;
153153

154154
template <typename T>

include/ur_client_library/control/script_command_interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ class ScriptCommandInterface : public ReverseInterface
164164
}
165165

166166
protected:
167-
virtual void connectionCallback(const int filedescriptor) override;
167+
virtual void connectionCallback(const socket_t filedescriptor) override;
168168

169-
virtual void disconnectionCallback(const int filedescriptor) override;
169+
virtual void disconnectionCallback(const socket_t filedescriptor) override;
170170

171-
virtual void messageCallback(const int filedescriptor, char* buffer, int nbytesrecv) override;
171+
virtual void messageCallback(const socket_t filedescriptor, char* buffer, int nbytesrecv) override;
172172

173173
private:
174174
/*!

include/ur_client_library/control/script_sender.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class ScriptSender
6363

6464
const std::string PROGRAM_REQUEST_ = std::string("request_program\n");
6565

66-
void connectionCallback(const int filedescriptor);
66+
void connectionCallback(const socket_t filedescriptor);
6767

68-
void disconnectionCallback(const int filedescriptor);
68+
void disconnectionCallback(const socket_t filedescriptor);
6969

70-
void messageCallback(const int filedescriptor, char* buffer);
70+
void messageCallback(const socket_t filedescriptor, char* buffer);
7171

72-
void sendProgram(const int filedescriptor);
72+
void sendProgram(const socket_t filedescriptor);
7373
};
7474

7575
} // namespace control

include/ur_client_library/control/trajectory_point_interface.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ class TrajectoryPointInterface : public ReverseInterface
145145
}
146146

147147
protected:
148-
virtual void connectionCallback(const int filedescriptor) override;
148+
virtual void connectionCallback(const socket_t filedescriptor) override;
149149

150-
virtual void disconnectionCallback(const int filedescriptor) override;
150+
virtual void disconnectionCallback(const socket_t filedescriptor) override;
151151

152-
virtual void messageCallback(const int filedescriptor, char* buffer, int nbytesrecv) override;
152+
virtual void messageCallback(const socket_t filedescriptor, char* buffer, int nbytesrecv) override;
153153

154154
private:
155155
std::function<void(TrajectoryResult)> handle_trajectory_end_;

include/ur_client_library/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static inline pthread_t pthread_self()
5252

5353
static inline int sched_get_priority_max(int policy)
5454
{
55+
(void)policy;
5556
return THREAD_PRIORITY_TIME_CRITICAL;
5657
}
5758

src/comm/tcp_server.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ void TCPServer::handleConnect()
172172
{
173173
struct sockaddr_storage client_addr;
174174
socklen_t addrlen = sizeof(client_addr);
175-
int client_fd = accept(listen_fd_, (struct sockaddr*)&client_addr, &addrlen);
176-
if (client_fd < 0)
175+
socket_t client_fd = accept(listen_fd_, (struct sockaddr*)&client_addr, &addrlen);
176+
if (client_fd == INVALID_SOCKET)
177177
{
178178
std::ostringstream ss;
179179
ss << "Failed to accept connection request on port " << port_;
@@ -207,7 +207,7 @@ void TCPServer::spin()
207207
tempfds_ = masterfds_;
208208

209209
// blocks until activity on any socket from tempfds
210-
int sel = select(maxfd_ + 1, &tempfds_, NULL, NULL, NULL);
210+
int sel = select(static_cast<int>(maxfd_ + 1), &tempfds_, NULL, NULL, NULL);
211211
if (sel < 0)
212212
{
213213
URCL_LOG_ERROR("select() failed. Shutting down socket event handler.");
@@ -239,7 +239,7 @@ void TCPServer::spin()
239239
}
240240
}
241241

242-
void TCPServer::handleDisconnect(const int fd)
242+
void TCPServer::handleDisconnect(const socket_t fd)
243243
{
244244
URCL_LOG_DEBUG("%d disconnected.", fd);
245245
ur_close(fd);
@@ -259,7 +259,7 @@ void TCPServer::handleDisconnect(const int fd)
259259
}
260260
}
261261

262-
void TCPServer::readData(const int fd)
262+
void TCPServer::readData(const socket_t fd)
263263
{
264264
memset(input_buffer_, 0, INPUT_BUFFER_SIZE); // clear input buffer
265265
int nbytesrecv = recv(fd, input_buffer_, INPUT_BUFFER_SIZE, 0);
@@ -307,7 +307,7 @@ void TCPServer::start()
307307
worker_thread_ = std::thread(&TCPServer::worker, this);
308308
}
309309

310-
bool TCPServer::write(const int fd, const uint8_t* buf, const size_t buf_len, size_t& written)
310+
bool TCPServer::write(const socket_t fd, const uint8_t* buf, const size_t buf_len, size_t& written)
311311
{
312312
written = 0;
313313

@@ -316,7 +316,7 @@ bool TCPServer::write(const int fd, const uint8_t* buf, const size_t buf_len, si
316316
// handle partial sends
317317
while (written < buf_len)
318318
{
319-
ssize_t sent = ::send(fd, reinterpret_cast<const char*>(buf + written), remaining, 0);
319+
ssize_t sent = ::send(fd, reinterpret_cast<const char*>(buf + written), static_cast<socklen_t>(remaining), 0);
320320

321321
if (sent <= 0)
322322
{

src/comm/tcp_socket.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bool TCPSocket::read(uint8_t* buf, const size_t buf_len, size_t& read)
191191
if (state_ != SocketState::Connected)
192192
return false;
193193

194-
ssize_t res = ::recv(socket_fd_, reinterpret_cast<char*>(buf), buf_len, 0);
194+
ssize_t res = ::recv(socket_fd_, reinterpret_cast<char*>(buf), static_cast<const socklen_t>(buf_len), 0);
195195

196196
if (res == 0)
197197
{
@@ -236,7 +236,7 @@ bool TCPSocket::write(const uint8_t* buf, const size_t buf_len, size_t& written)
236236
// handle partial sends
237237
while (written < buf_len)
238238
{
239-
ssize_t sent = ::send(socket_fd_, reinterpret_cast<const char*>(buf + written), remaining, 0);
239+
ssize_t sent = ::send(socket_fd_, reinterpret_cast<const char*>(buf + written), static_cast<socklen_t>(remaining), 0);
240240

241241
if (sent <= 0)
242242
{

src/control/reverse_interface.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace control
3535
{
3636
ReverseInterface::ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state,
3737
std::chrono::milliseconds step_time)
38-
: client_fd_(-1)
38+
: client_fd_(INVALID_SOCKET)
3939
, server_(port)
4040
, handle_program_state_(handle_program_state)
4141
, step_time_(step_time)
@@ -54,7 +54,7 @@ bool ReverseInterface::write(const vector6d_t* positions, const comm::ControlMod
5454
const RobotReceiveTimeout& robot_receive_timeout)
5555
{
5656
const int message_length = 7;
57-
if (client_fd_ == -1)
57+
if (client_fd_ == INVALID_SOCKET)
5858
{
5959
return false;
6060
}
@@ -115,7 +115,7 @@ bool ReverseInterface::writeTrajectoryControlMessage(const TrajectoryControlMess
115115
const RobotReceiveTimeout& robot_receive_timeout)
116116
{
117117
const int message_length = 3;
118-
if (client_fd_ == -1)
118+
if (client_fd_ == INVALID_SOCKET)
119119
{
120120
return false;
121121
}
@@ -162,7 +162,7 @@ bool ReverseInterface::writeFreedriveControlMessage(const FreedriveControlMessag
162162
const RobotReceiveTimeout& robot_receive_timeout)
163163
{
164164
const int message_length = 2;
165-
if (client_fd_ == -1)
165+
if (client_fd_ == INVALID_SOCKET)
166166
{
167167
return false;
168168
}
@@ -212,9 +212,9 @@ void ReverseInterface::setKeepaliveCount(const uint32_t count)
212212
keep_alive_count_modified_deprecated_ = true;
213213
}
214214

215-
void ReverseInterface::connectionCallback(const int filedescriptor)
215+
void ReverseInterface::connectionCallback(const socket_t filedescriptor)
216216
{
217-
if (client_fd_ < 0)
217+
if (client_fd_ == INVALID_SOCKET)
218218
{
219219
URCL_LOG_INFO("Robot connected to reverse interface. Ready to receive control commands.");
220220
client_fd_ = filedescriptor;
@@ -227,14 +227,14 @@ void ReverseInterface::connectionCallback(const int filedescriptor)
227227
}
228228
}
229229

230-
void ReverseInterface::disconnectionCallback(const int filedescriptor)
230+
void ReverseInterface::disconnectionCallback(const socket_t filedescriptor)
231231
{
232232
URCL_LOG_INFO("Connection to reverse interface dropped.", filedescriptor);
233-
client_fd_ = -1;
233+
client_fd_ = INVALID_SOCKET;
234234
handle_program_state_(false);
235235
}
236236

237-
void ReverseInterface::messageCallback(const int filedescriptor, char* buffer, int nbytesrecv)
237+
void ReverseInterface::messageCallback(const socket_t filedescriptor, char* buffer, int nbytesrecv)
238238
{
239239
URCL_LOG_WARN("Message on ReverseInterface received. The reverse interface currently does not support any message "
240240
"handling. This message will be ignored.");

0 commit comments

Comments
 (0)