-int create_server_socket(int port)
-{
- int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
- if (server_sock == -1) {
- perror("socket");
- exit(1);
- }
-
- int one = 1;
- if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
- perror("setsockopt(SO_REUSEADDR)");
- exit(1);
- }
-
- // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
- int zero = 0;
- if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
- perror("setsockopt(IPV6_V6ONLY)");
- exit(1);
- }
-
- // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
- if (ioctl(server_sock, FIONBIO, &one) == -1) {
- perror("ioctl(FIONBIO)");
- exit(1);
- }
-
- sockaddr_in6 addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin6_family = AF_INET6;
- addr.sin6_port = htons(port);
-
- if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
- perror("bind");
- exit(1);
- }
-
- if (listen(server_sock, 128) == -1) {
- perror("listen");
- exit(1);
- }
-
- return server_sock;
-}
-
-void *acceptor_thread_run(void *arg)
-{
- int server_sock = int(intptr_t(arg));
- while (!hupped) {
- // Since we are non-blocking, we need to wait for the right state first.
- // Wait up to 50 ms, then check hupped.
- pollfd pfd;
- pfd.fd = server_sock;
- pfd.events = POLLIN;
-
- int nfds = poll(&pfd, 1, 50);
- if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
- continue;
- }
- if (nfds == -1) {
- perror("poll");
- usleep(100000);
- continue;
- }
-
- sockaddr_in6 addr;
- socklen_t addrlen = sizeof(addr);
-
- // Get a new socket.
- int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
- if (sock == -1 && errno == EINTR) {
- continue;
- }
- if (sock == -1) {
- perror("accept");
- usleep(100000);
- continue;
- }
-
- // Set the socket as nonblocking.
- int one = 1;
- if (ioctl(sock, FIONBIO, &one) == -1) {
- perror("FIONBIO");
- exit(1);
- }
-
- // Pick a server, round-robin, and hand over the socket to it.
- servers->add_client(sock);
- }
- return NULL;
-}
-
-struct StatsThreadParameters {
- string stats_file;
- int stats_interval;
-};
-
-void *stats_thread_run(void *arg)
-{
- const StatsThreadParameters *parms = reinterpret_cast<StatsThreadParameters *>(arg);
- while (!hupped) {
- int fd;
- FILE *fp;
- time_t now;
- vector<ClientStats> client_stats;
-
- // Open a new, temporary file.
- char *filename = strdup((parms->stats_file + ".new.XXXXXX").c_str());
- fd = mkostemp(filename, O_WRONLY);
- if (fd == -1) {
- perror(filename);
- free(filename);
- goto sleep;
- }
-
- fp = fdopen(fd, "w");
- if (fp == NULL) {
- perror("fdopen");
- close(fd);
- unlink(filename);
- free(filename);
- goto sleep;
- }
-
- now = time(NULL);
- client_stats = servers->get_client_stats();
- for (size_t i = 0; i < client_stats.size(); ++i) {
- fprintf(fp, "%s %s %d %llu\n",
- client_stats[i].remote_addr.c_str(),
- client_stats[i].stream_id.c_str(),
- int(now - client_stats[i].connect_time),
- (long long unsigned)(client_stats[i].bytes_sent));
- }
- if (fclose(fp) == EOF) {
- perror("fclose");
- unlink(filename);
- free(filename);
- goto sleep;
- }
-
- if (rename(filename, parms->stats_file.c_str()) == -1) {
- perror("rename");
- unlink(filename);
- }
-
-sleep:
- int left_to_sleep = parms->stats_interval;
- do {
- left_to_sleep = sleep(left_to_sleep);
- } while (left_to_sleep > 0 && !hupped);
- }
- return NULL;
-}
-