lobby_moc.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef LOBBY_MOC_H
  2. #define LOBBY_MOC_H
  3. #include <QWidget>
  4. #include <QTcpSocket>
  5. #include <QAbstractSocket>
  6. class ProtocolError: public std::runtime_error
  7. {
  8. public:
  9. ProtocolError(const char * w): std::runtime_error(w) {}
  10. };
  11. enum ProtocolConsts
  12. {
  13. //client consts
  14. GREETING, USERNAME, MESSAGE, VERSION, CREATE, JOIN,
  15. //server consts
  16. SESSIONS, CREATED, JOINED, KICKED, ERROR, CHAT
  17. };
  18. const QMap<ProtocolConsts, QString> ProtocolStrings
  19. {
  20. //client consts
  21. {GREETING, "<GREETINGS>%1<VER>%2"},
  22. {USERNAME, "<USER>%1"},
  23. {MESSAGE, "<MSG>%1"},
  24. {CREATE, "<NEW>%1<PSWD>%2<COUNT>%3"},
  25. {JOIN, "<JOIN>%1<PSWD>%2"},
  26. //server consts
  27. {CREATED, "CREATED"},
  28. {SESSIONS, "SESSIONS"}, //amount:session_name:joined_players:total_players:is_protected
  29. {JOINED, "JOIN"}, //session_name:username
  30. {KICKED, "KICK"}, //session_name:username
  31. {ERROR, "ERROR"},
  32. {CHAT, "MSG"} //username:message
  33. };
  34. class ServerCommand
  35. {
  36. public:
  37. ServerCommand(ProtocolConsts, const QStringList & arguments);
  38. const ProtocolConsts command;
  39. const QStringList arguments;
  40. };
  41. class SocketLobby : public QObject
  42. {
  43. Q_OBJECT
  44. public:
  45. explicit SocketLobby(QObject *parent = 0);
  46. void connectServer(const QString & host, int port, const QString & username);
  47. void disconnectServer();
  48. void requestNewSession(const QString & session, int totalPlayers, const QString & pswd);
  49. void requestJoinSession(const QString & session, const QString & pswd);
  50. void send(const QString &);
  51. signals:
  52. void text(QString);
  53. void receive(QString);
  54. public slots:
  55. void connected();
  56. void disconnected();
  57. void bytesWritten(qint64 bytes);
  58. void readyRead();
  59. private:
  60. QTcpSocket *socket;
  61. bool isConnected = false;
  62. QString username;
  63. };
  64. namespace Ui {
  65. class Lobby;
  66. }
  67. class Lobby : public QWidget
  68. {
  69. Q_OBJECT
  70. public:
  71. explicit Lobby(QWidget *parent = nullptr);
  72. ~Lobby();
  73. private slots:
  74. void on_messageEdit_returnPressed();
  75. void chatMessage(QString);
  76. void dispatchMessage(QString);
  77. void serverCommand(const ServerCommand &);
  78. void on_connectButton_toggled(bool checked);
  79. void on_newButton_clicked();
  80. void on_joinButton_clicked();
  81. private:
  82. Ui::Lobby *ui;
  83. SocketLobby socketLobby;
  84. QString hostSession;
  85. QString session;
  86. QString username;
  87. private:
  88. void protocolAssert(bool);
  89. };
  90. #endif // LOBBY_MOC_H