Skip to content

Commit 7626fe5

Browse files
author
Felix Exner (fexner)
authored
Unify socket open (#174)
* Define TCPSocket::open(...) inside TCPSocket class Before, it as a virtual method that in its base implementation returned `false` only. It had to be (and was) implemented in several classes inheriting from it and all implementations were the same. This commit defines the method inside the base class directly. * Make TCPSocket::setOptions private and TCPSocket::open static
1 parent cd0d426 commit 7626fe5

File tree

10 files changed

+10
-59
lines changed

10 files changed

+10
-59
lines changed

include/ur_client_library/comm/stream.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ class URStream : public TCPSocket
114114
return host_;
115115
}
116116

117-
protected:
118-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
119-
{
120-
return ::connect(socket_fd, address, address_len) == 0;
121-
}
122-
123117
private:
124118
std::string host_;
125119
int port_;
@@ -145,7 +139,6 @@ bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)
145139

146140
while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
147141
{
148-
TCPSocket::setOptions(getSocketFD());
149142
if (initial)
150143
{
151144
remainder = T::HeaderType::getPackageLength(buf);

include/ur_client_library/comm/tcp_socket.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ class TCPSocket
5252
std::atomic<SocketState> state_;
5353
std::chrono::seconds reconnection_time_;
5454

55+
void setupOptions();
56+
5557
protected:
56-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
58+
static bool open(int socket_fd, struct sockaddr* address, size_t address_len)
5759
{
58-
return false;
60+
return ::connect(socket_fd, address, address_len) == 0;
5961
}
60-
virtual void setOptions(int socket_fd);
6162

6263
bool setup(std::string& host, int port);
6364

include/ur_client_library/ur/dashboard_client.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,6 @@ class DashboardClient : public comm::TCPSocket
429429
*/
430430
bool commandSaveLog();
431431

432-
protected:
433-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
434-
{
435-
return ::connect(socket_fd, address, address_len) == 0;
436-
}
437-
438432
private:
439433
/*!
440434
* \brief Makes sure that the dashboard_server's version is above the required version

src/comm/tcp_socket.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ TCPSocket::~TCPSocket()
4343
close();
4444
}
4545

46-
void TCPSocket::setOptions(int socket_fd)
46+
void TCPSocket::setupOptions()
4747
{
4848
int flag = 1;
49-
setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
50-
setsockopt(socket_fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(int));
49+
setsockopt(socket_fd_, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
50+
setsockopt(socket_fd_, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(int));
5151

5252
if (recv_timeout_ != nullptr)
5353
{
54-
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, recv_timeout_.get(), sizeof(timeval));
54+
setsockopt(socket_fd_, SOL_SOCKET, SO_RCVTIMEO, recv_timeout_.get(), sizeof(timeval));
5555
}
5656
}
5757

@@ -107,7 +107,7 @@ bool TCPSocket::setup(std::string& host, int port)
107107
std::this_thread::sleep_for(reconnection_time_);
108108
}
109109
}
110-
setOptions(socket_fd_);
110+
setupOptions();
111111
state_ = SocketState::Connected;
112112
URCL_LOG_DEBUG("Connection established for %s:%d", host.c_str(), port);
113113
return connected;
@@ -206,7 +206,7 @@ void TCPSocket::setReceiveTimeout(const timeval& timeout)
206206

207207
if (state_ == SocketState::Connected)
208208
{
209-
setOptions(socket_fd_);
209+
setupOptions();
210210
}
211211
}
212212

tests/test_reverse_interface.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ class ReverseIntefaceTest : public ::testing::Test
145145
readMessage(keep_alive_signal, pos, control_mode);
146146
return pos[0];
147147
}
148-
149-
protected:
150-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
151-
{
152-
return ::connect(socket_fd, address, address_len) == 0;
153-
}
154148
};
155149

156150
void SetUp()

tests/test_script_command_interface.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ class ScriptCommandInterfaceTest : public ::testing::Test
9898
b_pos += sizeof(int32_t);
9999
}
100100
}
101-
102-
protected:
103-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
104-
{
105-
return ::connect(socket_fd, address, address_len) == 0;
106-
}
107101
};
108102

109103
void SetUp()

tests/test_script_sender.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ class ScriptSenderTest : public ::testing::Test
7777
}
7878
return result.str();
7979
}
80-
81-
protected:
82-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
83-
{
84-
return ::connect(socket_fd, address, address_len) == 0;
85-
}
8680
};
8781

8882
void SetUp()

tests/test_tcp_server.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ class TCPServerTest : public ::testing::Test
7474
}
7575
return result.str();
7676
}
77-
78-
protected:
79-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
80-
{
81-
return ::connect(socket_fd, address, address_len) == 0;
82-
}
8377
};
8478

8579
// callback functions

tests/test_tcp_socket.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ class TCPSocketTest : public ::testing::Test
136136
return false;
137137
}
138138

139-
protected:
140-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
141-
{
142-
return ::connect(socket_fd, address, address_len) == 0;
143-
}
144-
145139
private:
146140
std::thread client_setup_thread_;
147141
int port_;

tests/test_trajectory_point_interface.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
6969
size_t remainder = sizeof(int32_t) * 21;
7070
while (remainder > 0)
7171
{
72-
TCPSocket::setOptions(getSocketFD());
7372
if (!TCPSocket::read(b_pos, remainder, read))
7473
{
7574
std::cout << "Failed to read from socket, this should not happen during a test!" << std::endl;
@@ -172,12 +171,6 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
172171
readMessage(spl.pos, spl.vel, spl.acc, spl.goal_time, spl.blend_radius_or_spline_type, spl.motion_type);
173172
return spl;
174173
}
175-
176-
protected:
177-
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
178-
{
179-
return ::connect(socket_fd, address, address_len) == 0;
180-
}
181174
};
182175

183176
void SetUp()

0 commit comments

Comments
 (0)