Working win32 port

This commit is contained in:
Michael Jumper 2011-04-21 15:54:29 -07:00
parent feff9f5138
commit 4e1e693c2d

View File

@ -44,21 +44,16 @@
#ifdef __MINGW32__ #ifdef __MINGW32__
#include <winsock2.h> #include <winsock2.h>
typedef int socklen_t;
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#endif #endif
#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#elif defined(__MINGW32__)
#include <windows.h>
#include <process.h>
#endif
#include <errno.h> #include <errno.h>
#include <guacamole/client.h> #include <guacamole/client.h>
#include <guacamole/thread.h>
#include <guacamole/log.h> #include <guacamole/log.h>
/* Windows / MINGW32 handles closing sockets differently */ /* Windows / MINGW32 handles closing sockets differently */
@ -73,7 +68,7 @@
char error[65536]; char error[65536];
char* lasterror() { char* lasterror() {
#ifdef __MINGW32__ #ifdef __MINGW32__
snprintf(error, sizeof(error)-1, "ERROR #%i", GetLastError()); snprintf(error, sizeof(error)-1, "ERROR #%li", GetLastError());
return error; return error;
#else #else
return strerror(errno); return strerror(errno);
@ -124,11 +119,11 @@ int main(int argc, char* argv[]) {
/* Server */ /* Server */
int socket_fd; int socket_fd;
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
int opt_on = 1; const char opt_on[] = {1};
/* Client */ /* Client */
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
unsigned int client_addr_len; socklen_t client_addr_len;
int connected_socket_fd; int connected_socket_fd;
/* Arguments */ /* Arguments */
@ -184,7 +179,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt_on, sizeof(opt_on))) { if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, opt_on, sizeof(opt_on))) {
fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror()); fprintf(stderr, "Warning: Unable to set socket options for reuse: %s\n", lasterror());
} }
@ -236,6 +231,7 @@ int main(int argc, char* argv[]) {
/* Otherwise, this is the daemon */ /* Otherwise, this is the daemon */
guac_log_info("Started, listening on port %i", listen_port); guac_log_info("Started, listening on port %i", listen_port);
#ifndef __MINGW32__
/* Ignore SIGPIPE */ /* Ignore SIGPIPE */
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
guac_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon."); guac_log_error("Could not set handler for SIGPIPE to ignore. SIGPIPE may cause termination of the daemon.");
@ -245,14 +241,15 @@ int main(int argc, char* argv[]) {
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
guac_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table."); guac_log_error("Could not set handler for SIGCHLD to ignore. Child processes may pile up in the process table.");
} }
#endif
/* Daemon loop */ /* Daemon loop */
for (;;) { for (;;) {
#ifdef HAVE_FORK #ifdef HAVE_FORK
pid_t child_pid; pid_t child_pid;
#elif HAVE_LIBPTHREAD #else
pthread_t thread; guac_thread_t thread;
#endif #endif
client_thread_data* data; client_thread_data* data;
@ -285,7 +282,7 @@ int main(int argc, char* argv[]) {
/*** FORK ***/ /*** FORK ***/
/* /*
* Note that we prefer fork() over pthreads for connection-handling * Note that we prefer fork() over threads for connection-handling
* processes as they give each connection its own memory area, and * processes as they give each connection its own memory area, and
* isolate the main daemon and other connections from errors in any * isolate the main daemon and other connections from errors in any
* particular client plugin. * particular client plugin.
@ -303,30 +300,10 @@ int main(int argc, char* argv[]) {
return 0; return 0;
} }
#elif HAVE_LIBPTHREAD
/*** PTHREADS ***/
if (pthread_create(&thread, NULL, start_client_thread, (void*) data))
guac_log_error("Could not create client thread: %s", lasterror());
else
pthread_detach(thread);
#elif __MINGW32__
/*** Windows threads ***/
if (_beginthread(start_client_thread, 0, (void*) data) == -1L)
guac_log_error("Could not create client thread: %s", lasterror());
#else #else
#warning THREAD SUPPORT NOT FOUND! guacd will only be able to handle one connection at a time.
/*** NO THREADS ***/ if (guac_thread_create(&thread, start_client_thread, (void*) data))
guac_log_error("Could not create client thread: %s", lasterror());
guac_log_info("Thread support not present at compile time.");
guac_log_info("guacd handling one connection at a time.");
start_client_thread(data);
#endif #endif