diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-07 21:43:05 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-07 21:43:05 +0000 |
| commit | a8110d0786e7931715a0d7116ed7d4c23697bdb6 (patch) | |
| tree | 40c8df3fcd445eedcddd3a4de6c3a1f85e8ace52 | |
| parent | 58852efe0561498aca378fb612e16d1c212cd29d (diff) | |
- prva verzia listServer, zatial ju _iba_ hodim na SVN server
git-svn-id: http://opensvn.csie.org/tuxanci_ng@61 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
| -rw-r--r-- | include/storage.h | 6 | ||||
| -rwxr-xr-x | src/Makefile-listserver | 27 | ||||
| -rw-r--r-- | src/listServer.c | 277 | ||||
| -rw-r--r-- | src/storage.c | 20 |
4 files changed, 318 insertions, 12 deletions
diff --git a/include/storage.h b/include/storage.h index a6f357d..81e554e 100644 --- a/include/storage.h +++ b/include/storage.h @@ -9,8 +9,8 @@ extern list_t* newStorage(); extern void addItemToStorage(list_t *list, char *group, char *name, void *data); extern void* getItemFromStorage(list_t *list, char *group, char *name); -extern void delItemFromStorage(list_t *list, char *group, char *name, void(*f)(void *)); -extern void delAllItemFromStorage(list_t *list, char *group, void(*f)(void *)); -extern void destroyStorage(list_t *p, void(*f)(void *) ); +extern void delItemFromStorage(list_t *list, char *group, char *name, void *f); +extern void delAllItemFromStorage(list_t *list, char *group, void *f); +extern void destroyStorage(list_t *p, void *f); #endif diff --git a/src/Makefile-listserver b/src/Makefile-listserver new file mode 100755 index 0000000..c2cba31 --- /dev/null +++ b/src/Makefile-listserver @@ -0,0 +1,27 @@ + +#CC = /usr/local/gcc/bin/gcc +CC = gcc + +#CFLAGS = -O2 -DSUPPORT_NET_SDL_UDP -I../include -Wall `sdl-config --cflags` +CFLAGS = -g -O0 -std=c99 -D_GNU_SOURCE=1 -I../include -Wall +#CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall + +BUILD_DIR = . +LIBS = + +FILES = listServer.o udp.o list.o + +listServer: $(FILES) + $(CC) $(CFLAGS) $(LIBS) -o $(BUILD_DIR)/listServer $(BUILD_DIR)/*.o + +listServer.o: listServer.c + $(CC) $(CFLAGS) -o $(BUILD_DIR)/listServer.o -c listServer.c + +list.o: list.c ../include/list.h + $(CC) $(CFLAGS) -o $(BUILD_DIR)/list.o -c list.c + +udp.o: udp.c ../include/udp.h + $(CC) $(CFLAGS) -o $(BUILD_DIR)/udp.o -c udp.c + +clean: + rm -rf *.o *.c~ ../include/*.h~ Makefile~ diff --git a/src/listServer.c b/src/listServer.c new file mode 100644 index 0000000..30dba93 --- /dev/null +++ b/src/listServer.c @@ -0,0 +1,277 @@ + +#include <string.h> +#include <stdio.h> +#include <time.h> +#include <assert.h> +#include <stdlib.h> +#include <sys/time.h> +#include <signal.h> + +#include "list.h" +#include "udp.h" + +#define STR_MSG_SIZE 512 +#define STR_IP4_SIZE 20 + +#define LIMIT_REGISTER "100" +#define LIMIT_TIME "10" + +static sock_udp_t *socketServer; +static sock_udp_t *socketClient; +static list_t *listClient; + +static int limitRegisterGameServer; +static int limitTime; + +typedef struct client_struct +{ + char *msg; + char *ip; + time_t time; +} client_t; + +static int my_argc; +static char **my_argv; + +char* getParam(char *s) +{ + int i; + int len; + + len = strlen(s); + + for( i = 1 ; i < my_argc ; i++ ) + { + //printf("%s %s\n", s, my_argv[i]); + + if( strlen(my_argv[i]) < len ) + { + continue; + } + + if( strncmp(s, my_argv[i], len) == 0 ) + { + return strchr(my_argv[i], '=')+1; + } + } + + return NULL; +} + +char* getParamElse(char *s1, char *s2) +{ + char *ret; + ret = getParam(s1); + + if( ret == NULL) + { + return s2; + } + + return ret; +} + +bool_t isParamFlag(char *s) +{ + int i; + + for( i = 0 ; i < my_argc ; i++ ) + { + if( strcmp(s, my_argv[i]) == 0 ) + { + return TRUE; + } + } + + return FALSE; +} + +client_t* newClient(char *str_ip, char *msg) +{ + client_t *new; + + new = malloc( sizeof(client_t) ); + memset(new, 0, sizeof(client_t)); + new->ip = strdup(str_ip); + new->msg = strdup(msg); + new->time = time(NULL); + + return new; +} + +void replaceClient(client_t *p, char *msg) +{ + assert( p != NULL ); + + free(p->msg); + p->msg = strdup(msg); + p->time = time(NULL); +} + +void destroyClient(client_t *p) +{ + assert( p != NULL ); + + free(p->ip); + free(p->msg); + free(p); +} + +void cmd_register(sock_udp_t *client, char *msg) +{ + char str_ip[STR_IP4_SIZE]; + int len; + int i; + + if( listClient->count > limitRegisterGameServer ) + { + return; + } + + getSockUdpIp(client, str_ip); + + len = strlen(msg); + + if( msg[len-1] == '\n' ) + { + msg[len-1] = '\0'; + len--; + } + + for( i = 0 ; i < len ; i++ ) + { + if( msg[i] < ' ' || msg[i] > '~' ) + { + msg[i] = '?'; + } + } + + for( i = 0 ; i < listClient->count ; i++ ) + { + client_t *client; + + client = (client_t *)listClient->list[i]; + + if( strcmp(client->ip, str_ip) == 0 ) + { + replaceClient(client, msg+9); + return; + } + } + + addList(listClient, newClient(str_ip, msg+9) ); +} + +void cmd_list(sock_udp_t *socketClient, char *msg) +{ + time_t currnetTime; + char *out_msg; + int len_out_msg; + int i; + + if( listClient->count == 0 ) + { + writeUdpSocket(socketServer, socketClient, "<|>\n", 4); + return; + } + + currnetTime = time(NULL); + + len_out_msg = listClient->count * STR_MSG_SIZE; + out_msg = malloc(len_out_msg); + memset(out_msg, 0, len_out_msg); + + len_out_msg--; + + for( i = 0 ; i < listClient->count ; i++ ) + { + client_t *client; + char line[STR_MSG_SIZE*2]; + int len_new_line; + + client = (client_t *)listClient->list[i]; + + if( currnetTime - client->time > limitTime ) + { + delListItem(listClient, i, destroyClient); + i--; + continue; + } + + sprintf(line, "ip=\"%s\" %s\n", client->ip, client->msg); + len_new_line = strlen(line); + + if( len_out_msg > len_new_line ) + { + strcat(out_msg, line); + len_out_msg -= len_new_line; + } + else + { + break; + } + } + + if( listClient->count == 0 ) + { + writeUdpSocket(socketServer, socketClient, "<|>\n", 4); + free(out_msg); + return; + } + + writeUdpSocket(socketServer, socketClient, out_msg, strlen(out_msg) ); + free(out_msg); +} + +void my_handler_quit(int n) +{ + printf("my_handler_quit\n"); + + destroyListItem(listClient, destroyClient); + closeUdpSocket(socketServer); + destroySockUdp(socketClient); + + exit(0); +} + +int main(int argc, char **argv) +{ + my_argc = argc; + my_argv = argv; + + signal(SIGPIPE, SIG_IGN); + signal(SIGINT, my_handler_quit); + signal(SIGTERM, my_handler_quit); + signal(SIGQUIT, my_handler_quit); + + listClient = newList(); + + limitRegisterGameServer = atoi( getParamElse("--limitclient", LIMIT_REGISTER) ); + limitTime = atoi( getParamElse("--limittime", LIMIT_TIME) ); + + socketServer = bindUdpSocket( + getParamElse("--ip", "0.0.0.0"), + atoi(getParamElse("--port", "8400")) ); + + socketClient = newSockUdp(); + + for(;;) + { + char msg[STR_MSG_SIZE]; + + memset(msg, 0, STR_MSG_SIZE); + readUdpSocket(socketServer, socketClient, msg, STR_MSG_SIZE); + + if( strncmp(msg, "register", 8) == 0 ) + { + cmd_register(socketClient, msg); + } + + if( strncmp(msg, "list", 4) == 0 ) + { + cmd_list(socketClient, msg); + } + } + + return 0; +} diff --git a/src/storage.c b/src/storage.c index b1c378c..e7afa45 100644 --- a/src/storage.c +++ b/src/storage.c @@ -35,12 +35,16 @@ static storage_item_t* newStorageItem(char *group, char *name, void *data) return new; } -static void destroyStorageItem(storage_item_t *p, void(*f)(void *)) +static void destroyStorageItem(storage_item_t *p, void *f) { + void(*fce)(void *); + assert( p != NULL ); assert( f != NULL ); - f(p->data); + fce = f; + + fce(p->data); free(p->name); free(p->group); free(p); @@ -54,11 +58,10 @@ void addItemToStorage(list_t *list, char *group, char *name, void *data) void* getItemFromStorage(list_t *list, char *group, char *name) { storage_item_t *this; + int i; assert( group != NULL ); assert( name != NULL ); - - int i; for(i = 0 ; i < list->count; i++) { @@ -74,15 +77,14 @@ void* getItemFromStorage(list_t *list, char *group, char *name) return NULL; } -void delItemFromStorage(list_t *list, char *group, char *name, void(*f)(void *)) +void delItemFromStorage(list_t *list, char *group, char *name, void *f) { storage_item_t *this; + int i; assert( group != NULL ); assert( name != NULL ); - int i; - for(i = 0 ; i < list->count; i++) { this = (storage_item_t *) list->list[i]; @@ -98,7 +100,7 @@ void delItemFromStorage(list_t *list, char *group, char *name, void(*f)(void *)) return; } -void delAllItemFromStorage(list_t *list, char *group, void(*f)(void *)) +void delAllItemFromStorage(list_t *list, char *group, void *f) { storage_item_t *this; int i; @@ -118,7 +120,7 @@ void delAllItemFromStorage(list_t *list, char *group, void(*f)(void *)) } } -void destroyStorage(list_t *p, void(*f)(void *) ) +void destroyStorage(list_t *p, void *f) { storage_item_t *this; int i; |