|
@@ -1,7 +1,8 @@
|
|
|
#include "lobby_moc.h"
|
|
#include "lobby_moc.h"
|
|
|
#include "ui_lobby_moc.h"
|
|
#include "ui_lobby_moc.h"
|
|
|
|
|
+#include "../lib/GameConstants.h"
|
|
|
|
|
|
|
|
-SocketTest::SocketTest(QObject *parent) :
|
|
|
|
|
|
|
+SocketLobby::SocketLobby(QObject *parent) :
|
|
|
QObject(parent)
|
|
QObject(parent)
|
|
|
{
|
|
{
|
|
|
socket = new QTcpSocket(this);
|
|
socket = new QTcpSocket(this);
|
|
@@ -11,43 +12,52 @@ SocketTest::SocketTest(QObject *parent) :
|
|
|
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
|
|
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::Test()
|
|
|
|
|
|
|
+void SocketLobby::connectServer(const QString & host, int port, const QString & usr)
|
|
|
{
|
|
{
|
|
|
- qDebug() << "Connecting,..";
|
|
|
|
|
- emit text("Connecting to 127.0.0.1:5002...");
|
|
|
|
|
|
|
+ const int connectionTimeout = 1000;
|
|
|
|
|
+ username = usr;
|
|
|
|
|
|
|
|
- socket->connectToHost("127.0.0.1", 5002);
|
|
|
|
|
|
|
+ emit text("Connecting to " + host + ":" + QString::number(port));
|
|
|
|
|
|
|
|
- if(!socket->waitForDisconnected(1000) && !isConnected)
|
|
|
|
|
|
|
+ socket->connectToHost(host, port);
|
|
|
|
|
+
|
|
|
|
|
+ if(!socket->waitForDisconnected(connectionTimeout) && !isConnected)
|
|
|
{
|
|
{
|
|
|
emit text("Error: " + socket->errorString());
|
|
emit text("Error: " + socket->errorString());
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+void SocketLobby::disconnectServer()
|
|
|
|
|
+{
|
|
|
|
|
+ socket->disconnectFromHost();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::send(const QString & msg)
|
|
|
|
|
|
|
+void SocketLobby::send(const QString & msg)
|
|
|
{
|
|
{
|
|
|
socket->write(qPrintable(msg));
|
|
socket->write(qPrintable(msg));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::connected()
|
|
|
|
|
|
|
+void SocketLobby::connected()
|
|
|
{
|
|
{
|
|
|
isConnected = true;
|
|
isConnected = true;
|
|
|
emit text("Connected!");
|
|
emit text("Connected!");
|
|
|
|
|
+
|
|
|
|
|
+ const QString greetingConst = ProtocolStrings[GREETING].arg(username) + ProtocolStrings[VERSION].arg(QString::fromStdString(GameConstants::VCMI_VERSION));
|
|
|
|
|
+ send(greetingConst);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::disconnected()
|
|
|
|
|
|
|
+void SocketLobby::disconnected()
|
|
|
{
|
|
{
|
|
|
isConnected = false;
|
|
isConnected = false;
|
|
|
emit text("Disconnected!");
|
|
emit text("Disconnected!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::bytesWritten(qint64 bytes)
|
|
|
|
|
|
|
+void SocketLobby::bytesWritten(qint64 bytes)
|
|
|
{
|
|
{
|
|
|
qDebug() << "We wrote: " << bytes;
|
|
qDebug() << "We wrote: " << bytes;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void SocketTest::readyRead()
|
|
|
|
|
|
|
+void SocketLobby::readyRead()
|
|
|
{
|
|
{
|
|
|
qDebug() << "Reading...";
|
|
qDebug() << "Reading...";
|
|
|
emit text(socket->readAll());
|
|
emit text(socket->readAll());
|
|
@@ -55,15 +65,14 @@ void SocketTest::readyRead()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
Lobby::Lobby(QWidget *parent) :
|
|
Lobby::Lobby(QWidget *parent) :
|
|
|
QWidget(parent),
|
|
QWidget(parent),
|
|
|
ui(new Ui::Lobby)
|
|
ui(new Ui::Lobby)
|
|
|
{
|
|
{
|
|
|
ui->setupUi(this);
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
- connect(&mTest, SIGNAL(text(QString)), this, SLOT(text(QString)));
|
|
|
|
|
-
|
|
|
|
|
- mTest.Test();
|
|
|
|
|
|
|
+ connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(text(QString)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Lobby::~Lobby()
|
|
Lobby::~Lobby()
|
|
@@ -71,10 +80,10 @@ Lobby::~Lobby()
|
|
|
delete ui;
|
|
delete ui;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Lobby::on_lineEdit_returnPressed()
|
|
|
|
|
|
|
+void Lobby::on_messageEdit_returnPressed()
|
|
|
{
|
|
{
|
|
|
- mTest.send(ui->lineEdit->text());
|
|
|
|
|
- ui->lineEdit->clear();
|
|
|
|
|
|
|
+ socketLobby.send(ProtocolStrings[MESSAGE].arg(ui->messageEdit->text()));
|
|
|
|
|
+ ui->messageEdit->clear();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Lobby::text(QString txt)
|
|
void Lobby::text(QString txt)
|
|
@@ -84,3 +93,15 @@ void Lobby::text(QString txt)
|
|
|
curs.insertText(txt + "\n");
|
|
curs.insertText(txt + "\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void Lobby::on_connectButton_toggled(bool checked)
|
|
|
|
|
+{
|
|
|
|
|
+ if(checked)
|
|
|
|
|
+ {
|
|
|
|
|
+ socketLobby.connectServer(ui->hostEdit->text(), ui->portEdit->text().toInt(), ui->userEdit->text());
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ socketLobby.disconnectServer();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|