mirror of
https://github.com/cesanta/mongoose.git
synced 2025-06-06 00:22:44 +08:00
Implementing MG interfaces
PUBLISHED_FROM=46496c2d5340a0bbe4fe1c6c9ff187bc65d2d35f
This commit is contained in:
parent
8bf3bd48a6
commit
fffb54e2c6
@ -15,6 +15,7 @@ items:
|
||||
- { name: mg_enable_multithreading.md }
|
||||
- { name: mg_mgr_free.md }
|
||||
- { name: mg_mgr_init.md }
|
||||
- { name: mg_mgr_init_opt.md }
|
||||
- { name: mg_mgr_poll.md }
|
||||
- { name: mg_next.md }
|
||||
- { name: mg_printf.md }
|
||||
@ -31,6 +32,7 @@ items:
|
||||
- { name: struct_mg_connect_opts.md }
|
||||
- { name: struct_mg_connection.md }
|
||||
- { name: struct_mg_mgr.md }
|
||||
- { name: struct_mg_mgr_init_opts.md }
|
||||
- { name: struct_mg_multithreading_opts.md }
|
||||
---
|
||||
|
||||
|
@ -3,7 +3,8 @@ title: "mg_event_handler_t"
|
||||
decl_name: "mg_event_handler_t"
|
||||
symbol_kind: "typedef"
|
||||
signature: |
|
||||
typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev, void *ev_data);
|
||||
typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev,
|
||||
void *ev_data);
|
||||
---
|
||||
|
||||
Callback function (event handler) prototype. Must be defined by the user.
|
||||
|
14
docs/c-api/net.h/mg_mgr_init_opt.md
Normal file
14
docs/c-api/net.h/mg_mgr_init_opt.md
Normal file
@ -0,0 +1,14 @@
|
||||
---
|
||||
title: "mg_mgr_init_opt()"
|
||||
decl_name: "mg_mgr_init_opt"
|
||||
symbol_kind: "func"
|
||||
signature: |
|
||||
void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data,
|
||||
struct mg_mgr_init_opts opts);
|
||||
---
|
||||
|
||||
Like `mg_mgr_init` but with more options.
|
||||
|
||||
Notably, this allows you to create a manger and choose
|
||||
dynamically which networking interface implementation to use.
|
||||
|
@ -42,6 +42,7 @@ signature: |
|
||||
} priv_1; /* Used by mg_enable_multithreading() */
|
||||
void *priv_2; /* Used by mg_enable_multithreading() */
|
||||
void *mgr_data; /* Implementation-specific event manager's data. */
|
||||
struct mg_iface *iface;
|
||||
unsigned long flags;
|
||||
/* Flags set by Mongoose */
|
||||
#define MG_F_LISTENING (1 << 0) /* This connection is listening */
|
||||
|
@ -12,7 +12,8 @@ signature: |
|
||||
sock_t ctl[2]; /* Socketpair for mg_broadcast() */
|
||||
#endif
|
||||
void *user_data; /* User data */
|
||||
void *mgr_data; /* Implementation-specific event manager's data. */
|
||||
int num_ifaces;
|
||||
struct mg_iface **ifaces; /* network interfaces */
|
||||
#if MG_ENABLE_JAVASCRIPT
|
||||
struct v7 *v7;
|
||||
#endif
|
||||
|
26
docs/c-api/net.h/struct_mg_mgr_init_opts.md
Normal file
26
docs/c-api/net.h/struct_mg_mgr_init_opts.md
Normal file
@ -0,0 +1,26 @@
|
||||
---
|
||||
title: "struct mg_mgr_init_opts"
|
||||
decl_name: "struct mg_mgr_init_opts"
|
||||
symbol_kind: "struct"
|
||||
signature: |
|
||||
struct mg_mgr_init_opts {
|
||||
struct mg_iface_vtable *main_iface;
|
||||
int num_ifaces;
|
||||
struct mg_iface_vtable **ifaces;
|
||||
};
|
||||
---
|
||||
|
||||
Optional parameters to `mg_mgr_init_opt()`.
|
||||
|
||||
If `main_iface` is not NULL, it will be used as the main interface in the
|
||||
default interface set. The pointer will be free'd by `mg_mgr_free`.
|
||||
Otherwise, the main interface will be autodetected based on the current
|
||||
platform.
|
||||
|
||||
If `num_ifaces` is 0 and `ifaces` is NULL, the default interface set will be
|
||||
used.
|
||||
This is an advanced option, as it requires you to construct a full interface
|
||||
set, including special networking interfaces required by some optional
|
||||
features such as TCP tunneling. Memory backing `ifaces` and each of the
|
||||
`num_ifaces` pointers it contains will be reclaimed by `mg_mgr_free`.
|
||||
|
571
mongoose.c
571
mongoose.c
File diff suppressed because it is too large
Load Diff
261
mongoose.h
261
mongoose.h
@ -2769,6 +2769,126 @@ struct { \
|
||||
|
||||
#endif /* CS_MONGOOSE_SRC_FEATURES_H_ */
|
||||
#ifdef MG_MODULE_LINES
|
||||
#line 1 "mongoose/src/net_if.h"
|
||||
#endif
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef CS_MONGOOSE_SRC_NET_IF_H_
|
||||
#define CS_MONGOOSE_SRC_NET_IF_H_
|
||||
|
||||
/* Amalgamated: #include "common/platform.h" */
|
||||
|
||||
/*
|
||||
* Internal async networking core interface.
|
||||
* Consists of calls made by the core, which should not block,
|
||||
* and callbacks back into the core ("..._cb").
|
||||
* Callbacks may (will) cause methods to be invoked from within,
|
||||
* but methods are not allowed to invoke callbacks inline.
|
||||
*
|
||||
* Implementation must ensure that only one callback is invoked at any time.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define MG_MAIN_IFACE 0
|
||||
#define MG_NUM_IFACES 1
|
||||
|
||||
struct mg_mgr;
|
||||
struct mg_connection;
|
||||
union socket_address;
|
||||
|
||||
struct mg_iface_vtable;
|
||||
|
||||
struct mg_iface {
|
||||
struct mg_mgr *mgr;
|
||||
void *data; /* Implementation-specific data */
|
||||
struct mg_iface_vtable *vtable;
|
||||
};
|
||||
|
||||
struct mg_iface_vtable {
|
||||
void (*init)(struct mg_iface *iface);
|
||||
void (*free)(struct mg_iface *iface);
|
||||
void (*add_conn)(struct mg_connection *nc);
|
||||
void (*remove_conn)(struct mg_connection *nc);
|
||||
time_t (*poll)(struct mg_iface *iface, int timeout_ms);
|
||||
|
||||
/* Set up a listening TCP socket on a given address. rv = 0 -> ok. */
|
||||
int (*listen_tcp)(struct mg_connection *nc, union socket_address *sa);
|
||||
/* Request that a "listening" UDP socket be created. */
|
||||
int (*listen_udp)(struct mg_connection *nc, union socket_address *sa);
|
||||
|
||||
/* Request that a TCP connection is made to the specified address. */
|
||||
void (*connect_tcp)(struct mg_connection *nc, const union socket_address *sa);
|
||||
/* Open a UDP socket. Doesn't actually connect anything. */
|
||||
void (*connect_udp)(struct mg_connection *nc);
|
||||
|
||||
/* Send functions for TCP and UDP. Sent data is copied before return. */
|
||||
void (*tcp_send)(struct mg_connection *nc, const void *buf, size_t len);
|
||||
void (*udp_send)(struct mg_connection *nc, const void *buf, size_t len);
|
||||
|
||||
void (*recved)(struct mg_connection *nc, size_t len);
|
||||
|
||||
/* Perform interface-related connection initialization. Return 1 on ok. */
|
||||
int (*create_conn)(struct mg_connection *nc);
|
||||
/* Perform interface-related cleanup on connection before destruction. */
|
||||
void (*destroy_conn)(struct mg_connection *nc);
|
||||
|
||||
/* Associate a socket to a connection. */
|
||||
void (*sock_set)(struct mg_connection *nc, sock_t sock);
|
||||
|
||||
/* Put connection's address into *sa, local (remote = 0) or remote. */
|
||||
void (*get_conn_addr)(struct mg_connection *nc, int remote,
|
||||
union socket_address *sa);
|
||||
};
|
||||
|
||||
extern struct mg_iface_vtable *mg_ifaces[];
|
||||
|
||||
/* Creates a new interface instance. */
|
||||
struct mg_iface *mg_if_create_iface(struct mg_iface_vtable *vtable,
|
||||
struct mg_mgr *mgr);
|
||||
|
||||
/*
|
||||
* Deliver a new TCP connection. Returns NULL in case on error (unable to
|
||||
* create connection, in which case interface state should be discarded.
|
||||
* This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered
|
||||
* when mg_if_accept_tcp_cb is invoked.
|
||||
*/
|
||||
struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc);
|
||||
void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
|
||||
size_t sa_len);
|
||||
|
||||
/* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */
|
||||
void mg_if_connect_cb(struct mg_connection *nc, int err);
|
||||
/* Callback that reports that data has been put on the wire. */
|
||||
void mg_if_sent_cb(struct mg_connection *nc, int num_sent);
|
||||
/*
|
||||
* Receive callback.
|
||||
* buf must be heap-allocated and ownership is transferred to the core.
|
||||
* Core will acknowledge consumption by calling iface::recved.
|
||||
*/
|
||||
void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len);
|
||||
void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
|
||||
union socket_address *sa, size_t sa_len);
|
||||
|
||||
/* void mg_if_close_conn(struct mg_connection *nc); */
|
||||
|
||||
/* Deliver a POLL event to the connection. */
|
||||
void mg_if_poll(struct mg_connection *nc, time_t now);
|
||||
|
||||
/* Deliver a TIMER event to the connection. */
|
||||
void mg_if_timer(struct mg_connection *c, double now);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* CS_MONGOOSE_SRC_NET_IF_H_ */
|
||||
#ifdef MG_MODULE_LINES
|
||||
#line 1 "mongoose/src/net.h"
|
||||
#endif
|
||||
/*
|
||||
@ -2807,6 +2927,7 @@ struct { \
|
||||
#endif
|
||||
|
||||
/* Amalgamated: #include "mongoose/src/common.h" */
|
||||
/* Amalgamated: #include "mongoose/src/net_if.h" */
|
||||
/* Amalgamated: #include "common/mbuf.h" */
|
||||
|
||||
#if MG_ENABLE_SSL
|
||||
@ -2850,7 +2971,8 @@ struct mg_connection;
|
||||
* Callback function (event handler) prototype. Must be defined by the user.
|
||||
* Mongoose calls the event handler, passing the events defined below.
|
||||
*/
|
||||
typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev, void *ev_data);
|
||||
typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev,
|
||||
void *ev_data);
|
||||
|
||||
/* Events. Meaning of event parameter (evp) is given in the comment. */
|
||||
#define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */
|
||||
@ -2873,7 +2995,8 @@ struct mg_mgr {
|
||||
sock_t ctl[2]; /* Socketpair for mg_broadcast() */
|
||||
#endif
|
||||
void *user_data; /* User data */
|
||||
void *mgr_data; /* Implementation-specific event manager's data. */
|
||||
int num_ifaces;
|
||||
struct mg_iface **ifaces; /* network interfaces */
|
||||
#if MG_ENABLE_JAVASCRIPT
|
||||
struct v7 *v7;
|
||||
#endif
|
||||
@ -2921,6 +3044,7 @@ struct mg_connection {
|
||||
} priv_1; /* Used by mg_enable_multithreading() */
|
||||
void *priv_2; /* Used by mg_enable_multithreading() */
|
||||
void *mgr_data; /* Implementation-specific event manager's data. */
|
||||
struct mg_iface *iface;
|
||||
unsigned long flags;
|
||||
/* Flags set by Mongoose */
|
||||
#define MG_F_LISTENING (1 << 0) /* This connection is listening */
|
||||
@ -2958,6 +3082,36 @@ struct mg_connection {
|
||||
*/
|
||||
void mg_mgr_init(struct mg_mgr *mgr, void *user_data);
|
||||
|
||||
/*
|
||||
* Optional parameters to `mg_mgr_init_opt()`.
|
||||
*
|
||||
* If `main_iface` is not NULL, it will be used as the main interface in the
|
||||
* default interface set. The pointer will be free'd by `mg_mgr_free`.
|
||||
* Otherwise, the main interface will be autodetected based on the current
|
||||
* platform.
|
||||
*
|
||||
* If `num_ifaces` is 0 and `ifaces` is NULL, the default interface set will be
|
||||
* used.
|
||||
* This is an advanced option, as it requires you to construct a full interface
|
||||
* set, including special networking interfaces required by some optional
|
||||
* features such as TCP tunneling. Memory backing `ifaces` and each of the
|
||||
* `num_ifaces` pointers it contains will be reclaimed by `mg_mgr_free`.
|
||||
*/
|
||||
struct mg_mgr_init_opts {
|
||||
struct mg_iface_vtable *main_iface;
|
||||
int num_ifaces;
|
||||
struct mg_iface_vtable **ifaces;
|
||||
};
|
||||
|
||||
/*
|
||||
* Like `mg_mgr_init` but with more options.
|
||||
*
|
||||
* Notably, this allows you to create a manger and choose
|
||||
* dynamically which networking interface implementation to use.
|
||||
*/
|
||||
void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data,
|
||||
struct mg_mgr_init_opts opts);
|
||||
|
||||
/*
|
||||
* De-initialises Mongoose manager.
|
||||
*
|
||||
@ -3324,97 +3478,6 @@ double mg_time(void);
|
||||
|
||||
#endif /* CS_MONGOOSE_SRC_NET_H_ */
|
||||
#ifdef MG_MODULE_LINES
|
||||
#line 1 "mongoose/src/net_if.h"
|
||||
#endif
|
||||
/*
|
||||
* Copyright (c) 2014-2016 Cesanta Software Limited
|
||||
* All rights reserved
|
||||
*/
|
||||
|
||||
#ifndef CS_MONGOOSE_SRC_NET_IF_H_
|
||||
#define CS_MONGOOSE_SRC_NET_IF_H_
|
||||
|
||||
/*
|
||||
* Internal async networking core interface.
|
||||
* Consists of calls made by the core, which should not block,
|
||||
* and callbacks back into the core ("..._cb").
|
||||
* Callbacks may (will) cause methods to be invoked from within,
|
||||
* but methods are not allowed to invoke callbacks inline.
|
||||
*
|
||||
* Implementation must ensure that only one callback is invoked at any time.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/* Request that a TCP connection is made to the specified address. */
|
||||
void mg_if_connect_tcp(struct mg_connection *nc,
|
||||
const union socket_address *sa);
|
||||
/* Open a UDP socket. Doesn't actually connect anything. */
|
||||
void mg_if_connect_udp(struct mg_connection *nc);
|
||||
/* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */
|
||||
void mg_if_connect_cb(struct mg_connection *nc, int err);
|
||||
|
||||
/* Set up a listening TCP socket on a given address. rv = 0 -> ok. */
|
||||
int mg_if_listen_tcp(struct mg_connection *nc, union socket_address *sa);
|
||||
|
||||
/*
|
||||
* Deliver a new TCP connection. Returns NULL in case on error (unable to
|
||||
* create connection, in which case interface state should be discarded.
|
||||
* This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered
|
||||
* when mg_if_accept_tcp_cb is invoked.
|
||||
*/
|
||||
struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc);
|
||||
void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa,
|
||||
size_t sa_len);
|
||||
|
||||
/* Request that a "listening" UDP socket be created. */
|
||||
int mg_if_listen_udp(struct mg_connection *nc, union socket_address *sa);
|
||||
|
||||
/* Send functions for TCP and UDP. Sent data is copied before return. */
|
||||
void mg_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len);
|
||||
void mg_if_udp_send(struct mg_connection *nc, const void *buf, size_t len);
|
||||
/* Callback that reports that data has been put on the wire. */
|
||||
void mg_if_sent_cb(struct mg_connection *nc, int num_sent);
|
||||
|
||||
/*
|
||||
* Receive callback.
|
||||
* buf must be heap-allocated and ownership is transferred to the core.
|
||||
* Core will acknowledge consumption by calling mg_if_recved.
|
||||
*/
|
||||
void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len);
|
||||
void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len,
|
||||
union socket_address *sa, size_t sa_len);
|
||||
void mg_if_recved(struct mg_connection *nc, size_t len);
|
||||
|
||||
/* Deliver a POLL event to the connection. */
|
||||
void mg_if_poll(struct mg_connection *nc, time_t now);
|
||||
|
||||
/* Deliver a TIMER event to the connection. */
|
||||
void mg_if_timer(struct mg_connection *c, double now);
|
||||
|
||||
/* Perform interface-related connection initialization. Return 1 on success. */
|
||||
int mg_if_create_conn(struct mg_connection *nc);
|
||||
|
||||
/* Perform interface-related cleanup on connection before destruction. */
|
||||
void mg_if_destroy_conn(struct mg_connection *nc);
|
||||
|
||||
void mg_close_conn(struct mg_connection *nc);
|
||||
|
||||
/* Put connection's address into *sa, local (remote = 0) or remote. */
|
||||
void mg_if_get_conn_addr(struct mg_connection *nc, int remote,
|
||||
union socket_address *sa);
|
||||
|
||||
/* Associate a socket to a connection. */
|
||||
void mg_sock_set(struct mg_connection *nc, sock_t sock);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* CS_MONGOOSE_SRC_NET_IF_H_ */
|
||||
#ifdef MG_MODULE_LINES
|
||||
#line 1 "mongoose/src/uri.h"
|
||||
#endif
|
||||
/*
|
||||
@ -3805,12 +3868,12 @@ struct mg_ssi_call_ctx {
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_HTTP_STREAMING_MULTIPART
|
||||
#define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */
|
||||
#define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_MULTIPART_REQUEST_END 125 /* struct mg_http_multipart_part \
|
||||
*/
|
||||
#define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */
|
||||
#define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */
|
||||
/* struct mg_http_multipart_part */
|
||||
#define MG_EV_HTTP_MULTIPART_REQUEST_END 125
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
Loading…
Reference in New Issue
Block a user