36
36
namespace QMQTT {
37
37
38
38
Client::Client (const QString & host, quint32 port, QObject * parent /* =0 */ )
39
- :pd_ptr(new ClientPrivate(this ))
39
+ :d_ptr(new ClientPrivate(this ))
40
+
40
41
{
41
- pd_func ()->init (host, port, parent);
42
+ Q_D (Client);
43
+ d->init (host, port, parent);
42
44
}
43
45
44
46
Client::~Client ()
45
47
{
46
48
// ?
49
+ Q_D (Client);
50
+ delete d;
47
51
}
48
52
49
53
/* ----------------------------------------------------------------
50
54
* Get/Set Property
51
55
----------------------------------------------------------------*/
52
56
QString Client::host () const
53
57
{
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 ;
55
63
}
56
64
57
65
void Client::setHost (const QString & host)
58
66
{
59
- pd_func ()->host = host;
67
+ // d->host = host;
68
+ Q_D (Client);
69
+ d->host = host;
60
70
}
61
71
62
72
quint32 Client::port () const
63
73
{
64
- return pd_func ()->port ;
74
+ Q_D (const Client);
75
+ return d->port ;
65
76
}
66
77
67
78
void Client::setPort (quint32 port)
68
79
{
69
- pd_func ()->port = port;
80
+ Q_D (Client);
81
+ d->port = port;
70
82
}
71
83
72
84
QString Client::clientId () const
73
85
{
74
- return pd_func ()->clientId ;
86
+ Q_D (const Client);
87
+ return d->clientId ;
75
88
}
76
89
77
90
void Client::setClientId (const QString &clientId)
78
91
{
79
- pd_func ()->clientId = clientId;
92
+ Q_D (Client);
93
+ d->clientId = clientId;
80
94
}
81
95
82
96
QString Client::username () const
83
97
{
84
- return pd_func ()->username ;
98
+ Q_D (const Client);
99
+ return d->username ;
85
100
}
86
101
87
102
void Client::setUsername (const QString & username)
88
103
{
89
- pd_func ()->username = username;
104
+ Q_D (Client);
105
+ d->username = username;
90
106
}
91
107
92
108
QString Client::password () const
93
109
{
94
- return pd_func ()->password ;
110
+ Q_D (const Client);
111
+ return d->password ;
95
112
}
96
113
97
114
void Client::setPassword (const QString & password)
98
115
{
99
- pd_func ()->password = password;
116
+ Q_D (Client);
117
+ d->password = password;
100
118
}
101
119
102
120
int Client::keepalive ()
103
121
{
104
- return pd_func ()->keepalive ;
122
+ Q_D ( Client);
123
+ return d->keepalive ;
105
124
}
106
125
107
126
void Client::setKeepAlive (int keepalive)
108
127
{
109
- pd_func ()->keepalive = keepalive;
128
+ Q_D (Client);
129
+ d->keepalive = keepalive;
110
130
}
111
131
112
132
bool Client::cleansess ()
113
133
{
114
- return pd_func ()->cleansess ;
134
+ Q_D (Client);
135
+ return d->cleansess ;
115
136
}
116
137
117
138
void Client::setCleansess (bool cleansess)
118
139
{
119
- pd_func ()->cleansess = cleansess;
140
+ Q_D (Client);
141
+ d->cleansess = cleansess;
120
142
}
121
143
122
144
bool Client::autoReconnect () const
123
145
{
124
- return pd_func ()->network ->autoReconnect ();
146
+ Q_D (const Client);
147
+ return d->network ->autoReconnect ();
125
148
}
126
149
127
150
void Client::setAutoReconnect (bool value)
128
151
{
129
- pd_func ()->network ->setAutoReconnect (value);
152
+ Q_D (Client);
153
+ d->network ->setAutoReconnect (value);
130
154
}
131
155
132
156
Will *Client::will ()
133
157
{
134
- return pd_func ()->will ;
158
+ Q_D (Client);
159
+ return d->will ;
135
160
}
136
161
137
162
void Client::setWill (Will *will)
138
163
{
139
- pd_func ()->will = will;
164
+ Q_D (Client);
165
+ d->will = will;
140
166
}
141
167
142
168
State Client::state () const
143
169
{
144
- return pd_func ()->state ;
170
+ Q_D (const Client);
171
+ return d->state ;
145
172
}
146
173
147
174
bool Client::isConnected ()
148
175
{
149
- return pd_func ()->network ->isConnected ();
176
+ Q_D (Client);
177
+ return d->network ->isConnected ();
150
178
}
151
179
152
180
@@ -155,56 +183,65 @@ bool Client::isConnected()
155
183
----------------------------------------------------------------*/
156
184
void Client::connect ()
157
185
{
158
- pd_func ()->sockConnect ();
186
+ Q_D (Client);
187
+ d->sockConnect ();
159
188
}
160
189
161
190
void Client::onConnected ()
162
191
{
192
+ Q_D (Client);
163
193
qCDebug (client) << " Sock Connected...." ;
164
- pd_func () ->sendConnect ();
165
- pd_func () ->startKeepalive ();
194
+ d ->sendConnect ();
195
+ d ->startKeepalive ();
166
196
emit connected ();
167
197
}
168
198
169
199
quint16 Client::publish (Message &message)
170
200
{
171
- quint16 msgid = pd_func ()->sendPublish (message);
201
+ Q_D (Client);
202
+ quint16 msgid = d->sendPublish (message);
172
203
emit published (message);
173
204
return msgid;
174
205
}
175
206
176
207
void Client::puback (quint8 type, quint16 msgid)
177
208
{
178
- pd_func ()->sendPuback (type, msgid);
209
+ Q_D (Client);
210
+ d->sendPuback (type, msgid);
179
211
emit pubacked (type, msgid);
180
212
}
181
213
182
214
quint16 Client::subscribe (const QString &topic, quint8 qos)
183
215
{
184
- quint16 msgid = pd_func ()->sendSubscribe (topic, qos);
216
+ Q_D (Client);
217
+ quint16 msgid = d->sendSubscribe (topic, qos);
185
218
emit subscribed (topic);
186
219
return msgid;
187
220
}
188
221
189
222
void Client::unsubscribe (const QString &topic)
190
223
{
191
- pd_func ()->sendUnsubscribe (topic);
224
+ Q_D (Client);
225
+ d->sendUnsubscribe (topic);
192
226
emit unsubscribed (topic);
193
227
}
194
228
195
229
void Client::ping ()
196
230
{
197
- pd_func ()->sendPing ();
231
+ Q_D (Client);
232
+ d->sendPing ();
198
233
}
199
234
200
235
void Client::disconnect ()
201
236
{
202
- pd_func ()->disconnect ();
237
+ Q_D (Client);
238
+ d->disconnect ();
203
239
}
204
240
205
241
void Client::onDisconnected ()
206
242
{
207
- pd_func ()->stopKeepalive ();
243
+ Q_D (Client);
244
+ d->stopKeepalive ();
208
245
emit disconnected ();
209
246
}
210
247
@@ -274,20 +311,22 @@ void Client::handleConnack(quint8 ack)
274
311
275
312
void Client::handlePublish (Message & message)
276
313
{
314
+ Q_D (Client);
277
315
if (message.qos () == MQTT_QOS1) {
278
- pd_func () ->sendPuback (PUBACK, message.id ());
316
+ d ->sendPuback (PUBACK, message.id ());
279
317
} else if (message.qos () == MQTT_QOS2) {
280
- pd_func () ->sendPuback (PUBREC, message.id ());
318
+ d ->sendPuback (PUBREC, message.id ());
281
319
}
282
320
emit received (message);
283
321
}
284
322
285
323
void Client::handlePuback (quint8 type, quint16 msgid)
286
324
{
325
+ Q_D (Client);
287
326
if (type == PUBREC) {
288
- pd_func () ->sendPuback (PUBREL, msgid);
327
+ d ->sendPuback (PUBREL, msgid);
289
328
} else if (type == PUBREL) {
290
- pd_func () ->sendPuback (PUBCOMP, msgid);
329
+ d ->sendPuback (PUBCOMP, msgid);
291
330
}
292
331
emit pubacked (type, msgid);
293
332
}
0 commit comments