# User Guide
## Introduction
Mongoose is a networking library for C/C++. It implements an event-driven,
non-blocking APIs for TCP, UDP, HTTP, WebSocket, MQTT. It has been designed
for connecting devices and bringing them online. On the market since 2004, used
by vast number of open source and commercial products - it even runs on the
International Space Station! Mongoose makes embedded network programming fast,
robust, and easy.
## Features
* Cross-platform: works on Linux/UNIX, MacOS, QNX, eCos, Windows, Android,
iPhone, FreeRTOS, etc
* Supported hardware platforms: TI CC3200, TI MSP432, NRF52, STM32, PIC32, ESP8266, ESP32 and more
* Builtin protocols:
- plain TCP, plain UDP, SSL/TLS (over TCP, one-way or two-way)
- HTTP client, HTTP server
- WebSocket client, WebSocket server
- MQTT client
- DNS client, async DNS resolver
* Single-threaded, asynchronous, non-blocking core with simple event-based API
* Native support for [LWIP embedded TCP/IP stack](https://en.wikipedia.org/wiki/LwIP)
* Tiny static and run-time footprint
* Source code is both ISO C and ISO C++ compliant
* Very easy to integrate: just copy
[mongoose.c](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c) and
[mongoose.h](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h)
files to your build tree
## Concept
Mongoose has three basic data structures:
- `struct mg_mgr` - an event manager that holds all active connections
- `struct mg_connection` - describes a connection
- `struct mg_iobuf` - describes data buffer (received or sent data)
Connections could be either listening, outbound or inbound. Outbound
connections are created by the `mg_connect()` call. Listening connections are
created by the `mg_listen()` call. Inbound connections are those accepted by a
listening connection. Each connection is described by a `struct mg_connection`
structure, which has a number of fields. All fields are exposed to the
application by design, to give an application a full visibility into the
Mongoose's internals.
An application that uses mongoose should follow a standard pattern of
event-driven application:
1. Declare and initialise an event manager:
```c
struct mg_mgr mgr;
mg_mgr_init(&mgr);
```
2. Create connections. For example, a server application should create listening
connections. When any connection is created (listening or outgoing), an
event handler function must be specified. An event handler function defines
connection's behavior.
```c
struct mg_connection *c = mg_http_listen(&mgr, "0.0.0.0:8000", fn, arg);
```
3. Create an event loop by calling `mg_mgr_poll()`:
```c
for (;;) {
mg_mgr_poll(&mgr, 1000);
}
```
`mg_mgr_poll()` iterates over all sockets, accepts new connections, sends and
receives data, closes connections and calls event handler functions for the
respective events.
Since the Mongoose's core is not protected against concurrent accesses,
make sure that all `mg_*` API functions are called from the same thread
or RTOS task.
## Send and receive buffers
Each connection has a send and receive buffer:
- `struct mg_connection::send` - data to be sent to a peer
- `struct mg_connection::recv` - data received from a peer
When data arrives, Mongoose appends received data to the `recv` and triggers an
`MG_EV_RECV` event. The user may send data back by calling one of the output
functions, like `mg_send()` or `mg_printf()`. Output functions append data to
the `send` buffer. When Mongoose successfully writes data to the socket, it
discards data from struct `mg_connection::send` and sends an `MG_EV_SEND`
event.
## Event handler function
Each connection has an event handler function associated with it. That function
must be implemented by the user. Event handler is the key element of the
Mongoose application, since it defines the connection's behaviour. This is
what an event handler function looks like:
```c
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
switch (ev) {
/* Event handler code that defines behavior of the connection */
...
}
}
```
- `struct mg_connection *c` - a connection that received an event
- `int ev` - an event number, defined in mongoose.h. For example, when data
arrives on an inbound connection, ev would be `MG_EV_RECV`
- `void *ev_data` - points to the event-specific data, and it has a different
meaning for different events. For example, for an `MG_EV_RECV` event,
`ev_data`
is an `int *` pointing to the number of bytes received from a remote
peer and saved into the `c->recv` IO buffer. The exact meaning of `ev_data` is
described for each event. Protocol-specific events usually have `ev_data`
pointing to structures that hold protocol-specific information
- `void *fn_data` - a user-defined pointer for the connection, which is a
placeholder for application-specific data
## Events
Below is the list of events trigged by Mongoose, taken as-is from `mongoose.h`.
For each event, a comment describes a meaning of the `ev_data` pointer passed
to an event handler:
```c
enum {
MG_EV_ERROR, // Error char *error_message
MG_EV_POLL, // mg_mgr_poll iteration unsigned long *millis
MG_EV_RESOLVE, // Host name is resolved NULL
MG_EV_CONNECT, // Connection established NULL
MG_EV_ACCEPT, // Connection accepted NULL
MG_EV_READ, // Data received from socket struct mg_str *
MG_EV_WRITE, // Data written to socket int *num_bytes_written
MG_EV_CLOSE, // Connection closed NULL
MG_EV_HTTP_MSG, // HTTP request/response struct mg_http_message *
MG_EV_WS_OPEN, // Websocket handshake done struct mg_http_message *
MG_EV_WS_MSG, // Websocket msg, text or bin struct mg_ws_message *
MG_EV_WS_CTL, // Websocket control msg struct mg_ws_message *
MG_EV_MQTT_CMD, // MQTT low-level command struct mg_mqtt_message *
MG_EV_MQTT_MSG, // MQTT PUBLISH received struct mg_mqtt_message *
MG_EV_MQTT_OPEN, // MQTT CONNACK received int *connack_status_code
MG_EV_SNTP_TIME, // SNTP time received struct timeval *
MG_EV_USER, // Starting ID for user events
};
```
## Connection flags
`struct mg_connection` has a bitfield with connection flags. Flags are binary,
they can be either 0 or 1. Some flags are set by Mongoose and must be not
changed by an application code, for example `is_udp` flag tells application if
that connection is UDP or not. Some flags can be changed by application, for
example, `is_draining` flag, if set by an application, tells Mongoose to send
the remaining data to peer, and when everything is sent, close the connection.
User-changeable flags are: `is_hexdumping`, `is_draining`, `is_closing`.
This is taken from `mongoose.h` as-is:
```c
struct mg_connection {
...
unsigned is_listening : 1; // Listening connection
unsigned is_client : 1; // Outbound (client) connection
unsigned is_accepted : 1; // Accepted (server) connection
unsigned is_resolving : 1; // Non-blocking DNS resolv is in progress
unsigned is_connecting : 1; // Non-blocking connect is in progress
unsigned is_tls : 1; // TLS-enabled connection
unsigned is_tls_hs : 1; // TLS handshake is in progress
unsigned is_udp : 1; // UDP connection
unsigned is_websocket : 1; // WebSocket connection
unsigned is_hexdumping : 1; // Hexdump in/out traffic
unsigned is_draining : 1; // Send remaining data, then close and free
unsigned is_closing : 1; // Close and free the connection immediately
unsigned is_readable : 1; // Connection is ready to read
unsigned is_writable : 1; // Connection is ready to write
};
```
## Build options
Mongoose source code ships in two files:
- [mongoose.h](https://github.com/cesanta/mongoose/blob/master/mongoose.h) - API definitions
- [mongoose.c](http
没有合适的资源?快使用搜索试试~ 我知道了~
c++mongoose 实现httpserver,httpclient
共203个文件
c:50个
h:37个
makefile:23个
5星 · 超过95%的资源 需积分: 25 19 下载量 15 浏览量
2022-09-30
21:06:41
上传
评论 1
收藏 1.61MB ZIP 举报
温馨提示
使用mongoose实现了httpserver,httpclient,websocketserver
资源详情
资源评论
资源推荐
收起资源包目录
c++mongoose 实现httpserver,httpclient (203个子文件)
mongoose.c 330KB
mongoose.c 131KB
unit_test.c 38KB
mjson.c 30KB
mjson.c 30KB
http.c 29KB
sock.c 17KB
tls.c 11KB
util.c 9KB
dns.c 9KB
mqtt.c 8KB
main.c 7KB
sha1.c 7KB
ws.c 7KB
main.c 6KB
lwip.c 6KB
md5.c 6KB
net.c 4KB
main.c 4KB
wifi.c 4KB
main.c 3KB
wifi.c 3KB
main.c 3KB
main.c 3KB
ssi.c 3KB
main.c 3KB
main.c 3KB
url.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
str.c 2KB
sntp.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
base64.c 2KB
main.c 2KB
main.c 2KB
main.c 2KB
log.c 2KB
iobuf.c 2KB
main.c 1KB
timer.c 1KB
fuzz.c 1KB
main.c 882B
event.c 563B
mongoose.c 22B
.clang-format 166B
restful_server.cpp 9KB
WebSock.cpp 4KB
HttpClient.cpp 2KB
HttpServerRun.cpp 868B
httpserver2.cpp 862B
bootstrap.min.css 152KB
bootstrap.min.css 152KB
style.css 563B
style.css 464B
style.css 407B
partitions.csv 212B
sdkconfig.defaults 137B
httpserver2.vcxproj.filters 2KB
mongoose.h 112KB
mongoose.h 27KB
mjson.h 7KB
mjson.h 7KB
request.h 3KB
net.h 3KB
restful_server.h 3KB
util.h 3KB
WebSock.h 2KB
arch.h 2KB
http.h 2KB
mqtt.h 2KB
arch_freertos.h 2KB
event.h 2KB
dns.h 1KB
config.h 1KB
arch_win32.h 1KB
license.h 778B
ws.h 771B
timer.h 745B
str.h 724B
log.h 719B
HttpClient.h 664B
tls.h 646B
sha1.h 470B
arch_unix.h 427B
HttpServerRun.h 368B
iobuf.h 356B
private.h 331B
sntp.h 324B
md5.h 288B
url.h 285B
base64.h 231B
arch_esp32.h 214B
arch_esp8266.h 210B
ssi.h 143B
共 203 条
- 1
- 2
- 3
qianbo_insist
- 粉丝: 1w+
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论10