more mutexes
This commit is contained in:
parent
51269f4b91
commit
eca941201d
5 changed files with 16 additions and 4 deletions
|
@ -9,6 +9,7 @@ LocalServerInterface::LocalServerInterface(LocalServer *_server)
|
||||||
|
|
||||||
LocalServerInterface::~LocalServerInterface()
|
LocalServerInterface::~LocalServerInterface()
|
||||||
{
|
{
|
||||||
|
server->removeClient(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocalServerInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem)
|
void LocalServerInterface::sendProtocolItem(ProtocolItem *item, bool deleteItem)
|
||||||
|
|
|
@ -38,6 +38,7 @@ Server::~Server()
|
||||||
|
|
||||||
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password)
|
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
if (name.size() > 35)
|
if (name.size() > 35)
|
||||||
name = name.left(35);
|
name = name.left(35);
|
||||||
AuthenticationResult authState = checkUserPassword(name, password);
|
AuthenticationResult authState = checkUserPassword(name, password);
|
||||||
|
@ -78,11 +79,13 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString
|
||||||
|
|
||||||
void Server::addClient(Server_ProtocolHandler *client)
|
void Server::addClient(Server_ProtocolHandler *client)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
clients << client;
|
clients << client;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::removeClient(Server_ProtocolHandler *client)
|
void Server::removeClient(Server_ProtocolHandler *client)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
clients.removeAt(clients.indexOf(client));
|
clients.removeAt(clients.indexOf(client));
|
||||||
ServerInfo_User *data = client->getUserInfo();
|
ServerInfo_User *data = client->getUserInfo();
|
||||||
if (data) {
|
if (data) {
|
||||||
|
@ -104,6 +107,7 @@ Server_Game *Server::getGame(int gameId) const
|
||||||
|
|
||||||
void Server::broadcastRoomUpdate()
|
void Server::broadcastRoomUpdate()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
Server_Room *room = static_cast<Server_Room *>(sender());
|
Server_Room *room = static_cast<Server_Room *>(sender());
|
||||||
QList<ServerInfo_Room *> eventRoomList;
|
QList<ServerInfo_Room *> eventRoomList;
|
||||||
eventRoomList.append(new ServerInfo_Room(room->getId(), room->getName(), room->getDescription(), room->getGames().size(), room->size(), room->getAutoJoin()));
|
eventRoomList.append(new ServerInfo_Room(room->getId(), room->getName(), room->getDescription(), room->getGames().size(), room->size(), room->getAutoJoin()));
|
||||||
|
@ -117,17 +121,20 @@ void Server::broadcastRoomUpdate()
|
||||||
|
|
||||||
void Server::gameCreated(Server_Game *game)
|
void Server::gameCreated(Server_Game *game)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
games.insert(game->getGameId(), game);
|
games.insert(game->getGameId(), game);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::gameClosing(int gameId)
|
void Server::gameClosing(int gameId)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
qDebug("Server::gameClosing");
|
qDebug("Server::gameClosing");
|
||||||
games.remove(gameId);
|
games.remove(gameId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::addRoom(Server_Room *newRoom)
|
void Server::addRoom(Server_Room *newRoom)
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
rooms.insert(newRoom->getId(), newRoom);
|
rooms.insert(newRoom->getId(), newRoom);
|
||||||
connect(newRoom, SIGNAL(roomInfoChanged()), this, SLOT(broadcastRoomUpdate()));
|
connect(newRoom, SIGNAL(roomInfoChanged()), this, SLOT(broadcastRoomUpdate()));
|
||||||
connect(newRoom, SIGNAL(gameCreated(Server_Game *)), this, SLOT(gameCreated(Server_Game *)));
|
connect(newRoom, SIGNAL(gameCreated(Server_Game *)), this, SLOT(gameCreated(Server_Game *)));
|
||||||
|
|
|
@ -22,10 +22,6 @@ Server_ProtocolHandler::Server_ProtocolHandler(Server *_server, QObject *parent)
|
||||||
|
|
||||||
Server_ProtocolHandler::~Server_ProtocolHandler()
|
Server_ProtocolHandler::~Server_ProtocolHandler()
|
||||||
{
|
{
|
||||||
// The socket has to be removed from the server's list before it is removed from the game's list
|
|
||||||
// so it will not receive the game update event.
|
|
||||||
server->removeClient(this);
|
|
||||||
|
|
||||||
QMapIterator<int, Server_Room *> roomIterator(rooms);
|
QMapIterator<int, Server_Room *> roomIterator(rooms);
|
||||||
while (roomIterator.hasNext())
|
while (roomIterator.hasNext())
|
||||||
roomIterator.next().value()->removeClient(this);
|
roomIterator.next().value()->removeClient(this);
|
||||||
|
|
|
@ -249,6 +249,7 @@ ServerInfo_User *Servatrice::getUserData(const QString &name)
|
||||||
|
|
||||||
int Servatrice::getUsersWithAddress(const QHostAddress &address) const
|
int Servatrice::getUsersWithAddress(const QHostAddress &address) const
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (int i = 0; i < clients.size(); ++i)
|
for (int i = 0; i < clients.size(); ++i)
|
||||||
if (static_cast<ServerSocketInterface *>(clients[i])->getPeerAddress() == address)
|
if (static_cast<ServerSocketInterface *>(clients[i])->getPeerAddress() == address)
|
||||||
|
@ -304,6 +305,7 @@ QMap<QString, ServerInfo_User *> Servatrice::getIgnoreList(const QString &name)
|
||||||
|
|
||||||
bool Servatrice::getUserBanned(Server_ProtocolHandler *client, const QString &userName) const
|
bool Servatrice::getUserBanned(Server_ProtocolHandler *client, const QString &userName) const
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
QHostAddress address = static_cast<ServerSocketInterface *>(client)->getPeerAddress();
|
QHostAddress address = static_cast<ServerSocketInterface *>(client)->getPeerAddress();
|
||||||
for (int i = 0; i < addressBanList.size(); ++i)
|
for (int i = 0; i < addressBanList.size(); ++i)
|
||||||
if (address == addressBanList[i].first)
|
if (address == addressBanList[i].first)
|
||||||
|
@ -316,6 +318,7 @@ bool Servatrice::getUserBanned(Server_ProtocolHandler *client, const QString &us
|
||||||
|
|
||||||
void Servatrice::updateBanTimer()
|
void Servatrice::updateBanTimer()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&serverMutex);
|
||||||
for (int i = 0; i < addressBanList.size(); )
|
for (int i = 0; i < addressBanList.size(); )
|
||||||
if (--(addressBanList[i].second) <= 0)
|
if (--(addressBanList[i].second) <= 0)
|
||||||
addressBanList.removeAt(i);
|
addressBanList.removeAt(i);
|
||||||
|
|
|
@ -58,12 +58,17 @@ ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_s
|
||||||
|
|
||||||
ServerSocketInterface::~ServerSocketInterface()
|
ServerSocketInterface::~ServerSocketInterface()
|
||||||
{
|
{
|
||||||
|
QMutexLocker locker(&servatrice->serverMutex);
|
||||||
logger->logMessage("ServerSocketInterface destructor");
|
logger->logMessage("ServerSocketInterface destructor");
|
||||||
|
|
||||||
flushXmlBuffer();
|
flushXmlBuffer();
|
||||||
delete xmlWriter;
|
delete xmlWriter;
|
||||||
delete xmlReader;
|
delete xmlReader;
|
||||||
delete socket;
|
delete socket;
|
||||||
|
socket = 0;
|
||||||
|
|
||||||
|
// This call has to stay here so that the mutex is not freed prematurely.
|
||||||
|
server->removeClient(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerSocketInterface::processProtocolItem(ProtocolItem *item)
|
void ServerSocketInterface::processProtocolItem(ProtocolItem *item)
|
||||||
|
|
Loading…
Reference in a new issue