~donmcc/ood

ref: 8ddb93d0ae62f4bb7ceed15404e24e7501336345 ood/src/dood/server.c -rw-r--r-- 3.7 KiB
8ddb93d0Don McCaughey Rename `server_add_connection()` function. 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "server.h"

#include <stdlib.h>
#include <stdio.h>


void
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);
    }
}


enum ood_result
server_add_listener(struct server *server,
                    in_addr_t in_addr,
                    in_port_t in_port,
                    int backlog,
                    ood_inet_socket_fn *on_read,
                    ood_inet_socket_fn *on_error)
{
    struct ood_inet_socket *listener = ood_inet_socket_alloc(in_addr, in_port);
    if (!listener) {
        return ood_error;
    }
    listener->data = server;
    listener->on_error = on_error;
    listener->on_read = on_read;
    ood_ptr_array_add(server->sockets, listener);

    if (!ood_inet_socket_listen(listener, backlog)) {
        return ood_error;
    }

    printf("dood: listening on %s\n", listener->address->formatted);
    return ood_okay;
}


void
server_add_sockets_to_select_sets(struct server *server,
                                  struct ood_select_sets *select_sets)
{
    ood_select_sets_clear(select_sets);

    int i = 0;
    struct ood_inet_socket *socket;
    while ((socket = ood_ptr_array_next(server->sockets, &i))) {
        if (socket->on_error) ood_select_sets_add_errorable(select_sets, socket->fd);
        if (socket->on_read) ood_select_sets_add_reader(select_sets, socket->fd);
        if (socket->on_write) ood_select_sets_add_writer(select_sets, socket->fd);
    }
}


struct server *
server_alloc(int socket_capacity)
{
    struct server *server = calloc(1, sizeof(struct server));
    if (!server) return NULL;

    server->sockets = ood_ptr_array_alloc(socket_capacity);
    if (!server->sockets) {
        free(server);
        return NULL;
    }

    return server;
}


bool
server_can_accept_connections(struct server *server)
{
    return ood_ptr_array_has_capacity(server->sockets);
}


void
server_close_all_listeners(struct server *server)
{
    for (int i = 0; i < server->sockets->capacity; ++i) {
        if (!server->sockets->items[i]) continue;
        struct ood_inet_socket *socket = (struct ood_inet_socket *)server->sockets->items[i];
        if (OOD_INET_SOCKET_FLAGS_LISTENING & socket->flags) {
            printf("dood: no longer listening on %s\n", socket->address->formatted);
            ood_inet_socket_free(socket);
            server->sockets->items = NULL;
            --server->sockets->count;
        }
    }
}


void
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(socket);
}


void
server_free(struct server *server)
{
    if (server) {
        ood_ptr_array_free(server->sockets);
        free(server);
    }
}


void
server_handle_events_in_select_sets(struct server *server,
                                    struct ood_select_sets *select_sets)
{
    int i = 0;
    struct ood_inet_socket *socket;
    while ((socket = ood_ptr_array_next(server->sockets, &i))) {
        if (socket->on_error && ood_select_sets_has_error(select_sets, socket->fd)) {
            socket->on_error(socket);
            continue;
        }

        if (socket->on_read && ood_select_sets_is_readable(select_sets, socket->fd)) {
            socket->on_read(socket);
        }

        if (socket->on_write && ood_select_sets_is_writable(select_sets, socket->fd)) {
            socket->on_write(socket);
        }
    }
}


bool
server_is_running(struct server *server)
{
    return server->sockets->count > 0;
}