53 #include <sys/epoll.h>
60 #define UNUSED(expr) do { (void)(expr); } while (0)
70 return WSAGetLastError();
80 std::stringstream ostream;
81 ostream <<
"WSA Error: " << err;
82 return ostream.str().c_str();
98 if ( WSAGetLastError() == WSAEWOULDBLOCK ) {
104 if ( ( errno == EAGAIN ) || ( errno == EWOULDBLOCK ) ) {
114 #if defined(HAVE_EPOLL)
115 epfd = ::epoll_create1(0);
118 ROS_ERROR(
"Unable to create epoll watcher: %s", strerror(errno));
130 #if defined(HAVE_EPOLL)
131 struct epoll_event ev;
132 bzero(&ev,
sizeof(ev));
137 if (::epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev))
139 ROS_ERROR(
"Unable to add FD to epoll: %s", strerror(errno));
148 #if defined(HAVE_EPOLL)
149 if (::epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL))
151 ROS_ERROR(
"Unable to remove FD to epoll: %s", strerror(errno));
160 #if defined(HAVE_EPOLL)
161 struct epoll_event ev;
162 bzero(&ev,
sizeof(ev));
166 if (::epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev))
168 ROS_ERROR(
"Unable to modify FD epoll: %s", strerror(errno));
196 fd_set readfds, writefds, exceptfds;
197 struct timeval tv, *ptv;
212 FD_ZERO (&exceptfds);
218 for (rc = -1, max_fd = 0, i = 0; i < nfds; i++) {
219 if (fds[i].fd == INVALID_SOCKET) {
222 if (fds[i].events & (POLLIN | POLLRDNORM)) {
223 FD_SET (fds[i].fd, &readfds);
225 if (fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND)) {
226 FD_SET (fds[i].fd, &writefds);
228 if (fds[i].events & (POLLPRI | POLLRDBAND)) {
229 FD_SET (fds[i].fd, &exceptfds);
231 if (fds[i].fd > max_fd &&
232 (fds[i].events & (POLLIN | POLLOUT | POLLPRI |
233 POLLRDNORM | POLLRDBAND |
234 POLLWRNORM | POLLWRBAND))) {
255 tv.tv_sec = timeout / 1000;
256 tv.tv_usec = (timeout % 1000) * 1000;
260 rc = select (max_fd + 1, &readfds, &writefds, &exceptfds, ptv);
264 ofds.reset(
new std::vector<socket_pollfd>);
269 for (rc = 0, i = 0; i < nfds; i++) {
270 if (fds[i].fd != INVALID_SOCKET) {
273 if (FD_ISSET(fds[i].fd, &readfds)) {
274 int save_errno = errno;
280 ret = recv(fds[i].fd, data, 64, MSG_PEEK);
283 (errno == WSAESHUTDOWN || errno == WSAECONNRESET ||
284 (errno == WSAECONNABORTED) || errno == WSAENETRESET))
287 (errno == ESHUTDOWN || errno == ECONNRESET ||
288 (errno == ECONNABORTED) || errno == ENETRESET))
291 fds[i].revents |= POLLHUP;
293 fds[i].revents |= fds[i].events & (POLLIN | POLLRDNORM);
297 if (FD_ISSET(fds[i].fd, &writefds)) {
298 fds[i].revents |= fds[i].events & (POLLOUT | POLLWRNORM | POLLWRBAND);
301 if (FD_ISSET(fds[i].fd, &exceptfds)) {
302 fds[i].revents |= fds[i].events & (POLLPRI | POLLRDBAND);
305 if (fds[i].revents & ~POLLHUP) {
309 fds[i].revents = POLLNVAL;
311 ofds->push_back(fds[i]);
314 #elif defined (HAVE_EPOLL)
317 struct epoll_event ev[nfds];
320 int fd_cnt = ::epoll_wait(epfd, ev, nfds, timeout);
326 ROS_ERROR(
"Error in epoll_wait! %s", strerror(errno));
332 ofds.reset(
new std::vector<socket_pollfd>);
333 for (
int i = 0; i < fd_cnt; i++)
336 pfd.fd = ev[i].data.fd;
337 pfd.revents = ev[i].events;
338 ofds->push_back(pfd);
346 for (nfds_t i = 0; i < nfds; i++)
352 int result = poll(fds, nfds, timeout);
358 ROS_ERROR(
"Error in poll! %s", strerror(errno));
362 for (nfds_t i = 0; i < nfds; i++)
366 ofds->push_back(fds[i]);
372 #endif // poll_sockets functions
388 struct epoll_event ev[nfds];
389 int fd_cnt = ::epoll_wait(epfd, ev, nfds, 0);
392 if (errno != EINTR) {
393 ROS_ERROR(
"Error in epoll_wait! %s", strerror(errno));
397 }
else if (fd_cnt == 0) {
400 }
else if (ev[0].events & EPOLLERR || ev[0].events & EPOLLHUP) {
403 socklen_t errlen =
sizeof(err);
404 getsockopt(socket, SOL_SOCKET, SO_ERROR, (
void *)&err, &errlen);
409 fd_set wfds, exceptfds;
412 FD_SET(socket, &wfds);
413 FD_SET(socket, &exceptfds);
417 int ret = select(socket + 1, NULL, &wfds, &exceptfds, &tv);
433 ROS_ASSERT(FD_ISSET(socket, &wfds) || FD_ISSET(socket, &exceptfds));
440 ROS_ASSERT((FD_ISSET(socket, &wfds) && !FD_ISSET(socket, &exceptfds)) ||
441 (!FD_ISSET(socket, &wfds) && FD_ISSET(socket, &exceptfds)));
442 if (FD_ISSET(socket, &exceptfds)) {
445 socklen_t errlen =
sizeof(int);
446 if (getsockopt(socket, SOL_SOCKET, SO_ERROR,
reinterpret_cast<char*
>(&errinfo), &errlen) == -1)
466 if (!FD_ISSET(socket, &wfds)) {
473 socklen_t errlen =
sizeof(int);
474 if (getsockopt(socket, SOL_SOCKET, SO_ERROR, &errinfo, &errlen) == -1)
503 u_long non_blocking = 1;
504 if(ioctlsocket( socket, FIONBIO, &non_blocking ) != 0 )
506 return WSAGetLastError();
509 if(fcntl(socket, F_SETFL, O_NONBLOCK) == -1)
524 if(::closesocket(socket) == SOCKET_ERROR ) {
530 if (::close(socket) < 0) {
547 #ifdef WIN32 // use a socket pair
548 signal_pair[0] = INVALID_SOCKET;
549 signal_pair[1] = INVALID_SOCKET;
552 struct sockaddr_in inaddr;
553 struct sockaddr addr;
555 socklen_t addrlen =
sizeof(a.inaddr);
561 listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
562 if (listen_socket == INVALID_SOCKET) {
568 if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (
char*) &reuse, (socklen_t)
sizeof(reuse)) == SOCKET_ERROR ) {
569 ::closesocket(listen_socket);
573 memset(&a, 0,
sizeof(a));
574 a.inaddr.sin_family = AF_INET;
575 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
578 a.inaddr.sin_port = 0;
580 if (bind(listen_socket, &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR) {
581 ::closesocket(listen_socket);
585 if (getsockname(listen_socket, &a.addr, &addrlen) == SOCKET_ERROR) {
586 ::closesocket(listen_socket);
590 if (listen(listen_socket, 1) == SOCKET_ERROR) {
591 ::closesocket(listen_socket);
600 DWORD overlapped_flag = 0;
601 signal_pair[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, overlapped_flag);
602 if (signal_pair[0] == INVALID_SOCKET) {
603 ::closesocket(listen_socket);
604 ::closesocket(signal_pair[0]);
608 if (connect(signal_pair[0], &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR) {
609 ::closesocket(listen_socket);
610 ::closesocket(signal_pair[0]);
616 signal_pair[1] = accept(listen_socket, NULL, NULL);
617 if (signal_pair[1] == INVALID_SOCKET) {
618 ::closesocket(listen_socket);
619 ::closesocket(signal_pair[0]);
620 ::closesocket(signal_pair[1]);
628 ::closesocket(listen_socket);
629 ::closesocket(signal_pair[0]);
630 ::closesocket(signal_pair[1]);
636 ::closesocket(listen_socket);
638 #else // use a pipe pair
643 if(pipe(signal_pair) != 0) {
647 if(fcntl(signal_pair[0], F_SETFL, O_NONBLOCK) == -1) {
651 if(fcntl(signal_pair[1], F_SETFL, O_NONBLOCK) == -1) {
656 #endif // create_pipe