Open a server socket, and listen on it.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 12:35:10 +0000 (14:35 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 12:35:10 +0000 (14:35 +0200)
cubemap.cpp

index 2bccac3..b02121d 100644 (file)
@@ -5,6 +5,9 @@
 #include <arpa/inet.h>
 #include <curl/curl.h>
 #include <sys/socket.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <errno.h>
 #include <vector>
 #include <string>
 #include <map>
@@ -162,12 +165,74 @@ size_t curl_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
        return bytes;
 }
 
+int create_server_socket(int port)
+{
+       int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
+       if (server_sock == -1) {
+               perror("socket");
+               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);
+       }
+
+       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));
+       for ( ;; ) {
+               sockaddr_in6 addr;
+               socklen_t addrlen = sizeof(addr);
+
+               int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
+               if (sock == -1 && errno == EINTR) {
+                       continue;
+               }
+               if (sock == -1) {
+                       perror("accept");
+                       exit(1);
+               }
+
+               printf("got a socket yaaaay\n");
+       }
+}
+
 int main(int argc, char **argv)
 {
+       for (int i = 0; i < NUM_SERVERS; ++i) {
+               Server *s = new Server;
+               //s->run();
+       }
+
+       int server_sock = create_server_socket(PORT);
+
+       pthread_t acceptor_thread;
+       pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
+
        Input input;
        CURL *curl = curl_easy_init();
-       curl_easy_setopt(curl, CURLOPT_URL, "http://gruessi.zrh.sesse.net:4013/");
-       // curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
+       curl_easy_setopt(curl, CURLOPT_URL, STREAM_URL);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
        curl_easy_perform(curl);