From a77d2d1e08e4e2e8dfb5e4fc326f6c8fe315a898 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 30 May 2009 13:37:06 +0200 Subject: Thread-sichere Signale implementiert --- src/Net/Connection.cpp | 8 ++--- src/Net/Connection.h | 32 +++++++++++++----- src/Net/Listener.cpp | 15 +++++---- src/Net/Listener.h | 11 ++++-- src/Net/Signals/Connection.h | 45 +++++++++++++++++++++++++ src/Net/Signals/Signal0.h | 43 ++++++++++++++++++++++++ src/Net/Signals/Signal1.h | 41 +++++++++++++++++++++++ src/Net/Signals/Signal2.h | 41 +++++++++++++++++++++++ src/Net/Signals/SignalBase.h | 80 ++++++++++++++++++++++++++++++++++++++++++++ src/Net/Signals/Signals.h | 27 +++++++++++++++ 10 files changed, 321 insertions(+), 22 deletions(-) create mode 100644 src/Net/Signals/Connection.h create mode 100644 src/Net/Signals/Signal0.h create mode 100644 src/Net/Signals/Signal1.h create mode 100644 src/Net/Signals/Signal2.h create mode 100644 src/Net/Signals/SignalBase.h create mode 100644 src/Net/Signals/Signals.h (limited to 'src/Net') diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 2f65bb0..1ff9926 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -55,7 +55,7 @@ void Connection::handleHandshake(const boost::system::error_code& error) { received = 0; } - ThreadManager::get()->pushWork(boost::bind((void (boost::signal0::*)())&boost::signal0::operator(), &connectedSignal)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &connectedSignal)); enterReceiveLoop(); } @@ -69,7 +69,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) { _setState(DISCONNECTED); - ThreadManager::get()->pushWork(boost::bind((void (boost::signal0::*)())&boost::signal0::operator(), &disconnectedSignal)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal0::emit, &disconnectedSignal)); } void Connection::enterReceiveLoop() { @@ -91,7 +91,7 @@ void Connection::handleHeaderReceive(const std::vector &data) { } if(header.length == 0) { - ThreadManager::get()->pushWork(boost::bind((void (boost::signal1::*)(const Packet&))&boost::signal1::operator(), &receiveSignal, Packet(ntohs(header.requestId)))); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, Packet(ntohs(header.requestId)))); enterReceiveLoop(); } @@ -105,7 +105,7 @@ void Connection::handleDataReceive(const std::vector &data) { boost::upgrade_lock lock(connectionLock); Packet packet(ntohs(header.requestId), data.data(), ntohs(header.length)); - ThreadManager::get()->pushWork(boost::bind((void (boost::signal1::*)(const Packet&))&boost::signal1::operator(), &receiveSignal, packet)); + ThreadManager::get()->pushWork(boost::bind(&Signals::Signal1::emit, &receiveSignal, packet)); } enterReceiveLoop(); diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 6678bd9..cda8bfe 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -23,12 +23,11 @@ #include #include "Packet.h" +#include "Signals/Signals.h" #include #include -#include - #include namespace Mad { @@ -71,9 +70,9 @@ class Connection : boost::noncopyable { Packet::Data header; - boost::signal1 receiveSignal; - boost::signal0 connectedSignal; - boost::signal0 disconnectedSignal; + Signals::Signal1 receiveSignal; + Signals::Signal0 connectedSignal; + Signals::Signal0 disconnectedSignal; bool receiving; unsigned long sending; @@ -172,9 +171,26 @@ class Connection : boost::noncopyable { bool send(const Packet &packet); - boost::signal1& signalReceive() {return receiveSignal;} - boost::signal0& signalConnected() {return connectedSignal;} - boost::signal0& signalDisconnected() {return disconnectedSignal;} + Signals::Connection connectSignalReceive(const Signals::Signal1::slot_type &slot) { + return receiveSignal.connect(slot); + } + void disconnectSignalReceive(const Signals::Connection &connection) { + receiveSignal.disconnect(connection); + } + + Signals::Connection connectSignalConnected(const Signals::Signal0::slot_type &slot) { + return connectedSignal.connect(slot); + } + void disconnectSignalConnected(const Signals::Connection &connection) { + connectedSignal.disconnect(connection); + } + + Signals::Connection connectSignalDisconnected(const Signals::Signal0::slot_type &slot) { + return disconnectedSignal.connect(slot); + } + void disconnectSignalDisconnected(const Signals::Connection &connection) { + disconnectedSignal.disconnect(connection); + } }; } diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index be227fd..6da2762 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -39,8 +39,8 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share con->_setState(Connection::CONNECT); - boost::signals::connection con1 = con->signalConnected().connect(boost::bind(&Listener::handleConnect, this, con)); - boost::signals::connection con2 = con->signalDisconnected().connect(boost::bind(&Listener::handleDisconnect, this, con)); + Signals::Connection con1 = con->connectSignalConnected(boost::bind(&Listener::handleConnect, this, con)); + Signals::Connection con2 = con->connectSignalDisconnected(boost::bind(&Listener::handleDisconnect, this, con)); connections.insert(std::make_pair(con, std::make_pair(con1, con2))); @@ -54,18 +54,19 @@ void Listener::handleAccept(const boost::system::error_code &error, boost::share void Listener::handleConnect(boost::shared_ptr con) { { boost::lock_guard lock(mutex); - std::map, std::pair >::iterator it = connections.find(con); + std::map, std::pair >::iterator it = connections.find(con); if(it == connections.end()) return; // Disconnect signal handlers - it->second.first.disconnect(); - it->second.second.disconnect(); + + con->disconnectSignalConnected(it->second.first); + con->disconnectSignalDisconnected(it->second.second); connections.erase(it); } - signal(con); + signal.emit(con); } void Listener::handleDisconnect(boost::shared_ptr con) { @@ -92,7 +93,7 @@ sslContext(Connection::ioService, boost::asio::ssl::context::sslv23) } Listener::~Listener() { - for(std::map,std::pair >::iterator con = connections.begin(); con != connections.end(); ++con) { + for(std::map,std::pair >::iterator con = connections.begin(); con != connections.end(); ++con) { con->first->disconnect(); // TODO wait... } diff --git a/src/Net/Listener.h b/src/Net/Listener.h index 903c907..ec0cd22 100644 --- a/src/Net/Listener.h +++ b/src/Net/Listener.h @@ -38,9 +38,9 @@ class Listener : boost::noncopyable { boost::asio::ip::tcp::acceptor acceptor; boost::asio::ssl::context sslContext; - std::map, std::pair > connections; + std::map, std::pair > connections; - boost::signal1 > signal; + Signals::Signal1 > signal; void handleAccept(const boost::system::error_code &error, boost::shared_ptr con); @@ -52,7 +52,12 @@ class Listener : boost::noncopyable { const boost::asio::ip::tcp::endpoint &address0 = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6666)) throw(Exception); virtual ~Listener(); - boost::signal1 >& signalNewConnection() {return signal;} + Signals::Connection connectSignalNewConnection(const Signals::Signal1 >::slot_type &slot) { + return signal.connect(slot); + } + void disconnectSignalNewConnection(const Signals::Connection &connection) { + signal.disconnect(connection); + } }; } diff --git a/src/Net/Signals/Connection.h b/src/Net/Signals/Connection.h new file mode 100644 index 0000000..0e66611 --- /dev/null +++ b/src/Net/Signals/Connection.h @@ -0,0 +1,45 @@ +/* + * Connection.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_NET_SIGNALS_CONNECTION_H_ +#define MAD_NET_SIGNALS_CONNECTION_H_ + +#include + +namespace Mad { +namespace Net { +namespace Signals { + +template class SignalBase; + +class Connection { + private: + template friend class SignalBase; + + boost::signals::connection connection; + + Connection(const boost::signals::connection &connection0) + : connection(connection0) {} +}; + +} +} +} + +#endif /* MAD_NET_SIGNALS_CONNECTION_H_ */ diff --git a/src/Net/Signals/Signal0.h b/src/Net/Signals/Signal0.h new file mode 100644 index 0000000..32859ab --- /dev/null +++ b/src/Net/Signals/Signal0.h @@ -0,0 +1,43 @@ +/* + * Signal0.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_NET_SIGNALS_SIGNAL0_H_ +#define MAD_NET_SIGNALS_SIGNAL0_H_ + +#include "SignalBase.h" + +namespace Mad { +namespace Net { +namespace Signals { + +template +class Signal0 : public SignalBase, boost::function0 > { + public: + R emit() { + boost::lock_guard lock(SignalBase, boost::function0 >::mutex); + + return SignalBase, boost::function0 >::signal(); + } +}; + +} +} +} + +#endif /* MAD_NET_SIGNALS_SIGNAL0_H_ */ diff --git a/src/Net/Signals/Signal1.h b/src/Net/Signals/Signal1.h new file mode 100644 index 0000000..78eaa47 --- /dev/null +++ b/src/Net/Signals/Signal1.h @@ -0,0 +1,41 @@ +/* + * Signal1.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_NET_SIGNALS_SIGNAL1_H_ +#define MAD_NET_SIGNALS_SIGNAL1_H_ + +namespace Mad { +namespace Net { +namespace Signals { + +template +class Signal1 : public SignalBase, boost::function1 > { + public: + R emit(T1 arg1) { + boost::lock_guard lock(SignalBase, boost::function1 >::mutex); + + return SignalBase, boost::function1 >::signal(arg1); + } +}; + +} +} +} + +#endif /* MAD_NET_SIGNALS_SIGNAL1_H_ */ diff --git a/src/Net/Signals/Signal2.h b/src/Net/Signals/Signal2.h new file mode 100644 index 0000000..afa528e --- /dev/null +++ b/src/Net/Signals/Signal2.h @@ -0,0 +1,41 @@ +/* + * Signal2.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_NET_SIGNALS_SIGNAL2_H_ +#define MAD_NET_SIGNALS_SIGNAL2_H_ + +namespace Mad { +namespace Net { +namespace Signals { + +template +class Signal2 : public SignalBase, boost::function2 > { + public: + R emit(T1 arg1, T2 arg2) { + boost::lock_guard lock(SignalBase, boost::function2 >::mutex); + + return SignalBase, boost::function2 >::signal(arg1, arg2); + } +}; + +} +} +} + +#endif /* MAD_NET_SIGNALS_SIGNAL2_H_ */ diff --git a/src/Net/Signals/SignalBase.h b/src/Net/Signals/SignalBase.h new file mode 100644 index 0000000..fc5f2c6 --- /dev/null +++ b/src/Net/Signals/SignalBase.h @@ -0,0 +1,80 @@ +/* + * SignalBase.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_SIGNALS_SIGNALBASE_H_ +#define MAD_SIGNALS_SIGNALBASE_H_ + +#include "Connection.h" + +#include +#include +#include +#include + +namespace Mad { +namespace Net { +namespace Signals { + +template +class SignalBase : private boost::noncopyable { + public: + typedef boost::slot slot_type; + + protected: + typedef SignalType signal_type; + + std::set connections; + boost::recursive_mutex mutex; + + signal_type signal; + + SignalBase() {} + ~SignalBase() { + // Wait for other threads + boost::lock_guard lock(mutex); + } + + public: + Connection connect(const slot_type &slot) { + boost::lock_guard lock(mutex); + + boost::signals::connection con(signal.connect(slot)); + + connections.insert(con); + return con; + } + + void disconnect(const Connection &connection) { + boost::lock_guard lock(mutex); + + std::set::iterator it = connections.find(connection.connection); + + if(it == connections.end()) + return; + + it->disconnect(); + connections.erase(it); + } +}; + +} +} +} + +#endif /* MAD_SIGNALS_SIGNALBASE_H_ */ diff --git a/src/Net/Signals/Signals.h b/src/Net/Signals/Signals.h new file mode 100644 index 0000000..4ee4f95 --- /dev/null +++ b/src/Net/Signals/Signals.h @@ -0,0 +1,27 @@ +/* + * Signals.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * 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 . + */ + +#ifndef MAD_SIGNALS_SIGNALS_H_ +#define MAD_SIGNALS_SIGNALS_H_ + +#include "Signal0.h" +#include "Signal1.h" +#include "Signal2.h" + +#endif /* MAD_SIGNALS_SIGNALS_H_ */ -- cgit v1.2.3