Skip to content

Commit 453c9b5

Browse files
committed
Merge pull request #29 from wuming123057/master
I have modified the global.h file, and the export message class
2 parents d88915e + 4a28dd2 commit 453c9b5

File tree

7 files changed

+107
-65
lines changed

7 files changed

+107
-65
lines changed

qmqtt_client.cpp

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,117 +36,145 @@
3636
namespace QMQTT {
3737

3838
Client::Client(const QString & host, quint32 port, QObject * parent /* =0 */)
39-
:pd_ptr(new ClientPrivate(this))
39+
:d_ptr(new ClientPrivate(this))
40+
4041
{
41-
pd_func()->init(host, port, parent);
42+
Q_D(Client);
43+
d->init(host, port, parent);
4244
}
4345

4446
Client::~Client()
4547
{
4648
//?
49+
Q_D(Client);
50+
delete d;
4751
}
4852

4953
/*----------------------------------------------------------------
5054
* Get/Set Property
5155
----------------------------------------------------------------*/
5256
QString Client::host() const
5357
{
54-
return pd_func()->host;
58+
Q_D(const Client);
59+
// QString* str = d->host;
60+
// return const_cast<QString *const>(d->host);
61+
// return d->host;
62+
return d->host;
5563
}
5664

5765
void Client::setHost(const QString & host)
5866
{
59-
pd_func()->host = host;
67+
// d->host = host;
68+
Q_D(Client);
69+
d->host = host;
6070
}
6171

6272
quint32 Client::port() const
6373
{
64-
return pd_func()->port;
74+
Q_D(const Client);
75+
return d->port;
6576
}
6677

6778
void Client::setPort(quint32 port)
6879
{
69-
pd_func()->port = port;
80+
Q_D(Client);
81+
d->port = port;
7082
}
7183

7284
QString Client::clientId() const
7385
{
74-
return pd_func()->clientId;
86+
Q_D(const Client);
87+
return d->clientId;
7588
}
7689

7790
void Client::setClientId(const QString &clientId)
7891
{
79-
pd_func()->clientId = clientId;
92+
Q_D(Client);
93+
d->clientId = clientId;
8094
}
8195

8296
QString Client::username() const
8397
{
84-
return pd_func()->username;
98+
Q_D(const Client);
99+
return d->username;
85100
}
86101

87102
void Client::setUsername(const QString & username)
88103
{
89-
pd_func()->username = username;
104+
Q_D(Client);
105+
d->username = username;
90106
}
91107

92108
QString Client::password() const
93109
{
94-
return pd_func()->password;
110+
Q_D(const Client);
111+
return d->password;
95112
}
96113

97114
void Client::setPassword(const QString & password)
98115
{
99-
pd_func()->password = password;
116+
Q_D(Client);
117+
d->password = password;
100118
}
101119

102120
int Client::keepalive()
103121
{
104-
return pd_func()->keepalive;
122+
Q_D( Client);
123+
return d->keepalive;
105124
}
106125

107126
void Client::setKeepAlive(int keepalive)
108127
{
109-
pd_func()->keepalive = keepalive;
128+
Q_D(Client);
129+
d->keepalive = keepalive;
110130
}
111131

112132
bool Client::cleansess()
113133
{
114-
return pd_func()->cleansess;
134+
Q_D(Client);
135+
return d->cleansess;
115136
}
116137

117138
void Client::setCleansess(bool cleansess)
118139
{
119-
pd_func()->cleansess = cleansess;
140+
Q_D(Client);
141+
d->cleansess = cleansess;
120142
}
121143

122144
bool Client::autoReconnect() const
123145
{
124-
return pd_func()->network->autoReconnect();
146+
Q_D(const Client);
147+
return d->network->autoReconnect();
125148
}
126149

127150
void Client::setAutoReconnect(bool value)
128151
{
129-
pd_func()->network->setAutoReconnect(value);
152+
Q_D(Client);
153+
d->network->setAutoReconnect(value);
130154
}
131155

132156
Will *Client::will()
133157
{
134-
return pd_func()->will;
158+
Q_D(Client);
159+
return d->will;
135160
}
136161

137162
void Client::setWill(Will *will)
138163
{
139-
pd_func()->will = will;
164+
Q_D(Client);
165+
d->will = will;
140166
}
141167

142168
State Client::state() const
143169
{
144-
return pd_func()->state;
170+
Q_D(const Client);
171+
return d->state;
145172
}
146173

147174
bool Client::isConnected()
148175
{
149-
return pd_func()->network->isConnected();
176+
Q_D(Client);
177+
return d->network->isConnected();
150178
}
151179

152180

@@ -155,56 +183,65 @@ bool Client::isConnected()
155183
----------------------------------------------------------------*/
156184
void Client::connect()
157185
{
158-
pd_func()->sockConnect();
186+
Q_D(Client);
187+
d->sockConnect();
159188
}
160189

161190
void Client::onConnected()
162191
{
192+
Q_D(Client);
163193
qCDebug(client) << "Sock Connected....";
164-
pd_func()->sendConnect();
165-
pd_func()->startKeepalive();
194+
d->sendConnect();
195+
d->startKeepalive();
166196
emit connected();
167197
}
168198

169199
quint16 Client::publish(Message &message)
170200
{
171-
quint16 msgid = pd_func()->sendPublish(message);
201+
Q_D(Client);
202+
quint16 msgid = d->sendPublish(message);
172203
emit published(message);
173204
return msgid;
174205
}
175206

176207
void Client::puback(quint8 type, quint16 msgid)
177208
{
178-
pd_func()->sendPuback(type, msgid);
209+
Q_D(Client);
210+
d->sendPuback(type, msgid);
179211
emit pubacked(type, msgid);
180212
}
181213

182214
quint16 Client::subscribe(const QString &topic, quint8 qos)
183215
{
184-
quint16 msgid = pd_func()->sendSubscribe(topic, qos);
216+
Q_D(Client);
217+
quint16 msgid = d->sendSubscribe(topic, qos);
185218
emit subscribed(topic);
186219
return msgid;
187220
}
188221

189222
void Client::unsubscribe(const QString &topic)
190223
{
191-
pd_func()->sendUnsubscribe(topic);
224+
Q_D(Client);
225+
d->sendUnsubscribe(topic);
192226
emit unsubscribed(topic);
193227
}
194228

195229
void Client::ping()
196230
{
197-
pd_func()->sendPing();
231+
Q_D(Client);
232+
d->sendPing();
198233
}
199234

200235
void Client::disconnect()
201236
{
202-
pd_func()->disconnect();
237+
Q_D(Client);
238+
d->disconnect();
203239
}
204240

205241
void Client::onDisconnected()
206242
{
207-
pd_func()->stopKeepalive();
243+
Q_D(Client);
244+
d->stopKeepalive();
208245
emit disconnected();
209246
}
210247

@@ -274,20 +311,22 @@ void Client::handleConnack(quint8 ack)
274311

275312
void Client::handlePublish(Message & message)
276313
{
314+
Q_D(Client);
277315
if(message.qos() == MQTT_QOS1) {
278-
pd_func()->sendPuback(PUBACK, message.id());
316+
d->sendPuback(PUBACK, message.id());
279317
} else if(message.qos() == MQTT_QOS2) {
280-
pd_func()->sendPuback(PUBREC, message.id());
318+
d->sendPuback(PUBREC, message.id());
281319
}
282320
emit received(message);
283321
}
284322

285323
void Client::handlePuback(quint8 type, quint16 msgid)
286324
{
325+
Q_D(Client);
287326
if(type == PUBREC) {
288-
pd_func()->sendPuback(PUBREL, msgid);
327+
d->sendPuback(PUBREL, msgid);
289328
} else if (type == PUBREL) {
290-
pd_func()->sendPuback(PUBCOMP, msgid);
329+
d->sendPuback(PUBCOMP, msgid);
291330
}
292331
emit pubacked(type, msgid);
293332
}

qmqtt_client.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ class QMQTTSHARED_EXPORT Client : public QObject
8888
Q_PROPERTY(int keepalive READ keepalive WRITE setKeepAlive)
8989
Q_PROPERTY(bool autoReconnect READ autoReconnect WRITE setAutoReconnect)
9090

91-
Q_DISABLE_COPY(Client)
92-
93-
P_DECLARE_PRIVATE(QMQTT::Client)
9491

95-
//friend class ClientPrivate;
92+
// friend class ClientPrivate;
9693

9794
public:
9895
Client(const QString &host = "localhost", quint32 port = 1883, QObject * parent = 0);
@@ -169,8 +166,11 @@ private slots:
169166
void handleConnack(quint8 ack);
170167
void handlePuback(quint8 type, quint16 msgid);
171168

172-
protected:
173-
ClientPrivate * const pd_ptr;
169+
private:
170+
ClientPrivate *const d_ptr;
171+
172+
Q_DISABLE_COPY(Client)
173+
Q_DECLARE_PRIVATE(Client)
174174

175175
};
176176

qmqtt_client_p.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ namespace QMQTT {
3737

3838
Q_LOGGING_CATEGORY(client, "qmqtt.client")
3939

40-
ClientPrivate::ClientPrivate(Client *q) :
40+
ClientPrivate::ClientPrivate(Client *qt) :
4141
host("localhost"),
4242
port(1883),
4343
keepalive(300),
44-
pq_ptr(q)
44+
q_ptr(qt)
4545
{
4646
gmid= 1;
4747
}
@@ -53,19 +53,20 @@ ClientPrivate::~ClientPrivate()
5353

5454
void ClientPrivate::init(QObject * parent)
5555
{
56-
pq_func()->setParent(parent);
56+
Q_Q(Client);
57+
q->setParent(parent);
5758
if(!timer) {
58-
timer = new QTimer(pq_func());
59+
timer = new QTimer(q);
5960
}
60-
QObject::connect(timer, SIGNAL(timeout()), pq_func(), SLOT(ping()));
61+
QObject::connect(timer, SIGNAL(timeout()), q, SLOT(ping()));
6162
if(!network){
62-
network = new Network(pq_func());
63+
network = new Network(q);
6364
}
6465
//TODO: FIXME LATER, how to handle socket error?
65-
QObject::connect(network, SIGNAL(connected()), pq_func(), SLOT(onConnected()));
66-
QObject::connect(network, SIGNAL(error(QAbstractSocket::SocketError)), pq_func(), SIGNAL(error(QAbstractSocket::SocketError)));
67-
QObject::connect(network, SIGNAL(disconnected()), pq_func(), SLOT(onDisconnected()));
68-
QObject::connect(network, SIGNAL(received(Frame &)), pq_func(), SLOT(onReceived(Frame &)));
66+
QObject::connect(network, SIGNAL(connected()), q, SLOT(onConnected()));
67+
QObject::connect(network, SIGNAL(error(QAbstractSocket::SocketError)), q, SIGNAL(error(QAbstractSocket::SocketError)));
68+
QObject::connect(network, SIGNAL(disconnected()), q, SLOT(onDisconnected()));
69+
QObject::connect(network, SIGNAL(received(Frame &)), q, SLOT(onReceived(Frame &)));
6970
}
7071

7172
void ClientPrivate::init(const QString &host, int port, QObject * parent)

qmqtt_client_p.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,16 @@
4242
#include "qmqtt_global.h"
4343
#include "qmqtt_message.h"
4444
#include "qmqtt_will.h"
45-
45+
#include "qmqtt_network.h"
4646
namespace QMQTT {
4747

4848
Q_DECLARE_LOGGING_CATEGORY(client)
49-
5049
class ClientPrivate
5150
{
5251

53-
P_DECLARE_PUBLIC(QMQTT::Client)
54-
52+
Q_DECLARE_PUBLIC(Client)
5553
public:
56-
explicit ClientPrivate(Client * q);
54+
ClientPrivate(Client * qt);
5755
~ClientPrivate();
5856
void init(QObject * parent = 0);
5957
void init(const QString & host, int port, QObject *parent = 0);
@@ -74,7 +72,7 @@ class ClientPrivate
7472
QPointer<QMQTT::Network> network;
7573
QPointer<QTimer> timer;
7674

77-
Client * const pq_ptr;
75+
7876

7977
public slots:
8078
void sockConnect();
@@ -92,6 +90,9 @@ public slots:
9290
private:
9391
QString randomClientId();
9492
quint16 nextmid();
93+
Client * const q_ptr;
94+
95+
9596

9697
};
9798

0 commit comments

Comments
 (0)