lobby.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, int timeout)
  23. {
  24. username = usr;
  25. socket->connectToHost(host, port);
  26. if(!socket->waitForDisconnected(timeout) && !isConnected)
  27. {
  28. emit text("Error: " + socket->errorString());
  29. emit disconnect();
  30. }
  31. }
  32. void SocketLobby::disconnectServer()
  33. {
  34. socket->disconnectFromHost();
  35. }
  36. void SocketLobby::requestNewSession(const QString & session, int totalPlayers, const QString & pswd, const QMap<QString, QString> & mods)
  37. {
  38. const QString sessionMessage = ProtocolStrings[CREATE].arg(session, pswd, QString::number(totalPlayers), prepareModsClientString(mods));
  39. send(sessionMessage);
  40. }
  41. void SocketLobby::requestJoinSession(const QString & session, const QString & pswd, const QMap<QString, QString> & mods)
  42. {
  43. const QString sessionMessage = ProtocolStrings[JOIN].arg(session, pswd, prepareModsClientString(mods));
  44. send(sessionMessage);
  45. }
  46. void SocketLobby::requestLeaveSession(const QString & session)
  47. {
  48. const QString sessionMessage = ProtocolStrings[LEAVE].arg(session);
  49. send(sessionMessage);
  50. }
  51. void SocketLobby::requestReadySession(const QString & session)
  52. {
  53. const QString sessionMessage = ProtocolStrings[READY].arg(session);
  54. send(sessionMessage);
  55. }
  56. void SocketLobby::send(const QString & msg)
  57. {
  58. QByteArray str = msg.toUtf8();
  59. int sz = str.size();
  60. QByteArray pack((const char *)&sz, sizeof(sz));
  61. pack.append(str);
  62. socket->write(pack);
  63. }
  64. void SocketLobby::connected()
  65. {
  66. isConnected = true;
  67. emit text("Connected!");
  68. QByteArray greetingBytes;
  69. greetingBytes.append(ProtocolVersion);
  70. greetingBytes.append(ProtocolEncoding.size());
  71. const QString greetingConst = QString(greetingBytes)
  72. + ProtocolStrings[GREETING].arg(QString::fromStdString(ProtocolEncoding),
  73. username,
  74. QString::fromStdString(GameConstants::VCMI_VERSION));
  75. send(greetingConst);
  76. }
  77. void SocketLobby::disconnected()
  78. {
  79. isConnected = false;
  80. emit disconnect();
  81. emit text("Disconnected!");
  82. }
  83. void SocketLobby::bytesWritten(qint64 bytes)
  84. {
  85. qDebug() << "We wrote: " << bytes;
  86. }
  87. void SocketLobby::readyRead()
  88. {
  89. qDebug() << "Reading...";
  90. emit receive(socket->readAll());
  91. }
  92. ServerCommand::ServerCommand(ProtocolConsts cmd, const QStringList & args):
  93. command(cmd),
  94. arguments(args)
  95. {
  96. }
  97. QString prepareModsClientString(const QMap<QString, QString> & mods)
  98. {
  99. QStringList result;
  100. for(auto & mod : mods.keys())
  101. {
  102. result << mod + "&" + mods[mod];
  103. }
  104. return result.join(";");
  105. }