2016-05-12 04:36:12 +08:00
|
|
|
---
|
|
|
|
title: Design Concept
|
|
|
|
---
|
|
|
|
|
2016-09-01 22:19:01 +08:00
|
|
|
Mongoose has three basic data structures:
|
2016-05-12 04:36:12 +08:00
|
|
|
|
|
|
|
- `struct mg_mgr` is an event manager that holds all active connections
|
|
|
|
- `struct mg_connection` describes a connection
|
|
|
|
- `struct mbuf` describes data buffer (received or sent data)
|
|
|
|
|
2016-07-21 21:30:03 +08:00
|
|
|
Connections could be either *listening*, *outbound* or *inbound*. Outbound
|
|
|
|
connections are created by the `mg_connect()` call. Listening connections are
|
|
|
|
created by the `mg_bind()` call. Inbound connections are those accepted by a
|
|
|
|
listening connection. Each connection is described by the `struct mg_connection`
|
2016-05-12 04:36:12 +08:00
|
|
|
structure, which has a number of fields like socket, event handler function,
|
2016-07-21 21:30:03 +08:00
|
|
|
send/receive buffer, flags, etc.
|
2016-05-12 04:36:12 +08:00
|
|
|
|
2016-09-01 22:19:01 +08:00
|
|
|
An application that uses mongoose should follow a standard pattern of
|
|
|
|
event-driven application:
|
|
|
|
|
|
|
|
1. declare and initialise event manager:
|
|
|
|
|
|
|
|
```c
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_mgr_init(&mgr, NULL);
|
|
|
|
```
|
|
|
|
2. Create connections. For example, a server application should create
|
|
|
|
listening connections:
|
|
|
|
|
|
|
|
```c
|
|
|
|
struct mg_connection *c = mg_bind(&mgr, "80", ev_handler_function);
|
|
|
|
mg_set_protocol_http_websocket(c);
|
|
|
|
```
|
|
|
|
|
|
|
|
3. create an event loop by calling `mg_mgr_poll()` in a loop:
|
|
|
|
|
|
|
|
```c
|
|
|
|
for (;;) {
|
|
|
|
mg_mgr_poll(&mgr, 1000);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-12 04:36:12 +08:00
|
|
|
`mg_mgr_poll()` iterates over all sockets, accepts new connections, sends and
|
2016-07-21 21:30:03 +08:00
|
|
|
receives data, closes connections and calls event handler functions for the
|
2016-09-01 22:19:01 +08:00
|
|
|
respective events. For the full example, see
|
|
|
|
[Usage Example](#/overview/usage-example.md/)
|
|
|
|
which implements TCP echo server.
|