Commits

Anonymous committed 7cc86f2

Merged Bane's enhancements, added shell building, fixed win32/posix builds.

Comments (0)

Files changed (2)

+#!/bin/sh
+g++ ./pixmap_viz_client.cpp ./client.cpp -o ./viz -lglut
+echo "use ./viz to run."
 #ifdef _WIN32
-#define WINDOWS
-#endif
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef WINDOWS
-#include <Ws2tcpip.h>
-#include <winsock.h>
-#else
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <unistd.h>
-#endif
-
-#include "client.hpp"
-
-#define PORT "57483" // Port to connect to
-
-int connect_socket;
-client_info c_info;
-struct addrinfo hints, *server_info, *p;
-
-void client_initialize() {
-
-#ifdef WINDOWS
+#define WINDOWS
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef WINDOWS
+#include <Ws2tcpip.h>
+#include <winsock.h>
+#else
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+#include "client.hpp"
+
+#define PORT "57483" // Port to connect to
+
+int connect_socket;
+client_info c_info;
+struct addrinfo hints, *server_info, *p;
+
+void client_initialize() {
+
+#ifdef WINDOWS
     WSADATA wsaData;   // if this doesn't work
     //WSAData wsaData; // then try this instead
 
     if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
         fprintf(stderr, "WSAStartup failed.\n");
         return;
-    }
-#endif
-
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = AF_INET;
-    hints.ai_socktype = SOCK_STREAM;
-}
-
-client_info handshake(const char* const IP) {
-    c_info.width = c_info.height = 0;
-    int status;
-    while(1) {
-      status = getaddrinfo(IP, PORT, &hints, &server_info);
-      if (status != 0) {
-        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
-        continue;
-      }
-
-      /* Loop through all results and connect to the first one we can */
-      for (p= server_info; p != NULL; p = p->ai_next) {
-        if ((connect_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
-          perror("Client: socket");
-          continue;
-        }
-
-        if (connect(connect_socket, p->ai_addr, p->ai_addrlen) == -1) {
-#ifdef WINDOWS
-          closesocket(connect_socket);
-#else
-          close(connect_socket);
-#endif
-          perror("Client: connect");
-          continue;
-        }
-        break;
-      }
-
-      if (p == NULL) { /* Failed to connect */
-        fprintf(stderr, "Client: failed to connect\n");
-        continue; /* Keep trying */
-      } else {
-        break;
-      }
-    }
-
-    freeaddrinfo(server_info);
-
-    int buf[2];
-    int num = recv(connect_socket, (char*)buf, 2*sizeof(int), 0);
-    if (num == -1) {
-        perror("Client: recv");
-        c_info.width = c_info.height = 0; // Failure
-        return c_info;
-    }
-    if ((unsigned int)num < sizeof(client_info)) {
-        fprintf(stderr, "Client: only received %d out of %d for info struct\n", num, (int) sizeof(client_info));
-        c_info.width = c_info.height = 0; // Failure
-        return c_info;
-    }
-    c_info.width = buf[0];
-    c_info.height = buf[1];
-
-    return c_info;
-}
-
-int next_image(unsigned char* Data) {
-    int len = c_info.width*c_info.height*4;
-    int chars_recv;
-
-    /* Receive from server */
-    chars_recv = recv(connect_socket, (char*) Data, len, 0);
-    if (chars_recv == -1 || chars_recv == 0) {
-        perror("Client: recv data");
-        return 0;
-    }
-    if (chars_recv < len) {
-        fprintf(stderr, "Client: only received %d out of %d chars\n", chars_recv, len);
-        return 0;
-    }
-    return 1;
-}
-
-void close_connection() {
-#ifdef WINDOWS
-    closesocket(connect_socket);
-#else
-    close(connect_socket);
-#endif
-}
-
-void client_deinitialize() {
-#ifdef WINDOWS
-    WSACleanup();
-#endif
-}
-#pragma comment(lib, "ws2_32")
+    }
+#endif
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+}
+
+client_info handshake(const char* const IP) {
+    c_info.width = c_info.height = 0;
+    int status;
+    while(1) {
+      status = getaddrinfo(IP, PORT, &hints, &server_info);
+      if (status != 0) {
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
+        continue;
+      }
+
+      /* Loop through all results and connect to the first one we can */
+      for (p= server_info; p != NULL; p = p->ai_next) {
+        if ((connect_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
+          perror("Client: socket");
+          continue;
+        }
+
+        if (connect(connect_socket, p->ai_addr, p->ai_addrlen) == -1) {
+#ifdef WINDOWS
+          closesocket(connect_socket);
+#else
+            close(connect_socket);
+#endif
+          perror("Client: connect");
+          continue;
+        }
+        break;
+      }
+
+      if (p == NULL) { /* Failed to connect */
+        fprintf(stderr, "Client: failed to connect\n");
+        continue; /* Keep trying */
+      } else {
+        break;
+      }
+    }
+
+    freeaddrinfo(server_info);
+
+    int buf[2];
+    int num = recv(connect_socket, (char*)buf, 2*sizeof(int), 0);
+    if (num == -1) {
+        perror("Client: recv");
+        c_info.width = c_info.height = 0; // Failure
+        return c_info;
+    }
+    if ((unsigned int)num < sizeof(client_info)) {
+        fprintf(stderr, "Client: only received %d out of %d for info struct\n", num, (int) sizeof(client_info));
+        c_info.width = c_info.height = 0; // Failure
+        return c_info;
+    }
+    c_info.width = buf[0];
+    c_info.height = buf[1];
+
+    return c_info;
+}
+
+int next_image(unsigned char* Data) {
+    int len = c_info.width*c_info.height*4;
+    int chars_recv;
+
+    /* Receive from server */
+    chars_recv = recv(connect_socket, (char*) Data, len, 0);
+    if (chars_recv == -1 || chars_recv == 0) {
+        perror("Client: recv data");
+        return 0;
+    }
+    if (chars_recv < len) {
+        fprintf(stderr, "Client: only received %d out of %d chars\n", chars_recv, len);
+        return 0;
+    }
+    return 1;
+}
+
+void close_connection() {
+#ifdef WINDOWS
+    closesocket(connect_socket);
+#else
+    close(connect_socket);
+#endif
+}
+
+void client_deinitialize() {
+#ifdef WINDOWS
+    WSACleanup();
+#endif
+}
+#pragma comment(lib, "ws2_32")