20 # include <sys/socket.h>
33 TcpConnector::TcpConnector() =
default;
35 TcpConnector::~TcpConnector() =
default;
53 if (open(new_stream) == -1) {
59 servAddr.sin_addr.s_addr = INADDR_ANY;
60 servAddr.sin_family = AF_INET;
61 servAddr.sin_port = htons(address.
getPort());
62 memset(servAddr.sin_zero,
'\0',
sizeof servAddr.sin_zero);
64 struct hostent* hostInfo = yarp::os::impl::gethostbyname(address.
getHost().c_str());
66 bcopy(hostInfo->h_addr,
reinterpret_cast<char*
>(&servAddr.sin_addr), hostInfo->h_length);
68 inet_pton(AF_INET, address.
getHost().c_str(), &servAddr.sin_addr);
82 if ((arg = fcntl(handle, F_GETFL, NULL)) < 0) {
83 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_GETFL): %d, %s", errno, strerror(errno));
87 if (fcntl(handle, F_SETFL, arg) < 0) {
88 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_SETFL): %d, %s", errno, strerror(errno));
92 res = ::connect(handle,
reinterpret_cast<sockaddr*
>(&servAddr),
sizeof(servAddr));
95 if (errno == EINPROGRESS) {
97 FD_SET(handle, &myset);
98 res = select(handle + 1,
nullptr, &myset,
nullptr, timeout);
99 if (res < 0 && errno != EINTR) {
100 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error connecting: %d, %s", errno, strerror(errno));
102 }
else if (res > 0) {
106 if (getsockopt(handle, SOL_SOCKET, SO_ERROR,
reinterpret_cast<void*
>(&valopt), &lon) < 0) {
107 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error in getsockopt(): %d, %s", errno, strerror(errno));
116 yCError(TCPCONNECTOR_POSIX,
"connect fail: Timeout in select() - Cancelling!: %d, %s", errno, strerror(errno));
120 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error connecting: %d, %s", errno, strerror(errno));
126 char buf[INET_ADDRSTRLEN];
128 "Connect [handle=%d] at %s:%d",
130 inet_ntop(AF_INET, &servAddr.sin_addr, buf, INET_ADDRSTRLEN),
136 if ((arg = fcntl(handle, F_GETFL,
nullptr)) < 0) {
137 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_GETFL): %d, %s", errno, strerror(errno));
140 arg &= (~O_NONBLOCK);
141 if (fcntl(handle, F_SETFL, arg) < 0) {
142 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_SETFL): %d, %s", errno, strerror(errno));
#define yCError(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
struct timeval YARP_timeval
An interface to the operating system, including Port based communication.