mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-12 20:59:03 +08:00
Handle MG_EV_CLOSE in examples
PUBLISHED_FROM=57c62a4123a54ecb455dbf91f3fe562fb0cf2ec2
This commit is contained in:
parent
e99088348d
commit
e62dc8f3b2
@ -7,10 +7,10 @@ To create a CoAP client, follow this pattern:
|
|||||||
1. Create an outbound connection by calling `mg_connect`
|
1. Create an outbound connection by calling `mg_connect`
|
||||||
2. Call `mg_set_protocol_coap` for created connection
|
2. Call `mg_set_protocol_coap` for created connection
|
||||||
3. Create an event handler function that handles the following events:
|
3. Create an event handler function that handles the following events:
|
||||||
- `MG_EV_COAP_CON`
|
- `MG_EV_COAP_CON`
|
||||||
- `MG_EV_COAP_NOC`
|
- `MG_EV_COAP_NOC`
|
||||||
- `MG_EV_COAP_ACK`
|
- `MG_EV_COAP_ACK`
|
||||||
- `MG_EV_COAP_RST`
|
- `MG_EV_COAP_RST`
|
||||||
|
|
||||||
Here's an example of the simplest CoAP client.
|
Here's an example of the simplest CoAP client.
|
||||||
Error checking is omitted for the sake of clarity:
|
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;
|
s_time_to_exit = 1;
|
||||||
break;
|
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);
|
fwrite(hm->message.p, 1, hm->message.len, stdout);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
exit_flag = 1;
|
exit_flag = 1;
|
||||||
}
|
} else if (ev == MG_EV_CLOSE) {
|
||||||
|
exit_flag = 1;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
@ -34,9 +34,17 @@ static void coap_handler(struct mg_connection *nc, int ev, void *p) {
|
|||||||
case MG_EV_COAP_RST: {
|
case MG_EV_COAP_RST: {
|
||||||
struct mg_coap_message *cm = (struct mg_coap_message *) p;
|
struct mg_coap_message *cm = (struct mg_coap_message *) p;
|
||||||
printf("ACK/RST for message with msg_id = %d received\n", cm->msg_id);
|
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;
|
s_time_to_exit = 1;
|
||||||
break;
|
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');
|
putchar('\n');
|
||||||
s_exit_flag = 1;
|
s_exit_flag = 1;
|
||||||
break;
|
break;
|
||||||
|
case MG_EV_CLOSE:
|
||||||
|
if(s_exit_flag == 0) {
|
||||||
|
printf("Server closed connection\n");
|
||||||
|
s_exit_flag = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
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;
|
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||||
s_exit_flag = 1;
|
s_exit_flag = 1;
|
||||||
break;
|
break;
|
||||||
|
case MG_EV_CLOSE:
|
||||||
|
if (s_exit_flag == 0) {
|
||||||
|
printf("Server closed connection\n");
|
||||||
|
s_exit_flag = 1;
|
||||||
|
};
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user