summaryrefslogtreecommitdiffstats
path: root/src/Net/FdManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/FdManager.cpp')
-rw-r--r--src/Net/FdManager.cpp174
1 files changed, 0 insertions, 174 deletions
diff --git a/src/Net/FdManager.cpp b/src/Net/FdManager.cpp
deleted file mode 100644
index d8faef4..0000000
--- a/src/Net/FdManager.cpp
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * FdManager.cpp
- *
- * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
- *
- * This program is free software: you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "FdManager.h"
-#include "ThreadManager.h"
-
-#include <signal.h>
-#include <unistd.h>
-#include <sys/fcntl.h>
-
-
-namespace Mad {
-namespace Net {
-
-FdManager FdManager::fdManager;
-
-
-FdManager::FdManager() : running(false) {
- pipe(interruptPipe);
-
- int flags = fcntl(interruptPipe[0], F_GETFL, 0);
- fcntl(interruptPipe[0], F_SETFL, flags | O_NONBLOCK);
-
- flags = fcntl(interruptPipe[1], F_GETFL, 0);
- fcntl(interruptPipe[1], F_SETFL, flags | O_NONBLOCK);
-
- registerFd(interruptPipe[0], boost::bind(&FdManager::readInterrupt, this), POLLIN);
-}
-
-FdManager::~FdManager() {
- unregisterFd(interruptPipe[0]);
-
- close(interruptPipe[0]);
- close(interruptPipe[1]);
-}
-
-
-bool FdManager::registerFd(int fd, const boost::function1<void, short> &handler, short events) {
- struct pollfd pollfd = {fd, events, 0};
-
- boost::lock(handlerLock, eventLock);
- pollfds.insert(std::make_pair(fd, pollfd));
-
- bool ret = handlers.insert(std::make_pair(fd, handler)).second;
-
- eventLock.unlock();
- handlerLock.unlock();
-
- interrupt();
-
- return ret;
-}
-
-bool FdManager::unregisterFd(int fd) {
- boost::lock(handlerLock, eventLock);
- pollfds.erase(fd);
- bool ret = handlers.erase(fd);
- eventLock.unlock();
- handlerLock.unlock();
-
- interrupt();
-
- return ret;
-}
-
-bool FdManager::setFdEvents(int fd, short events) {
- boost::unique_lock<boost::shared_mutex> lock(eventLock);
-
- std::map<int, struct pollfd>::iterator pollfd = pollfds.find(fd);
-
- if(pollfd == pollfds.end())
- return false;
-
- if(pollfd->second.events != events) {
- pollfd->second.events = events;
- interrupt();
- }
-
- return true;
-}
-
-short FdManager::getFdEvents(int fd) {
- boost::shared_lock<boost::shared_mutex> lock(eventLock);
-
- std::map<int, struct pollfd>::const_iterator pollfd = pollfds.find(fd);
-
- if(pollfd == pollfds.end())
- return -1;
-
- return pollfd->second.events;
-}
-
-void FdManager::readInterrupt() {
- char buf[20];
-
- while(read(interruptPipe[0], buf, sizeof(buf)) > 0) {}
-}
-
-void FdManager::interrupt() {
- char buf = 0;
-
- write(interruptPipe[1], &buf, sizeof(buf));
-}
-
-void FdManager::ioThread() {
- runLock.lock();
- running = true;
- runLock.unlock_and_lock_shared();
-
- while(running) {
- runLock.unlock_shared();
-
- handlerLock.lock_shared();
- eventLock.lock_shared();
- readInterrupt();
-
- size_t count = pollfds.size();
- struct pollfd *fdarray = new struct pollfd[count];
-
- std::map<int, struct pollfd>::iterator pollfd = pollfds.begin();
-
- for(size_t n = 0; n < count; ++n) {
- fdarray[n] = pollfd->second;
- ++pollfd;
- }
-
- eventLock.unlock_shared();
- handlerLock.unlock_shared();
-
- if(poll(fdarray, count, -1) > 0) {
- handlerLock.lock_shared();
-
- std::queue<boost::function0<void> > calls;
-
- for(size_t n = 0; n < count; ++n) {
- if(fdarray[n].revents)
- calls.push(boost::bind(handlers[fdarray[n].fd], fdarray[n].revents));
- }
-
- handlerLock.unlock_shared();
-
- while(!calls.empty()) {
- calls.front()();
- calls.pop();
- }
-
- }
-
- delete [] fdarray;
-
- runLock.lock_shared();
- }
-
- runLock.unlock_shared();
-}
-
-}
-}