lobby.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * lobby.h, 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. #pragma once
  11. #include <QTcpSocket>
  12. #include <QAbstractSocket>
  13. const unsigned int ProtocolVersion = 3;
  14. const std::string ProtocolEncoding = "utf8";
  15. class ProtocolError: public std::runtime_error
  16. {
  17. public:
  18. ProtocolError(const char * w): std::runtime_error(w) {}
  19. };
  20. enum ProtocolConsts
  21. {
  22. //client consts
  23. GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN, LEAVE, KICK, READY, FORCESTART,
  24. //server consts
  25. SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST, MODS, CLIENTMODS
  26. };
  27. const QMap<ProtocolConsts, QString> ProtocolStrings
  28. {
  29. //=== client commands ===
  30. //handshaking with server
  31. //%1: first byte is protocol_version, then size of encoding string in bytes, then encoding string
  32. //%2: client name
  33. //%3: VCMI version
  34. {GREETING, "%1<GREETINGS>%2<VER>%3"},
  35. //[unsupported] autorization with username
  36. //%1: username
  37. {USERNAME, "<USER>%1"},
  38. //sending message to the chat
  39. //%1: message text
  40. {MESSAGE, "<MSG>%1"},
  41. //create new room
  42. //%1: room name
  43. //%2: password for the room
  44. //%3: max number of players
  45. //%4: mods used by host
  46. // each mod has a format modname&modversion, mods should be separated by ; symbol
  47. {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3<MODS>%4"},
  48. //join to the room
  49. //%1: room name
  50. //%2: password for the room
  51. //%3: list of mods used by player
  52. // each mod has a format modname&modversion, mods should be separated by ; symbol
  53. {JOIN, "<JOIN>%1<PSWD>%2<MODS>%3"},
  54. //leave the room
  55. //%1: room name
  56. {LEAVE, "<LEAVE>%1"},
  57. //kick user from the current room
  58. //%1: player username
  59. {KICK, "<KICK>%1"},
  60. //signal that player is ready for game
  61. //%1: room name
  62. {READY, "<READY>%1"},
  63. //[unsupported] start session immediately
  64. //%1: room name
  65. {FORCESTART, "<FORCESTART>%1"},
  66. //=== server commands ===
  67. //server commands are started from :>>, arguments are enumerated by : symbol
  68. //new session was created
  69. //arg[0]: room name
  70. {CREATED, "CREATED"},
  71. //list of existing sessions
  72. //arg[0]: amount of sessions, following arguments depending on it
  73. //arg[x]: session name
  74. //arg[x+1]: amount of players in the session
  75. //arg[x+2]: total amount of players allowed
  76. //arg[x+3]: True if session is protected by password
  77. {SESSIONS, "SESSIONS"},
  78. //user has joined to the session
  79. //arg[0]: session name
  80. //arg[1]: username (who was joined)
  81. {JOINED, "JOIN"},
  82. //user has left the session
  83. //arg[0]: session name
  84. //arg[1]: username (who has left)
  85. {KICKED, "KICK"},
  86. //session has been started
  87. //arg[0]: session name
  88. //arg[1]: uuid to be used for connection
  89. {START, "START"},
  90. //host ownership for the game session
  91. //arg[0]: uuid to be used by vcmiserver
  92. //arg[1]: amount of players (clients) to be connected
  93. {HOST, "HOST"},
  94. //room status
  95. //arg[0]: amount of players, following arguments depending on it
  96. //arg[x]: player username
  97. //arg[x+1]: True if player is ready
  98. {STATUS, "STATUS"}, //joined_players:player_name:is_ready
  99. //server error
  100. //arg[0]: error message
  101. {SRVERROR, "ERROR"},
  102. //mods used in the session by host player
  103. //arg[0]: amount of mods, following arguments depending on it
  104. //arg[x]: mod name
  105. //arg[x+1]: mod version
  106. {MODS, "MODS"},
  107. //mods used by user
  108. //arg[0]: username
  109. //arg[1]: amount of mods, following arguments depending on it
  110. //arg[x]: mod name
  111. //arg[x+1]: mod version
  112. {CLIENTMODS, "MODSOTHER"},
  113. //received chat message
  114. //arg[0]: sender username
  115. //arg[1]: message text
  116. {CHAT, "MSG"}
  117. };
  118. class ServerCommand
  119. {
  120. public:
  121. ServerCommand(ProtocolConsts, const QStringList & arguments);
  122. const ProtocolConsts command;
  123. const QStringList arguments;
  124. };
  125. class SocketLobby : public QObject
  126. {
  127. Q_OBJECT
  128. public:
  129. explicit SocketLobby(QObject *parent = 0);
  130. void connectServer(const QString & host, int port, const QString & username, int timeout);
  131. void disconnectServer();
  132. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd, const QMap<QString, QString> & mods);
  133. void requestJoinSession(const QString & session, const QString & pswd, const QMap<QString, QString> & mods);
  134. void requestLeaveSession(const QString & session);
  135. void requestReadySession(const QString & session);
  136. void send(const QString &);
  137. signals:
  138. void text(QString);
  139. void receive(QString);
  140. void disconnect();
  141. public slots:
  142. void connected();
  143. void disconnected();
  144. void bytesWritten(qint64 bytes);
  145. void readyRead();
  146. private:
  147. QTcpSocket *socket;
  148. bool isConnected = false;
  149. QString username;
  150. };
  151. QString prepareModsClientString(const QMap<QString, QString> & mods);