2016-05-12 04:36:12 +08:00
|
|
|
---
|
|
|
|
title: Events
|
|
|
|
---
|
|
|
|
|
2016-07-21 21:44:30 +08:00
|
|
|
Mongoose accepts incoming connections, reads and writes data and calls
|
|
|
|
specified event handlers for each connection when appropriate. A typical event
|
2016-05-12 04:36:12 +08:00
|
|
|
sequence is this:
|
|
|
|
|
2016-07-21 21:44:30 +08:00
|
|
|
- For an outbound connection: `MG_EV_CONNECT` -> (`MG_EV_RECV`, `MG_EV_SEND`,
|
2016-05-12 04:36:12 +08:00
|
|
|
`MG_EV_POLL` ...) -> `MG_EV_CLOSE`
|
2016-07-21 21:44:30 +08:00
|
|
|
- For an inbound connection: `MG_EV_ACCEPT` -> (`MG_EV_RECV`, `MG_EV_SEND`,
|
2016-05-12 04:36:12 +08:00
|
|
|
`MG_EV_POLL` ...) -> `MG_EV_CLOSE`
|
|
|
|
|
|
|
|
|
|
|
|
Below is a list of core events triggered by Mongoose (note that each protocol
|
|
|
|
triggers protocol-specific events in addition to the core ones):
|
|
|
|
|
2016-07-21 21:44:30 +08:00
|
|
|
- `MG_EV_ACCEPT`: sent when a new server connection is accepted by a listening
|
2016-05-12 04:36:12 +08:00
|
|
|
connection. `void *ev_data` is `union socket_address` of the remote peer.
|
|
|
|
|
|
|
|
- `MG_EV_CONNECT`: sent when a new outbound connection created by `mg_connect()`
|
|
|
|
either failed or succeeded. `void *ev_data` is `int *success`. If `success`
|
2016-07-21 21:44:30 +08:00
|
|
|
is 0, then the connection has been established, otherwise it contains an error code.
|
2016-05-12 04:36:12 +08:00
|
|
|
See `mg_connect_opt()` function for code example.
|
|
|
|
|
|
|
|
- `MG_EV_RECV`: New data is received and appended to the end of `recv_mbuf`.
|
|
|
|
`void *ev_data` is `int *num_received_bytes`. Typically, event handler should
|
|
|
|
check received data in `nc->recv_mbuf`, discard processed data by calling
|
|
|
|
`mbuf_remove()`, set connection flags `nc->flags` if necessary (see `struct
|
2016-07-21 21:44:30 +08:00
|
|
|
mg_connection`) and write data the remote peer by output functions like
|
2016-05-12 04:36:12 +08:00
|
|
|
`mg_send()`.
|
|
|
|
|
2016-07-21 21:44:30 +08:00
|
|
|
**WARNING**: Mongoose uses `realloc()` to expand the receive buffer. It is
|
|
|
|
the user's responsibility to discard processed data from the beginning of the receive
|
2016-05-12 04:36:12 +08:00
|
|
|
buffer, note the `mbuf_remove()` call in the example above.
|
|
|
|
|
|
|
|
- `MG_EV_SEND`: Mongoose has written data to the remote peer and discarded
|
|
|
|
written data from the `mg_connection::send_mbuf`. `void *ev_data` is `int
|
|
|
|
*num_sent_bytes`.
|
|
|
|
|
|
|
|
**NOTE**: Mongoose output functions only append data to the
|
2016-07-21 21:44:30 +08:00
|
|
|
`mg_connection::send_mbuf`. They do not do any socket writes. An actual IO
|
|
|
|
is done by `mg_mgr_poll()`. An `MG_EV_SEND` event is just a notification about
|
2016-05-12 04:36:12 +08:00
|
|
|
an IO has been done.
|
|
|
|
|
|
|
|
- `MG_EV_POLL`: Sent to all connections on each invocation of `mg_mgr_poll()`.
|
2016-07-21 21:44:30 +08:00
|
|
|
This event could be used to do any housekeeping, for example check whether a
|
|
|
|
certain timeout has expired and closes the connection or send heartbeat
|
|
|
|
message, etc.
|
2016-05-12 04:36:12 +08:00
|
|
|
|
|
|
|
- `MG_EV_TIMER`: Sent to the connection if `mg_set_timer()` was called.
|
|
|
|
|