tools/daemon/smud.cpp
changeset 2444 5565c93eee91
parent 2129 11d73bf97119
child 2462 6040047a9da1
     1.1 --- a/tools/daemon/smud.cpp	Sat Nov 29 18:49:10 2008 -0500
     1.2 +++ b/tools/daemon/smud.cpp	Sat Nov 29 20:07:43 2008 -0600
     1.3 @@ -1,3 +1,4 @@
     1.4 +#include <errno.h>
     1.5  #include "smud.h"
     1.6  #include "smud_threads.h"
     1.7  
     1.8 @@ -29,7 +30,9 @@
     1.9  	char filename[100];
    1.10  	struct stat sbuf;
    1.11  
    1.12 -	printf("Loading Gamedata files into memory\n");
    1.13 +#if defined DEBUG
    1.14 +	fprintf(stdout, "Loading Gamedata files into memory\n");
    1.15 +#endif
    1.16  
    1.17  	for (int i=0; i<NUM_FILES; i++)
    1.18  	{
    1.19 @@ -38,34 +41,44 @@
    1.20  
    1.21  		if (!file)
    1.22  		{
    1.23 +			fprintf(stderr, "Could not find file: %s", filename);
    1.24  			return 1;
    1.25  		}
    1.26  
    1.27  		if (stat(filename, &sbuf) == -1)
    1.28  		{
    1.29 +			fprintf(stderr, "Could not stat file: %s (error: %s)", filename, strerror(errno));
    1.30  			return 1;
    1.31  		}
    1.32  
    1.33 -		if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, file, 0)) == (caddr_t)(-1))
    1.34 +		if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, file, 0)) == MAP_FAILED)
    1.35  		{
    1.36 +			fprintf(stderr, "Could not mmap file: %s (error: %s)", filename, strerror(errno));
    1.37  			return 1;
    1.38  		}
    1.39  
    1.40  		fileLength[i] = sbuf.st_size;
    1.41  
    1.42 -		printf("Initialised file of %s of length %i\n", fileNames[i], fileLength[i]);
    1.43 +#if defined DEBUG
    1.44 +		fprintf(stdout, "Initialised file of %s of length %i\n", fileNames[i], fileLength[i]);
    1.45 +#endif
    1.46  	}
    1.47  
    1.48 -	printf("Initializing Thread Pool\n");
    1.49 +#if defined DEBUG
    1.50 +	fprintf(stdout, "Initializing Thread Pool\n");
    1.51 +#endif
    1.52  
    1.53  	pool = new ThreadPool();
    1.54  	
    1.55  	if (!pool->Start())
    1.56  	{
    1.57 +		fprintf(stderr, "Could not initialize thread pool!\n");
    1.58  		return 1;
    1.59  	}
    1.60  
    1.61 +#if defined DEBUG
    1.62  	printf("Create Server Socket\n");
    1.63 +#endif
    1.64  
    1.65  	memset(&serverAddress, 0, sizeof(serverAddress));
    1.66  	serverAddress.sin_family = AF_INET;
    1.67 @@ -76,6 +89,7 @@
    1.68  
    1.69  	if (pProtocol == NULL)
    1.70  	{
    1.71 +		fprintf(stderr, "Could not get tcp proto: %s", strerror(errno));
    1.72  		return 1;
    1.73  	}
    1.74  
    1.75 @@ -83,6 +97,7 @@
    1.76  
    1.77  	if (serverSocket < 0) 
    1.78  	{
    1.79 +		fprintf(stderr, "Could not open socket: %s", strerror(errno));
    1.80  		return 1;
    1.81  	}
    1.82  
    1.83 @@ -91,34 +106,45 @@
    1.84  
    1.85  	if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) 
    1.86  	{
    1.87 +		fprintf(stderr, "Could not bind socket: %s", strerror(errno));
    1.88  		return 1;
    1.89  	}
    1.90  
    1.91  	if (listen(serverSocket, LISTEN_QUEUE_LENGTH) < 0) 
    1.92  	{
    1.93 +		fprintf(stderr, "Could not listen on socket: %s", strerror(errno));
    1.94  		return 1;
    1.95  	}
    1.96  
    1.97 -	printf("Entering Main Loop\n");
    1.98 +	fprintf(stdout, "Server has started.\n");
    1.99  
   1.100  	while (1)
   1.101  	{
   1.102  		addressLen = sizeof(clientAddress);
   1.103  
   1.104 -		if ( (clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, (socklen_t *)&addressLen)) < 0) 
   1.105 +		clientSocket = accept(serverSocket,
   1.106 +							  (struct sockaddr *)&clientAddress,
   1.107 +							  (socklen_t *)&addressLen);
   1.108 +		if (clientSocket < 0)
   1.109  		{
   1.110 +			fprintf(stderr, "Could not accept client: %s", strerror(errno));
   1.111  			continue;
   1.112  		}
   1.113  
   1.114  		opts = fcntl(clientSocket, F_GETFL, 0);
   1.115  		if (fcntl(clientSocket, F_SETFL, opts|O_NONBLOCK) < 0)
   1.116  		{
   1.117 +			fprintf(stderr, "Could not non-block client: %s", strerror(errno));
   1.118  			closesocket(clientSocket);
   1.119  			continue;
   1.120  		}
   1.121  
   1.122 -
   1.123 -		printf("Connection Received!\n");
   1.124 +#if defined DEBUG
   1.125 +		fprintf(stdout,
   1.126 +				"Accepting connection from client (sock %d, ip %s)",
   1.127 +				clientSocket,
   1.128 +				inet_ntoa(&clientAddress));
   1.129 +#endif
   1.130  
   1.131  		pool->AddConnection(clientSocket);
   1.132  	}