-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.h
69 lines (61 loc) · 1.77 KB
/
client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef CLIENT_H
#define CLIENT_H
#include <QMainWindow>
#include <QObject>
#include <QTcpSocket>
#include <QTime>
#include <QDataStream>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonParseError>
class Client : public QObject //singleton
{
Q_OBJECT
Q_DISABLE_COPY(Client)
private:
Client(QObject* parent = nullptr);
~Client();
public:
static Client* instance(QObject* parent = nullptr);
const QTcpSocket* socketInfo();
//---------getters-----------
const QString& getUserName();
const QString& getUserPassword();
quint16 getRoomNum();
const QDateTime& getLastMessageTime();
//---------setters-----------
void setUserName(QString userName);
void setUserPassword(QString hostName);
void setRoomNum(quint16 roomNum);
void setLastMessageTime();
signals:
void connected();
void disconnected();
void loggedIn();
void loginError(const QString& reason);
void messageReceived(const QString& sender, const QString& text);
void errorSignal(QAbstractSocket::SocketError socket_error);
void userJoined(const QString& username);
void userLeft(const QString& username);
public slots:
void connectToServer(const QHostAddress& address, quint16 port);
void login(const QString& username);
void sendMessage(const QString& text);
void disconnectFromHost();
private slots:
void onReadyRead();
private:
void initSocket();
void jsonReceived(const QJsonObject& doc);
private:
QTcpSocket* client_socket;
QString user_name;
QString user_password;
quint16 room_number;
QDateTime last_message_time = {};
bool logged_in;
quint16 nextBlockSize = 0; //the variable for keep size of reciving data
};
#endif // CLIENT_H