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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/epoll.h>
#include "page.cpp"
#include "point.cpp"
#include "buffer.cpp"
#include "client.cpp"
#define PORT 6969
#define MAX_COMMAND_SIZE 128
#define MAX_EVENTS 10
int create_listener() {
int s = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addr = { AF_INET, htons(PORT), htonl(INADDR_LOOPBACK)};
int opt = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
bind(s, (sockaddr *) &addr, sizeof(sockaddr_in));
listen(s, MAX_EVENTS);
return s;
}
void parse_command(char *command, Client *client) {
char *token = strtok(command, " \n");
while (token) {
if (strcmp(token, "show") == 0) {
client->show();
} else if (strcmp(token, "move") == 0) {
client->move();
} else if (strcmp(token, "push") == 0) {
client->push();
} else if (strcmp(token, "pop") == 0) {
client->pop();
} else {
client->args.push(atoi(token));
}
token = strtok(0, " \n");
}
}
int main() {
Buffer scratch("scratch");
scratch.read("LICENSE");
int listener = create_listener();
int epollfd = epoll_create1(0);
epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = listener;
epoll_ctl(epollfd, EPOLL_CTL_ADD, listener, &ev);
epoll_event events[MAX_EVENTS];
Client *clients[1024] = {};
for (;;) {
int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == listener) {
int clientfd = accept(listener, 0, 0);
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = clientfd;
if (clients[clientfd]) {
delete clients[clientfd];
}
Client *c = new Client(scratch);
c->sockfd = clientfd;
clients[clientfd] = c;
epoll_ctl(epollfd, EPOLL_CTL_ADD, clientfd, &ev);
} else {
char command[MAX_COMMAND_SIZE] = {};
int clientfd = events[i].data.fd;
read(clientfd, command, MAX_COMMAND_SIZE - 1);
parse_command(command, clients[clientfd]);
}
}
}
close(listener);
}
|