diff --git a/socket.cpp b/socket.cpp index ffddddf..6470f15 100644 --- a/socket.cpp +++ b/socket.cpp @@ -1,12 +1,12 @@ #include +#include #include #include +#include #include #include -#include -#include #include #include #include @@ -31,6 +31,49 @@ static bool configureTcpSocket(int sockId) { +Poll::Poll() { } +Poll::~Poll() { } + +bool Poll::wait(Time duration) { + const size_t count = list.size(); + const size_t size = sizeof(struct pollfd) * count; + if (data.size() < size) + data.resize(size); + + int mills = (int)((duration + rand()%1000)/1000); + int res; + + if (size) { + memset(data.data(), 0, size); + struct pollfd *pfdFirst = (struct pollfd *)data.data(); + struct pollfd *pfd = pfdFirst; + for(List::const_iterator i = list.begin(); i != list.end(); ++i, ++pfd) { + pfd->fd = i->sockId; + pfd->events = POLLRDHUP; + if (i->wantRead) pfd->events |= POLLIN; + if (i->wantWrite) pfd->events |= POLLOUT; + } + + res = poll(pfdFirst, count, mills); + + if (res > 0) { + struct pollfd *pfd = pfdFirst; + for(List::iterator i = list.begin(); i != list.end(); ++i, ++pfd) { + int e = pfd->revents; + i->canRead = (bool)(e & POLLIN); + i->canWrite = (bool)(e & POLLOUT); + i->closed = (bool)(e & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)); + } + } + } else { + res = poll(NULL, 0, mills); + } + + return res >= 0; +} + + + int Socket::tcpConnect(const Address &address) { int sockId = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockId < 0) return -1; diff --git a/socket.h b/socket.h index 445e077..538e4d6 100644 --- a/socket.h +++ b/socket.h @@ -35,7 +35,7 @@ public: Poll(); ~Poll(); - bool wait(Time time); + bool wait(Time duration); };