1
0
mirror of https://gitlab.com/obbart/universal_robots_ros_driver.git synced 2026-04-12 11:00:47 +02:00

setup timeout for tcp sockets

If we want to be able to stop the producer, we'll have to make sure,
it is not hanging inside the recv function because no new packages came in.
This commit is contained in:
Felix Mauch
2019-06-11 17:55:03 +02:00
parent bb36999868
commit e1e71b5cb4
3 changed files with 29 additions and 0 deletions

View File

@@ -44,6 +44,10 @@ public:
void setupProducer() void setupProducer()
{ {
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
stream_.setReceiveTimeout(tv);
stream_.connect(); stream_.connect();
} }
void teardownProducer() void teardownProducer()

View File

@@ -23,6 +23,7 @@
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <memory>
namespace ur_driver namespace ur_driver
{ {
@@ -44,6 +45,7 @@ class TCPSocket
private: private:
std::atomic<int> socket_fd_; std::atomic<int> socket_fd_;
std::atomic<SocketState> state_; std::atomic<SocketState> state_;
std::unique_ptr<timeval> recv_timeout_;
protected: protected:
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len) virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
@@ -111,6 +113,13 @@ public:
* \brief Closes the connection to the socket. * \brief Closes the connection to the socket.
*/ */
void close(); void close();
/*!
* \brief Setup Receive timeout used for this socket.
*
* \param timeout Timeout used for setting things up
*/
void setReceiveTimeout(const timeval& timeout);
}; };
} // namespace comm } // namespace comm
} // namespace ur_driver } // namespace ur_driver

View File

@@ -44,6 +44,11 @@ void TCPSocket::setOptions(int socket_fd)
int flag = 1; int flag = 1;
setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int)); 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_QUICKACK, &flag, sizeof(int));
if (recv_timeout_ != nullptr)
{
setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, recv_timeout_.get(), sizeof(timeval));
}
} }
bool TCPSocket::setup(std::string& host, int port) bool TCPSocket::setup(std::string& host, int port)
@@ -188,5 +193,16 @@ bool TCPSocket::write(const uint8_t* buf, const size_t buf_len, size_t& written)
return true; return true;
} }
void TCPSocket::setReceiveTimeout(const timeval& timeout)
{
recv_timeout_.reset(new timeval(timeout));
if (state_ == SocketState::Connected)
{
setOptions(socket_fd_);
}
}
} // namespace comm } // namespace comm
} // namespace ur_driver } // namespace ur_driver