Some improvements to Servatice network code (#3969)

* Some improvements to Servatice network code

1. fix crash on fuzzy connection (tcp server only)
2. ensure websockets are parent()ed to avoid leaking them
3. quick catch disconnect()ed sockets instead of waiting for a socket error to happen
4. supporto mulltiple connection pools on the websocket server; they are still bound to the same thread due to a qt5 limitation.
This commit is contained in:
ctrlaltca 2020-04-24 22:26:59 +02:00 committed by GitHub
parent 46fe0cd725
commit d30691559a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 12 deletions

View file

@ -111,21 +111,30 @@ Servatrice_ConnectionPool *Servatrice_GameServer::findLeastUsedConnectionPool()
#define WEBSOCKET_POOL_NUMBER 999
Servatrice_WebsocketGameServer::Servatrice_WebsocketGameServer(Servatrice *_server,
int /* _numberPools */,
int _numberPools,
const QSqlDatabase &_sqlDatabase,
QObject *parent)
: QWebSocketServer("Servatrice", QWebSocketServer::NonSecureMode, parent), server(_server)
{
// Qt limitation: websockets can't be moved to another thread
auto newDatabaseInterface = new Servatrice_DatabaseInterface(WEBSOCKET_POOL_NUMBER, server);
auto newPool = new Servatrice_ConnectionPool(newDatabaseInterface);
for (int i = 0; i < _numberPools; ++i) {
int poolNumber = WEBSOCKET_POOL_NUMBER + i;
auto newDatabaseInterface = new Servatrice_DatabaseInterface(poolNumber, server);
auto newPool = new Servatrice_ConnectionPool(newDatabaseInterface);
server->addDatabaseInterface(thread(), newDatabaseInterface);
newDatabaseInterface->initDatabase(_sqlDatabase);
auto newThread = new QThread;
newThread->setObjectName("pool_" + QString::number(poolNumber));
newPool->moveToThread(newThread);
newDatabaseInterface->moveToThread(newThread);
server->addDatabaseInterface(newThread, newDatabaseInterface);
connectionPools.append(newPool);
newThread->start();
QMetaObject::invokeMethod(newDatabaseInterface, "initDatabase", Qt::BlockingQueuedConnection,
Q_ARG(QSqlDatabase, _sqlDatabase));
connect(this, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
connectionPools.append(newPool);
connect(this, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
}
Servatrice_WebsocketGameServer::~Servatrice_WebsocketGameServer()
@ -143,7 +152,11 @@ void Servatrice_WebsocketGameServer::onNewConnection()
Servatrice_ConnectionPool *pool = findLeastUsedConnectionPool();
auto ssi = new WebsocketServerSocketInterface(server, pool->getDatabaseInterface());
// ssi->moveToThread(pool->thread());
/*
* Due to a Qt limitation, websockets can't be moved to another thread.
* This will hopefully change in Qt6 if QtWebSocket will be integrated in QtNetwork
*/
// ssi->moveToThread(pool->thread());
pool->addClient();
connect(ssi, SIGNAL(destroyed()), pool, SLOT(removeClient()));