diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-06-27 03:19:56 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-06-27 03:19:56 +0200 |
commit | f5377412a89b2a7f422decd771dc8de49a5498fc (patch) | |
tree | bda661debda3c324189240f3a68cca7a9f4e316b /src/Core | |
parent | ea9fe3ef923000a7bfa4d7afc306669d5442e0fc (diff) | |
download | mad-f5377412a89b2a7f422decd771dc8de49a5498fc.tar mad-f5377412a89b2a7f422decd771dc8de49a5498fc.zip |
Einfaches Polling implementiert
Diffstat (limited to 'src/Core')
-rw-r--r-- | src/Core/ConnectionManager.cpp | 39 | ||||
-rw-r--r-- | src/Core/ConnectionManager.h | 9 |
2 files changed, 37 insertions, 11 deletions
diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp index 3b99875..fe9f639 100644 --- a/src/Core/ConnectionManager.cpp +++ b/src/Core/ConnectionManager.cpp @@ -29,21 +29,48 @@ namespace Mad { namespace Core { void ConnectionManager::refreshPollfds() { + // TODO: refreshPollfds() + pollfds.clear(); + + for(std::list<Net::Listener*>::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) { + std::vector<struct pollfd> fds = (*listener)->getPollfds(); + pollfds.insert(pollfds.end(), fds.begin(), fds.end()); + } + + for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) + pollfds.push_back((*con)->getPollfd()); + + for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) + pollfds.push_back((*con)->getPollfd()); } -void ConnectionManager::daemonReceiveHandler(const Net::Connection*, const Net::Packet &packet) const { +void ConnectionManager::daemonReceiveHandler(const Net::Connection *connection, const Net::Packet &packet) { std::cout << "Received daemon packet:" << std::endl; std::cout << " Type: " << packet.getType() << std::endl; std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl; std::cout << " Length: " << packet.getLength() << std::endl; + + for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) { + if(*con == connection) { + (*con)->disconnect(); + break; + } + } } -void ConnectionManager::clientReceiveHandler(const Net::Connection*, const Net::Packet &packet) const { +void ConnectionManager::clientReceiveHandler(const Net::Connection *connection, const Net::Packet &packet) { std::cout << "Received client packet:" << std::endl; std::cout << " Type: " << packet.getType() << std::endl; std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl; std::cout << " Length: " << packet.getLength() << std::endl; + + for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) { + if(*con == connection) { + (*con)->disconnect(); + break; + } + } } ConnectionManager::ConnectionManager() { @@ -64,12 +91,6 @@ ConnectionManager::~ConnectionManager() { delete *con; } -void ConnectionManager::wait(int timeout) { - // TODO: wait() - - usleep(timeout); -} - void ConnectionManager::run() { // TODO: Logging @@ -109,6 +130,8 @@ void ConnectionManager::run() { } } } + + refreshPollfds(); } } diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h index 49c9398..b68919c 100644 --- a/src/Core/ConnectionManager.h +++ b/src/Core/ConnectionManager.h @@ -50,14 +50,17 @@ class ConnectionManager { void refreshPollfds(); - void daemonReceiveHandler(const Net::Connection*, const Net::Packet &packet) const; - void clientReceiveHandler(const Net::Connection*, const Net::Packet &packet) const; + void daemonReceiveHandler(const Net::Connection *connection, const Net::Packet &packet); + void clientReceiveHandler(const Net::Connection *connection, const Net::Packet &packet); public: ConnectionManager(); virtual ~ConnectionManager(); - void wait(int timeout); + void wait(int timeout) { + poll(pollfds.data(), pollfds.size(), timeout); + } + void run(); }; |