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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#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[i]
typedef struct {
u8 author[AUTHOR_LEN]; // matches author property on other message types
Bool initialized; // boolean
} Client;
// Send anyMessage to all clients in fds from fdsArena except for fds[i].
void
sendToOthers(Arena* fdsArena, struct pollfd* fds, u32 i, HeaderMessage* header, void* anyMessage)
{
s32 nsend;
for (u32 j = FDS_CLIENTS; j < FDS_SIZE; j++) {
if (fds[j].fd == fds[i].fd) continue;
if (fds[j].fd == -1) continue;
// send header
u32 nsend_total = 0;
nsend = send(fds[j].fd, header, sizeof(*header), 0);
assert(nsend != -1);
assert(nsend == sizeof(*header));
nsend_total += nsend;
// send message
switch (header->type) {
case HEADER_TYPE_PRESENCE: {
PresenceMessage* message = (PresenceMessage*)anyMessage;
nsend = send(fds[j].fd, message, sizeof(*message), 0);
assert(nsend != -1);
assert(nsend == sizeof(*message));
fprintf(stdout, " Notifying(%d->%d).\n", fds[i].fd, fds[j].fd);
break;
}
case HEADER_TYPE_TEXT: {
TextMessage* message = (TextMessage*)anyMessage;
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;
break;
}
default:
fprintf(stdout, " Cannot retransmit %s\n", headerTypeString(header->type));
}
fprintf(stdout, " Retransmitted(%d->%d) %d bytes.\n", fds[i].fd, fds[j].fd, nsend_total);
}
}
void
disconnect(Arena* fdsArena, struct pollfd* fds, u32 i, Client* client)
{
fprintf(stdout, "Disconnected(%d). \n", fds[i].fd);
shutdown(fds[i].fd, SHUT_RDWR);
close(fds[i].fd); // send close to client
// Send disconnection to other connected clients
HeaderMessage header = HEADER_PRESENCEMESSAGE;
PresenceMessage message = {
.type = PRESENCE_TYPE_DISCONNECTED
};
memcpy(message.author, client->author, AUTHOR_LEN);
sendToOthers(fdsArena, fds, i, &header, &message);
fds[i].fd = -1; // ignore in the future
client->initialized = False; // deinitialize client
}
// Initialize a client that connects for the first time or reconnects.
// Receive HeaderMessage and PresenceMessage from fd and set client with the data from
// PresenceMessage.
// Notify fds in fdsArena.
// TODO: handle wrong messages
void
initClient(Arena* fdsArena, struct pollfd* fds, s32 fd, Client* client)
{
s32 nrecv = 0;
s32 nsend = 0;
fprintf(stdout, " Adding to clients(%d).\n", fd);
HeaderMessage header;
nrecv = recv(fd, &header, sizeof(header), 0);
assert(nrecv != -1);
assert(nrecv == sizeof(header));
if (header.type != HEADER_TYPE_PRESENCE) {
// reject connection
close(fd);
fprintf(stdout, " Got wrong header(%d).\n", fd);
return;
}
fprintf(stdout, " Got header(%d).\n", fd);
PresenceMessage message;
nrecv = recv(fd, &message, sizeof(message), 0);
assert(nrecv != -1);
assert(nrecv == sizeof(message));
fprintf(stdout, " Got presence message(%d).\n", fd);
// Copy author from PresenceMessage.
memcpy(client->author, message.author, AUTHOR_LEN);
// Notify other clients from this new one
// Reuse header and message
for (u32 j = FDS_CLIENTS; j < FDS_SIZE; j++) {
if (fds[j].fd == fd)
continue;
if (fds[j].fd == -1)
continue;
fprintf(stdout, " Notifying(%d->%d).\n", fd, 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));
}
}
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
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);
// If there is a slot in fds with fds[found].fd == -1 use it instead, otherwise allocate
// some space on the arena.
u8 found;
for (found = FDS_CLIENTS; found < FDS_SIZE; found++)
if (fds[found].fd == -1)
break;
if (found == MAX_CONNECTIONS) {
// TODO: reject connection
close(clientfd);
fprintf(stdout, "Max clients reached.");
} else if (found == FDS_SIZE) {
// no more space, allocate
struct pollfd* pollfd = ArenaPush(fdsArena, sizeof(*pollfd));
pollfd->fd = clientfd;
pollfd->events = POLLIN;
} 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);
Client* client = clients + i;
// Initialize the client if this is the first time
if (!client->initialized) {
initClient(fdsArena, fds, fds[i].fd, client);
client->initialized = True;
fprintf(stdout, " Added to clients(%d): %s\n", fds[i].fd, client->author);
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) {
disconnect(fdsArena, fds, i, (clients + i));
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);
// Send message to all other clients
sendToOthers(fdsArena, fds, i, &header, message);
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;
}
|