2016-05-12 04:36:12 +08:00
|
|
|
---
|
|
|
|
title: Event handler function
|
|
|
|
---
|
|
|
|
|
|
|
|
Each connection has an event handler function associated with it. That function
|
2016-07-21 21:44:30 +08:00
|
|
|
must be implemented by the user. Event handler is the key element of the Mongoose
|
2016-08-05 03:45:07 +08:00
|
|
|
application, since it defines the application's behaviour. This is what an event
|
2016-05-12 04:36:12 +08:00
|
|
|
handler function looks like:
|
|
|
|
|
|
|
|
```c
|
|
|
|
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
|
|
switch (ev) {
|
|
|
|
/* Event handler code that defines behavior of the connection */
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- `struct mg_connection *nc`: Connection that has received an event.
|
|
|
|
- `int ev`: Event number, defined in `mongoose.h`. For example, when data
|
2016-07-21 21:44:30 +08:00
|
|
|
arrives on an inbound connection, `ev` would be `MG_EV_RECV`.
|
2016-05-12 04:36:12 +08:00
|
|
|
- `void *ev_data`: This pointer points to the event-specific data, and it has
|
2016-07-21 21:44:30 +08:00
|
|
|
a different meaning for different events. For example, for an `MG_EV_RECV` event,
|
2016-05-12 04:36:12 +08:00
|
|
|
`ev_data` is an `int *` pointer, pointing to the number of bytes received
|
2016-07-21 21:44:30 +08:00
|
|
|
from the remote peer and saved into the receive IO buffer. The exact meaning of
|
2016-05-12 04:36:12 +08:00
|
|
|
`ev_data` is described for each event. Protocol-specific events usually have
|
|
|
|
`ev_data` pointing to structures that hold protocol-specific information.
|
|
|
|
|
|
|
|
NOTE: `struct mg_connection` has `void *user_data` which is a placeholder for
|
2016-07-21 21:44:30 +08:00
|
|
|
application-specific data. Mongoose does not use that pointer. Event handler
|
2016-05-12 04:36:12 +08:00
|
|
|
can store any kind of information there.
|