/*----------------------------------------------------------------------------- * pollerpoll.cpp * Copyright (C) 2003, 2004, 2005 Akito Nozaki * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #include #include #include #include NAMESPACE_TANOSHI { int PollerPoll::init() { d = new PollerPollPrivate; d->_cur_pfdnum = -1; d->_pfds_used = 0; d->_pfds_alloc = 16; d->_pfds = new pollfd[d->_pfds_alloc]; Poller::init(); return 0; } void PollerPoll::shutdown() { delete [] d->_pfds; d->_pfds = 0; delete d; } PollerEvent *PollerPoll::add(int fd, short eventmask) { _resize(); d->client[fd].pfd = d->_pfds_used; d->_pfds[d->_pfds_used].fd = fd; d->_pfds[d->_pfds_used].events = eventmask; d->_pfds_used++; d->_pfds[d->_pfds_used].fd = -1; return &d->client[fd]; } int PollerPoll::del(int fd) { if(fd <= 0 || d->client.find(fd) == d->client.end()) return ENOENT; d->client[d->_pfds[d->_pfds_used - 1].fd].pfd = d->client[fd].pfd; d->_pfds[d->client[fd].pfd] = d->_pfds[d->_pfds_used - 1]; d->_pfds[d->_pfds_used - 1].fd = -1; d->client.erase(fd); d->_pfds_used--; return 0; } int PollerPoll::getMask(int fd) { if(fd <= 0 || d->client.find(fd) == d->client.end()) return ENOENT; return d->_pfds[d->client[fd].pfd].events; } int PollerPoll::setMask(int fd, short eventmask) { if(fd <= 0 || d->client.find(fd) == d->client.end()) return ENOENT; d->_pfds[d->client[fd].pfd].events = eventmask; return 0; } int PollerPoll::waitForEvents(int timeout_millisec) { d->_rfds = poll(d->_pfds, d->_pfds_used, timeout_millisec); if (d->_rfds == -1) { d->_cur_pfdnum = -1; return errno; } d->_cur_pfdnum = d->_pfds_used; return d->_rfds ? 0 : EWOULDBLOCK; } int PollerPoll::getNextEvent(PollerEvent *e) { if(d->_rfds < 1) return EWOULDBLOCK; if(d->_cur_pfdnum > d->_pfds_used) d->_cur_pfdnum = d->_pfds_used; while(--d->_cur_pfdnum > -1) { if(d->_pfds[d->_cur_pfdnum].revents) break; } if(d->_cur_pfdnum == -1) return EWOULDBLOCK; if(d->_pfds[d->_cur_pfdnum].fd == -1) return EWOULDBLOCK; e->fd = d->_pfds[d->_cur_pfdnum].fd; e->revents = d->_pfds[d->_cur_pfdnum].revents; d->_rfds--; return 0; } int PollerPoll::waitAndExecEvents(int timeout_millisec) { int err; PollerEvent event; err = waitForEvents(timeout_millisec); if(err && err != EWOULDBLOCK) return err; while(d->_rfds > 0) { err = getNextEvent(&event); if(err) break; d->client[event.fd].eventReady(event.fd, event.revents); } return 0; } int PollerPoll::_resize() { if(d->_pfds_used + 2 >= d->_pfds_alloc) { int n = d->_pfds_alloc + 10; struct pollfd *fds = new pollfd[n]; for(int i = 0; i < d->_pfds_alloc; i++) { fds[i] = d->_pfds[i]; } delete [] d->_pfds; d->_pfds = fds; d->_pfds_alloc = n; } return 0; } }