6#ifndef RABIT_INTERNAL_SOCKET_H_
7#define RABIT_INTERNAL_SOCKET_H_
8#include "xgboost/collective/socket.h"
19#include <netinet/in.h>
21#include <sys/socket.h>
31#include <unordered_map>
41using sock_size_t = size_t;
44#define IS_MINGW() defined(__MINGW32__)
46#if IS_MINGW() && !defined(POLLRDNORM) && !defined(POLLRDBAND)
61#pragma message("Distributed training on mingw is not supported.")
62typedef struct pollfd {
66} WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;
69#define POLLIN (0x0100 | 0x0200)
79template <
typename PollFD>
80int PollImpl(PollFD *pfd,
int nfds, std::chrono::seconds timeout) {
84 xgboost::MingWError();
87 return WSAPoll(pfd, nfds, std::chrono::milliseconds(timeout).count());
91 return poll(pfd, nfds, std::chrono::milliseconds(timeout).count());
105 pfd.events |= POLLIN;
116 pfd.events |= POLLOUT;
129 pfd.events |= POLLPRI;
139 const auto& pfd = fds.find(fd);
140 return pfd != fds.end() && ((pfd->second.events & POLLIN) != 0);
151 const auto& pfd = fds.find(fd);
152 return pfd != fds.end() && ((pfd->second.events & POLLOUT) != 0);
162 inline void Poll(std::chrono::seconds timeout) {
163 std::vector<pollfd> fdset;
164 fdset.reserve(fds.size());
165 for (
auto kv : fds) {
166 fdset.push_back(kv.second);
168 int ret = PollImpl(fdset.data(), fdset.size(), timeout);
170 LOG(FATAL) <<
"Poll timeout";
171 }
else if (ret < 0) {
172 LOG(FATAL) <<
"Failed to poll.";
174 for (
auto& pfd : fdset) {
175 auto revents = pfd.revents & pfd.events;
179 fds[pfd.fd].events = revents;
185 std::unordered_map<SOCKET, pollfd> fds;
190#if IS_MINGW() && !defined(POLLRDNORM) && !defined(POLLRDBAND)
TCP socket for simple communication.
Definition socket.h:249
HandleT const & Handle() const
Return the native socket file descriptor.
Definition socket.h:397
namespace of rabit
Definition engine.h:18
helper data structure to perform poll
Definition socket.h:96
void WatchException(SOCKET fd)
add file descriptor to watch for exception
Definition socket.h:126
bool CheckRead(SOCKET fd) const
Check if the descriptor is ready for read.
Definition socket.h:138
bool CheckWrite(SOCKET fd) const
Check if the descriptor is ready for write.
Definition socket.h:150
void WatchWrite(SOCKET fd)
add file descriptor to watch for write
Definition socket.h:113
void Poll(std::chrono::seconds timeout)
perform poll on the set defined, read, write, exception
Definition socket.h:162
void WatchRead(SOCKET fd)
add file descriptor to watch for read
Definition socket.h:102
simple utils to support the code