mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-24 11:54:49 +00:00
Updated net_skeleton, added extra_headers param to mg_send_file()
This commit is contained in:
1 parent
0918df1566
commit
4ca4c862a5
14 files changed
+225
-163
No files matched your search
@@ -14,7 +14,7 @@
|
||||
// Alternatively, you can license this software under a commercial
|
||||
// license, as set out in <http://cesanta.com/>.
|
||||
//
|
||||
// $Date: 2014-09-09 17:09:33 UTC $
|
||||
// $Date: 2014-09-28 05:04:41 UTC $
|
||||
|
||||
#include "net_skeleton.h"
|
||||
|
||||
@@ -38,13 +38,19 @@ struct ctl_msg {
|
||||
char message[1024 * 8];
|
||||
};
|
||||
|
||||
void iobuf_init(struct iobuf *iobuf, size_t size) {
|
||||
void iobuf_resize(struct iobuf *io, size_t new_size) {
|
||||
char *p;
|
||||
if ((new_size > io->size || (new_size < io->size && new_size >= io->len)) &&
|
||||
(p = (char *) NS_REALLOC(io->buf, new_size)) != NULL) {
|
||||
io->size = new_size;
|
||||
io->buf = p;
|
||||
}
|
||||
}
|
||||
|
||||
void iobuf_init(struct iobuf *iobuf, size_t initial_size) {
|
||||
iobuf->len = iobuf->size = 0;
|
||||
iobuf->buf = NULL;
|
||||
|
||||
if (size > 0 && (iobuf->buf = (char *) NS_MALLOC(size)) != NULL) {
|
||||
iobuf->size = size;
|
||||
}
|
||||
iobuf_resize(iobuf, initial_size);
|
||||
}
|
||||
|
||||
void iobuf_free(struct iobuf *iobuf) {
|
||||
@@ -85,8 +91,8 @@ void iobuf_remove(struct iobuf *io, size_t n) {
|
||||
|
||||
static size_t ns_out(struct ns_connection *nc, const void *buf, size_t len) {
|
||||
if (nc->flags & NSF_UDP) {
|
||||
long n = send(nc->sock, buf, len, 0);
|
||||
DBG(("%p %d send %ld (%d)", nc, nc->sock, n, errno));
|
||||
long n = sendto(nc->sock, buf, len, 0, &nc->sa.sa, sizeof(nc->sa.sin));
|
||||
DBG(("%p %d send %ld (%d %s)", nc, nc->sock, n, errno, strerror(errno)));
|
||||
return n < 0 ? 0 : n;
|
||||
} else {
|
||||
return iobuf_append(&nc->send_iobuf, buf, len);
|
||||
@@ -191,7 +197,7 @@ int ns_printf(struct ns_connection *conn, const char *fmt, ...) {
|
||||
}
|
||||
|
||||
static void hexdump(struct ns_connection *nc, const char *path,
|
||||
int num_bytes, enum ns_event ev) {
|
||||
int num_bytes, int ev) {
|
||||
const struct iobuf *io = ev == NS_SEND ? &nc->send_iobuf : &nc->recv_iobuf;
|
||||
FILE *fp;
|
||||
char *buf, src[60], dst[60];
|
||||
@@ -201,7 +207,7 @@ static void hexdump(struct ns_connection *nc, const char *path,
|
||||
ns_sock_to_str(nc->sock, src, sizeof(src), 3);
|
||||
ns_sock_to_str(nc->sock, dst, sizeof(dst), 7);
|
||||
fprintf(fp, "%lu %p %s %s %s %d\n", (unsigned long) time(NULL),
|
||||
nc->connection_data, src,
|
||||
nc->user_data, src,
|
||||
ev == NS_RECV ? "<-" : ev == NS_SEND ? "->" :
|
||||
ev == NS_ACCEPT ? "<A" : ev == NS_CONNECT ? "C>" : "XX",
|
||||
dst, num_bytes);
|
||||
@@ -215,12 +221,13 @@ static void hexdump(struct ns_connection *nc, const char *path,
|
||||
}
|
||||
}
|
||||
|
||||
static void ns_call(struct ns_connection *conn, enum ns_event ev, void *p) {
|
||||
if (conn->mgr->hexdump_file != NULL && ev != NS_POLL) {
|
||||
static void ns_call(struct ns_connection *nc, int ev, void *p) {
|
||||
if (nc->mgr->hexdump_file != NULL && ev != NS_POLL) {
|
||||
int len = (ev == NS_RECV || ev == NS_SEND) ? * (int *) p : 0;
|
||||
hexdump(conn, conn->mgr->hexdump_file, len, ev);
|
||||
hexdump(nc, nc->mgr->hexdump_file, len, ev);
|
||||
}
|
||||
if (conn->mgr->callback) conn->mgr->callback(conn, ev, p);
|
||||
|
||||
nc->callback(nc, ev, p);
|
||||
}
|
||||
|
||||
static void ns_destroy_conn(struct ns_connection *conn) {
|
||||
@@ -443,7 +450,8 @@ static int ns_use_cert(SSL_CTX *ctx, const char *pem_file) {
|
||||
}
|
||||
#endif // NS_ENABLE_SSL
|
||||
|
||||
struct ns_connection *ns_bind(struct ns_mgr *srv, const char *str, void *data) {
|
||||
struct ns_connection *ns_bind(struct ns_mgr *srv, const char *str,
|
||||
ns_callback_t callback, void *user_data) {
|
||||
union socket_address sa;
|
||||
struct ns_connection *nc = NULL;
|
||||
int use_ssl, proto;
|
||||
@@ -454,12 +462,13 @@ struct ns_connection *ns_bind(struct ns_mgr *srv, const char *str, void *data) {
|
||||
if (use_ssl && cert[0] == '\0') return NULL;
|
||||
|
||||
if ((sock = ns_open_listening_socket(&sa, proto)) == INVALID_SOCKET) {
|
||||
} else if ((nc = ns_add_sock(srv, sock, NULL)) == NULL) {
|
||||
} else if ((nc = ns_add_sock(srv, sock, callback, NULL)) == NULL) {
|
||||
closesocket(sock);
|
||||
} else {
|
||||
nc->sa = sa;
|
||||
nc->flags |= NSF_LISTENING;
|
||||
nc->connection_data = data;
|
||||
nc->user_data = user_data;
|
||||
nc->callback = callback;
|
||||
|
||||
if (proto == SOCK_DGRAM) {
|
||||
nc->flags |= NSF_UDP;
|
||||
@@ -490,7 +499,8 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
|
||||
|
||||
// NOTE(lsm): on Windows, sock is always > FD_SETSIZE
|
||||
if ((sock = accept(ls->sock, &sa.sa, &len)) == INVALID_SOCKET) {
|
||||
} else if ((c = ns_add_sock(ls->mgr, sock, NULL)) == NULL) {
|
||||
} else if ((c = ns_add_sock(ls->mgr, sock, ls->callback,
|
||||
ls->user_data)) == NULL) {
|
||||
closesocket(sock);
|
||||
#ifdef NS_ENABLE_SSL
|
||||
} else if (ls->ssl_ctx != NULL &&
|
||||
@@ -502,6 +512,7 @@ static struct ns_connection *accept_conn(struct ns_connection *ls) {
|
||||
#endif
|
||||
} else {
|
||||
c->listener = ls;
|
||||
c->proto_data = ls->proto_data;
|
||||
ns_call(c, NS_ACCEPT, &sa);
|
||||
DBG(("%p %d %p %p", c, c->sock, c->ssl_ctx, c->ssl));
|
||||
}
|
||||
@@ -698,10 +709,16 @@ static void ns_handle_udp(struct ns_connection *ls) {
|
||||
if (n <= 0) {
|
||||
DBG(("%p recvfrom: %s", ls, strerror(errno)));
|
||||
} else {
|
||||
nc.mgr = ls->mgr;
|
||||
nc.recv_iobuf.buf = buf;
|
||||
nc.recv_iobuf.len = nc.recv_iobuf.size = n;
|
||||
nc.sock = ls->sock;
|
||||
nc.callback = ls->callback;
|
||||
nc.user_data = ls->user_data;
|
||||
nc.proto_data = ls->proto_data;
|
||||
nc.mgr = ls->mgr;
|
||||
nc.listener = ls;
|
||||
nc.flags = NSF_UDP;
|
||||
DBG(("%p %d bytes received", ls, n));
|
||||
ns_call(&nc, NS_RECV, &n);
|
||||
}
|
||||
@@ -716,11 +733,10 @@ static void ns_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) {
|
||||
}
|
||||
}
|
||||
|
||||
int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
|
||||
time_t ns_mgr_poll(struct ns_mgr *mgr, int milli) {
|
||||
struct ns_connection *conn, *tmp_conn;
|
||||
struct timeval tv;
|
||||
fd_set read_set, write_set;
|
||||
int num_active_connections = 0;
|
||||
sock_t max_fd = INVALID_SOCKET;
|
||||
time_t current_time = time(NULL);
|
||||
|
||||
@@ -730,7 +746,9 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
|
||||
|
||||
for (conn = mgr->active_connections; conn != NULL; conn = tmp_conn) {
|
||||
tmp_conn = conn->next;
|
||||
ns_call(conn, NS_POLL, ¤t_time);
|
||||
if (!(conn->flags & (NSF_LISTENING | NSF_CONNECTING))) {
|
||||
ns_call(conn, NS_POLL, ¤t_time);
|
||||
}
|
||||
if (!(conn->flags & NSF_WANT_WRITE)) {
|
||||
//DBG(("%p read_set", conn));
|
||||
ns_add_to_set(conn->sock, &read_set, &max_fd);
|
||||
@@ -799,47 +817,41 @@ int ns_mgr_poll(struct ns_mgr *mgr, int milli) {
|
||||
|
||||
for (conn = mgr->active_connections; conn != NULL; conn = tmp_conn) {
|
||||
tmp_conn = conn->next;
|
||||
num_active_connections++;
|
||||
if ((conn->flags & NSF_CLOSE_IMMEDIATELY) ||
|
||||
(conn->send_iobuf.len == 0 &&
|
||||
(conn->flags & NSF_FINISHED_SENDING_DATA))) {
|
||||
ns_close_conn(conn);
|
||||
}
|
||||
}
|
||||
//DBG(("%d active connections", num_active_connections));
|
||||
|
||||
return num_active_connections;
|
||||
return current_time;
|
||||
}
|
||||
|
||||
struct ns_connection *ns_connect(struct ns_mgr *mgr,
|
||||
const char *address, void *param) {
|
||||
struct ns_connection *ns_connect(struct ns_mgr *mgr, const char *address,
|
||||
ns_callback_t callback, void *user_data) {
|
||||
sock_t sock = INVALID_SOCKET;
|
||||
struct ns_connection *nc = NULL;
|
||||
union socket_address sa;
|
||||
char cert[100], ca_cert[100];
|
||||
int connect_ret_val, use_ssl, proto;
|
||||
int rc, use_ssl, proto;
|
||||
|
||||
ns_parse_address(address, &sa, &proto, &use_ssl, cert, ca_cert);
|
||||
if ((sock = socket(AF_INET, proto, 0)) == INVALID_SOCKET) {
|
||||
return NULL;
|
||||
}
|
||||
ns_set_non_blocking_mode(sock);
|
||||
connect_ret_val = connect(sock, &sa.sa, sizeof(sa.sin));
|
||||
rc = (proto == SOCK_DGRAM) ? 0 : connect(sock, &sa.sa, sizeof(sa.sin));
|
||||
|
||||
if (connect_ret_val != 0 && ns_is_error(connect_ret_val)) {
|
||||
if (rc != 0 && ns_is_error(rc)) {
|
||||
closesocket(sock);
|
||||
return NULL;
|
||||
} else if ((nc = ns_add_sock(mgr, sock, param)) == NULL) {
|
||||
} else if ((nc = ns_add_sock(mgr, sock, callback, user_data)) == NULL) {
|
||||
closesocket(sock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nc->sa = sa; // Essential, cause UDP conns will use sendto()
|
||||
if (proto == SOCK_DGRAM) {
|
||||
nc->flags = NSF_UDP;
|
||||
} else {
|
||||
nc->flags = NSF_CONNECTING;
|
||||
}
|
||||
nc->sa = sa; // Important, cause UDP conns will use sendto()
|
||||
nc->flags = (proto == SOCK_DGRAM) ? NSF_UDP : NSF_CONNECTING;
|
||||
|
||||
#ifdef NS_ENABLE_SSL
|
||||
if (use_ssl) {
|
||||
@@ -858,14 +870,16 @@ struct ns_connection *ns_connect(struct ns_mgr *mgr,
|
||||
return nc;
|
||||
}
|
||||
|
||||
struct ns_connection *ns_add_sock(struct ns_mgr *s, sock_t sock, void *p) {
|
||||
struct ns_connection *ns_add_sock(struct ns_mgr *s, sock_t sock,
|
||||
ns_callback_t callback, void *user_data) {
|
||||
struct ns_connection *conn;
|
||||
if ((conn = (struct ns_connection *) NS_MALLOC(sizeof(*conn))) != NULL) {
|
||||
memset(conn, 0, sizeof(*conn));
|
||||
ns_set_non_blocking_mode(sock);
|
||||
ns_set_close_on_exec(sock);
|
||||
conn->sock = sock;
|
||||
conn->connection_data = p;
|
||||
conn->user_data = user_data;
|
||||
conn->callback = callback;
|
||||
conn->mgr = s;
|
||||
conn->last_io_time = time(NULL);
|
||||
ns_add_conn(s, conn);
|
||||
@@ -890,11 +904,10 @@ void ns_broadcast(struct ns_mgr *mgr, ns_callback_t cb,void *data, size_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
void ns_mgr_init(struct ns_mgr *s, void *user_data, ns_callback_t cb) {
|
||||
void ns_mgr_init(struct ns_mgr *s, void *user_data) {
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->ctl[0] = s->ctl[1] = INVALID_SOCKET;
|
||||
s->user_data = user_data;
|
||||
s->callback = cb;
|
||||
|
||||
#ifdef _WIN32
|
||||
{ WSADATA data; WSAStartup(MAKEWORD(2, 2), &data); }
|
||||
|
||||
Reference in new issue
Block a user