lobby.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = 1;
  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, READY,
  24. //server consts
  25. SESSIONS, CREATED, JOINED, KICKED, SRVERROR, CHAT, START, STATUS, HOST
  26. };
  27. const QMap<ProtocolConsts, QString> ProtocolStrings
  28. {
  29. //client consts
  30. {GREETING, "%1<GREETINGS>%2<VER>%3"}, //protocol_version byte, encoding bytes, encoding, name, version
  31. {USERNAME, "<USER>%1"},
  32. {MESSAGE, "<MSG>%1"},
  33. {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3"},
  34. {JOIN, "<JOIN>%1<PSWD>%2"},
  35. {LEAVE, "<LEAVE>%1"}, //session
  36. {READY, "<READY>%1"}, //session
  37. //server consts
  38. {CREATED, "CREATED"},
  39. {SESSIONS, "SESSIONS"}, //amount:session_name:joined_players:total_players:is_protected
  40. {JOINED, "JOIN"}, //session_name:username
  41. {KICKED, "KICK"}, //session_name:username
  42. {START, "START"}, //session_name:uuid
  43. {HOST, "HOST"}, //host_uuid:players_count
  44. {STATUS, "STATUS"}, //joined_players:player_name:is_ready
  45. {SRVERROR, "ERROR"},
  46. {CHAT, "MSG"} //username:message
  47. };
  48. class ServerCommand
  49. {
  50. public:
  51. ServerCommand(ProtocolConsts, const QStringList & arguments);
  52. const ProtocolConsts command;
  53. const QStringList arguments;
  54. };
  55. class SocketLobby : public QObject
  56. {
  57. Q_OBJECT
  58. public:
  59. explicit SocketLobby(QObject *parent = 0);
  60. void connectServer(const QString & host, int port, const QString & username, int timeout);
  61. void disconnectServer();
  62. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd);
  63. void requestJoinSession(const QString & session, const QString & pswd);
  64. void requestLeaveSession(const QString & session);
  65. void requestReadySession(const QString & session);
  66. void send(const QString &);
  67. signals:
  68. void text(QString);
  69. void receive(QString);
  70. void disconnect();
  71. public slots:
  72. void connected();
  73. void disconnected();
  74. void bytesWritten(qint64 bytes);
  75. void readyRead();
  76. private:
  77. QTcpSocket *socket;
  78. bool isConnected = false;
  79. QString username;
  80. };