-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_mosq.c
399 lines (355 loc) · 10.1 KB
/
messages_mosq.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
Copyright (c) 2010-2014 Roger Light <[email protected]>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <mosquitto_internal.h>
#include <mosquitto.h>
#include <memory_mosq.h>
#include <messages_mosq.h>
#include <send_mosq.h>
#include <time_mosq.h>
void _mosquitto_message_cleanup(struct mosquitto_message_all **message)
{
struct mosquitto_message_all *msg;
if(!message || !*message) return;
msg = *message;
if(msg->msg.topic) _mosquitto_free(msg->msg.topic);
if(msg->msg.payload) _mosquitto_free(msg->msg.payload);
_mosquitto_free(msg);
}
void _mosquitto_message_cleanup_all(struct mosquitto *mosq)
{
struct mosquitto_message_all *tmp;
assert(mosq);
while(mosq->in_messages){
tmp = mosq->in_messages->next;
_mosquitto_message_cleanup(&mosq->in_messages);
mosq->in_messages = tmp;
}
while(mosq->out_messages){
tmp = mosq->out_messages->next;
_mosquitto_message_cleanup(&mosq->out_messages);
mosq->out_messages = tmp;
}
}
int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto_message *src)
{
if(!dst || !src) return MOSQ_ERR_INVAL;
dst->mid = src->mid;
dst->topic = _mosquitto_strdup(src->topic);
if(!dst->topic) return MOSQ_ERR_NOMEM;
dst->qos = src->qos;
dst->retain = src->retain;
if(src->payloadlen){
dst->payload = _mosquitto_malloc(src->payloadlen);
if(!dst->payload){
_mosquitto_free(dst->topic);
return MOSQ_ERR_NOMEM;
}
memcpy(dst->payload, src->payload, src->payloadlen);
dst->payloadlen = src->payloadlen;
}else{
dst->payloadlen = 0;
dst->payload = NULL;
}
return MOSQ_ERR_SUCCESS;
}
int _mosquitto_message_delete(struct mosquitto *mosq, uint16_t mid, enum mosquitto_msg_direction dir)
{
struct mosquitto_message_all *message;
int rc;
assert(mosq);
rc = _mosquitto_message_remove(mosq, mid, dir, &message);
if(rc == MOSQ_ERR_SUCCESS){
_mosquitto_message_cleanup(&message);
}
return rc;
}
void mosquitto_message_free(struct mosquitto_message **message)
{
struct mosquitto_message *msg;
if(!message || !*message) return;
msg = *message;
if(msg->topic) _mosquitto_free(msg->topic);
if(msg->payload) _mosquitto_free(msg->payload);
_mosquitto_free(msg);
}
/*
* Function: _mosquitto_message_queue
*
* Returns:
* 0 - to indicate an outgoing message can be started
* 1 - to indicate that the outgoing message queue is full (inflight limit has been reached)
*/
int _mosquitto_message_queue(struct mosquitto *mosq, struct mosquitto_message_all *message, enum mosquitto_msg_direction dir)
{
int rc = 0;
/* mosq->*_message_mutex should be locked before entering this function */
assert(mosq);
assert(message);
if(dir == mosq_md_out){
mosq->out_queue_len++;
message->next = NULL;
if(mosq->out_messages_last){
mosq->out_messages_last->next = message;
}else{
mosq->out_messages = message;
}
mosq->out_messages_last = message;
if(message->msg.qos > 0){
if(mosq->max_inflight_messages == 0 || mosq->inflight_messages < mosq->max_inflight_messages){
mosq->inflight_messages++;
}else{
rc = 1;
}
}
}else{
mosq->in_queue_len++;
message->next = NULL;
if(mosq->in_messages_last){
mosq->in_messages_last->next = message;
}else{
mosq->in_messages = message;
}
mosq->in_messages_last = message;
}
return rc;
}
void _mosquitto_messages_reconnect_reset(struct mosquitto *mosq)
{
struct mosquitto_message_all *message;
struct mosquitto_message_all *prev = NULL;
assert(mosq);
pthread_mutex_lock(&mosq->in_message_mutex);
message = mosq->in_messages;
mosq->in_queue_len = 0;
while(message){
mosq->in_queue_len++;
message->timestamp = 0;
if(message->msg.qos != 2){
if(prev){
prev->next = message->next;
_mosquitto_message_cleanup(&message);
message = prev;
}else{
mosq->in_messages = message->next;
_mosquitto_message_cleanup(&message);
message = mosq->in_messages;
}
}else{
/* Message state can be preserved here because it should match
* whatever the client has got. */
}
prev = message;
message = message->next;
}
mosq->in_messages_last = prev;
pthread_mutex_unlock(&mosq->in_message_mutex);
pthread_mutex_lock(&mosq->out_message_mutex);
mosq->inflight_messages = 0;
message = mosq->out_messages;
mosq->out_queue_len = 0;
while(message){
mosq->out_queue_len++;
message->timestamp = 0;
if(message->msg.qos > 0){
mosq->inflight_messages++;
}
if(mosq->max_inflight_messages == 0 || mosq->inflight_messages < mosq->max_inflight_messages){
if(message->msg.qos == 1){
message->state = mosq_ms_wait_for_puback;
}else if(message->msg.qos == 2){
/* Should be able to preserve state. */
}
}else{
message->state = mosq_ms_invalid;
}
prev = message;
message = message->next;
}
mosq->out_messages_last = prev;
pthread_mutex_unlock(&mosq->out_message_mutex);
}
int _mosquitto_message_remove(struct mosquitto *mosq, uint16_t mid, enum mosquitto_msg_direction dir, struct mosquitto_message_all **message)
{
struct mosquitto_message_all *cur, *prev = NULL;
bool found = false;
int rc;
assert(mosq);
assert(message);
if(dir == mosq_md_out){
pthread_mutex_lock(&mosq->out_message_mutex);
cur = mosq->out_messages;
while(cur){
if(cur->msg.mid == mid){
if(prev){
prev->next = cur->next;
}else{
mosq->out_messages = cur->next;
}
*message = cur;
mosq->out_queue_len--;
if(cur->next == NULL){
mosq->out_messages_last = prev;
}else if(!mosq->out_messages){
mosq->out_messages_last = NULL;
}
if(cur->msg.qos > 0){
mosq->inflight_messages--;
}
found = true;
break;
}
prev = cur;
cur = cur->next;
}
if(found){
cur = mosq->out_messages;
while(cur){
if(mosq->max_inflight_messages == 0 || mosq->inflight_messages < mosq->max_inflight_messages){
if(cur->msg.qos > 0 && cur->state == mosq_ms_invalid){
mosq->inflight_messages++;
if(cur->msg.qos == 1){
cur->state = mosq_ms_wait_for_puback;
}else if(cur->msg.qos == 2){
cur->state = mosq_ms_wait_for_pubrec;
}
rc = _mosquitto_send_publish(mosq, cur->msg.mid, cur->msg.topic, cur->msg.payloadlen, cur->msg.payload, cur->msg.qos, cur->msg.retain, cur->dup);
if(rc){
pthread_mutex_unlock(&mosq->out_message_mutex);
return rc;
}
}
}else{
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_SUCCESS;
}
cur = cur->next;
}
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_SUCCESS;
}else{
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_NOT_FOUND;
}
}else{
pthread_mutex_lock(&mosq->in_message_mutex);
cur = mosq->in_messages;
while(cur){
if(cur->msg.mid == mid){
if(prev){
prev->next = cur->next;
}else{
mosq->in_messages = cur->next;
}
*message = cur;
mosq->in_queue_len--;
if(cur->next == NULL){
mosq->in_messages_last = prev;
}else if(!mosq->in_messages){
mosq->in_messages_last = NULL;
}
found = true;
break;
}
prev = cur;
cur = cur->next;
}
pthread_mutex_unlock(&mosq->in_message_mutex);
if(found){
return MOSQ_ERR_SUCCESS;
}else{
return MOSQ_ERR_NOT_FOUND;
}
}
}
#ifdef WITH_THREADING
void _mosquitto_message_retry_check_actual(struct mosquitto *mosq, struct mosquitto_message_all *messages, pthread_mutex_t *mutex)
#else
void _mosquitto_message_retry_check_actual(struct mosquitto *mosq, struct mosquitto_message_all *messages)
#endif
{
time_t now = mosquitto_time();
assert(mosq);
#ifdef WITH_THREADING
pthread_mutex_lock(mutex);
#endif
while(messages){
if(messages->timestamp + mosq->message_retry < now){
switch(messages->state){
case mosq_ms_wait_for_puback:
case mosq_ms_wait_for_pubrec:
messages->timestamp = now;
messages->dup = true;
_mosquitto_send_publish(mosq, messages->msg.mid, messages->msg.topic, messages->msg.payloadlen, messages->msg.payload, messages->msg.qos, messages->msg.retain, messages->dup);
break;
case mosq_ms_wait_for_pubrel:
messages->timestamp = now;
messages->dup = true;
_mosquitto_send_pubrec(mosq, messages->msg.mid);
break;
case mosq_ms_wait_for_pubcomp:
messages->timestamp = now;
messages->dup = true;
_mosquitto_send_pubrel(mosq, messages->msg.mid);
break;
default:
break;
}
}
messages = messages->next;
}
#ifdef WITH_THREADING
pthread_mutex_unlock(mutex);
#endif
}
void _mosquitto_message_retry_check(struct mosquitto *mosq)
{
#ifdef WITH_THREADING
_mosquitto_message_retry_check_actual(mosq, mosq->out_messages, &mosq->out_message_mutex);
_mosquitto_message_retry_check_actual(mosq, mosq->in_messages, &mosq->in_message_mutex);
#else
_mosquitto_message_retry_check_actual(mosq, mosq->out_messages);
_mosquitto_message_retry_check_actual(mosq, mosq->in_messages);
#endif
}
void mosquitto_message_retry_set(struct mosquitto *mosq, unsigned int message_retry)
{
assert(mosq);
if(mosq) mosq->message_retry = message_retry;
}
int _mosquitto_message_out_update(struct mosquitto *mosq, uint16_t mid, enum mosquitto_msg_state state)
{
struct mosquitto_message_all *message;
assert(mosq);
pthread_mutex_lock(&mosq->out_message_mutex);
message = mosq->out_messages;
while(message){
if(message->msg.mid == mid){
message->state = state;
message->timestamp = mosquitto_time();
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_SUCCESS;
}
message = message->next;
}
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_NOT_FOUND;
}
int mosquitto_max_inflight_messages_set(struct mosquitto *mosq, unsigned int max_inflight_messages)
{
if(!mosq) return MOSQ_ERR_INVAL;
mosq->max_inflight_messages = max_inflight_messages;
return MOSQ_ERR_SUCCESS;
}