Skip to content

Commit 1494f5e

Browse files
authored
Merge pull request #3201 from cesanta/sntp
fix SNTP through proper protocol handler setup
2 parents 8bfd0c2 + 8bf9d09 commit 1494f5e

File tree

8 files changed

+39
-30
lines changed

8 files changed

+39
-30
lines changed

mongoose.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,9 +2589,7 @@ void mg_hello(const char *url) {
25892589

25902590
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
25912591
mg_event_handler_t fn, void *fn_data) {
2592-
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
2593-
if (c != NULL) c->pfn = http_cb;
2594-
return c;
2592+
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
25952593
}
25962594

25972595
struct mg_connection *mg_http_listen(struct mg_mgr *mgr, const char *url,
@@ -3858,12 +3856,11 @@ void mg_mqtt_disconnect(struct mg_connection *c,
38583856
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
38593857
const struct mg_mqtt_opts *opts,
38603858
mg_event_handler_t fn, void *fn_data) {
3861-
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
3859+
struct mg_connection *c = mg_connect_svc(mgr, url, fn, fn_data, mqtt_cb, NULL);
38623860
if (c != NULL) {
38633861
struct mg_mqtt_opts empty;
38643862
memset(&empty, 0, sizeof(empty));
38653863
mg_mqtt_login(c, opts == NULL ? &empty : opts);
3866-
c->pfn = mqtt_cb;
38673864
}
38683865
return c;
38693866
}
@@ -4036,8 +4033,8 @@ void mg_close_conn(struct mg_connection *c) {
40364033
mg_free(c);
40374034
}
40384035

4039-
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
4040-
mg_event_handler_t fn, void *fn_data) {
4036+
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
4037+
mg_event_handler_t fn, void *fn_data, mg_event_handler_t pfn, void *pfn_data) {
40414038
struct mg_connection *c = NULL;
40424039
if (url == NULL || url[0] == '\0') {
40434040
MG_ERROR(("null url"));
@@ -4051,13 +4048,20 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
40514048
c->is_client = true;
40524049
c->fn_data = fn_data;
40534050
c->is_tls = (mg_url_is_ssl(url) != 0);
4051+
c->pfn = pfn;
4052+
c->pfn_data = pfn_data;
40544053
mg_call(c, MG_EV_OPEN, (void *) url);
40554054
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
40564055
mg_resolve(c, url);
40574056
}
40584057
return c;
40594058
}
40604059

4060+
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
4061+
mg_event_handler_t fn, void *fn_data) {
4062+
return mg_connect_svc(mgr, url, fn, fn_data, NULL, NULL);
4063+
}
4064+
40614065
struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
40624066
mg_event_handler_t fn, void *fn_data) {
40634067
struct mg_connection *c = NULL;
@@ -8596,8 +8600,10 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
85968600
static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
85978601
uint64_t *expiration_time = (uint64_t *) c->data;
85988602
if (ev == MG_EV_OPEN) {
8603+
MG_INFO(("%lu PFN OPEN", c->id));
85998604
*expiration_time = mg_millis() + 3000; // Store expiration time in 3s
86008605
} else if (ev == MG_EV_CONNECT) {
8606+
MG_INFO(("%lu PFN CONNECT, sending request", c->id));
86018607
mg_sntp_request(c);
86028608
} else if (ev == MG_EV_READ) {
86038609
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
@@ -8631,14 +8637,9 @@ void mg_sntp_request(struct mg_connection *c) {
86318637
}
86328638

86338639
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
8634-
mg_event_handler_t fn, void *fnd) {
8635-
struct mg_connection *c = NULL;
8640+
mg_event_handler_t fn, void *fn_data) {
86368641
if (url == NULL) url = "udp://time.google.com:123";
8637-
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) {
8638-
c->pfn = sntp_cb;
8639-
sntp_cb(c, MG_EV_OPEN, (void *) url);
8640-
}
8641-
return c;
8642+
return mg_connect_svc(mgr, url, fn, fn_data, sntp_cb, NULL);
86428643
}
86438644

86448645
#ifdef MG_ENABLE_LINES

mongoose.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,9 @@ bool mg_wakeup(struct mg_mgr *, unsigned long id, const void *buf, size_t len);
15931593
bool mg_wakeup_init(struct mg_mgr *);
15941594
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
15951595
unsigned flags, void (*fn)(void *), void *arg);
1596+
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
1597+
mg_event_handler_t fn, void *fn_data,
1598+
mg_event_handler_t pfn, void *pfn_data);
15961599

15971600

15981601

src/http.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,7 @@ void mg_hello(const char *url) {
11481148

11491149
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
11501150
mg_event_handler_t fn, void *fn_data) {
1151-
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
1152-
if (c != NULL) c->pfn = http_cb;
1153-
return c;
1151+
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
11541152
}
11551153

11561154
struct mg_connection *mg_http_listen(struct mg_mgr *mgr, const char *url,

src/mqtt.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,11 @@ void mg_mqtt_disconnect(struct mg_connection *c,
529529
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
530530
const struct mg_mqtt_opts *opts,
531531
mg_event_handler_t fn, void *fn_data) {
532-
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
532+
struct mg_connection *c = mg_connect_svc(mgr, url, fn, fn_data, mqtt_cb, NULL);
533533
if (c != NULL) {
534534
struct mg_mqtt_opts empty;
535535
memset(&empty, 0, sizeof(empty));
536536
mg_mqtt_login(c, opts == NULL ? &empty : opts);
537-
c->pfn = mqtt_cb;
538537
}
539538
return c;
540539
}

src/net.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ void mg_close_conn(struct mg_connection *c) {
156156
mg_free(c);
157157
}
158158

159-
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
160-
mg_event_handler_t fn, void *fn_data) {
159+
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
160+
mg_event_handler_t fn, void *fn_data, mg_event_handler_t pfn, void *pfn_data) {
161161
struct mg_connection *c = NULL;
162162
if (url == NULL || url[0] == '\0') {
163163
MG_ERROR(("null url"));
@@ -171,13 +171,20 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
171171
c->is_client = true;
172172
c->fn_data = fn_data;
173173
c->is_tls = (mg_url_is_ssl(url) != 0);
174+
c->pfn = pfn;
175+
c->pfn_data = pfn_data;
174176
mg_call(c, MG_EV_OPEN, (void *) url);
175177
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
176178
mg_resolve(c, url);
177179
}
178180
return c;
179181
}
180182

183+
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
184+
mg_event_handler_t fn, void *fn_data) {
185+
return mg_connect_svc(mgr, url, fn, fn_data, NULL, NULL);
186+
}
187+
181188
struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
182189
mg_event_handler_t fn, void *fn_data) {
183190
struct mg_connection *c = NULL;

src/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ bool mg_wakeup(struct mg_mgr *, unsigned long id, const void *buf, size_t len);
105105
bool mg_wakeup_init(struct mg_mgr *);
106106
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
107107
unsigned flags, void (*fn)(void *), void *arg);
108+
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
109+
mg_event_handler_t fn, void *fn_data,
110+
mg_event_handler_t pfn, void *pfn_data);

src/sntp.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,7 @@ void mg_sntp_request(struct mg_connection *c) {
8282
}
8383

8484
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
85-
mg_event_handler_t fn, void *fnd) {
86-
struct mg_connection *c = NULL;
85+
mg_event_handler_t fn, void *fn_data) {
8786
if (url == NULL) url = "udp://time.google.com:123";
88-
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) {
89-
c->pfn = sntp_cb;
90-
sntp_cb(c, MG_EV_OPEN, (void *) url);
91-
}
92-
return c;
87+
return mg_connect_svc(mgr, url, fn, fn_data, sntp_cb, NULL);
9388
}

tutorials/udp/sntp-time-sync/main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data) {
4444
// Called every 5 seconds. Increase that for production case.
4545
static void timer_fn(void *arg) {
4646
struct mg_mgr *mgr = (struct mg_mgr *) arg;
47-
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
48-
if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
47+
if (s_sntp_conn == NULL) { // connection issues a request
48+
s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
49+
} else {
50+
mg_sntp_request(s_sntp_conn);
51+
}
4952
}
5053

5154
int main(void) {

0 commit comments

Comments
 (0)