~donmcc/ood

8ddb93d0ae62f4bb7ceed15404e24e7501336345 — Don McCaughey 2 months ago ae0becb
Rename `server_add_connection()` function.

Change the `server_add_connection()` and `server_close_connection()`
functions to `server_add_socket()` and `server_close_socket()` since
these are used for both listeners and connnection sockets.

Also, in `ood_inet_socket_accept()`, make sure to close the file
descriptor if `alloc_connection()` fails.
4 files changed, 22 insertions(+), 17 deletions(-)

M src/dood/main.c
M src/dood/server.c
M src/dood/server.h
M src/libood/ood/inet/socket.c
M src/dood/main.c => src/dood/main.c +7 -7
@@ 34,7 34,7 @@ connection_on_error(struct ood_inet_socket *connection)
    struct server *server = connection->data;

    fprintf(stderr, "dood: error on connection to %s\n", connection->address->formatted);
    server_close_connection(server, connection);
    server_close_socket(server, connection);
}




@@ 50,7 50,7 @@ connection_on_read(struct ood_inet_socket *connection)
    if (bytes_read < 0) {
        if (EINTR != errno && EWOULDBLOCK != errno) {
            perror("dood");
            server_close_connection(server, connection);
            server_close_socket(server, connection);
        }
    } else {
        // TODO: parse request and handle zero byte read (i.e. EOF)


@@ 61,7 61,7 @@ connection_on_read(struct ood_inet_socket *connection)
        connection->on_write = connection_on_write;
        if (!ood_inet_socket_stop_reading(connection)) {
            perror("dood");
            server_close_connection(server, connection);
            server_close_socket(server, connection);
        }
    }
}


@@ 85,7 85,7 @@ connection_on_write(struct ood_inet_socket *connection)
    if (bytes_written < 0) {
        if (EINTR != errno && EWOULDBLOCK != errno) {
            perror("dood");
            server_close_connection(server, connection);
            server_close_socket(server, connection);
        }
    } else {
        printf("dood: wrote %zi bytes\n", bytes_written);


@@ 94,7 94,7 @@ connection_on_write(struct ood_inet_socket *connection)
            // TODO: handle partial writes
        }
        printf("dood: closing client connection\n");
        server_close_connection(server, connection);
        server_close_socket(server, connection);
    }
}



@@ 107,7 107,7 @@ listener_on_error(struct ood_inet_socket *listener)
    struct server *server = listener->data;

    fprintf(stderr, "dood: error listening on %s\n", listener->address->formatted);
    server_close_connection(server, listener);
    server_close_socket(server, listener);
}




@@ 131,7 131,7 @@ listener_on_read(struct ood_inet_socket *listener)
        connection->on_write = NULL;
        connection->data = server;

        server_add_connection(server, connection);
        server_add_socket(server, connection);
        printf("dood: accepted connection from %s\n", connection->address->formatted);
    }
}

M src/dood/server.c => src/dood/server.c +7 -7
@@ 5,9 5,9 @@


void
server_add_connection(struct server *server, struct ood_inet_socket *connection) {
    if (!ood_ptr_array_add(server->sockets, connection)) {
        fprintf(stderr, "dood: unable to add connection\n");
server_add_socket(struct server *server, struct ood_inet_socket *socket) {
    if (!ood_ptr_array_add(server->sockets, socket)) {
        fprintf(stderr, "dood: unable to add socket\n");
        exit(EXIT_FAILURE);
    }
}


@@ 95,12 95,12 @@ server_close_all_listeners(struct server *server)


void
server_close_connection(struct server *server, struct ood_inet_socket *connection) {
    if (!ood_ptr_array_clear(server->sockets, connection)) {
        fprintf(stderr, "dood: unable to close connection\n");
server_close_socket(struct server *server, struct ood_inet_socket *socket) {
    if (!ood_ptr_array_clear(server->sockets, socket)) {
        fprintf(stderr, "dood: unable to close socket\n");
        exit(EXIT_FAILURE);
    }
    ood_inet_socket_free(connection);
    ood_inet_socket_free(socket);
}



M src/dood/server.h => src/dood/server.h +2 -2
@@ 29,10 29,10 @@ void
server_close_all_listeners(struct server *server);

void
server_add_connection(struct server *server, struct ood_inet_socket *connection);
server_add_socket(struct server *server, struct ood_inet_socket *socket);

void
server_close_connection(struct server *server, struct ood_inet_socket *connection);
server_close_socket(struct server *server, struct ood_inet_socket *socket);

void
server_add_sockets_to_select_sets(struct server *server,

M src/libood/ood/inet/socket.c => src/libood/ood/inet/socket.c +6 -1
@@ 47,7 47,12 @@ ood_inet_socket_accept(struct ood_inet_socket *listener, struct ood_inet_socket 
    if (fd >= 0) {
        assert(AF_INET == client_address.sin_family);
        *connection_out = alloc_connection(fd, &client_address);
        return *connection_out ? ood_okay : ood_error;
        if (*connection_out) {
            return ood_okay;
        } else {
            close(fd);
            return ood_error;
        }
    }
    if (EINTR == errno || EWOULDBLOCK == errno) {
        *connection_out = NULL;