tools/daemon/smud.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2462 6040047a9da1
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     1 #include <errno.h>
     2 #include "smud.h"
     3 #include "smud_threads.h"
     4 
     5 #define LISTEN_PORT 6500
     6 #define LISTEN_QUEUE_LENGTH 6
     7 
     8 char fileNames[NUM_FILES][30] = {
     9 	"core.games.txt",
    10 	"sdktools.games.txt",
    11 	"sdktools.games.ep2.txt",
    12 	"sdktools.games.l4d.txt",
    13 	"sm-cstrike.games.txt",
    14 	"sm-tf2.games.txt",
    15 };
    16 
    17 void *fileLocations[NUM_FILES];
    18 int fileLength[NUM_FILES];
    19 
    20 int main(int argc, char **argv)
    21 {
    22 	ThreadPool *pool;
    23 	struct protoent *pProtocol;
    24 	struct sockaddr_in serverAddress;
    25 	struct sockaddr_in clientAddress;
    26 	int serverSocket;
    27 	int clientSocket;
    28 	int addressLen;
    29 	int opts;
    30 	int file;
    31 	char filename[100];
    32 	struct stat sbuf;
    33 
    34 #if defined DEBUG
    35 	fprintf(stdout, "Loading Gamedata files into memory\n");
    36 #endif
    37 
    38 	for (int i=0; i<NUM_FILES; i++)
    39 	{
    40 		snprintf(filename, sizeof(filename), "./md5/%s", fileNames[i]);
    41 		file = open(filename, O_RDWR);
    42 
    43 		if (!file)
    44 		{
    45 			fprintf(stderr, "Could not find file: %s", filename);
    46 			return 1;
    47 		}
    48 
    49 		if (stat(filename, &sbuf) == -1)
    50 		{
    51 			fprintf(stderr, "Could not stat file: %s (error: %s)", filename, strerror(errno));
    52 			return 1;
    53 		}
    54 
    55 		if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, file, 0)) == MAP_FAILED)
    56 		{
    57 			fprintf(stderr, "Could not mmap file: %s (error: %s)", filename, strerror(errno));
    58 			return 1;
    59 		}
    60 
    61 		fileLength[i] = sbuf.st_size;
    62 
    63 #if defined DEBUG
    64 		fprintf(stdout, "Initialised file of %s of length %i\n", fileNames[i], fileLength[i]);
    65 #endif
    66 	}
    67 
    68 #if defined DEBUG
    69 	fprintf(stdout, "Initializing Thread Pool\n");
    70 #endif
    71 
    72 	pool = new ThreadPool();
    73 	
    74 	if (!pool->Start())
    75 	{
    76 		fprintf(stderr, "Could not initialize thread pool!\n");
    77 		return 1;
    78 	}
    79 
    80 #if defined DEBUG
    81 	printf("Create Server Socket\n");
    82 #endif
    83 
    84 	memset(&serverAddress, 0, sizeof(serverAddress));
    85 	serverAddress.sin_family = AF_INET;
    86 	serverAddress.sin_addr.s_addr = INADDR_ANY;
    87 	serverAddress.sin_port = htons(LISTEN_PORT);
    88 
    89 	pProtocol = getprotobyname("tcp");
    90 
    91 	if (pProtocol == NULL)
    92 	{
    93 		fprintf(stderr, "Could not get tcp proto: %s", strerror(errno));
    94 		return 1;
    95 	}
    96 
    97 	serverSocket = socket(AF_INET, SOCK_STREAM, pProtocol->p_proto);
    98 
    99 	if (serverSocket < 0) 
   100 	{
   101 		fprintf(stderr, "Could not open socket: %s", strerror(errno));
   102 		return 1;
   103 	}
   104 
   105 	opts = 1;
   106 	setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
   107 
   108 	if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) 
   109 	{
   110 		fprintf(stderr, "Could not bind socket: %s", strerror(errno));
   111 		return 1;
   112 	}
   113 
   114 	if (listen(serverSocket, LISTEN_QUEUE_LENGTH) < 0) 
   115 	{
   116 		fprintf(stderr, "Could not listen on socket: %s", strerror(errno));
   117 		return 1;
   118 	}
   119 
   120 	fprintf(stdout, "Server has started.\n");
   121 
   122 	while (1)
   123 	{
   124 		addressLen = sizeof(clientAddress);
   125 
   126 		clientSocket = accept(serverSocket,
   127 							  (struct sockaddr *)&clientAddress,
   128 							  (socklen_t *)&addressLen);
   129 		if (clientSocket < 0)
   130 		{
   131 			fprintf(stderr, "Could not accept client: %s", strerror(errno));
   132 			continue;
   133 		}
   134 
   135 		opts = fcntl(clientSocket, F_GETFL, 0);
   136 		if (fcntl(clientSocket, F_SETFL, opts|O_NONBLOCK) < 0)
   137 		{
   138 			fprintf(stderr, "Could not non-block client: %s", strerror(errno));
   139 			closesocket(clientSocket);
   140 			continue;
   141 		}
   142 
   143 #if defined DEBUG
   144 		fprintf(stdout,
   145 				"Accepting connection from client (sock %d, ip %s)",
   146 				clientSocket,
   147 				inet_ntoa(clientAddress.sin_addr));
   148 #endif
   149 
   150 		pool->AddConnection(clientSocket);
   151 	}
   152 
   153 	delete pool;
   154 }
   155 
   156 
   157