aboutsummaryrefslogtreecommitdiff
path: root/server.c
blob: 18ce0f98a90b9f453e65a50a5f1ee2c58d4b4cec (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "chatty.h"

#include <assert.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdarg.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

// timeout on polling
#define TIMEOUT 60 * 1000
// max pending connections
#define MAX_CONNECTIONS 16
// Get number of connections from arena position
// NOTE: this is somewhat wrong, because of when disconnections happen
#define FDS_SIZE (fdsArena->pos / sizeof(*fds))

// enum for indexing the fds array
enum { FDS_STDIN = 0,
       FDS_SERVER,
       FDS_CLIENTS };

// Has information on clients
// For each pollfd in fds there should be a matching client in clients
// clients[i - FDS_CLIENTS] <=> fds[i]
typedef struct {
    u8 author[AUTHOR_LEN]; // matches author property on other message types
    Bool initialized;      // boolean
} Client;

int
main(void)
{
    s32 err, serverfd, clientfd;
    u32 on = 1;

    // Start listening on the socket
    {
        serverfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
        assert(serverfd > 2);

        err = setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, (u8*)&on, sizeof(on));
        assert(err == 0);

        const struct sockaddr_in address = {
            AF_INET,
            htons(PORT),
            {0},
        };

        err = bind(serverfd, (const struct sockaddr*)&address, sizeof(address));
        assert(err == 0);

        err = listen(serverfd, MAX_CONNECTIONS);
        assert(err == 0);
    }

    Arena* msgsArena = ArenaAlloc(Megabytes(128)); // storing received messages
                                                   // NOTE: sent messages?
    s32 nrecv = 0;                                 // number of bytes received
    s32 nsend = 0;                                 // number of bytes sent

    Arena* clientsArena = ArenaAlloc(MAX_CONNECTIONS * sizeof(Client));
    Arena* fdsArena = ArenaAlloc(MAX_CONNECTIONS * sizeof(struct pollfd));
    struct pollfd* fds = fdsArena->addr;
    Client* clients = clientsArena->addr;

    struct pollfd* fdsAddr;
    struct pollfd newpollfd = {-1, POLLIN, 0};

    // initialize fds structure
    newpollfd.fd = 0;
    fdsAddr = ArenaPush(fdsArena, sizeof(*fds));
    memcpy(fdsAddr, &newpollfd, sizeof(*fds));
    // add serverfd
    newpollfd.fd = serverfd;
    fdsAddr = ArenaPush(fdsArena, sizeof(*fds));
    memcpy(fdsAddr, &newpollfd, sizeof(*fds));
    newpollfd.fd = -1;

    // Initialize the rest of the fds array
    for (u32 i = FDS_CLIENTS; i < MAX_CONNECTIONS; i++)
        fds[i] = newpollfd;

    while (1) {
        err = poll(fds, FDS_SIZE, TIMEOUT);
        assert(err != -1);

        if (fds[FDS_STDIN].revents & POLLIN) {
            // helps for testing and exiting gracefully
            break;
        } else if (fds[FDS_SERVER].revents & POLLIN) {
            clientfd = accept(serverfd, NULL, NULL);
            assert(clientfd != -1);
            assert(clientfd > serverfd);
            fprintf(stdout, "New connection(%d).\n", clientfd);

            // fill up a hole
            u8 found;
            for (found = FDS_CLIENTS; found < FDS_SIZE; found++)
                if (fds[found].fd == -1)
                    break;
            if (found == FDS_SIZE) {
                // no more space, allocate
                struct pollfd* pollfd = ArenaPush(fdsArena, sizeof(*pollfd));
                pollfd->fd = clientfd;
                pollfd->events = POLLIN;
            } else if (found == MAX_CONNECTIONS) {
                // TODO: reject connection
                close(clientfd);
                fprintf(stdout, "Max clients reached.");
            } else {
                // hole found
                fds[found].fd = clientfd;
                fds[found].events = POLLIN;
                fprintf(stdout, "Added pollfd(%d).\n", clientfd);
            }
        }

        // Check for messages from clients
        for (u32 i = FDS_CLIENTS; i < (FDS_SIZE); i++) {
            if (!(fds[i].revents & POLLIN))
                continue;
            assert(fds[i].fd != -1);

            fprintf(stdout, "Message(%d).\n", fds[i].fd);
            // If this is the first message from the client it must be a presence message indicated
            // it connected.
            Client* client = clients + i - FDS_CLIENTS;
            if (!client->initialized) {
                fprintf(stdout, " Adding to clients(%d).\n", fds[i].fd);
                // Wait for PresenceMessage from new client to get author information
                HeaderMessage header;
                // TODO: handle wrong message, disconnection, etc.
                nrecv = recv(clientfd, &header, sizeof(header), 0);
                assert(nrecv != -1);
                assert(nrecv == sizeof(header));
                if (header.type != HEADER_TYPE_PRESENCE) {
                    // TODO: reject connection
                    close(clientfd);
                    continue;
                }
                fprintf(stdout, "  Got header(%d).\n", fds[i].fd);

                PresenceMessage message;
                // TODO: handle wrong message
                nrecv = recv(clientfd, &message, sizeof(message), 0);
                assert(nrecv != -1);
                assert(nrecv == sizeof(message));
                fprintf(stdout, "  Got presence message(%d).\n", fds[i].fd);

                memcpy(client->author, message.author, AUTHOR_LEN);
                client->initialized = True;

                fprintf(stdout, "  Added to clients(%d): %s\n", fds[i].fd, client->author);

                // Notify other clients from this new one
                // Reuse header and message
                for (u32 j = FDS_CLIENTS; j < (FDS_SIZE); j++) {
                    if (fds[j].fd == fds[i].fd)
                        continue;
                    if (fds[j].fd == -1)
                        continue;
                    fprintf(stdout, "  Notifying (%d)\n", fds[j].fd);

                    nsend = send(fds[j].fd, &header, sizeof(header), 0);
                    assert(nsend != -1);
                    assert(nsend == sizeof(header));
                    nsend = send(fds[j].fd, &message, sizeof(message), 0);
                    assert(nsend != -1);
                    assert(nsend == sizeof(message));
                }
                continue;
            }

            // We received a message, try to parse the header
            HeaderMessage header;
            nrecv = recv(fds[i].fd, &header, sizeof(header), 0);
            assert(nrecv != -1);

            if (nrecv == 0) {
                fprintf(stdout, "Disconnected(%d). \n", fds[i].fd);
                shutdown(fds[i].fd, SHUT_RDWR);
                close(fds[i].fd);                             // send close to client
                fds[i].fd = -1;                               // ignore in the future
                clients[i - FDS_CLIENTS].initialized = False; // deinitialize client
                                                              //
                // Send disconnection to other connected clients
                HeaderMessage header = HEADER_PRESENCEMESSAGE;
                PresenceMessage message = {
                    .type = PRESENCE_TYPE_DISCONNECTED
                };
                memcpy(message.author, clients[i - FDS_CLIENTS].author, AUTHOR_LEN);
                for (u32 j = FDS_CLIENTS; j < FDS_SIZE; j++) {
                    if (fds[j].fd == fds[i].fd)
                        continue;
                    if (fds[j].fd == -1)
                        continue;
                    nsend = send(fds[j].fd, &header, sizeof(header), 0);
                    assert(nsend != -1);
                    assert(nsend == sizeof(header));
                    nsend = send(fds[j].fd, &message, sizeof(message), 0);
                    assert(nsend != -1);
                    assert(nsend == sizeof(message));
                }

                continue;
            }

            assert(nrecv == sizeof(header));
            fprintf(stderr, " Received(%d): %d bytes -> " PH_FMT "\n", fds[i].fd, nrecv, PH_ARG(header));

            switch (header.type) {
            case HEADER_TYPE_TEXT:;
                TextMessage* message;
                nrecv = recvTextMessage(msgsArena, fds[i].fd, &message);
                fprintf(stderr, " Received(%d): %d bytes -> ", fds[i].fd, nrecv);
                printTextMessage(message, 0);

                HeaderMessage header = HEADER_TEXTMESSAGE;
                // Send message to all other clients
                for (u32 j = FDS_CLIENTS; j < FDS_SIZE; j++) {
                    if (fds[j].fd == fds[i].fd) continue;
                    if (fds[j].fd == -1) continue;

                    // NOTE: I wonder if this is more expensive than constructing a buffer and sending
                    // that
                    u32 nsend_total = 0;
                    nsend = send(fds[j].fd, &header, sizeof(header), 0);
                    assert(nsend != 1);
                    assert(nsend == sizeof(header));
                    nsend_total += nsend;
                    nsend = send(fds[j].fd, message, TEXTMESSAGE_SIZE, 0);
                    assert(nsend != -1);
                    assert(nsend == TEXTMESSAGE_SIZE);
                    nsend_total += nsend;
                    nsend = send(fds[j].fd, &message->text, message->len * sizeof(*message->text), 0);
                    assert(nsend != -1);
                    assert(nsend == (message->len * sizeof(*message->text)));
                    nsend_total += nsend;

                    fprintf(stdout, "  Retransmitted(%d->%d) %d bytes.\n", fds[i].fd, fds[j].fd, nsend_total);
                }
                break;
            default:
                fprintf(stdout, " Got unhandled message type '%s' from client %d", headerTypeString(header.type), fds[i].fd);
                continue;
            }
        }
    }

    ArenaRelease(clientsArena);
    ArenaRelease(fdsArena);
    ArenaRelease(msgsArena);

    return 0;
}