From c65f327e712eca4bddc47c07ec41c3be920b704b Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Sun, 27 Jan 2013 15:15:27 +0100 Subject: [PATCH] server crash fixes --- CMakeLists.txt | 5 ++++- common/server.cpp | 4 ++-- common/server_protocolhandler.cpp | 4 ++++ common/serverinfo_user_container.cpp | 3 +-- servatrice/src/main.cpp | 2 ++ servatrice/src/servatrice.cpp | 1 + servatrice/src/serversocketinterface.cpp | 7 ++++++- 7 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 84a67b73..69d22d76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,8 +7,11 @@ if (NOT WITHOUT_CLIENT) add_subdirectory(cockatrice) add_subdirectory(oracle) endif(NOT WITHOUT_CLIENT) +if (WITH_TESTCLIENT) + add_subdirectory(testclient) +endif(WITH_TESTCLIENT) FILE(GLOB sounds "${CMAKE_CURRENT_SOURCE_DIR}/sounds/*.raw") INSTALL(FILES ${sounds} DESTINATION share/cockatrice/sounds) FILE(GLOB zonebg "${CMAKE_CURRENT_SOURCE_DIR}/zonebg/*.*") -INSTALL(FILES ${zonebg} DESTINATION share/cockatrice/zonebg) \ No newline at end of file +INSTALL(FILES ${zonebg} DESTINATION share/cockatrice/zonebg) diff --git a/common/server.cpp b/common/server.cpp index 72eeaec0..5c960273 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -140,7 +140,7 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString } users.insert(name, session); - qDebug() << "Server::loginUser: name=" << name; + qDebug() << "Server::loginUser:" << session << "name=" << name; data.set_session_id(databaseInterface->startSession(name, session->getAddress())); databaseInterface->unlockSessionTables(); @@ -228,7 +228,7 @@ void Server::removeClient(Server_ProtocolHandler *client) qDebug() << "closed session id:" << sessionId; } } - qDebug() << "Server::removeClient:" << clients.size() << "clients; " << users.size() << "users left"; + qDebug() << "Server::removeClient: removed" << (void *) client << ";" << clients.size() << "clients; " << users.size() << "users left"; } void Server::externalUserJoined(const ServerInfo_User &userInfo) diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index a067fb3d..858550a4 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -277,6 +277,10 @@ Response::ResponseCode Server_ProtocolHandler::processAdminCommandContainer(cons void Server_ProtocolHandler::processCommandContainer(const CommandContainer &cont) { + // Command processing must be disabled after prepareDestroy() has been called. + if (deleted) + return; + lastDataReceived = timeRunning; ResponseContainer responseContainer(cont.has_cmd_id() ? cont.cmd_id() : -1); diff --git a/common/serverinfo_user_container.cpp b/common/serverinfo_user_container.cpp index 56ce6b67..deead4e0 100644 --- a/common/serverinfo_user_container.cpp +++ b/common/serverinfo_user_container.cpp @@ -26,8 +26,7 @@ ServerInfo_User_Container::~ServerInfo_User_Container() void ServerInfo_User_Container::setUserInfo(const ServerInfo_User &_userInfo) { - userInfo = new ServerInfo_User; - userInfo->CopyFrom(_userInfo); + userInfo = new ServerInfo_User(_userInfo); } ServerInfo_User &ServerInfo_User_Container::copyUserInfo(ServerInfo_User &result, bool complete, bool internalInfo, bool sessionInfo) const diff --git a/servatrice/src/main.cpp b/servatrice/src/main.cpp index 17338337..39c0297c 100644 --- a/servatrice/src/main.cpp +++ b/servatrice/src/main.cpp @@ -151,6 +151,8 @@ int main(int argc, char *argv[]) sigemptyset(&segv.sa_mask); sigaction(SIGSEGV, &segv, 0); sigaction(SIGABRT, &segv, 0); + + signal(SIGPIPE, SIG_IGN); #endif rng = new RNG_SFMT; diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index cc8a95c9..4017242c 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -289,6 +289,7 @@ bool Servatrice::initServer() const int numberPools = settings->value("server/number_pools", 1).toInt(); gameServer = new Servatrice_GameServer(this, numberPools, servatriceDatabaseInterface->getDatabase(), this); + gameServer->setMaxPendingConnections(1000); const int gamePort = settings->value("server/port", 4747).toInt(); qDebug() << "Starting server on port" << gamePort; if (gameServer->listen(QHostAddress::Any, gamePort)) diff --git a/servatrice/src/serversocketinterface.cpp b/servatrice/src/serversocketinterface.cpp index 675d2dfb..f91029d7 100644 --- a/servatrice/src/serversocketinterface.cpp +++ b/servatrice/src/serversocketinterface.cpp @@ -87,6 +87,10 @@ ServerSocketInterface::~ServerSocketInterface() void ServerSocketInterface::initConnection(int socketDescriptor) { + // Add this object to the server's list of connections before it can receive socket events. + // Otherwise, in case a of a socket error, it could be removed from the list before it is added. + server->addClient(this); + socket->setSocketDescriptor(socketDescriptor); logger->logMessage(QString("Incoming connection: %1").arg(socket->peerAddress().toString()), this); initSessionDeprecated(); @@ -123,7 +127,6 @@ bool ServerSocketInterface::initSession() return false; } - server->addClient(this); return true; } @@ -716,6 +719,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason())); sqlInterface->execSqlQuery(query); + servatrice->clientsLock.lockForRead(); QList userList = servatrice->getUsersWithAddressAsList(QHostAddress(address)); ServerSocketInterface *user = static_cast(server->getUsers().value(userName)); if (user && !userList.contains(user)) @@ -734,6 +738,7 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban QMetaObject::invokeMethod(userList[i], "prepareDestroy", Qt::QueuedConnection); } } + servatrice->clientsLock.unlock(); return Response::RespOk; }