7 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
19 #define STREAM_ID "stream"
20 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
21 #define BACKLOG_SIZE 1048576
26 // Locks a pthread mutex, RAII-style.
29 MutexLock(pthread_mutex_t *mutex);
33 pthread_mutex_t *mutex;
36 MutexLock::MutexLock(pthread_mutex_t *mutex)
39 pthread_mutex_lock(mutex);
42 MutexLock::~MutexLock()
44 pthread_mutex_unlock(mutex);
48 enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
51 // The HTTP request, as sent by the client. If we are in READING_REQUEST,
52 // this might not be finished.
53 string client_request;
56 // What stream we're connecting to; parsed from client_request.
57 // Not relevant for READING_REQUEST.
61 // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
62 size_t header_bytes_sent;
64 // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
69 // The HTTP response header, plus the video stream header (if any).
72 // The stream data itself, stored in a circular buffer.
73 char data[BACKLOG_SIZE];
75 // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
76 // since the buffer wraps.
84 // Start a new thread that handles clients.
86 void add_client(int sock);
87 void add_stream(const string &stream_id);
88 void set_header(const string &stream_id, const string &header);
89 void add_data(const string &stream_id, const char *data, size_t bytes);
92 pthread_mutex_t mutex;
94 // Map from stream ID to stream.
95 map<string, Stream> streams;
97 // Map from file descriptor to client.
98 map<int, Client> clients;
102 // Recover the this pointer, and call do_work().
103 static void *do_work_thunk(void *arg);
105 // The actual worker thread.
109 Server *servers = NULL;
113 pthread_mutex_init(&mutex, NULL);
115 epoll_fd = epoll_create(1024); // Size argument is ignored.
116 if (epoll_fd == -1) {
125 pthread_create(&thread, NULL, Server::do_work_thunk, this);
128 void *Server::do_work_thunk(void *arg)
130 Server *server = static_cast<Server *>(arg);
135 void Server::do_work()
138 MutexLock lock(&mutex);
139 printf("server thread running\n");
144 void Server::add_client(int sock)
146 MutexLock lock(&mutex);
148 new_client.state = Client::READING_REQUEST;
149 new_client.header_bytes_sent = 0;
150 new_client.bytes_sent = 0;
152 clients.insert(make_pair(sock, new_client));
154 // Start listening on data from this socket.
158 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
159 perror("epoll_ctl(EPOLL_CTL_ADD)");
164 void Server::add_stream(const string &stream_id)
169 void Server::set_header(const string &stream_id, const string &header)
172 printf("got header! %lu bytes\n", header.size());
175 void Server::add_data(const string &stream_id, const char *data, size_t bytes)
182 Input(const string &stream_id);
184 // Connect to the given URL and start streaming.
185 // WARNING: Currently this blocks; it does not run in a separate thread!
186 void run(const string &url);
189 // Recovers the this pointer and calls curl_callback().
190 static size_t curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata);
192 // Stores the given data, looks for Metacube blocks (skipping data if needed),
193 // and calls process_block() for each one.
194 void curl_callback(char *ptr, size_t bytes);
195 void process_block(const char *data, uint32_t size, uint32_t flags);
197 // Drops <num_bytes> bytes from the head of <pending_data>,
198 // and outputs a warning.
199 void drop_pending_data(size_t num_bytes);
203 // Data we have received but not fully processed yet.
204 vector<char> pending_data;
206 // If <pending_data> starts with a Metacube header,
208 bool has_metacube_header;
211 Input::Input(const string &stream_id)
212 : stream_id(stream_id),
213 has_metacube_header(false)
217 void Input::run(const string &url)
219 CURL *curl = curl_easy_init();
220 curl_easy_setopt(curl, CURLOPT_URL, STREAM_URL);
221 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
222 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
223 curl_easy_perform(curl);
226 size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
228 Input *input = static_cast<Input *>(userdata);
229 size_t bytes = size * nmemb;
230 input->curl_callback(ptr, bytes);
234 void Input::curl_callback(char *ptr, size_t bytes)
236 pending_data.insert(pending_data.end(), ptr, ptr + bytes);
239 // If we don't have enough data (yet) for even the Metacube header, just return.
240 if (pending_data.size() < sizeof(metacube_block_header)) {
244 // Make sure we have the Metacube sync header at the start.
245 // We may need to skip over junk data (it _should_ not happen, though).
246 if (!has_metacube_header) {
247 char *ptr = static_cast<char *>(
248 memmem(pending_data.data(), pending_data.size(),
249 METACUBE_SYNC, strlen(METACUBE_SYNC)));
251 // OK, so we didn't find the sync marker. We know then that
252 // we do not have the _full_ marker in the buffer, but we
253 // could have N-1 bytes. Drop everything before that,
255 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
258 // Yay, we found the header. Drop everything (if anything) before it.
259 drop_pending_data(ptr - pending_data.data());
260 has_metacube_header = true;
262 // Re-check that we have the entire header; we could have dropped data.
263 if (pending_data.size() < sizeof(metacube_block_header)) {
269 // Now it's safe to read the header.
270 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());
271 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
272 uint32_t size = ntohl(hdr->size);
273 uint32_t flags = ntohl(hdr->flags);
275 // See if we have the entire block. If not, wait for more data.
276 if (pending_data.size() < sizeof(metacube_block_header) + size) {
280 process_block(pending_data.data(), size, flags);
282 // Consume this block. This isn't the most efficient way of dealing with things
283 // should we have many blocks, but these routines don't need to be too efficient
285 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
289 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
291 if (flags & METACUBE_FLAGS_HEADER) {
292 string header(data, data + size);
293 for (int i = 0; i < NUM_SERVERS; ++i) {
294 servers[i].set_header(stream_id, header);
297 for (int i = 0; i < NUM_SERVERS; ++i) {
298 servers[i].add_data(stream_id, data, size);
303 void Input::drop_pending_data(size_t num_bytes)
305 if (num_bytes == 0) {
308 fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
309 (long long)num_bytes);
310 pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
313 int create_server_socket(int port)
315 int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
316 if (server_sock == -1) {
321 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
323 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
324 perror("setsockopt(IPV6_V6ONLY)");
329 memset(&addr, 0, sizeof(addr));
330 addr.sin6_family = AF_INET6;
331 addr.sin6_port = htons(port);
333 if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
338 if (listen(server_sock, 128) == -1) {
346 void *acceptor_thread_run(void *arg)
348 int server_sock = int(intptr_t(arg));
349 int num_accepted = 0;
352 socklen_t addrlen = sizeof(addr);
355 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
356 if (sock == -1 && errno == EINTR) {
364 // Set the socket as nonblocking.
366 if (ioctl(sock, FIONBIO, &one) == -1) {
371 // Pick a server, round-robin, and hand over the socket to it.
372 servers[num_accepted % NUM_SERVERS].add_client(sock);
377 int main(int argc, char **argv)
379 servers = new Server[NUM_SERVERS];
380 for (int i = 0; i < NUM_SERVERS; ++i) {
381 servers[i].add_stream(STREAM_ID);
385 int server_sock = create_server_socket(PORT);
387 pthread_t acceptor_thread;
388 pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
390 Input input(STREAM_ID);
391 input.run(STREAM_URL);