diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-06-09 19:01:02 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-06-09 19:01:02 +0200 |
commit | 766c56a693e8b1bd4293459bb256abdc0515a0b5 (patch) | |
tree | da8e51003cf801344e22b0b2b2e28a48d6a8b74c /src/Net | |
parent | 452320b5ec31447a526735016fa07589cb848032 (diff) | |
download | mad-766c56a693e8b1bd4293459bb256abdc0515a0b5.tar mad-766c56a693e8b1bd4293459bb256abdc0515a0b5.zip |
Teile der Namespaces Common und Net in den neuen Namespace Core verschoben
Diffstat (limited to 'src/Net')
-rw-r--r-- | src/Net/CMakeLists.txt | 13 | ||||
-rw-r--r-- | src/Net/ClientConnection.cpp | 4 | ||||
-rw-r--r-- | src/Net/ClientConnection.h | 6 | ||||
-rw-r--r-- | src/Net/Connection.cpp | 12 | ||||
-rw-r--r-- | src/Net/Connection.h | 25 | ||||
-rw-r--r-- | src/Net/Exception.cpp | 60 | ||||
-rw-r--r-- | src/Net/Exception.h | 65 | ||||
-rw-r--r-- | src/Net/Listener.cpp | 20 | ||||
-rw-r--r-- | src/Net/Listener.h | 12 | ||||
-rw-r--r-- | src/Net/Signals/Connection.h | 53 | ||||
-rw-r--r-- | src/Net/Signals/GenericSignal.h | 61 | ||||
-rw-r--r-- | src/Net/Signals/Signal0.h | 46 | ||||
-rw-r--r-- | src/Net/Signals/Signal1.h | 47 | ||||
-rw-r--r-- | src/Net/Signals/Signal2.h | 47 | ||||
-rw-r--r-- | src/Net/Signals/SignalBase.h | 53 | ||||
-rw-r--r-- | src/Net/Signals/Signals.h | 27 | ||||
-rw-r--r-- | src/Net/ThreadManager.cpp | 146 | ||||
-rw-r--r-- | src/Net/ThreadManager.h | 107 |
18 files changed, 39 insertions, 765 deletions
diff --git a/src/Net/CMakeLists.txt b/src/Net/CMakeLists.txt index 6bf700e..298aebe 100644 --- a/src/Net/CMakeLists.txt +++ b/src/Net/CMakeLists.txt @@ -1,20 +1,9 @@ include_directories(${INCLUDES}) -link_directories(${Boost_LIBRARY_DIRS}) add_library(Net - Signals/Connection.h - Signals/GenericSignal.h - Signals/Signal0.h - Signals/Signal1.h - Signals/Signal2.h - Signals/SignalBase.h - Signals/Signals.h - ClientConnection.cpp ClientConnection.h Connection.cpp Connection.h - Exception.cpp Exception.h Listener.cpp Listener.h Packet.cpp Packet.h - ThreadManager.cpp ThreadManager.h ) -target_link_libraries(Net ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES}) +target_link_libraries(Net Core ${OPENSSL_LIBRARIES}) diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 07a9121..9196d89 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -19,8 +19,6 @@ #include "ClientConnection.h" -#include <Common/Logger.h> - namespace Mad { namespace Net { @@ -36,7 +34,7 @@ void ClientConnection::handleConnect(const boost::system::error_code& error) { socket.async_handshake(boost::asio::ssl::stream_base::client, boost::bind(&ClientConnection::handleHandshake, this, boost::asio::placeholders::error)); } -void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Exception) { +void ClientConnection::connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception) { boost::lock_guard<boost::shared_mutex> lock(connectionLock); if(_isConnected()) { diff --git a/src/Net/ClientConnection.h b/src/Net/ClientConnection.h index f93c2fc..dcb6906 100644 --- a/src/Net/ClientConnection.h +++ b/src/Net/ClientConnection.h @@ -21,7 +21,7 @@ #define MAD_NET_CLIENTCONNECTION_H_ #include "Connection.h" -#include "Exception.h" +#include <Core/Exception.h> #include <boost/utility/base_from_member.hpp> @@ -37,13 +37,13 @@ class ClientConnection : private boost::base_from_member<boost::asio::ssl::conte public: ClientConnection() - : boost::base_from_member<boost::asio::ssl::context>(boost::ref(Connection::ioService), boost::asio::ssl::context::sslv23), + : boost::base_from_member<boost::asio::ssl::context>(boost::ref(Core::ThreadManager::get()->getIOService()), boost::asio::ssl::context::sslv23), Connection(member) { member.set_verify_mode(boost::asio::ssl::context::verify_none); } - void connect(const boost::asio::ip::tcp::endpoint &address) throw(Exception); + void connect(const boost::asio::ip::tcp::endpoint &address) throw(Core::Exception); }; } diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index d9ff994..81f4c64 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -19,7 +19,7 @@ #include "Connection.h" -#include <Common/Logger.h> +#include <Core/Logger.h> #include <cstring> #include <boost/bind.hpp> @@ -27,8 +27,6 @@ namespace Mad { namespace Net { -boost::asio::io_service Connection::ioService; - Connection::~Connection() { if(_isConnected()) @@ -37,7 +35,7 @@ Connection::~Connection() { void Connection::handleHandshake(const boost::system::error_code& error) { if(error) { - Common::Logger::logf("Error: %s", error.message().c_str()); + Core::Logger::logf("Error: %s", error.message().c_str()); // TODO Error handling doDisconnect(); @@ -63,7 +61,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) { boost::lock_guard<boost::shared_mutex> lock(connectionLock); if(error) { - Common::Logger::logf(Common::Logger::VERBOSE, "Shutdown error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Shutdown error: %s", error.message().c_str()); } _setState(DISCONNECTED); @@ -112,7 +110,7 @@ void Connection::handleDataReceive(const std::vector<boost::uint8_t> &data) { void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1<void, const std::vector<boost::uint8_t>& > ¬ify) { if(error || (bytes_transferred+received) < length) { - Common::Logger::logf(Common::Logger::VERBOSE, "Read error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Read error: %s", error.message().c_str()); // TODO Error doDisconnect(); @@ -185,7 +183,7 @@ void Connection::handleWrite(const boost::system::error_code& error, std::size_t } if(error) { - Common::Logger::logf(Common::Logger::VERBOSE, "Write error: %s", error.message().c_str()); + Core::Logger::logf(Core::Logger::VERBOSE, "Write error: %s", error.message().c_str()); // TODO Error doDisconnect(); diff --git a/src/Net/Connection.h b/src/Net/Connection.h index e1ca63b..6323164 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -23,7 +23,8 @@ #include <config.h> #include "Packet.h" -#include "Signals/Signals.h" +#include <Core/Signals.h> +#include <Core/ThreadManager.h> #include <boost/asio.hpp> #include <boost/asio/ssl.hpp> @@ -70,9 +71,9 @@ class Connection : boost::noncopyable { Packet::Data header; - Signals::Signal1<boost::shared_ptr<Packet> > receiveSignal; - Signals::Signal0 connectedSignal; - Signals::Signal0 disconnectedSignal; + Core::Signals::Signal1<boost::shared_ptr<Packet> > receiveSignal; + Core::Signals::Signal0 connectedSignal; + Core::Signals::Signal0 disconnectedSignal; bool receiving; unsigned long sending; @@ -91,8 +92,6 @@ class Connection : boost::noncopyable { void rawSend(const uint8_t *data, std::size_t length); protected: - static boost::asio::io_service ioService; - boost::shared_mutex connectionLock; boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket; @@ -117,7 +116,7 @@ class Connection : boost::noncopyable { void doDisconnect(); Connection(boost::asio::ssl::context &sslContext) : - state(DISCONNECTED), receiveBuffer(1024*1024), socket(ioService, sslContext) {} + state(DISCONNECTED), receiveBuffer(1024*1024), socket(Core::ThreadManager::get()->getIOService(), sslContext) {} public: virtual ~Connection(); @@ -171,24 +170,24 @@ class Connection : boost::noncopyable { bool send(const Packet &packet); - Signals::Connection connectSignalReceive(const Signals::Signal1<boost::shared_ptr<Packet> >::slot_type &slot) { + Core::Signals::Connection connectSignalReceive(const Core::Signals::Signal1<boost::shared_ptr<Packet> >::slot_type &slot) { return receiveSignal.connect(slot); } - void disconnectSignalReceive(const Signals::Connection &connection) { + void disconnectSignalReceive(const Core::Signals::Connection &connection) { receiveSignal.disconnect(connection); } - Signals::Connection connectSignalConnected(const Signals::Signal0::slot_type &slot) { + Core::Signals::Connection connectSignalConnected(const Core::Signals::Signal0::slot_type &slot) { return connectedSignal.connect(slot); } - void disconnectSignalConnected(const Signals::Connection &connection) { + void disconnectSignalConnected(const Core::Signals::Connection &connection) { connectedSignal.disconnect(connection); } - Signals::Connection connectSignalDisconnected(const Signals::Signal0::slot_type &slot) { + Core::Signals::Connection connectSignalDisconnected(const Core::Signals::Signal0::slot_type &slot) { return disconnectedSignal.connect(slot); } - void disconnectSignalDisconnected(const Signals::Connection &connection) { + void disconnectSignalDisconnected(const Core::Signals::Connection &connection) { disconnectedSignal.disconnect(connection); } }; diff --git a/src/Net/Exception.cpp b/src/Net/Exception.cpp deleted file mode 100644 index e082948..0000000 --- a/src/Net/Exception.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Exception.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 "Exception.h" - -#include <cstring> - -namespace Mad { -namespace Net { - -std::string Exception::strerror() const { - std::string ret; - - if(!where.empty()) - ret = where + ": "; - - switch(errorCode) { - case SUCCESS: - return ret + "Success"; - case UNEXPECTED_PACKET: - return ret + "An unexpected packet was received"; - case INVALID_ACTION: - return ret + "The action is invalid"; - case NOT_AVAILABLE: - return ret + "Not available"; - case NOT_FINISHED: - return ret + "Not finished"; - case NOT_IMPLEMENTED: - return ret + "Not implemented"; - case INTERNAL_ERRNO: - return ret + std::strerror(subCode); - case INVALID_ADDRESS: - return ret + "Invalid address"; - case ALREADY_IDENTIFIED: - return ret + "The host is already identified"; - case UNKNOWN_DAEMON: - return ret + "The daemon is unknown"; - default: - return ret + "Unknown error"; - } -} - -} -} diff --git a/src/Net/Exception.h b/src/Net/Exception.h deleted file mode 100644 index 1ddf5f4..0000000 --- a/src/Net/Exception.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Exception.h - * - * 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/>. - */ - -#ifndef MAD_NET_EXCEPTION_H_ -#define MAD_NET_EXCEPTION_H_ - -#include <string> - -namespace Mad { -namespace Net { - -class Exception { - public: - enum ErrorCode { - SUCCESS = 0x0000, UNEXPECTED_PACKET = 0x0001, INVALID_ACTION = 0x0002, NOT_AVAILABLE = 0x0003, NOT_FINISHED = 0x0004, NOT_IMPLEMENTED = 0x0005, - INTERNAL_ERRNO = 0x0010, - INVALID_ADDRESS = 0x0020, - ALREADY_IDENTIFIED = 0x0030, UNKNOWN_DAEMON = 0x0031 - }; - - private: - std::string where; - - ErrorCode errorCode; - long subCode; - long subSubCode; - - public: - Exception(const std::string &where0, ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) - : where(where0), errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - Exception(ErrorCode errorCode0 = SUCCESS, long subCode0 = 0, long subSubCode0 = 0) : errorCode(errorCode0), subCode(subCode0), subSubCode(subSubCode0) {} - virtual ~Exception() {} - - const std::string& getWhere() const {return where;} - ErrorCode getErrorCode() const {return errorCode;} - long getSubCode() const {return subCode;} - long getSubSubCode() const {return subSubCode;} - - std::string strerror() const; - - operator bool() const { - return (errorCode != SUCCESS); - } -}; - -} -} - -#endif /* MAD_NET_EXCEPTION_H_ */ diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index 6da2762..780e862 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -19,7 +19,8 @@ #include "Listener.h" -#include <Common/Logger.h> +#include <Core/Logger.h> +#include <Core/ThreadManager.h> #include <cerrno> #include <cstring> @@ -39,8 +40,8 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share con->_setState(Connection::CONNECT); - Signals::Connection con1 = con->connectSignalConnected(boost::bind(&Listener::handleConnect, this, con)); - Signals::Connection con2 = con->connectSignalDisconnected(boost::bind(&Listener::handleDisconnect, this, con)); + Core::Signals::Connection con1 = con->connectSignalConnected(boost::bind(&Listener::handleConnect, this, con)); + Core::Signals::Connection con2 = con->connectSignalDisconnected(boost::bind(&Listener::handleDisconnect, this, con)); connections.insert(std::make_pair(con, std::make_pair(con1, con2))); @@ -54,7 +55,7 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share void Listener::handleConnect(boost::shared_ptr<Connection> con) { { boost::lock_guard<boost::mutex> lock(mutex); - std::map<boost::shared_ptr<Connection>, std::pair<Signals::Connection, Signals::Connection> >::iterator it = connections.find(con); + std::map<boost::shared_ptr<Connection>, std::pair<Core::Signals::Connection, Core::Signals::Connection> >::iterator it = connections.find(con); if(it == connections.end()) return; @@ -76,9 +77,9 @@ void Listener::handleDisconnect(boost::shared_ptr<Connection> con) { Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, - const boost::asio::ip::tcp::endpoint &address0) throw(Exception) -: x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0), acceptor(Connection::ioService, address), -sslContext(Connection::ioService, boost::asio::ssl::context::sslv23) + const boost::asio::ip::tcp::endpoint &address0) throw(Core::Exception) +: x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0), acceptor(Core::ThreadManager::get()->getIOService(), address), +sslContext(Core::ThreadManager::get()->getIOService(), boost::asio::ssl::context::sslv23) { sslContext.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 @@ -93,9 +94,10 @@ sslContext(Connection::ioService, boost::asio::ssl::context::sslv23) } Listener::~Listener() { - for(std::map<boost::shared_ptr<Connection>,std::pair<Signals::Connection, Signals::Connection> >::iterator con = connections.begin(); con != connections.end(); ++con) { + for(std::map<boost::shared_ptr<Connection>,std::pair<Core::Signals::Connection, Core::Signals::Connection> >::iterator con = connections.begin(); con != connections.end(); ++con) { con->first->disconnect(); - // TODO wait... + con->first->waitWhileConnected(); + // TODO Optimize } } diff --git a/src/Net/Listener.h b/src/Net/Listener.h index 2e25051..af0888b 100644 --- a/src/Net/Listener.h +++ b/src/Net/Listener.h @@ -24,7 +24,7 @@ #include <string> #include "Connection.h" -#include "Exception.h" +#include <Core/Exception.h> namespace Mad { namespace Net { @@ -38,9 +38,9 @@ class Listener : boost::noncopyable { boost::asio::ip::tcp::acceptor acceptor; boost::asio::ssl::context sslContext; - std::map<boost::shared_ptr<Connection>, std::pair<Signals::Connection, Signals::Connection> > connections; + std::map<boost::shared_ptr<Connection>, std::pair<Core::Signals::Connection, Core::Signals::Connection> > connections; - Signals::Signal1<boost::shared_ptr<Connection> > signal; + Core::Signals::Signal1<boost::shared_ptr<Connection> > signal; void handleAccept(const boost::system::error_code &error, boost::shared_ptr<Connection> con); @@ -49,13 +49,13 @@ class Listener : boost::noncopyable { public: Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, - const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Exception); + const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Core::Exception); virtual ~Listener(); - Signals::Connection connectSignalNewConnection(const Signals::Signal1<boost::shared_ptr<Connection> >::slot_type &slot) { + Core::Signals::Connection connectSignalNewConnection(const Core::Signals::Signal1<boost::shared_ptr<Connection> >::slot_type &slot) { return signal.connect(slot); } - void disconnectSignalNewConnection(const Signals::Connection &connection) { + void disconnectSignalNewConnection(const Core::Signals::Connection &connection) { signal.disconnect(connection); } }; diff --git a/src/Net/Signals/Connection.h b/src/Net/Signals/Connection.h deleted file mode 100644 index 7730e13..0000000 --- a/src/Net/Signals/Connection.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Connection.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_SIGNALS_CONNECTION_H_ -#define MAD_NET_SIGNALS_CONNECTION_H_ - -namespace Mad { -namespace Net { -namespace Signals { - -class SignalBase; - -class Connection { - private: - friend class SignalBase; - - SignalBase *signal; - unsigned long id; - - Connection(SignalBase *signal0, unsigned long id0) - : signal(signal0), id(id0) {} - - public: - bool operator==(const Connection &o) const { - return (signal == o.signal && id == o.id); - } - - bool operator<(const Connection &o) const { - return (signal != o.signal) ? (signal < o.signal) : (id < o.id); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_CONNECTION_H_ */ diff --git a/src/Net/Signals/GenericSignal.h b/src/Net/Signals/GenericSignal.h deleted file mode 100644 index aa6cfa1..0000000 --- a/src/Net/Signals/GenericSignal.h +++ /dev/null @@ -1,61 +0,0 @@ -/* - * GenericSignal.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_SIGNALS_GENERICSIGNAL_H_ -#define MAD_NET_SIGNALS_GENERICSIGNAL_H_ - -#include "SignalBase.h" - -#include <map> -#include <boost/thread/locks.hpp> - -namespace Mad { -namespace Net { -namespace Signals { - -template <typename FunctionType> -class GenericSignal : protected SignalBase { - public: - typedef FunctionType slot_type; - - protected: - std::map<Connection, slot_type> handlers; - - public: - Connection connect(const slot_type &slot) { - boost::lock_guard<boost::mutex> lock(mutex); - - Connection con = getNewConnection(); - handlers.insert(std::make_pair(con, slot)); - - return con; - } - - bool disconnect(const Connection &connection) { - boost::lock_guard<boost::mutex> lock(mutex); - - return handlers.erase(connection); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_GENERICSIGNAL_H_ */ diff --git a/src/Net/Signals/Signal0.h b/src/Net/Signals/Signal0.h deleted file mode 100644 index 5c0a691..0000000 --- a/src/Net/Signals/Signal0.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Signal0.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL0_H_ -#define MAD_NET_SIGNALS_SIGNAL0_H_ - -#include "GenericSignal.h" -#include <Net/ThreadManager.h> - -#include <boost/function.hpp> - -namespace Mad { -namespace Net { -namespace Signals { - -class Signal0 : public GenericSignal<boost::function0<void> > { - public: - void emit() { - boost::lock_guard<boost::mutex> lock(mutex); - - for(std::map<Connection, slot_type>::iterator handler = handlers.begin(); handler != handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(handler->second); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL0_H_ */ diff --git a/src/Net/Signals/Signal1.h b/src/Net/Signals/Signal1.h deleted file mode 100644 index 331c5cb..0000000 --- a/src/Net/Signals/Signal1.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Signal1.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL1_H_ -#define MAD_NET_SIGNALS_SIGNAL1_H_ - -#include "GenericSignal.h" -#include <Net/ThreadManager.h> - -#include <boost/function.hpp> - -namespace Mad { -namespace Net { -namespace Signals { - -template <typename T1> -class Signal1 : public GenericSignal<boost::function1<void, T1> > { - public: - void emit(T1 arg1) { - boost::lock_guard<boost::mutex> lock(this->mutex); - - for(typename std::map<Connection, typename GenericSignal<boost::function1<void, T1> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(boost::bind(handler->second, arg1)); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL1_H_ */ diff --git a/src/Net/Signals/Signal2.h b/src/Net/Signals/Signal2.h deleted file mode 100644 index 3fb9315..0000000 --- a/src/Net/Signals/Signal2.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Signal2.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_SIGNALS_SIGNAL2_H_ -#define MAD_NET_SIGNALS_SIGNAL2_H_ - -#include "GenericSignal.h" -#include <Net/ThreadManager.h> - -#include <boost/function.hpp> - -namespace Mad { -namespace Net { -namespace Signals { - -template <typename T1, typename T2> -class Signal2 : public GenericSignal<boost::function2<void, T1, T2> > { - public: - void emit(T1 arg1, T2 arg2) { - boost::lock_guard<boost::mutex> lock(this->mutex); - - for(typename std::map<Connection, typename GenericSignal<boost::function2<void, T1, T2> >::slot_type>::iterator handler = this->handlers.begin(); handler != this->handlers.end(); ++handler) - Net::ThreadManager::get()->pushWork(boost::bind(handler->second, arg1, arg2)); - } -}; - -} -} -} - -#endif /* MAD_NET_SIGNALS_SIGNAL2_H_ */ diff --git a/src/Net/Signals/SignalBase.h b/src/Net/Signals/SignalBase.h deleted file mode 100644 index ea1b1e9..0000000 --- a/src/Net/Signals/SignalBase.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SignalBase.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_SIGNALS_SIGNALBASE_H_ -#define MAD_SIGNALS_SIGNALBASE_H_ - -#include "Connection.h" - -#include <set> -#include <boost/thread/mutex.hpp> - -namespace Mad { -namespace Net { -namespace Signals { - -class SignalBase : private boost::noncopyable { - private: - friend class Connection; - - unsigned long connectionId; - - protected: - boost::mutex mutex; - - Connection getNewConnection() { - return Connection(this, connectionId++); - } - - SignalBase() : connectionId(0) {} - virtual ~SignalBase() {} -}; - -} -} -} - -#endif /* MAD_SIGNALS_SIGNALBASE_H_ */ diff --git a/src/Net/Signals/Signals.h b/src/Net/Signals/Signals.h deleted file mode 100644 index 4ee4f95..0000000 --- a/src/Net/Signals/Signals.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Signals.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_SIGNALS_SIGNALS_H_ -#define MAD_SIGNALS_SIGNALS_H_ - -#include "Signal0.h" -#include "Signal1.h" -#include "Signal2.h" - -#endif /* MAD_SIGNALS_SIGNALS_H_ */ diff --git a/src/Net/ThreadManager.cpp b/src/Net/ThreadManager.cpp deleted file mode 100644 index 377e6e1..0000000 --- a/src/Net/ThreadManager.cpp +++ /dev/null @@ -1,146 +0,0 @@ -/* - * ThreadManager.cpp - * - * Copyright (C) 2009 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 "ThreadManager.h" -#include "Connection.h" - -#include <Common/Logger.h> -#include <Common/LogManager.h> - -#include <boost/bind.hpp> - -#include <fcntl.h> - -namespace Mad { -namespace Net { - -ThreadManager ThreadManager::threadManager; - - -void ThreadManager::workerFunc() { - while(true) { - boost::unique_lock<boost::mutex> lock(runLock); - - if(!running || !isThisWorkerThread()) - break; - - lock.unlock(); - - boost::unique_lock<boost::mutex> lock2(workLock); - while(work.empty()) { - workCond.wait(lock2); - - if(!running) - return; - } - - boost::function0<void> nextWork = work.front(); - work.pop(); - - lock2.unlock(); - - nextWork(); - } - - // And let the new worker thread join us... - pushWork(boost::bind(&ThreadManager::threadFinished, this, boost::this_thread::get_id())); -} - -void ThreadManager::detach() { - if(isThisMainThread()) { - Common::Logger::log(Common::Logger::CRITICAL, "Tried to detach main thread! This is just WRONG!"); - return; - } - - { - boost::lock_guard<boost::mutex> lock(runLock); - if(!running) - return; // There's no point in creating a new worker thread when we aren't running anymore - } - - threadLock.lock(); - - if(workerThread->get_id() == boost::this_thread::get_id()) {// Already detached? - threads.insert(std::make_pair(boost::this_thread::get_id(), workerThread)); - - workerThread.reset(new boost::thread(std::mem_fun(&ThreadManager::workerFunc), this)); - } - - threadLock.unlock(); -} - -void ThreadManager::pushWork(const boost::function0<void> &newWork) { - workLock.lock(); - work.push(newWork); - workLock.unlock(); - - workCond.notify_one(); -} - - -void ThreadManager::doInit() { - running = true; - - threadLock.lock(); - - ioWorker.reset(new boost::asio::io_service::work(Connection::ioService)); - - mainThreadId = boost::this_thread::get_id(); - workerThread.reset(new boost::thread(&ThreadManager::workerFunc, this)); - loggerThread.reset(new boost::thread(&Common::LogManager::loggerThread, Common::LogManager::get())); - ioThread.reset(new boost::thread((std::size_t(boost::asio::io_service::*)())&boost::asio::io_service::run, &Connection::ioService)); - - threadLock.unlock(); -} - -void ThreadManager::doDeinit() { - if(!isThisMainThread()) { - // TODO Critical error!!! - return; - } - - // First set running = false so the worker threads quit - boost::lock(runLock, workLock); - running = false; - - workLock.unlock(); - runLock.unlock(); - - workCond.notify_one(); - - // We don't have to lock threadLock as detach() won't change workerThread when running is false - workerThread->join(); - - // Now wait for all detached threads - while(!threads.empty()) { - threadFinished(threads.begin()->first); - } - - // IO thread is next - ioWorker.reset(); - Connection::ioService.stop(); - ioThread->join(); - - // Finally, the logger thread has to die - Common::LogManager::get()->stopLoggerThread(); - loggerThread->join(); -} - -} -} diff --git a/src/Net/ThreadManager.h b/src/Net/ThreadManager.h deleted file mode 100644 index b8c4bec..0000000 --- a/src/Net/ThreadManager.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * ThreadManager.h - * - * Copyright (C) 2009 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/>. - */ - -#ifndef MAD_NET_THREADMANAGER_H_ -#define MAD_NET_THREADMANAGER_H_ - -#include <config.h> - -#include <Common/Initializable.h> - -#include <queue> -#include <map> - -#include <boost/asio.hpp> - -#include <boost/thread/thread.hpp> -#include <boost/thread/condition_variable.hpp> -#include <boost/thread/locks.hpp> - -namespace Mad { -namespace Net { - -class ThreadManager : public Common::Initializable { - private: - boost::thread::id mainThreadId; - boost::shared_ptr<boost::thread> workerThread, loggerThread, ioThread; - std::map<boost::thread::id, boost::shared_ptr<boost::thread> > threads; - - boost::mutex threadLock; - - boost::mutex runLock; - bool running; - - boost::mutex workLock; - boost::condition_variable workCond; - std::queue<boost::function0<void> > work; - - boost::scoped_ptr<boost::asio::io_service::work> ioWorker; - - static ThreadManager threadManager; - - ThreadManager() {} - - void workerFunc(); - void ioFunc(); - - void threadFinished(boost::thread::id threadId) { - boost::shared_ptr<boost::thread> thread; - - { - boost::lock_guard<boost::mutex> lock(threadLock); - - std::map<boost::thread::id, boost::shared_ptr<boost::thread> >::iterator it = threads.find(threadId); - - if(it == threads.end()) - return; - - thread = it->second; - threads.erase(it); - } - - thread->join(); - } - - protected: - virtual void doInit(); - virtual void doDeinit(); - - public: - bool isThisMainThread() { - return (mainThreadId == boost::this_thread::get_id()); - } - - bool isThisWorkerThread() { - boost::lock_guard<boost::mutex> lock(threadLock); - return (workerThread->get_id() == boost::this_thread::get_id()); - } - - void detach(); - - void pushWork(const boost::function0<void> &newWork); - - static ThreadManager* get() { - return &threadManager; - } -}; - -} -} - -#endif /* MAD_NET_THREADMANAGER_H_ */ |