mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-15 11:38:49 -07:00
Client Websockets (#3545)
* Websockets * Add setting to get websocket IP from header * Add QT version guard * Minor cleanup Signed-off-by: Zach Halpern <ZaHalpern+github@gmail.com> * - Make QWebSocket required - Remove QWEBSOCEKT_LIB guards - Only TCP on port 4747 - Fix peerName lookup * fix check Signed-off-by: Zach Halpern <ZaHalpern+github@gmail.com> * Update CMakeLists.txt * Update CMakeLists.txt
This commit is contained in:
parent
5e38214675
commit
9a8c81cf5e
8 changed files with 130 additions and 77 deletions
|
|
@ -104,7 +104,6 @@ Servatrice_ConnectionPool *Servatrice_GameServer::findLeastUsedConnectionPool()
|
|||
return connectionPools[poolIndex];
|
||||
}
|
||||
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
#define WEBSOCKET_POOL_NUMBER 999
|
||||
|
||||
Servatrice_WebsocketGameServer::Servatrice_WebsocketGameServer(Servatrice *_server,
|
||||
|
|
@ -163,7 +162,6 @@ Servatrice_ConnectionPool *Servatrice_WebsocketGameServer::findLeastUsedConnecti
|
|||
qDebug() << "Pool utilisation:" << debugStr;
|
||||
return connectionPools[poolIndex];
|
||||
}
|
||||
#endif
|
||||
|
||||
void Servatrice_IslServer::incomingConnection(qintptr socketDescriptor)
|
||||
{
|
||||
|
|
@ -431,7 +429,6 @@ bool Servatrice::initServer()
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
// WEBSOCKET SERVER
|
||||
if (getNumberOfWebSocketPools() > 0) {
|
||||
websocketGameServer = new Servatrice_WebsocketGameServer(this, getNumberOfWebSocketPools(),
|
||||
|
|
@ -447,7 +444,6 @@ bool Servatrice::initServer()
|
|||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (getIdleClientTimeout() > 0) {
|
||||
qDebug() << "Idle client timeout value: " << getIdleClientTimeout();
|
||||
|
|
|
|||
|
|
@ -20,10 +20,6 @@
|
|||
#ifndef SERVATRICE_H
|
||||
#define SERVATRICE_H
|
||||
|
||||
#include <QTcpServer>
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
#include <QWebSocketServer>
|
||||
#endif
|
||||
#include "server.h"
|
||||
#include <QHostAddress>
|
||||
#include <QMetaType>
|
||||
|
|
@ -32,6 +28,8 @@
|
|||
#include <QSqlDatabase>
|
||||
#include <QSslCertificate>
|
||||
#include <QSslKey>
|
||||
#include <QTcpServer>
|
||||
#include <QWebSocketServer>
|
||||
#include <utility>
|
||||
|
||||
Q_DECLARE_METATYPE(QSqlDatabase)
|
||||
|
|
@ -66,7 +64,6 @@ protected:
|
|||
Servatrice_ConnectionPool *findLeastUsedConnectionPool();
|
||||
};
|
||||
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
class Servatrice_WebsocketGameServer : public QWebSocketServer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -86,7 +83,6 @@ protected:
|
|||
protected slots:
|
||||
void onNewConnection();
|
||||
};
|
||||
#endif
|
||||
|
||||
class Servatrice_IslServer : public QTcpServer
|
||||
{
|
||||
|
|
@ -158,9 +154,7 @@ private:
|
|||
DatabaseType databaseType;
|
||||
QTimer *pingClock, *statusUpdateClock;
|
||||
Servatrice_GameServer *gameServer;
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
Servatrice_WebsocketGameServer *websocketGameServer;
|
||||
#endif
|
||||
Servatrice_IslServer *islServer;
|
||||
mutable QMutex loginMessageMutex;
|
||||
QString loginMessage;
|
||||
|
|
|
|||
|
|
@ -1037,8 +1037,8 @@ Response::ResponseCode AbstractServerSocketInterface::cmdRegisterAccount(const C
|
|||
return Response::RespUserAlreadyExists;
|
||||
}
|
||||
|
||||
if (servatrice->getMaxAccountsPerEmail() &&
|
||||
!(sqlInterface->checkNumberOfUserAccounts(emailAddress) < servatrice->getMaxAccountsPerEmail())) {
|
||||
if (servatrice->getMaxAccountsPerEmail() > 0 &&
|
||||
sqlInterface->checkNumberOfUserAccounts(emailAddress) >= servatrice->getMaxAccountsPerEmail()) {
|
||||
if (servatrice->getEnableRegistrationAudit())
|
||||
sqlInterface->addAuditRecord(QString::fromStdString(cmd.user_name()).simplified(), this->getAddress(),
|
||||
QString::fromStdString(cmd.clientid()).simplified(), "REGISTER_ACCOUNT",
|
||||
|
|
@ -1629,7 +1629,6 @@ bool TcpServerSocketInterface::initTcpSession()
|
|||
return true;
|
||||
}
|
||||
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
WebsocketServerSocketInterface::WebsocketServerSocketInterface(Servatrice *_server,
|
||||
Servatrice_DatabaseInterface *_databaseInterface,
|
||||
QObject *parent)
|
||||
|
|
@ -1647,6 +1646,22 @@ WebsocketServerSocketInterface::~WebsocketServerSocketInterface()
|
|||
void WebsocketServerSocketInterface::initConnection(void *_socket)
|
||||
{
|
||||
socket = (QWebSocket *)_socket;
|
||||
address = socket->peerAddress();
|
||||
|
||||
QByteArray websocketIPHeader = settingsCache->value("server/web_socket_ip_header", "").toByteArray();
|
||||
if (websocketIPHeader.length() > 0) {
|
||||
#if QT_VERSION >= 0x050600
|
||||
if (socket->request().hasRawHeader(websocketIPHeader)) {
|
||||
QString header(socket->request().rawHeader(websocketIPHeader));
|
||||
QHostAddress parsed(header);
|
||||
if (!parsed.isNull())
|
||||
address = parsed;
|
||||
}
|
||||
#else
|
||||
logger->logMessage(QString("Reading the websocket IP header is unsupported on this version of QT."));
|
||||
#endif
|
||||
}
|
||||
|
||||
connect(socket, SIGNAL(binaryMessageReceived(const QByteArray &)), this,
|
||||
SLOT(binaryMessageReceived(const QByteArray &)));
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this,
|
||||
|
|
@ -1656,7 +1671,9 @@ void WebsocketServerSocketInterface::initConnection(void *_socket)
|
|||
// Otherwise, in case a of a socket error, it could be removed from the list before it is added.
|
||||
server->addClient(this);
|
||||
|
||||
logger->logMessage(QString("Incoming websocket connection: %1").arg(socket->peerAddress().toString()), this);
|
||||
logger->logMessage(
|
||||
QString("Incoming websocket connection: %1 (%2)").arg(address.toString()).arg(socket->peerAddress().toString()),
|
||||
this);
|
||||
|
||||
if (!initWebsocketSession())
|
||||
prepareDestroy();
|
||||
|
|
@ -1746,5 +1763,3 @@ void WebsocketServerSocketInterface::binaryMessageReceived(const QByteArray &mes
|
|||
|
||||
processCommandContainer(newCommandContainer);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -20,13 +20,11 @@
|
|||
#ifndef SERVERSOCKETINTERFACE_H
|
||||
#define SERVERSOCKETINTERFACE_H
|
||||
|
||||
#include <QTcpSocket>
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
#include <QWebSocket>
|
||||
#endif
|
||||
#include "server_protocolhandler.h"
|
||||
#include <QHostAddress>
|
||||
#include <QMutex>
|
||||
#include <QTcpSocket>
|
||||
#include <QWebSocket>
|
||||
|
||||
class Servatrice;
|
||||
class Servatrice_DatabaseInterface;
|
||||
|
|
@ -181,7 +179,6 @@ public slots:
|
|||
void initConnection(int socketDescriptor);
|
||||
};
|
||||
|
||||
#ifdef QT_WEBSOCKETS_LIB
|
||||
class WebsocketServerSocketInterface : public AbstractServerSocketInterface
|
||||
{
|
||||
Q_OBJECT
|
||||
|
|
@ -193,11 +190,11 @@ public:
|
|||
|
||||
QHostAddress getPeerAddress() const
|
||||
{
|
||||
return socket->peerAddress();
|
||||
return address;
|
||||
}
|
||||
QString getAddress() const
|
||||
{
|
||||
return socket->peerAddress().toString();
|
||||
return address.toString();
|
||||
}
|
||||
QString getConnectionType() const
|
||||
{
|
||||
|
|
@ -206,6 +203,7 @@ public:
|
|||
|
||||
private:
|
||||
QWebSocket *socket;
|
||||
QHostAddress address;
|
||||
|
||||
protected:
|
||||
void writeToSocket(QByteArray &data)
|
||||
|
|
@ -223,6 +221,5 @@ protected slots:
|
|||
public slots:
|
||||
void initConnection(void *_socket);
|
||||
};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue