Implement epoll main loop in Server, and parse header.
[cubemap] / server.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <curl/curl.h>
7 #include <sys/socket.h>
8 #include <pthread.h>
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
12 #include <errno.h>
13 #include <vector>
14 #include <string>
15 #include <map>
16
17 #include "metacube.h"
18 #include "server.h"
19 #include "mutexlock.h"
20
21 using namespace std;
22
23 Server::Server()
24 {
25         pthread_mutex_init(&mutex, NULL);
26
27         epoll_fd = epoll_create(1024);  // Size argument is ignored.
28         if (epoll_fd == -1) {
29                 perror("epoll_fd");
30                 exit(1);
31         }
32 }
33
34 void Server::run()
35 {
36         pthread_t thread;
37         pthread_create(&thread, NULL, Server::do_work_thunk, this);
38 }
39
40 void *Server::do_work_thunk(void *arg)
41 {
42         Server *server = static_cast<Server *>(arg);
43         server->do_work();
44         return NULL;
45 }
46
47 void Server::do_work()
48 {
49         for ( ;; ) {
50                 int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS);
51
52                 MutexLock lock(&mutex);  // We release the mutex between iterations.
53                 if (nfds == -1) {
54                         perror("epoll_wait");
55                         exit(1);
56                 }
57                 
58                 for (int i = 0; i < nfds; ++i) {
59                         int fd = events[i].data.fd;
60                         assert(clients.count(fd) != 0);
61                         Client *client = &clients[fd];
62
63                         if (events[i].events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
64                                 close_client(client);
65                                 continue;
66                         }
67
68                         process_client(client);
69                 }
70         }
71 }
72         
73 void Server::add_client(int sock)
74 {
75         MutexLock lock(&mutex);
76         Client new_client;
77         new_client.sock = sock;
78         new_client.client_request.reserve(1024);
79         new_client.state = Client::READING_REQUEST;
80         new_client.header_bytes_sent = 0;
81         new_client.bytes_sent = 0;
82
83         clients.insert(make_pair(sock, new_client));
84
85         // Start listening on data from this socket.
86         epoll_event ev;
87         ev.events = EPOLLIN | EPOLLRDHUP;
88         ev.data.fd = sock;
89         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
90                 perror("epoll_ctl(EPOLL_CTL_ADD)");
91                 exit(1);
92         }
93 }
94         
95 void Server::add_stream(const string &stream_id)
96 {
97         // TODO
98 }
99         
100 void Server::set_header(const string &stream_id, const string &header)
101 {
102         // TODO
103         printf("got header! %lu bytes\n", header.size());
104 }
105         
106 void Server::add_data(const string &stream_id, const char *data, size_t bytes)
107 {
108         // TODO
109 }
110         
111 void Server::process_client(Client *client)
112 {
113         switch (client->state) {
114         case Client::READING_REQUEST: {
115                 // Try to read more of the request.
116                 char buf[1024];
117                 int ret = read(client->sock, buf, sizeof(buf));
118                 if (ret == -1) {
119                         perror("read");
120                         close_client(client);
121                         return;
122                 }
123                 if (ret == 0) {
124                         // No data? This really means that we were triggered for something else than
125                         // POLLIN (which suggests a logic error in epoll).
126                         fprintf(stderr, "WARNING: fd %d returned unexpectedly 0 bytes!\n", client->sock);
127                         close_client(client);
128                         return;
129                 }
130
131                 // Guard against overlong requests gobbling up all of our space.
132                 if (client->client_request.size() + ret > MAX_CLIENT_REQUEST) {
133                         fprintf(stderr, "WARNING: fd %d sent overlong request!\n", client->sock);
134                         close_client(client);
135                         return;
136                 }       
137
138                 // See if we have \r\n\r\n anywhere in the request. We start three bytes
139                 // before what we just appended, in case we just got the final character.
140                 size_t existing_req_bytes = client->client_request.size();
141                 client->client_request.append(string(buf, buf + ret));
142         
143                 size_t start_at = (existing_req_bytes >= 3 ? existing_req_bytes - 3 : 0);
144                 const char *ptr = reinterpret_cast<char *>(
145                         memmem(client->client_request.data() + start_at, client->client_request.size() - start_at,
146                                "\r\n\r\n", 4));
147                 if (ptr == NULL) {
148                         // OK, we don't have the entire header yet. Fine; we'll get it later.
149                         return;
150                 }
151
152                 if (ptr != client->client_request.data() + client->client_request.size() - 4) {
153                         fprintf(stderr, "WARNING: fd %d had junk data after request!\n", client->sock);
154                         close_client(client);
155                         return;
156                 }
157
158                 client->state = Client::SENDING_HEADER;
159
160                 epoll_event ev;
161                 ev.events = EPOLLOUT | EPOLLRDHUP;
162                 ev.data.fd = client->sock;
163
164                 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
165                         perror("epoll_ctl(EPOLL_CTL_MOD)");
166                         exit(1);
167                 }
168                 break;
169         }
170         default:
171                 // TODO
172                 assert(false);
173         }
174 }
175         
176 void Server::close_client(Client *client)
177 {
178         if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
179                 perror("epoll_ctl(EPOLL_CTL_DEL)");
180                 exit(1);
181         }
182         
183         // Bye-bye!
184         close(client->sock);
185         clients.erase(client->sock);
186 }