Comments in headers are changed a bit: removed adoc-specific stuff, markdown is used instead PUBLISHED_FROM=9242cce85cc52a47a197d377e7e23804721a6bb5
2.3 KiB
title |
---|
Events |
Mongoose accepts incoming connections, reads and writes data, and calls specified event handler for each connection when appropriate. Typical event sequence is this:
- For outbound connection:
MG_EV_CONNECT
-> (MG_EV_RECV
,MG_EV_SEND
,MG_EV_POLL
...) ->MG_EV_CLOSE
- For inbound connection:
MG_EV_ACCEPT
-> (MG_EV_RECV
,MG_EV_SEND
,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):
-
MG_EV_ACCEPT
: sent when new server connection is accepted by a listening connection.void *ev_data
isunion socket_address
of the remote peer. -
MG_EV_CONNECT
: sent when a new outbound connection created bymg_connect()
either failed or succeeded.void *ev_data
isint *success
. Ifsuccess
is 0, then connection has been established, otherwise it contains error code. Seemg_connect_opt()
function for code example. -
MG_EV_RECV
: New data is received and appended to the end ofrecv_mbuf
.void *ev_data
isint *num_received_bytes
. Typically, event handler should check received data innc->recv_mbuf
, discard processed data by callingmbuf_remove()
, set connection flagsnc->flags
if necessary (seestruct mg_connection
), and write data the remote peer by output functions likemg_send()
.WARNING: Mongoose uses
realloc()
to expand receive buffer. It is user's responsibility to discard processed data from the beginning of receive buffer, note thembuf_remove()
call in the example above. -
MG_EV_SEND
: Mongoose has written data to the remote peer and discarded written data from themg_connection::send_mbuf
.void *ev_data
isint *num_sent_bytes
.NOTE: Mongoose output functions only append data to the
mg_connection::send_mbuf
, they do not do any socket writes. An actual IO is done bymg_mgr_poll()
.MG_EV_SEND
event is just a notification about an IO has been done. -
MG_EV_POLL
: Sent to all connections on each invocation ofmg_mgr_poll()
. This event could be used to do any housekeeping, for example check whether certain timeout has expired and close the connection, or send heartbeet message, et cetera. -
MG_EV_TIMER
: Sent to the connection ifmg_set_timer()
was called.