-
Notifications
You must be signed in to change notification settings - Fork 10
/
loop.c
316 lines (285 loc) · 12.2 KB
/
loop.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
/*
Copyright (c) 2009-2013 Roger Light <roger@atchoo.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of mosquitto nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <poll.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <mosquitto_broker.h>
#include <memory_mosq.h>
#include <time_mosq.h>
#include <util_mosq.h>
#ifndef POLLRDHUP
/* Ignore POLLRDHUP flag on systems where it doesn't exist. */
#define POLLRDHUP 0
#endif
extern bool flag_reload;
#ifdef WITH_PERSISTENCE
extern bool flag_db_backup;
#endif
extern bool flag_tree_print;
extern int run;
#ifdef WITH_SYS_TREE
extern int g_clients_expired;
#endif
static void loop_handle_errors(struct mosquitto_db *db, struct pollfd *pollfds);
static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds);
int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock_count, int listener_max)
{
time_t start_time = mosquitto_time();
time_t last_backup = mosquitto_time();//上次数据库备份的时间点,autosave_on_changes没设置的时候采用间隔多久备份
time_t last_store_clean = mosquitto_time();
time_t now;
int time_count;
int fdcount;
sigset_t sigblock, origsig;
int i;
struct pollfd *pollfds = NULL;
int pollfd_count = 0;
int pollfd_index;
sigemptyset(&sigblock);
sigaddset(&sigblock, SIGINT);
while(run){
#ifdef WITH_SYS_TREE
mqtt3_db_sys_update(db, db->config->sys_interval, start_time);
#endif
if( db->finished_auth_list != NULL){
try_wakeup_finished_auth_connections(db);
}
if(listensock_count + db->context_count > pollfd_count){
pollfd_count = listensock_count + db->context_count;//申请pollfd数组存储
pollfds = _mosquitto_realloc(pollfds, sizeof(struct pollfd)*pollfd_count);
if(!pollfds){
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}
memset(pollfds, -1, sizeof(struct pollfd)*pollfd_count);
pollfd_index = 0;
for(i=0; i<listensock_count; i++){//注册监听sock的pollfd可读事件。也就是新连接事件
pollfds[pollfd_index].fd = listensock[i];
pollfds[pollfd_index].events = POLLIN;
pollfds[pollfd_index].revents = 0;
pollfd_index++;
}
time_count = 0;
for(i=0; i<db->context_count; i++){//遍历每一个客户端连接,尝试将其加入poll数组中
if(db->contexts[i]){
if(time_count > 0){//time_count用来隔1000次循环更新一次now时间
time_count--;
}else{
time_count = 1000;
now = mosquitto_time();
}
db->contexts[i]->pollfd_index = -1;
if(db->contexts[i]->sock != INVALID_SOCKET){
/* Local bridges never time out in this fashion. */
if(!(db->contexts[i]->keepalive) //不需要keepalive
|| db->contexts[i]->bridge //或者是bridge
|| now - db->contexts[i]->last_msg_in < (time_t)(db->contexts[i]->keepalive)*3/2){//或者没有超时,那么需要进行监听
//在进入poll等待之前,先尝试将未发送的数据发送出去
if(mqtt3_db_message_write(db->contexts[i]) == MOSQ_ERR_SUCCESS){
pollfds[pollfd_index].fd = db->contexts[i]->sock;
pollfds[pollfd_index].events = POLLIN | POLLRDHUP;
pollfds[pollfd_index].revents = 0;
if(db->contexts[i]->current_out_packet){//如果当前有需要发送的数据,才监听可写事件
pollfds[pollfd_index].events |= POLLOUT;
}
db->contexts[i]->pollfd_index = pollfd_index;//记录我这个fd在本次pollfds中的下标,待会会扫描所有fd
pollfd_index++;
}else{//尝试发送失败,连接出问题了
mqtt3_context_disconnect(db, db->contexts[i]);
}
}else{//超过1.5倍的时间,超时关闭连接
if(db->config->connection_messages == true){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s has exceeded timeout, disconnecting.", db->contexts[i]->id);
}
/* Client has exceeded keepalive*1.5 */
mqtt3_context_disconnect(db, db->contexts[i]);//关闭连接,清空数据,后续还可以用.sock=INVALID_SOCKET
}
}else{//db->contexts[i]->sock == INVALID_SOCKET
if(db->contexts[i]->clean_session == true){
//这个连接上次由于什么原因,挂了,设置了clean session,所以这里直接彻底清空其结构
mqtt3_context_cleanup(db, db->contexts[i], true);
db->contexts[i] = NULL;
}else if(db->config->persistent_client_expiration > 0){
//协议规定persistent_client的状态必须永久保存,这里避免连接永远无法删除,增加这个超时选项。
//也就是如果一个客户端断开连接一段时间了,那么我们会主动干掉他
/* This is a persistent client, check to see if the
* last time it connected was longer than
* persistent_client_expiration seconds ago. If so,
* expire it and clean up.
*/
if(now > db->contexts[i]->disconnect_t+db->config->persistent_client_expiration){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Expiring persistent client %s due to timeout.", db->contexts[i]->id);
#ifdef WITH_SYS_TREE
g_clients_expired++;
#endif
db->contexts[i]->clean_session = true;//标记强制清空session
mqtt3_context_cleanup(db, db->contexts[i], true);
db->contexts[i] = NULL;
}
}
}
}
}
//检测消息超时,设置重发标志等,其实没有必要,TCP不会丢包,乱序的
mqtt3_db_message_timeout_check(db, db->config->retry_interval);
sigprocmask(SIG_SETMASK, &sigblock, &origsig);
fdcount = poll(pollfds, pollfd_index, 100);
//poll返回0代表超时了,没事件。-1代表出错,大于0代表有这么多事件
sigprocmask(SIG_SETMASK, &origsig, NULL);
if(fdcount == -1){
loop_handle_errors(db, pollfds);
}else{
//处理客户端连接的读写事件,里面是扫描所有sock的
loop_handle_reads_writes(db, pollfds);
//独立处理listen端口
for(i=0; i<listensock_count; i++){
if(pollfds[i].revents & (POLLIN | POLLPRI)){
//listen sock可读的时候可能有多个新连接,需要循环accept
while(mqtt3_socket_accept(db, listensock[i]) != -1){
}
}
}
}
#ifdef WITH_PERSISTENCE
if(db->config->persistence && db->config->autosave_interval){
if(db->config->autosave_on_changes){//设置了,则用累积了多少条改动后持久化
if(db->persistence_changes > db->config->autosave_interval){
mqtt3_db_backup(db, false, false);
db->persistence_changes = 0;
}
}else{//否则按间隔时间来持久化
if(last_backup + db->config->autosave_interval < mosquitto_time()){
mqtt3_db_backup(db, false, false);
last_backup = mosquitto_time();
}
}
}
#endif
//store_clean_interval为Time in seconds between cleaning the internal message store of unreferenced messages.
//定期扫描db->msg_store里面存储的消息列表,去掉哪些已经么有使用的,比如全部都发出去了的,得去掉
if(!db->config->store_clean_interval || last_store_clean + db->config->store_clean_interval < mosquitto_time()){
mqtt3_db_store_clean(db);
last_store_clean = mosquitto_time();
}
#ifdef WITH_PERSISTENCE
if(flag_db_backup){
mqtt3_db_backup(db, false, false);
flag_db_backup = false;
}
#endif
if(flag_reload){
_mosquitto_log_printf(NULL, MOSQ_LOG_INFO, "Reloading config.");
mqtt3_config_read(db->config, true);
mosquitto_security_cleanup(db, true);
mosquitto_security_init(db, true);
mosquitto_security_apply(db);
mqtt3_log_init(db->config->log_type, db->config->log_dest);
flag_reload = false;
}
if(flag_tree_print){//直接printf一下订阅的树形结构,线上慎用
mqtt3_sub_tree_print(&db->subs, 0);
flag_tree_print = false;
}
}
if(pollfds) _mosquitto_free(pollfds);
return MOSQ_ERR_SUCCESS;
}
static void do_disconnect(struct mosquitto_db *db, int context_index)
{
if(db->config->connection_messages == true){
if(db->contexts[context_index]->state != mosq_cs_disconnecting){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket error on client %s, disconnecting.", db->contexts[context_index]->id);
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[context_index]->id);
}
}
mqtt3_context_disconnect(db, db->contexts[context_index]);
}
/* Error ocurred, probably an fd has been closed.
* Loop through and check them all.
*/
static void loop_handle_errors(struct mosquitto_db *db, struct pollfd *pollfds)
{
int i;
for(i=0; i<db->context_count; i++){
if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){
if(pollfds[db->contexts[i]->pollfd_index].revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)){
do_disconnect(db, i);
}
}
}
}
static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds)
{//mosquitto_main_loop调用这里来处理客户端连接的读写事件
int i;
for(i=0; i<db->context_count; i++){
//循环遍历每一个客户端连接,根据pollfd_index找到其在pollfds数组中的元素,看其是否有事件可以处理的
if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){
assert(pollfds[db->contexts[i]->pollfd_index].fd == db->contexts[i]->sock);
if(pollfds[db->contexts[i]->pollfd_index].revents & POLLOUT){
//连接可写,则尝试发送数据
if(_mosquitto_packet_write(db->contexts[i])){//将mosq->out_packet链表上的数据发送出去
if(db->config->connection_messages == true){
if(db->contexts[i]->state != mosq_cs_disconnecting){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket write error on client %s, disconnecting.", db->contexts[i]->id);
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id);
}
}
/* Write error or other that means we should disconnect */
mqtt3_context_disconnect(db, db->contexts[i]);
}
}
}
if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){
assert(pollfds[db->contexts[i]->pollfd_index].fd == db->contexts[i]->sock);
if(pollfds[db->contexts[i]->pollfd_index].revents & POLLIN){
//连接可读,则读取出数据并处理之
if(_mosquitto_packet_read(db, db->contexts[i])){
if(db->config->connection_messages == true){
if(db->contexts[i]->state != mosq_cs_disconnecting){
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Socket read error on client %s, disconnecting.", db->contexts[i]->id);
}else{
_mosquitto_log_printf(NULL, MOSQ_LOG_NOTICE, "Client %s disconnected.", db->contexts[i]->id);
}
}
/* Read error or other that means we should disconnect */
mqtt3_context_disconnect(db, db->contexts[i]);
}
}
}
if(db->contexts[i] && db->contexts[i]->sock != INVALID_SOCKET){
if(pollfds[db->contexts[i]->pollfd_index].revents & (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL)){
do_disconnect(db, i);//连接挂了
}
}
}
}