Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify socket open #174

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions include/ur_client_library/comm/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ class URStream : public TCPSocket
return host_;
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}

private:
std::string host_;
int port_;
Expand All @@ -145,7 +139,6 @@ bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)

while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
{
TCPSocket::setOptions(getSocketFD());
if (initial)
{
remainder = T::HeaderType::getPackageLength(buf);
Expand Down
7 changes: 4 additions & 3 deletions include/ur_client_library/comm/tcp_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ class TCPSocket
std::atomic<SocketState> state_;
std::chrono::seconds reconnection_time_;

void setupOptions();

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
static bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return false;
return ::connect(socket_fd, address, address_len) == 0;
}
virtual void setOptions(int socket_fd);

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

Expand Down
6 changes: 0 additions & 6 deletions include/ur_client_library/ur/dashboard_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,6 @@ class DashboardClient : public comm::TCPSocket
*/
bool commandSaveLog();

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}

private:
/*!
* \brief Makes sure that the dashboard_server's version is above the required version
Expand Down
12 changes: 6 additions & 6 deletions src/comm/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ TCPSocket::~TCPSocket()
close();
}

void TCPSocket::setOptions(int socket_fd)
void TCPSocket::setupOptions()
{
int flag = 1;
setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
setsockopt(socket_fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(int));
setsockopt(socket_fd_, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
setsockopt(socket_fd_, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(int));

if (recv_timeout_ != nullptr)
{
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, recv_timeout_.get(), sizeof(timeval));
setsockopt(socket_fd_, SOL_SOCKET, SO_RCVTIMEO, recv_timeout_.get(), sizeof(timeval));
}
}

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

if (state_ == SocketState::Connected)
{
setOptions(socket_fd_);
setupOptions();
}
}

Expand Down
6 changes: 0 additions & 6 deletions tests/test_reverse_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ class ReverseIntefaceTest : public ::testing::Test
readMessage(keep_alive_signal, pos, control_mode);
return pos[0];
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}
};

void SetUp()
Expand Down
6 changes: 0 additions & 6 deletions tests/test_script_command_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ class ScriptCommandInterfaceTest : public ::testing::Test
b_pos += sizeof(int32_t);
}
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}
};

void SetUp()
Expand Down
6 changes: 0 additions & 6 deletions tests/test_script_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ class ScriptSenderTest : public ::testing::Test
}
return result.str();
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}
};

void SetUp()
Expand Down
6 changes: 0 additions & 6 deletions tests/test_tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ class TCPServerTest : public ::testing::Test
}
return result.str();
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}
};

// callback functions
Expand Down
6 changes: 0 additions & 6 deletions tests/test_tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ class TCPSocketTest : public ::testing::Test
return false;
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}

private:
std::thread client_setup_thread_;
int port_;
Expand Down
7 changes: 0 additions & 7 deletions tests/test_trajectory_point_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
size_t remainder = sizeof(int32_t) * 21;
while (remainder > 0)
{
TCPSocket::setOptions(getSocketFD());
if (!TCPSocket::read(b_pos, remainder, read))
{
std::cout << "Failed to read from socket, this should not happen during a test!" << std::endl;
Expand Down Expand Up @@ -172,12 +171,6 @@ class TrajectoryPointInterfaceTest : public ::testing::Test
readMessage(spl.pos, spl.vel, spl.acc, spl.goal_time, spl.blend_radius_or_spline_type, spl.motion_type);
return spl;
}

protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
{
return ::connect(socket_fd, address, address_len) == 0;
}
};

void SetUp()
Expand Down
Loading