42 #include <boost/bind/bind.hpp>
46 #include <sys/socket.h>
56 , async_connected_(false)
58 , expecting_read_(false)
59 , expecting_write_(false)
86 ROS_ERROR(
"setting socket [%d] as non_blocking failed with error [%d]",
sock_, result);
115 std::stringstream ss;
136 #ifdef ROSCPP_USE_TCP_NODELAY
145 #if defined(POLLRDHUP) // POLLRDHUP is not part of POSIX!
162 if (
header.getValue(
"tcp_nodelay", nodelay) && nodelay ==
"1")
171 int flag = nodelay ? 1 : 0;
172 int result = setsockopt(
sock_, IPPROTO_TCP, TCP_NODELAY, (
char *) &flag,
sizeof(
int));
184 if (setsockopt(
sock_, SOL_SOCKET, SO_KEEPALIVE,
reinterpret_cast<const char*
>(&val),
sizeof(val)) != 0)
190 #if defined(SOL_TCP) && defined(TCP_KEEPIDLE)
192 if (setsockopt(
sock_, SOL_TCP, TCP_KEEPIDLE, &val,
sizeof(val)) != 0)
200 #if defined(SOL_TCP) && defined(TCP_KEEPINTVL)
202 if (setsockopt(
sock_, SOL_TCP, TCP_KEEPINTVL, &val,
sizeof(val)) != 0)
210 #if defined(SOL_TCP) && defined(TCP_KEEPCNT)
212 if (setsockopt(
sock_, SOL_TCP, TCP_KEEPCNT, &val,
sizeof(val)) != 0)
223 if (setsockopt(
sock_, SOL_SOCKET, SO_KEEPALIVE,
reinterpret_cast<const char*
>(&val),
sizeof(val)) != 0)
247 sockaddr_storage sas;
252 if (inet_pton(AF_INET, host.c_str(), &ina) == 1)
254 sockaddr_in *address = (sockaddr_in*) &sas;
255 sas_len =
sizeof(sockaddr_in);
258 address->sin_family = AF_INET;
259 address->sin_port = htons(port);
260 address->sin_addr.s_addr = ina.s_addr;
262 else if (inet_pton(AF_INET6, host.c_str(), &in6a) == 1)
264 sockaddr_in6 *address = (sockaddr_in6*) &sas;
265 sas_len =
sizeof(sockaddr_in6);
266 la_len_ =
sizeof(sockaddr_in6);
267 address->sin6_family = AF_INET6;
268 address->sin6_port = htons(port);
269 memcpy(address->sin6_addr.s6_addr, in6a.s6_addr,
sizeof(in6a.s6_addr));
273 struct addrinfo* addr;
274 struct addrinfo hints;
275 memset(&hints, 0,
sizeof(hints));
276 hints.ai_family = AF_UNSPEC;
278 if (getaddrinfo(host.c_str(), NULL, &hints, &addr) != 0)
281 ROS_ERROR(
"couldn't resolve publisher host [%s]", host.c_str());
286 struct addrinfo* it = addr;
287 char namebuf[128] = {};
288 for (; it; it = it->ai_next)
292 sockaddr_in *address = (sockaddr_in*) &sas;
293 sas_len =
sizeof(*address);
295 memcpy(address, it->ai_addr, it->ai_addrlen);
296 address->sin_family = it->ai_family;
297 address->sin_port = htons(port);
299 strncpy(namebuf, inet_ntoa(address->sin_addr),
sizeof(namebuf)-1);
305 sockaddr_in6 *address = (sockaddr_in6*) &sas;
306 sas_len =
sizeof(*address);
308 memcpy(address, it->ai_addr, it->ai_addrlen);
309 address->sin6_family = it->ai_family;
310 address->sin6_port = htons((u_short) port);
313 inet_ntop(AF_INET6, (
void*)&(address->sin6_addr), namebuf,
sizeof(namebuf));
323 ROS_ERROR(
"Couldn't resolve an address for [%s]\n", host.c_str());
327 ROSCPP_LOG_DEBUG(
"Resolved publisher host [%s] to [%s] for socket [%d]", host.c_str(), namebuf,
sock_);
351 std::stringstream ss;
352 ss << host <<
":" << port <<
" on socket " <<
sock_;
379 sock_ = socket(AF_INET6, SOCK_STREAM, 0);
381 address->sin6_family = AF_INET6;
385 address->sin6_port = htons(port);
386 sa_len_ =
sizeof(sockaddr_in6);
390 sock_ = socket(AF_INET, SOCK_STREAM, 0);
392 address->sin_family = AF_INET;
394 htonl(INADDR_LOOPBACK) :
396 address->sin_port = htons(port);
481 disconnect_cb(shared_from_this());
497 }
else if (ret == -1) {
516 uint32_t read_size = std::min(size,
static_cast<uint32_t
>(INT_MAX));
517 int num_bytes = ::recv(
sock_,
reinterpret_cast<char*
>(buffer), read_size, 0);
530 else if (num_bytes == 0)
552 }
else if (ret == -1) {
571 uint32_t writesize = std::min(size,
static_cast<uint32_t
>(INT_MAX));
572 int num_bytes = ::send(
sock_,
reinterpret_cast<const char*
>(buffer), writesize, 0);
673 sockaddr client_address;
674 socklen_t len =
sizeof(client_address);
675 int new_sock =
::accept(
sock_, (sockaddr *)&client_address, &len);
681 if (!transport->setSocket(new_sock))
683 ROS_ERROR(
"Failed to set socket on transport for socket %d", new_sock);
737 if((events & POLLERR) ||
738 (events & POLLHUP) ||
739 #
if defined(POLLRDHUP)
740 (events & POLLRDHUP) ||
745 socklen_t len =
sizeof(error);
746 if (getsockopt(
sock_, SOL_SOCKET, SO_ERROR,
reinterpret_cast<char*
>(&error), &len) < 0)
752 strerror_s(err,60,error);
755 ROSCPP_LOG_DEBUG(
"Socket %d closed with (ERR|HUP|NVAL) events %d: %s",
sock_, events, strerror(error));
764 std::stringstream str;
773 sockaddr_storage sas;
774 socklen_t sas_len =
sizeof(sas);
775 getpeername(
sock_, (sockaddr *)&sas, &sas_len);
777 sockaddr_in *sin = (sockaddr_in *)&sas;
778 sockaddr_in6 *sin6 = (sockaddr_in6 *)&sas;
780 char namebuf[128] = {};
783 switch (sas.ss_family)
786 port = ntohs(sin->sin_port);
787 strncpy(namebuf, inet_ntoa(sin->sin_addr),
sizeof(namebuf)-1);
790 port = ntohs(sin6->sin6_port);
791 inet_ntop(AF_INET6, (
void*)&(sin6->sin6_addr), namebuf,
sizeof(namebuf));
799 std::string ip = namebuf;
800 std::stringstream uri;
801 uri << ip <<
":" << port;