Commit 2a4ca9d5 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Marko Mikulicic

Introduce a network interface API, refactor UDP

    PUBLISHED_FROM=6e961e2760b2b64e211978ede5df8ca353ea5512
parent 747e40e7
...@@ -43,6 +43,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { ...@@ -43,6 +43,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
*/ */
mg_dns_send_reply(nc, &reply); mg_dns_send_reply(nc, &reply);
nc->flags |= MG_F_SEND_AND_CLOSE;
break; break;
} }
} }
......
...@@ -4,7 +4,7 @@ CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -DMG_ENABLE_IPV6 -DMG_ENABLE_ ...@@ -4,7 +4,7 @@ CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -DMG_ENABLE_IPV6 -DMG_ENABLE_
all: $(PROG) all: $(PROG)
$(PROG): $(SOURCES) $(PROG): $(SOURCES)
$(CC) $(SOURCES) -o $@ $(CFLAGS) $(CC) $(SOURCES) -g -o $@ $(CFLAGS)
$(PROG).exe: $(SOURCES) $(PROG).exe: $(SOURCES)
cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /MD /Fe$@ cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /MD /Fe$@
......
This diff is collapsed.
...@@ -673,6 +673,7 @@ struct mg_connection { ...@@ -673,6 +673,7 @@ struct mg_connection {
struct mg_mgr *mgr; /* Pointer to containing manager */ struct mg_mgr *mgr; /* Pointer to containing manager */
sock_t sock; /* Socket to the remote peer */ sock_t sock; /* Socket to the remote peer */
int err;
union socket_address sa; /* Remote peer address */ union socket_address sa; /* Remote peer address */
size_t recv_mbuf_limit; /* Max size of recv buffer */ size_t recv_mbuf_limit; /* Max size of recv buffer */
struct mbuf recv_mbuf; /* Received data */ struct mbuf recv_mbuf; /* Received data */
...@@ -923,13 +924,11 @@ const char *mg_set_ssl(struct mg_connection *nc, const char *cert, ...@@ -923,13 +924,11 @@ const char *mg_set_ssl(struct mg_connection *nc, const char *cert,
/* /*
* Send data to the connection. * Send data to the connection.
* *
* Return number of written bytes. Note that sending * Note that sending functions do not actually push data to the socket.
* functions do not actually push data to the socket. They just append data * They just append data to the output buffer. MG_EV_SEND will be delivered when
* to the output buffer. The exception is UDP connections. For UDP, data is * the data has actually been pushed out.
* sent immediately, and returned value indicates an actual number of bytes
* sent to the socket.
*/ */
int mg_send(struct mg_connection *, const void *buf, int len); void mg_send(struct mg_connection *, const void *buf, int len);
/* Enables format string warnings for mg_printf */ /* Enables format string warnings for mg_printf */
#if defined(__GNUC__) #if defined(__GNUC__)
...@@ -1013,6 +1012,57 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7, ...@@ -1013,6 +1012,57 @@ enum v7_err mg_enable_javascript(struct mg_mgr *m, struct v7 *v7,
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* MG_NET_HEADER_INCLUDED */ #endif /* MG_NET_HEADER_INCLUDED */
#ifndef MG_NET_IF_HEADER_INCLUDED
#define MG_NET_IF_HEADER_INCLUDED
/*
* 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.
*/
/* 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. */
void mg_if_accept_tcp_cb(struct mg_connection *lc, sock_t sock,
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.
* No more than one chunk of data can be unacknowledged at any time.
*/
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);
/* Perform interface-related cleanup on connection before destruction. */
void mg_if_destroy_conn(struct mg_connection *nc);
#endif /* MG_NET_IF_HEADER_INCLUDED */
/* /*
* Copyright (c) 2014 Cesanta Software Limited * Copyright (c) 2014 Cesanta Software Limited
* All rights reserved * All rights reserved
...@@ -2344,7 +2394,7 @@ int mg_dns_reply_record(struct mg_dns_reply *, struct mg_dns_resource_record *, ...@@ -2344,7 +2394,7 @@ int mg_dns_reply_record(struct mg_dns_reply *, struct mg_dns_resource_record *,
* Once sent, the IO buffer will be trimmed unless the reply IO buffer * Once sent, the IO buffer will be trimmed unless the reply IO buffer
* is the connection's send buffer and the connection is not in UDP mode. * is the connection's send buffer and the connection is not in UDP mode.
*/ */
int mg_dns_send_reply(struct mg_connection *, struct mg_dns_reply *); void mg_dns_send_reply(struct mg_connection *, struct mg_dns_reply *);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment