summaryrefslogtreecommitdiffstats
path: root/src/Server/ConnectionManager.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-05-20 01:08:16 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-05-20 01:08:16 +0200
commit776377bb21ee1cfe0bcdbc000f7c6fa0be227226 (patch)
treea60e8bdd92678dece5fbb96e75535eba2b61b7da /src/Server/ConnectionManager.cpp
parentbadc0da3b74d99c90b7b28180d08cd6d08830254 (diff)
downloadmad-776377bb21ee1cfe0bcdbc000f7c6fa0be227226.tar
mad-776377bb21ee1cfe0bcdbc000f7c6fa0be227226.zip
Netzwerk-Code auf boost::asio umgestellt
Diffstat (limited to 'src/Server/ConnectionManager.cpp')
-rw-r--r--src/Server/ConnectionManager.cpp88
1 files changed, 46 insertions, 42 deletions
diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp
index 6ef918a..bbaf673 100644
--- a/src/Server/ConnectionManager.cpp
+++ b/src/Server/ConnectionManager.cpp
@@ -33,8 +33,6 @@
#include "RequestHandlers/LogRequestHandler.h"
#include "RequestHandlers/UserInfoRequestHandler.h"
#include "RequestHandlers/UserListRequestHandler.h"
-#include <Net/FdManager.h>
-#include <Net/ServerConnection.h>
#include <Net/Packet.h>
#include <Net/Listener.h>
@@ -46,49 +44,46 @@ namespace Server {
ConnectionManager ConnectionManager::connectionManager;
-bool ConnectionManager::Connection::send(const Net::Packet &packet) {
+bool ConnectionManager::ServerConnection::send(const Net::Packet &packet) {
return connection->send(packet);
}
-ConnectionManager::Connection::Connection(Net::ServerConnection *connection0)
-: connection(connection0), type(connection0->isDaemonConnection() ? DAEMON : CLIENT), hostInfo(0) {
- connection->signalReceive().connect(boost::bind(&Connection::receive, this, _1));
+ConnectionManager::ServerConnection::ServerConnection(boost::shared_ptr<Net::Connection> connection0)
+: connection(connection0), type(UNKNOWN), hostInfo(0) {
+ connection->signalReceive().connect(boost::bind(&ServerConnection::receive, this, _1));
}
-ConnectionManager::Connection::~Connection() {
- delete connection;
-}
-
-bool ConnectionManager::Connection::isConnected() const {
+bool ConnectionManager::ServerConnection::isConnected() const {
return connection->isConnected();
}
-bool ConnectionManager::Connection::disconnect() {
+bool ConnectionManager::ServerConnection::disconnect() {
connection->disconnect();
return true;
}
-void* ConnectionManager::Connection::getCertificate(size_t *size) const {
+/*void* ConnectionManager::ServerConnection::getCertificate(size_t *size) const {
const gnutls_datum_t *cert = connection->getCertificate();
*size = cert->size;
return cert->data;
}
-void* ConnectionManager::Connection::getPeerCertificate(size_t *size) const {
+void* ConnectionManager::ServerConnection::getPeerCertificate(size_t *size) const {
const gnutls_datum_t *cert = connection->getPeerCertificate();
*size = cert->size;
return cert->data;
-}
+}*/
void ConnectionManager::updateState(Common::HostInfo *hostInfo, Common::HostInfo::State state) {
hostInfo->setState(state);
- for(std::set<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con) {
- if((*con)->getConnectionType() == Connection::CLIENT)
- Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(*con, boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state);
+
+ for(std::set<boost::shared_ptr<ServerConnection> >::iterator con = connections.begin(); con != connections.end(); ++con) {
+ if((*con)->getConnectionType() == ServerConnection::CLIENT)
+ Common::RequestManager::get()->sendRequest<Requests::DaemonStateUpdateRequest>(con->get(), boost::bind(&ConnectionManager::updateStateFinished, this, _1), hostInfo->getName(), state);
}
}
@@ -98,7 +93,7 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool
if(entry[0].getKey().matches("Listen") && entry[1].empty()) {
try {
- listenerAddresses.push_back(Net::IPAddress(entry[0][0]));
+ listenerAddresses.push_back(boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(entry[0][0]), 6666));
}
catch(Net::Exception &e) {
// TODO Log error
@@ -147,8 +142,8 @@ bool ConnectionManager::handleConfigEntry(const Common::ConfigEntry &entry, bool
void ConnectionManager::configFinished() {
if(listenerAddresses.empty()) {
try {
- Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile);
- listener->signalNewConnection().connect(boost::bind(&ConnectionManager::newConnectionHandler, this, _1));
+ boost::shared_ptr<Net::Listener> listener(new Net::Listener(x509CertFile, x509KeyFile));
+ listener->signalNewConnection().connect(boost::bind(&ConnectionManager::handleNewConnection, this, _1));
listeners.push_back(listener);
}
catch(Net::Exception &e) {
@@ -156,10 +151,10 @@ void ConnectionManager::configFinished() {
}
}
else {
- for(std::vector<Net::IPAddress>::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) {
+ for(std::vector<boost::asio::ip::tcp::endpoint>::const_iterator address = listenerAddresses.begin(); address != listenerAddresses.end(); ++address) {
try {
- Net::Listener *listener = new Net::Listener(x509CertFile, x509KeyFile, *address);
- listener->signalNewConnection().connect(boost::bind(&ConnectionManager::newConnectionHandler, this, _1));
+ boost::shared_ptr<Net::Listener> listener(new Net::Listener(x509CertFile, x509KeyFile, *address));
+ listener->signalNewConnection().connect(boost::bind(&ConnectionManager::handleNewConnection, this, _1));
listeners.push_back(listener);
}
catch(Net::Exception &e) {
@@ -169,28 +164,27 @@ void ConnectionManager::configFinished() {
}
}
-void ConnectionManager::newConnectionHandler(Net::ServerConnection *con) {
- Connection *connection = new Connection(con);
- con->signalDisconnected().connect(boost::bind(&ConnectionManager::disconnectHandler, this, connection));
+void ConnectionManager::handleNewConnection(boost::shared_ptr<Net::Connection> con) {
+ boost::shared_ptr<ServerConnection> connection(new ServerConnection(con));
+ con->signalDisconnected().connect(boost::bind(&ConnectionManager::handleDisconnect, this, connection));
connections.insert(connection);
- Common::RequestManager::get()->registerConnection(connection);
+ Common::RequestManager::get()->registerConnection(connection.get());
}
-void ConnectionManager::disconnectHandler(Connection *con) {
- if(con->isIdentified())
+void ConnectionManager::handleDisconnect(boost::shared_ptr<ServerConnection> con) {
+ if(con->getHostInfo())
updateState(con->getHostInfo(), Common::HostInfo::INACTIVE);
connections.erase(con);
- Common::RequestManager::get()->unregisterConnection(con);
- delete con;
+ Common::RequestManager::get()->unregisterConnection(con.get());
}
void ConnectionManager::doInit() {
Common::RequestManager::get()->setServer(true);
- Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI");
+ //Common::RequestManager::get()->registerPacketType<RequestHandlers::GSSAPIAuthRequestHandler>("AuthGSSAPI");
Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonCommandRequestHandler>("DaemonCommand");
Common::RequestManager::get()->registerPacketType<RequestHandlers::DaemonFSInfoRequestHandler>("DaemonFSInfo");
Common::RequestManager::get()->registerPacketType<Common::RequestHandlers::FSInfoRequestHandler>("FSInfo");
@@ -204,11 +198,9 @@ void ConnectionManager::doInit() {
}
void ConnectionManager::doDeinit() {
- for(std::set<Connection*>::iterator con = connections.begin(); con != connections.end(); ++con)
- delete *con;
+ connections.clear();
-
- Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI");
+ //Common::RequestManager::get()->unregisterPacketType("AuthGSSAPI");
Common::RequestManager::get()->unregisterPacketType("DaemonCommand");
Common::RequestManager::get()->unregisterPacketType("DaemonFSInfo");
Common::RequestManager::get()->unregisterPacketType("FSInfo");
@@ -221,7 +213,7 @@ void ConnectionManager::doDeinit() {
Common::RequestManager::get()->unregisterPacketType("Log");
}
-Common::Connection* ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) {
+boost::shared_ptr<Common::Connection> ConnectionManager::getDaemonConnection(const std::string &name) const throw (Net::Exception&) {
const Common::HostInfo *hostInfo;
try {
@@ -232,7 +224,7 @@ Common::Connection* ConnectionManager::getDaemonConnection(const std::string &na
}
if(hostInfo->getState() != Common::HostInfo::INACTIVE) {
- for(std::set<Connection*>::const_iterator it = connections.begin(); it != connections.end(); ++it) {
+ for(std::set<boost::shared_ptr<ServerConnection> >::const_iterator it = connections.begin(); it != connections.end(); ++it) {
if((*it)->getHostInfo() == hostInfo) {
return *it;
}
@@ -243,7 +235,7 @@ Common::Connection* ConnectionManager::getDaemonConnection(const std::string &na
}
std::string ConnectionManager::getDaemonName(const Common::Connection *con) const throw (Net::Exception&) {
- const Connection *connection = dynamic_cast<const Connection*>(con);
+ const ServerConnection *connection = dynamic_cast<const ServerConnection*>(con);
if(connection) {
if(connection->isIdentified()) {
@@ -257,9 +249,9 @@ std::string ConnectionManager::getDaemonName(const Common::Connection *con) cons
void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const std::string &name) throw (Net::Exception&) {
// TODO Logging
- Connection *connection = dynamic_cast<Connection*>(con);
+ ServerConnection *connection = dynamic_cast<ServerConnection*>(con);
- if(!connection || (connection->getConnectionType() != Connection::DAEMON))
+ if(!connection)
throw Net::Exception(Net::Exception::INVALID_ACTION);
if(connection->isIdentified())
@@ -284,6 +276,18 @@ void ConnectionManager::identifyDaemonConnection(Common::Connection *con, const
Common::Logger::logf("Identified as '%s'.", name.c_str());
}
+void ConnectionManager::identifyClientConnection(Common::Connection *con) throw (Net::Exception&) {
+ ServerConnection *connection = dynamic_cast<ServerConnection*>(con);
+
+ if(!connection)
+ throw Net::Exception(Net::Exception::INVALID_ACTION);
+
+ if(connection->isIdentified())
+ throw Net::Exception(Net::Exception::ALREADY_IDENTIFIED);
+
+ connection->identify();
+}
+
std::vector<Common::HostInfo> ConnectionManager::getDaemonList() const {
std::vector<Common::HostInfo> ret;