From e62dc8f3b2419d4ba25ec9706600b056da0d2fdc Mon Sep 17 00:00:00 2001 From: Alexander Alashkin Date: Tue, 1 Nov 2016 09:46:09 +0100 Subject: [PATCH] Handle MG_EV_CLOSE in examples PUBLISHED_FROM=57c62a4123a54ecb455dbf91f3fe562fb0cf2ec2 --- docs/coap/client_example.md | 15 +++++++++++---- docs/http/client_example.md | 4 +++- examples/coap_client/coap_client.c | 8 ++++++++ examples/http_client/http_client.c | 6 ++++++ examples/restful_client/restful_client.c | 6 ++++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/docs/coap/client_example.md b/docs/coap/client_example.md index 4dcd4f47..20cdf588 100644 --- a/docs/coap/client_example.md +++ b/docs/coap/client_example.md @@ -7,10 +7,10 @@ To create a CoAP client, follow this pattern: 1. Create an outbound connection by calling `mg_connect` 2. Call `mg_set_protocol_coap` for created connection 3. Create an event handler function that handles the following events: -- `MG_EV_COAP_CON` -- `MG_EV_COAP_NOC` -- `MG_EV_COAP_ACK` -- `MG_EV_COAP_RST` +- `MG_EV_COAP_CON` +- `MG_EV_COAP_NOC` +- `MG_EV_COAP_ACK` +- `MG_EV_COAP_RST` Here's an example of the simplest CoAP client. Error checking is omitted for the sake of clarity: @@ -39,6 +39,13 @@ static void coap_handler(struct mg_connection *nc, int ev, void *p) { s_time_to_exit = 1; break; } + case MG_EV_CLOSE: { + if (s_time_to_exit == 0) { + printf("Server closed connection\n"); + s_time_to_exit = 1; + } + break; + } } } diff --git a/docs/http/client_example.md b/docs/http/client_example.md index 0875824b..3d1646c5 100644 --- a/docs/http/client_example.md +++ b/docs/http/client_example.md @@ -22,7 +22,9 @@ static void ev_handler(struct mg_connection *c, int ev, void *p) { fwrite(hm->message.p, 1, hm->message.len, stdout); putchar('\n'); exit_flag = 1; - } + } else if (ev == MG_EV_CLOSE) { + exit_flag = 1; + }; } int main(void) { diff --git a/examples/coap_client/coap_client.c b/examples/coap_client/coap_client.c index f2f59171..5e392d68 100644 --- a/examples/coap_client/coap_client.c +++ b/examples/coap_client/coap_client.c @@ -34,9 +34,17 @@ static void coap_handler(struct mg_connection *nc, int ev, void *p) { case MG_EV_COAP_RST: { struct mg_coap_message *cm = (struct mg_coap_message *) p; printf("ACK/RST for message with msg_id = %d received\n", cm->msg_id); + nc->flags |= MG_F_SEND_AND_CLOSE; s_time_to_exit = 1; break; } + case MG_EV_CLOSE: { + if (s_time_to_exit == 0) { + printf("Server closed connection\n"); + s_time_to_exit = 1; + } + break; + } } } diff --git a/examples/http_client/http_client.c b/examples/http_client/http_client.c index f65073b1..f2c3df12 100644 --- a/examples/http_client/http_client.c +++ b/examples/http_client/http_client.c @@ -31,6 +31,12 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { putchar('\n'); s_exit_flag = 1; break; + case MG_EV_CLOSE: + if(s_exit_flag == 0) { + printf("Server closed connection\n"); + s_exit_flag = 1; + } + break; default: break; } diff --git a/examples/restful_client/restful_client.c b/examples/restful_client/restful_client.c index 60a755be..53af56e6 100644 --- a/examples/restful_client/restful_client.c +++ b/examples/restful_client/restful_client.c @@ -28,6 +28,12 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { nc->flags |= MG_F_SEND_AND_CLOSE; s_exit_flag = 1; break; + case MG_EV_CLOSE: + if (s_exit_flag == 0) { + printf("Server closed connection\n"); + s_exit_flag = 1; + }; + break; default: break; }