tools/daemon/smud.cpp
changeset 2129 11d73bf97119
child 2444 5565c93eee91
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/daemon/smud.cpp	Mon Jun 30 02:34:21 2008 +0000
     1.3 @@ -0,0 +1,130 @@
     1.4 +#include "smud.h"
     1.5 +#include "smud_threads.h"
     1.6 +
     1.7 +#define LISTEN_PORT 6500
     1.8 +#define LISTEN_QUEUE_LENGTH 6
     1.9 +
    1.10 +char fileNames[NUM_FILES][30] = {
    1.11 +	"core.games.txt",
    1.12 +	"sdktools.games.txt",
    1.13 +	"sdktools.games.ep2.txt",
    1.14 +	"sm-cstrike.games.txt",
    1.15 +	"sm-tf2.games.txt",
    1.16 +};
    1.17 +
    1.18 +void *fileLocations[NUM_FILES];
    1.19 +int fileLength[NUM_FILES];
    1.20 +
    1.21 +int main(int argc, char **argv)
    1.22 +{
    1.23 +	ThreadPool *pool;
    1.24 +	struct protoent *pProtocol;
    1.25 +	struct sockaddr_in serverAddress;
    1.26 +	struct sockaddr_in clientAddress;
    1.27 +	int serverSocket;
    1.28 +	int clientSocket;
    1.29 +	int addressLen;
    1.30 +	int opts;
    1.31 +	int file;
    1.32 +	char filename[100];
    1.33 +	struct stat sbuf;
    1.34 +
    1.35 +	printf("Loading Gamedata files into memory\n");
    1.36 +
    1.37 +	for (int i=0; i<NUM_FILES; i++)
    1.38 +	{
    1.39 +		snprintf(filename, sizeof(filename), "./md5/%s", fileNames[i]);
    1.40 +		file = open(filename, O_RDWR);
    1.41 +
    1.42 +		if (!file)
    1.43 +		{
    1.44 +			return 1;
    1.45 +		}
    1.46 +
    1.47 +		if (stat(filename, &sbuf) == -1)
    1.48 +		{
    1.49 +			return 1;
    1.50 +		}
    1.51 +
    1.52 +		if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, file, 0)) == (caddr_t)(-1))
    1.53 +		{
    1.54 +			return 1;
    1.55 +		}
    1.56 +
    1.57 +		fileLength[i] = sbuf.st_size;
    1.58 +
    1.59 +		printf("Initialised file of %s of length %i\n", fileNames[i], fileLength[i]);
    1.60 +	}
    1.61 +
    1.62 +	printf("Initializing Thread Pool\n");
    1.63 +
    1.64 +	pool = new ThreadPool();
    1.65 +	
    1.66 +	if (!pool->Start())
    1.67 +	{
    1.68 +		return 1;
    1.69 +	}
    1.70 +
    1.71 +	printf("Create Server Socket\n");
    1.72 +
    1.73 +	memset(&serverAddress, 0, sizeof(serverAddress));
    1.74 +	serverAddress.sin_family = AF_INET;
    1.75 +	serverAddress.sin_addr.s_addr = INADDR_ANY;
    1.76 +	serverAddress.sin_port = htons(LISTEN_PORT);
    1.77 +
    1.78 +	pProtocol = getprotobyname("tcp");
    1.79 +
    1.80 +	if (pProtocol == NULL)
    1.81 +	{
    1.82 +		return 1;
    1.83 +	}
    1.84 +
    1.85 +	serverSocket = socket(AF_INET, SOCK_STREAM, pProtocol->p_proto);
    1.86 +
    1.87 +	if (serverSocket < 0) 
    1.88 +	{
    1.89 +		return 1;
    1.90 +	}
    1.91 +
    1.92 +	opts = 1;
    1.93 +	setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
    1.94 +
    1.95 +	if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) 
    1.96 +	{
    1.97 +		return 1;
    1.98 +	}
    1.99 +
   1.100 +	if (listen(serverSocket, LISTEN_QUEUE_LENGTH) < 0) 
   1.101 +	{
   1.102 +		return 1;
   1.103 +	}
   1.104 +
   1.105 +	printf("Entering Main Loop\n");
   1.106 +
   1.107 +	while (1)
   1.108 +	{
   1.109 +		addressLen = sizeof(clientAddress);
   1.110 +
   1.111 +		if ( (clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, (socklen_t *)&addressLen)) < 0) 
   1.112 +		{
   1.113 +			continue;
   1.114 +		}
   1.115 +
   1.116 +		opts = fcntl(clientSocket, F_GETFL, 0);
   1.117 +		if (fcntl(clientSocket, F_SETFL, opts|O_NONBLOCK) < 0)
   1.118 +		{
   1.119 +			closesocket(clientSocket);
   1.120 +			continue;
   1.121 +		}
   1.122 +
   1.123 +
   1.124 +		printf("Connection Received!\n");
   1.125 +
   1.126 +		pool->AddConnection(clientSocket);
   1.127 +	}
   1.128 +
   1.129 +	delete pool;
   1.130 +}
   1.131 +
   1.132 +
   1.133 +