#define AGAIN 1
#define ERROR 2
+#define ATTACHED(s) ((s)->flags & EVCOM_ATTACHED)
+#define LISTENING(s) ((s)->flags & EVCOM_LISTENING)
+#define CONNECTED(s) ((s)->flags & EVCOM_CONNECTED)
+#define SECURE(s) ((s)->flags & EVCOM_SECURE)
+#define GOT_HALF_CLOSE(s) ((s)->flags & EVCOM_GOT_HALF_CLOSE)
+#define GOT_FULL_CLOSE(s) ((s)->flags & EVCOM_GOT_FULL_CLOSE)
+#define TOO_MANY_CONN(s) ((s)->flags & EVCOM_TOO_MANY_CONN)
+
+
+static void
+accept_new_connections (evcom_server *server)
+{
+ if (TOO_MANY_CONN(server)) {
+#if EV_MULTIPLICITY
+ evcom_server_attach(server->loop, server);
+#else
+ evcom_server_attach(server);
+#endif
+ server->flags &= ~EVCOM_TOO_MANY_CONN;
+ }
+}
+
+static void
+dont_accept_new_connections (evcom_server *server)
+{
+ evcom_server_detach(server);
+ server->flags |= EVCOM_TOO_MANY_CONN;
+}
+
EV_INLINE int
set_nonblock (int fd)
{
return buf;
}
-#define CLOSE_ASAP(socket) do { \
- (socket)->read_action = full_close; \
- (socket)->write_action = full_close; \
-} while (0)
-
static int
full_close (evcom_socket *socket)
{
return OKAY;
}
+static inline void
+close_asap (evcom_socket *socket)
+{
+ socket->read_action = full_close;
+ socket->write_action = full_close;
+}
+
static int
half_close (evcom_socket *socket)
{
return;
}
- if (socket->got_half_close == FALSE) {
- if (socket->got_full_close == FALSE) {
+ if (!GOT_HALF_CLOSE(socket)) {
+ if (!GOT_FULL_CLOSE(socket)) {
/* Normal situation. Didn't get any close signals. */
ev_io_stop(SOCKET_LOOP_ &socket->write_watcher);
} else {
/* Got Full Close. */
if (socket->read_action) {
#if EVCOM_HAVE_GNUTLS
- socket->read_action = socket->secure ? secure_full_goodbye : full_close;
+ socket->read_action = SECURE(socket) ? secure_full_goodbye : full_close;
#else
socket->read_action = full_close;
#endif
if (socket->write_action) {
#if EVCOM_HAVE_GNUTLS
- socket->write_action = socket->secure ? secure_full_goodbye : full_close;
+ socket->write_action = SECURE(socket) ? secure_full_goodbye : full_close;
#else
socket->write_action = full_close;
#endif
/* Got Half Close. */
if (socket->write_action) {
#if EVCOM_HAVE_GNUTLS
- socket->write_action = socket->secure ? secure_half_goodbye : half_close;
+ socket->write_action = SECURE(socket) ? secure_half_goodbye : half_close;
#else
socket->write_action = half_close;
#endif
nosigpipe_push (gnutls_transport_ptr_t data, const void *buf, size_t len)
{
evcom_socket *socket = (evcom_socket*)data;
- assert(socket->secure);
+ assert(SECURE(socket));
int flags = 0;
#ifdef MSG_NOSIGNAL
flags |= MSG_NOSIGNAL;
static int
secure_handshake (evcom_socket *socket)
{
- assert(socket->secure);
+ assert(SECURE(socket));
int r = gnutls_handshake(socket->session);
evcom_socket_reset_timeout(socket);
- if (!socket->connected) {
- socket->connected = TRUE;
+ if (!CONNECTED(socket)) {
+ socket->flags |= EVCOM_CONNECTED;
if (socket->on_connect) socket->on_connect(socket);
}
evcom_queue *q = evcom_queue_last(&socket->out_stream);
evcom_buf *to_write = evcom_queue_data(q, evcom_buf, queue);
- assert(socket->secure);
+ assert(SECURE(socket));
sent = gnutls_record_send(socket->session,
to_write->base + to_write->written,
size_t recv_buffer_size = socket->chunksize;
ssize_t recved;
- assert(socket->secure);
+ assert(SECURE(socket));
recved = gnutls_record_recv(socket->session, recv_buffer, recv_buffer_size);
/* Got EOF */
if (recved == 0) {
socket->read_action = NULL;
- if (socket->write_action == NULL) CLOSE_ASAP(socket);
+ if (socket->write_action == NULL) close_asap(socket);
}
if (socket->write_action) {
static int
secure_full_goodbye (evcom_socket *socket)
{
- assert(socket->secure);
+ assert(SECURE(socket));
int r = gnutls_bye(socket->session, GNUTLS_SHUT_RDWR);
if (r == GNUTLS_E_INTERRUPTED || r == GNUTLS_E_AGAIN) return AGAIN;
- CLOSE_ASAP(socket);
+ close_asap(socket);
return OKAY;
}
static int
secure_half_goodbye (evcom_socket *socket)
{
- assert(socket->secure);
+ assert(SECURE(socket));
int r = gnutls_bye(socket->session, GNUTLS_SHUT_WR);
evcom_socket_set_secure_session (evcom_socket *socket, gnutls_session_t session)
{
socket->session = session;
- socket->secure = TRUE;
+ socket->flags |= EVCOM_SECURE;
}
#endif /* HAVE GNUTLS */
{
ssize_t sent;
- assert(socket->secure == FALSE);
+ assert(!SECURE(socket));
if (evcom_queue_empty(&socket->out_stream)) {
ev_io_stop(SOCKET_LOOP_ &socket->write_watcher);
evcom_socket_reset_timeout(socket);
- if (!socket->connected) {
- socket->connected = TRUE;
+ if (!CONNECTED(socket)) {
+ socket->flags |= EVCOM_CONNECTED;
if (socket->on_connect) socket->on_connect(socket);
}
size_t buf_size = TCP_MAXWIN;
ssize_t recved;
- assert(socket->secure == FALSE);
+ assert(!SECURE(socket));
- if (!socket->connected) {
- socket->connected = TRUE;
+ if (!CONNECTED(socket)) {
+ socket->flags |= EVCOM_CONNECTED;
if (socket->on_connect) socket->on_connect(socket);
//return OKAY;
}
if (recved == 0) {
evcom_socket_read_stop(socket);
socket->read_action = NULL;
- if (socket->write_action == NULL) CLOSE_ASAP(socket);
+ if (socket->write_action == NULL) close_asap(socket);
}
/* NOTE: EOF is signaled with recved == 0 on callback */
socket->write_action = socket_send;
#if EVCOM_HAVE_GNUTLS
- if (socket->secure) {
+ if (SECURE(socket)) {
gnutls_transport_set_lowat(socket->session, 0);
gnutls_transport_set_push_function(socket->session, nosigpipe_push);
gnutls_transport_set_ptr2(socket->session,
static void
server_close_with_error (evcom_server *server, int errorno)
{
- if (server->listening) {
+ if (LISTENING(server)) {
evcom_server_detach(server);
close(server->fd); /* TODO do this on the loop? check return value? */
server->fd = -1;
- server->listening = FALSE;
+ server->flags &= ~EVCOM_LISTENING;
if (server->on_close) {
server->on_close(server, errorno);
int fd = accept(server->fd, (struct sockaddr*)&address, &addr_len);
if (fd < 0) {
-#ifdef EWOULDBLOCK
- if (errno == EWOULDBLOCK) return NULL;
-#else
- if (errno == EAGAIN) return NULL;
-#endif
- goto error;
+ switch (errno) {
+ case EMFILE:
+ dont_accept_new_connections(server);
+ return NULL;
+
+ case EAGAIN:
+ return NULL;
+
+ default:
+ goto error;
+ }
}
evcom_socket *socket = NULL;
{
evcom_server *server = watcher->data;
- assert(server->listening);
+ assert(LISTENING(server));
#if EV_MULTIPLICITY
assert(server->loop == loop);
#endif
evcom_server_listen (evcom_server *server, struct addrinfo *addrinfo, int backlog)
{
int fd = -1;
- assert(server->listening == FALSE);
+ assert(!LISTENING(server));
fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
addrinfo->ai_protocol);
}
server->fd = fd;
- server->listening = TRUE;
+ server->flags |= EVCOM_LISTENING;
ev_io_set (&server->connection_watcher, server->fd, EV_READ);
return 0;
#if EV_MULTIPLICITY
server->loop = loop;
#endif
- server->attached = TRUE;
+ server->flags |= EVCOM_ATTACHED;
}
void
#if EV_MULTIPLICITY
server->loop = NULL;
#endif
- server->attached = FALSE;
+ server->flags &= ~EVCOM_ATTACHED;
}
void
evcom_server_init (evcom_server *server)
{
- server->attached = FALSE;
- server->listening = FALSE;
+ server->flags = 0;
server->fd = -1;
server->connection_watcher.data = server;
ev_init (&server->connection_watcher, on_connection);
if (revents & EV_ERROR) {
socket->errorno = 1;
- CLOSE_ASAP(socket);
+ close_asap(socket);
}
int r;
if (r == AGAIN) {
have_read_event = FALSE;
} else {
- if (r == ERROR) CLOSE_ASAP(socket);
+ if (r == ERROR) close_asap(socket);
}
}
if (r == AGAIN) {
have_write_event = FALSE;
} else {
- if (r == ERROR) CLOSE_ASAP(socket);
+ if (r == ERROR) close_asap(socket);
}
}
}
evcom_socket_detach(socket);
assert(socket->fd == -1);
+ if (socket->server) {
+ accept_new_connections(socket->server);
+ }
+
if (socket->on_close) socket->on_close(socket);
/* WARNING: user can free socket in on_close so no more
* access beyond this point. */
#if EV_MULTIPLICITY
socket->loop = NULL;
#endif
- socket->attached = FALSE;
- socket->connected = FALSE;
+ socket->flags = 0;
evcom_queue_init(&socket->out_stream);
ev_init(&socket->read_watcher, on_io_event);
socket->read_watcher.data = socket;
- socket->got_full_close = FALSE;
- socket->got_half_close = FALSE;
-
socket->errorno = 0;
- socket->secure = FALSE;
#if EVCOM_HAVE_GNUTLS
socket->gnutls_errorno = 0;
socket->session = NULL;
void
evcom_socket_close (evcom_socket *socket)
{
- socket->got_half_close = TRUE;
+ socket->flags |= EVCOM_GOT_HALF_CLOSE;
if (evcom_queue_empty(&socket->out_stream)) {
change_state_for_empty_out_stream(socket);
}
void
evcom_socket_full_close (evcom_socket *socket)
{
- socket->got_full_close = TRUE;
+ socket->flags |= EVCOM_GOT_FULL_CLOSE;
if (evcom_queue_empty(&socket->out_stream)) {
change_state_for_empty_out_stream(socket);
}
socket->write_action = socket->read_action = NULL;
// socket->errorno = EVCOM_SOCKET_ERROR_FORCE_CLOSE
- if (socket->fd > 0) close(socket->fd);
+ if (socket->fd > 0) {
+ close(socket->fd);
+ if (socket->server) {
+ accept_new_connections(socket->server);
+ }
+ }
+
socket->fd = -1;
evcom_socket_detach(socket);
assert(0 && "Do not write to a closed socket");
goto error;
}
- if (socket->got_full_close == TRUE || socket->got_half_close == TRUE) {
+ if (GOT_FULL_CLOSE(socket) || GOT_HALF_CLOSE(socket)) {
assert(0 && "Do not write to a closing socket");
goto error;
}
evcom_queue_insert_head(&socket->out_stream, &buf->queue);
buf->written = 0;
- if (socket->attached) {
+ if (ATTACHED(socket)) {
ev_io_start(SOCKET_LOOP_ &socket->write_watcher);
}
return;
#if EV_MULTIPLICITY
socket->loop = loop;
#endif
- socket->attached = TRUE;
+ socket->flags |= EVCOM_ATTACHED;
ev_timer_again(EV_A_ &socket->timeout_watcher);
void
evcom_socket_detach (evcom_socket *socket)
{
- if (socket->attached) {
+ if (ATTACHED(socket)) {
ev_io_stop(SOCKET_LOOP_ &socket->write_watcher);
ev_io_stop(SOCKET_LOOP_ &socket->read_watcher);
ev_timer_stop(SOCKET_LOOP_ &socket->timeout_watcher);
#if EV_MULTIPLICITY
socket->loop = NULL;
#endif
- socket->attached = FALSE;
+ socket->flags &= ~EVCOM_ATTACHED;
}
}