diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-10-10 15:04:28 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-10-10 15:04:28 +0200 |
commit | fcab8098d6a9a385e0e5edfb26f4abf615da77ca (patch) | |
tree | 7ae3ff0d47bab59a409ccebb036e339894493b80 /src/Net/FdManager.cpp | |
parent | 535a6e799ee98e745c85c655c5db3279fd25c1bc (diff) | |
download | mad-fcab8098d6a9a385e0e5edfb26f4abf615da77ca.tar mad-fcab8098d6a9a385e0e5edfb26f4abf615da77ca.zip |
FdManager hinzugef?gt und Verbindungsklassen angepasst
Diffstat (limited to 'src/Net/FdManager.cpp')
-rw-r--r-- | src/Net/FdManager.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/Net/FdManager.cpp b/src/Net/FdManager.cpp new file mode 100644 index 0000000..aab634d --- /dev/null +++ b/src/Net/FdManager.cpp @@ -0,0 +1,102 @@ +/* + * 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 <signal.h> + + +namespace Mad { +namespace Net { + +FdManager FdManager::fdManager; + + +FdManager::FdManager() { + // TODO Auto-generated constructor stub + +} + +FdManager::~FdManager() { + // TODO Auto-generated destructor stub +} + + +bool FdManager::registerFd(int fd, const sigc::slot<void, short> &handler, short events) { + struct pollfd pollfd = {fd, events, 0}; + + pollfds.insert(std::make_pair(fd, pollfd)); + + return handlers.insert(std::make_pair(fd, handler)).second; +} + +bool FdManager::unregisterFd(int fd) { + pollfds.erase(fd); + return handlers.erase(fd); +} + +bool FdManager::setFdEvents(int fd, short events) { + 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) const { + std::map<int, struct pollfd>::const_iterator pollfd = pollfds.find(fd); + + if(pollfd == pollfds.end()) + return -1; + + return pollfd->second.events; +} + +void FdManager::interrupt() { + // TODO Implement this. +} + +void FdManager::run() { + 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; + } + + if(poll(fdarray, count, -1) > 0) { + for(size_t n = 0; n < count; ++n) { + if(fdarray[n].revents) + handlers[fdarray[n].fd](fdarray[n].revents); + } + } + + delete [] fdarray; +} + +} +} |