From 289986b5f02d3b8a76a95bfa41a63f4a5633859c Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Mon, 31 Mar 2008 19:04:11 +0200 Subject: [PATCH] Finished implementing error handling in GameServer. --- src/network/GameServer.cpp | 48 +++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/network/GameServer.cpp b/src/network/GameServer.cpp index 9a7c200..b6a8870 100644 --- a/src/network/GameServer.cpp +++ b/src/network/GameServer.cpp @@ -84,17 +84,16 @@ GameServer::GameServer(QWidget *gui, HistoryModel *history) : Network(gui) GameServer::~GameServer() { ServerSocket *s; - foreach (s, _pendingConnections) { - delete s; - } foreach (s, _remotePlayers) { if (s) { delete s; } } - foreach (s, _spectators) { - delete s; - } + + qDeleteAll(_spectators); + _spectators.clear(); + qDeleteAll(_pendingConnections); + _pendingConnections.clear(); delete _listener; delete _settings; @@ -239,13 +238,36 @@ void GameServer::handleError(QString errorMessage) return; } - qDebug() << errorMessage; - - QLinkedList::iterator itp = qFind(_pendingConnections.begin(), _pendingConnections.end(), socket); - if (itp != _pendingConnections.end()) { - _pendingConnections.erase(itp); - delete socket; - return; + QLinkedList::iterator it = qFind(_pendingConnections.begin(), _pendingConnections.end(), socket); + if (it != _pendingConnections.end()) { + _pendingConnections.erase(it); + socket->deleteLater(); + } + int i = _spectators.indexOf(socket); + if (i >= 0) { + _spectators.removeAt(i); + _names.removeAt(i + _numberOfPlayers); + socket->deleteLater(); + } + i = _remotePlayers.indexOf(socket); + if (i >= 0) { + _remotePlayers.removeAt(i); + _names[i] = ""; + if (_gameInProgress) { + emit error(errorMessage); + } else { + emit playerLeft(i); + ServerSocket *s; + foreach (s, _remotePlayers) { + if (s && s != socket) { + s->sendPlayerLeft(i); + } + } + foreach (s, _spectators) { + s->sendPlayerLeft(i); + } + } + socket->deleteLater(); } } -- 2.11.4.GIT