lobby.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * lobby.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "lobby.h"
  12. #include "../lib/GameConstants.h"
  13. SocketLobby::SocketLobby(QObject *parent) :
  14. QObject(parent)
  15. {
  16. socket = new QTcpSocket(this);
  17. connect(socket, SIGNAL(connected()), this, SLOT(connected()));
  18. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
  19. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
  20. connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
  21. }
  22. void SocketLobby::connectServer(const QString & host, int port, const QString & usr)
  23. {
  24. const int connectionTimeout = 2000;
  25. username = usr;
  26. emit text("Connecting to " + host + ":" + QString::number(port));
  27. socket->connectToHost(host, port);
  28. if(!socket->waitForDisconnected(connectionTimeout) && !isConnected)
  29. {
  30. emit text("Error: " + socket->errorString());
  31. }
  32. }
  33. void SocketLobby::disconnectServer()
  34. {
  35. socket->disconnectFromHost();
  36. }
  37. void SocketLobby::requestNewSession(const QString & session, int totalPlayers, const QString & pswd)
  38. {
  39. const QString sessionMessage = ProtocolStrings[CREATE].arg(session, pswd, QString::number(totalPlayers));
  40. send(sessionMessage);
  41. }
  42. void SocketLobby::requestJoinSession(const QString & session, const QString & pswd)
  43. {
  44. const QString sessionMessage = ProtocolStrings[JOIN].arg(session, pswd);
  45. send(sessionMessage);
  46. }
  47. void SocketLobby::requestLeaveSession(const QString & session)
  48. {
  49. const QString sessionMessage = ProtocolStrings[LEAVE].arg(session);
  50. send(sessionMessage);
  51. }
  52. void SocketLobby::requestReadySession(const QString & session)
  53. {
  54. const QString sessionMessage = ProtocolStrings[READY].arg(session);
  55. send(sessionMessage);
  56. }
  57. void SocketLobby::send(const QString & msg)
  58. {
  59. QByteArray str = msg.toUtf8();
  60. int sz = str.size();
  61. QByteArray pack((const char *)&sz, sizeof(sz));
  62. pack.append(str);
  63. socket->write(pack);
  64. }
  65. void SocketLobby::connected()
  66. {
  67. isConnected = true;
  68. emit text("Connected!");
  69. QByteArray greetingBytes;
  70. greetingBytes.append(ProtocolVersion);
  71. greetingBytes.append(ProtocolEncoding.size());
  72. const QString greetingConst = QString(greetingBytes)
  73. + ProtocolStrings[GREETING].arg(QString::fromStdString(ProtocolEncoding),
  74. username,
  75. QString::fromStdString(GameConstants::VCMI_VERSION));
  76. send(greetingConst);
  77. }
  78. void SocketLobby::disconnected()
  79. {
  80. isConnected = false;
  81. emit disconnect();
  82. emit text("Disconnected!");
  83. }
  84. void SocketLobby::bytesWritten(qint64 bytes)
  85. {
  86. qDebug() << "We wrote: " << bytes;
  87. }
  88. void SocketLobby::readyRead()
  89. {
  90. qDebug() << "Reading...";
  91. emit receive(socket->readAll());
  92. }
  93. ServerCommand::ServerCommand(ProtocolConsts cmd, const QStringList & args):
  94. command(cmd),
  95. arguments(args)
  96. {
  97. }