mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-22 11:36:21 +00:00
fix SNTP through proper protocol handler setup
This commit is contained in:
1 parent
67255ce8f5
commit
8bf9d09d96
8 files changed
+39
-30
No files matched your search
+15
-14
@@ -2589,9 +2589,7 @@ void mg_hello(const char *url) {
|
||||
|
||||
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
|
||||
if (c != NULL) c->pfn = http_cb;
|
||||
return c;
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
|
||||
}
|
||||
|
||||
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,
|
||||
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
|
||||
const struct mg_mqtt_opts *opts,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
|
||||
struct mg_connection *c = mg_connect_svc(mgr, url, fn, fn_data, mqtt_cb, NULL);
|
||||
if (c != NULL) {
|
||||
struct mg_mqtt_opts empty;
|
||||
memset(&empty, 0, sizeof(empty));
|
||||
mg_mqtt_login(c, opts == NULL ? &empty : opts);
|
||||
c->pfn = mqtt_cb;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
@@ -4036,8 +4033,8 @@ void mg_close_conn(struct mg_connection *c) {
|
||||
mg_free(c);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data, mg_event_handler_t pfn, void *pfn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
if (url == NULL || url[0] == '\0') {
|
||||
MG_ERROR(("null url"));
|
||||
@@ -4051,6 +4048,8 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
c->is_client = true;
|
||||
c->fn_data = fn_data;
|
||||
c->is_tls = (mg_url_is_ssl(url) != 0);
|
||||
c->pfn = pfn;
|
||||
c->pfn_data = pfn_data;
|
||||
mg_call(c, MG_EV_OPEN, (void *) url);
|
||||
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
|
||||
mg_resolve(c, url);
|
||||
@@ -4058,6 +4057,11 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
return c;
|
||||
}
|
||||
|
||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, NULL, NULL);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
@@ -8596,8 +8600,10 @@ int64_t mg_sntp_parse(const unsigned char *buf, size_t len) {
|
||||
static void sntp_cb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
uint64_t *expiration_time = (uint64_t *) c->data;
|
||||
if (ev == MG_EV_OPEN) {
|
||||
MG_INFO(("%lu PFN OPEN", c->id));
|
||||
*expiration_time = mg_millis() + 3000; // Store expiration time in 3s
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
MG_INFO(("%lu PFN CONNECT, sending request", c->id));
|
||||
mg_sntp_request(c);
|
||||
} else if (ev == MG_EV_READ) {
|
||||
int64_t milliseconds = mg_sntp_parse(c->recv.buf, c->recv.len);
|
||||
@@ -8631,14 +8637,9 @@ void mg_sntp_request(struct mg_connection *c) {
|
||||
}
|
||||
|
||||
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fnd) {
|
||||
struct mg_connection *c = NULL;
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
if (url == NULL) url = "udp://time.google.com:123";
|
||||
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) {
|
||||
c->pfn = sntp_cb;
|
||||
sntp_cb(c, MG_EV_OPEN, (void *) url);
|
||||
}
|
||||
return c;
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, sntp_cb, NULL);
|
||||
}
|
||||
|
||||
#ifdef MG_ENABLE_LINES
|
||||
|
||||
@@ -1593,6 +1593,9 @@ bool mg_wakeup(struct mg_mgr *, unsigned long id, const void *buf, size_t len);
|
||||
bool mg_wakeup_init(struct mg_mgr *);
|
||||
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
unsigned flags, void (*fn)(void *), void *arg);
|
||||
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data,
|
||||
mg_event_handler_t pfn, void *pfn_data);
|
||||
|
||||
|
||||
|
||||
|
||||
+1
-3
@@ -1148,9 +1148,7 @@ void mg_hello(const char *url) {
|
||||
|
||||
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
|
||||
if (c != NULL) c->pfn = http_cb;
|
||||
return c;
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_http_listen(struct mg_mgr *mgr, const char *url,
|
||||
|
||||
+1
-2
@@ -529,12 +529,11 @@ void mg_mqtt_disconnect(struct mg_connection *c,
|
||||
struct mg_connection *mg_mqtt_connect(struct mg_mgr *mgr, const char *url,
|
||||
const struct mg_mqtt_opts *opts,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = mg_connect(mgr, url, fn, fn_data);
|
||||
struct mg_connection *c = mg_connect_svc(mgr, url, fn, fn_data, mqtt_cb, NULL);
|
||||
if (c != NULL) {
|
||||
struct mg_mqtt_opts empty;
|
||||
memset(&empty, 0, sizeof(empty));
|
||||
mg_mqtt_login(c, opts == NULL ? &empty : opts);
|
||||
c->pfn = mqtt_cb;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
@@ -156,8 +156,8 @@ void mg_close_conn(struct mg_connection *c) {
|
||||
mg_free(c);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data, mg_event_handler_t pfn, void *pfn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
if (url == NULL || url[0] == '\0') {
|
||||
MG_ERROR(("null url"));
|
||||
@@ -171,6 +171,8 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
c->is_client = true;
|
||||
c->fn_data = fn_data;
|
||||
c->is_tls = (mg_url_is_ssl(url) != 0);
|
||||
c->pfn = pfn;
|
||||
c->pfn_data = pfn_data;
|
||||
mg_call(c, MG_EV_OPEN, (void *) url);
|
||||
MG_DEBUG(("%lu %ld %s", c->id, c->fd, url));
|
||||
mg_resolve(c, url);
|
||||
@@ -178,6 +180,11 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
return c;
|
||||
}
|
||||
|
||||
struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, NULL, NULL);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_listen(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
struct mg_connection *c = NULL;
|
||||
|
||||
@@ -105,3 +105,6 @@ bool mg_wakeup(struct mg_mgr *, unsigned long id, const void *buf, size_t len);
|
||||
bool mg_wakeup_init(struct mg_mgr *);
|
||||
struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
|
||||
unsigned flags, void (*fn)(void *), void *arg);
|
||||
struct mg_connection *mg_connect_svc(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data,
|
||||
mg_event_handler_t pfn, void *pfn_data);
|
||||
+2
-7
@@ -82,12 +82,7 @@ void mg_sntp_request(struct mg_connection *c) {
|
||||
}
|
||||
|
||||
struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fnd) {
|
||||
struct mg_connection *c = NULL;
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
if (url == NULL) url = "udp://time.google.com:123";
|
||||
if ((c = mg_connect(mgr, url, fn, fnd)) != NULL) {
|
||||
c->pfn = sntp_cb;
|
||||
sntp_cb(c, MG_EV_OPEN, (void *) url);
|
||||
}
|
||||
return c;
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, sntp_cb, NULL);
|
||||
}
|
||||
@@ -44,8 +44,11 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
// Called every 5 seconds. Increase that for production case.
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
||||
if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
|
||||
if (s_sntp_conn == NULL) { // connection issues a request
|
||||
s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
||||
} else {
|
||||
mg_sntp_request(s_sntp_conn);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
Reference in new issue
Block a user