1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/launcher/lobby/lobby.cpp

124 lines
3.1 KiB
C++
Raw Normal View History

2022-11-10 02:20:44 +02:00
/*
* lobby.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "lobby.h"
#include "../lib/GameConstants.h"
SocketLobby::SocketLobby(QObject *parent) :
QObject(parent)
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
}
2022-11-16 02:22:48 +02:00
void SocketLobby::connectServer(const QString & host, int port, const QString & usr, int timeout)
2022-11-10 02:20:44 +02:00
{
username = usr;
socket->connectToHost(host, port);
2022-11-16 02:22:48 +02:00
if(!socket->waitForDisconnected(timeout) && !isConnected)
2022-11-10 02:20:44 +02:00
{
emit text("Error: " + socket->errorString());
2022-11-16 21:53:54 +02:00
emit disconnect();
2022-11-10 02:20:44 +02:00
}
}
void SocketLobby::disconnectServer()
{
socket->disconnectFromHost();
}
2022-11-17 01:15:26 +02:00
void SocketLobby::requestNewSession(const QString & session, int totalPlayers, const QString & pswd, const QMap<QString, QString> & mods)
2022-11-10 02:20:44 +02:00
{
2022-11-17 01:15:26 +02:00
const QString sessionMessage = ProtocolStrings[CREATE].arg(session, pswd, QString::number(totalPlayers), prepareModsClientString(mods));
2022-11-10 02:20:44 +02:00
send(sessionMessage);
}
2022-11-17 01:15:26 +02:00
void SocketLobby::requestJoinSession(const QString & session, const QString & pswd, const QMap<QString, QString> & mods)
2022-11-10 02:20:44 +02:00
{
2022-11-17 01:15:26 +02:00
const QString sessionMessage = ProtocolStrings[JOIN].arg(session, pswd, prepareModsClientString(mods));
2022-11-10 02:20:44 +02:00
send(sessionMessage);
}
void SocketLobby::requestLeaveSession(const QString & session)
{
const QString sessionMessage = ProtocolStrings[LEAVE].arg(session);
send(sessionMessage);
}
void SocketLobby::requestReadySession(const QString & session)
{
const QString sessionMessage = ProtocolStrings[READY].arg(session);
send(sessionMessage);
}
void SocketLobby::send(const QString & msg)
{
2022-11-15 04:11:51 +02:00
QByteArray str = msg.toUtf8();
2022-11-15 02:44:43 +02:00
int sz = str.size();
2022-11-10 02:20:44 +02:00
QByteArray pack((const char *)&sz, sizeof(sz));
2022-11-15 02:44:43 +02:00
pack.append(str);
2022-11-10 02:20:44 +02:00
socket->write(pack);
}
void SocketLobby::connected()
{
isConnected = true;
emit text("Connected!");
QByteArray greetingBytes;
greetingBytes.append(ProtocolVersion);
greetingBytes.append(ProtocolEncoding.size());
const QString greetingConst = QString(greetingBytes)
+ ProtocolStrings[GREETING].arg(QString::fromStdString(ProtocolEncoding),
username,
QString::fromStdString(GameConstants::VCMI_VERSION));
send(greetingConst);
}
void SocketLobby::disconnected()
{
isConnected = false;
emit disconnect();
emit text("Disconnected!");
}
void SocketLobby::bytesWritten(qint64 bytes)
{
qDebug() << "We wrote: " << bytes;
}
void SocketLobby::readyRead()
{
qDebug() << "Reading...";
emit receive(socket->readAll());
}
ServerCommand::ServerCommand(ProtocolConsts cmd, const QStringList & args):
command(cmd),
arguments(args)
{
}
2022-11-17 01:15:26 +02:00
QString prepareModsClientString(const QMap<QString, QString> & mods)
{
QStringList result;
for(auto & mod : mods.keys())
{
result << mod + "&" + mods[mod];
}
return result.join(";");
}