Commit e62dc8f3 authored by Alexander Alashkin's avatar Alexander Alashkin Committed by Cesanta Bot

Handle MG_EV_CLOSE in examples

PUBLISHED_FROM=57c62a4123a54ecb455dbf91f3fe562fb0cf2ec2
parent e9908834
......@@ -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;
}
}
}
......
......@@ -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) {
......
......@@ -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;
}
}
}
......
......@@ -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;
}
......
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment