From a0f73cf7421e55e84b61065b4eb7ed13b05ce055 Mon Sep 17 00:00:00 2001 From: oroborus Date: Fri, 27 Jun 2008 16:46:09 +0000 Subject: - šetřit možnosti podvádění – rychlost pohybu - delObjectFromSpace ma casovu narocnost log(2,n) pri odstraneni objetu zo zonamu objektov ( btw. z hladiska programovania su veci pre 0.20 hotove ) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://opensvn.csie.org/tuxanci_ng@75 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- config.h | 4 +-- include/protect.h | 30 ++++++++++++++++ include/server.h | 12 ++++--- src/Makefile | 5 ++- src/Makefile-publicServer | 5 ++- src/checkFront.c | 8 ++--- src/protect.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++ src/proto.c | 40 +++++++++++++++------ src/publicServer.c | 1 + src/server.c | 71 ++++++++++++++++++++++++-------------- src/space.c | 35 ++++++------------- src/tux.c | 17 --------- 12 files changed, 227 insertions(+), 89 deletions(-) create mode 100644 include/protect.h create mode 100644 src/protect.c diff --git a/config.h b/config.h index ebf5323..1dfa2df 100644 --- a/config.h +++ b/config.h @@ -6,9 +6,9 @@ #define DEBUG_CLIENT_RECV */ -#define TUXANCI_NG_VERSION "svn74" +#define TUXANCI_NG_VERSION "svn75" #define DESTDIR "/usr/local/" #define SUPPORT_NET_UNIX_UDP //#define SUPPORT_NET_SDL_UDP -#define PUBLIC_SERVER +//#define PUBLIC_SERVER diff --git a/include/protect.h b/include/protect.h new file mode 100644 index 0000000..e0553dd --- /dev/null +++ b/include/protect.h @@ -0,0 +1,30 @@ + +#ifndef PROTECT_H + +#define PROTECT_H + +#include "main.h" +#include "myTimer.h" + +#define PROTECT_PING_INTERVAL_TIMEOUT 5000 +#define PROTECT_SPEED_INTERVAL_TIMEOUT 40 +#define PROTECT_SPEED_AVARAGE 10 + +typedef struct protect_struct +{ + my_time_t lastPing; + + my_time_t lastMove; + my_time_t avarage; + int count; + + bool_t isDown; +} protect_t; + +extern protect_t* newProtect(); +extern void refreshLastMove(protect_t *p); +extern void rereshLastPing(protect_t *p); +extern bool_t isDown(protect_t *p); +extern void destroyProtect(protect_t *p); + +#endif diff --git a/include/server.h b/include/server.h index f1abc8f..24c6e0a 100644 --- a/include/server.h +++ b/include/server.h @@ -9,6 +9,7 @@ #include "tux.h" #include "buffer.h" #include "myTimer.h" +#include "protect.h" #ifndef PUBLIC_SERVER #include "interface.h" @@ -27,7 +28,7 @@ #endif -#define SERVER_TIMEOUT 5000 +#define SERVER_TIMEOUT 1000 #define SERVER_TIME_SYNC 1000 #define SERVER_TIME_PING 1000 #define SERVER_MAX_CLIENTS 100 @@ -45,10 +46,13 @@ typedef struct client_struct sock_sdl_udp_t *socket_sdl_udp; #endif int status; + tux_t *tux; - my_time_t lastPing; - list_t *listCheck; - list_t *buffer; + + protect_t *protect; + + list_t *listSendMsg; + list_t *listRecvMsg; } client_t; #ifdef PUBLIC_SERVER diff --git a/src/Makefile b/src/Makefile index 9ff8aa6..13f5ba4 100755 --- a/src/Makefile +++ b/src/Makefile @@ -9,7 +9,7 @@ CFLAGS = -g -O0 -std=c99 -I../include -Wall `sdl-config --cflags` BUILD_DIR = . LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net -FILES = interface.o space.o idManager.o storage.o font.o list.o modules.o\ +FILES = interface.o protect.o space.o idManager.o storage.o font.o list.o modules.o\ image.o layer.o director.o configFile.o tux.o main.o checkFront.o\ screen.o screen_world.o shot.o myTimer.o panel.o net_multiplayer.o \ arena.o arenaFile.o textFile.o audio.o sound.o music.o gun.o \ @@ -24,6 +24,9 @@ tuxanci-ng: $(FILES) main.o: main.c $(CC) $(CFLAGS) -o $(BUILD_DIR)/main.o -c main.c +protect.o: protect.c + $(CC) $(CFLAGS) -o $(BUILD_DIR)/protect.o -c protect.c + hashTable.o: hashTable.c ../include/hashTable.h $(CC) $(CFLAGS) -o $(BUILD_DIR)/hashTable.o -c hashTable.c diff --git a/src/Makefile-publicServer b/src/Makefile-publicServer index 437e989..7f1f30c 100755 --- a/src/Makefile-publicServer +++ b/src/Makefile-publicServer @@ -10,7 +10,7 @@ CFLAGS = -g -O0 -I../include -Wall BUILD_DIR = ../build-server LIBS = -ldl -FILES = list.o space.o heightScore.o modules.o director.o configFile.o tux.o shot.o myTimer.o \ +FILES = list.o space.o protect.o heightScore.o modules.o director.o configFile.o tux.o shot.o myTimer.o \ arena.o arenaFile.o textFile.o gun.o item.o idManager.o checkFront.o\ udp.o proto.o server.o publicServer.o net_multiplayer.o serverConfigFile.o main.o @@ -20,6 +20,9 @@ $(BUILD_DIR)/publicserver: mkdir_build $(FILES) mkdir_build: mkdir -p $(BUILD_DIR) +protect.o: protect.c ../include/protect.h + $(CC) $(CFLAGS) -o $(BUILD_DIR)/protect.o -c protect.c + space.o: space.c ../include/space.h $(CC) $(CFLAGS) -o $(BUILD_DIR)/space.o -c space.c diff --git a/src/checkFront.c b/src/checkFront.c index 7baa4f2..f6164f3 100644 --- a/src/checkFront.c +++ b/src/checkFront.c @@ -63,17 +63,17 @@ void eventMsgInCheckFront(client_t *client) currentTime = getMyTime(); - for( i = 0 ; i < client->listCheck->count ; i++ ) + for( i = 0 ; i < client->listSendMsg->count ; i++ ) { checkfront_t *this; - this = (checkfront_t *)client->listCheck->list[i]; + this = (checkfront_t *)client->listSendMsg->list[i]; switch( this->type ) { case CHECK_FORNT_TYPE_SIMPLE : sendClient(client, this->msg); - delListItem(client->listCheck, i, destroyCheck); + delListItem(client->listSendMsg, i, destroyCheck); i--; break; @@ -92,7 +92,7 @@ void eventMsgInCheckFront(client_t *client) delID(this->id); } - delListItem(client->listCheck, i, destroyCheck); + delListItem(client->listSendMsg, i, destroyCheck); i--; } break; diff --git a/src/protect.c b/src/protect.c new file mode 100644 index 0000000..fe9d29d --- /dev/null +++ b/src/protect.c @@ -0,0 +1,88 @@ + +#include +#include +#include +#include + +#include "main.h" +#include "myTimer.h" +#include "protect.h" + +protect_t* newProtect() +{ + protect_t *new; + + new = malloc( sizeof(protect_t) ); + new->lastMove = getMyTime(); + new->lastPing = getMyTime(); + new->avarage = 0; + new->count = 0; + new->isDown = FALSE; + return new; +} + +void refreshLastMove(protect_t *p) +{ + my_time_t currentTime; + my_time_t interval; + + currentTime = getMyTime(); + + interval = currentTime - p->lastMove; + p->lastMove = getMyTime(); + + p->avarage += interval; + p->count++; + + if( p->count == PROTECT_SPEED_AVARAGE ) + { + int index; + + index = p->avarage/PROTECT_SPEED_AVARAGE; + p->avarage = 0; + p->count = 0; + + if( index < PROTECT_SPEED_INTERVAL_TIMEOUT ) + { + p->isDown = TRUE; + } + + //printf("speed index = %d\n", index); + } +} + +void rereshLastPing(protect_t *p) +{ + //my_time_t currentTime; + //my_time_t interval; + + //currentTime = getMyTime(); + + //interval = currentTime - p->lastPing; + p->lastPing = getMyTime(); + + //printf("interval = %d\n", interval); +} + +bool_t isDown(protect_t *p) +{ + my_time_t currentTime; + my_time_t interval; + + currentTime = getMyTime(); + + interval = currentTime - p->lastPing; + + if( interval > PROTECT_PING_INTERVAL_TIMEOUT ) + { + p->isDown = TRUE; + } + + return p->isDown; +} + +void destroyProtect(protect_t *p) +{ + assert( p != NULL ); + free(p); +} diff --git a/src/proto.c b/src/proto.c index dd6aeeb..4f71531 100644 --- a/src/proto.c +++ b/src/proto.c @@ -16,6 +16,7 @@ #include "net_multiplayer.h" #include "checkFront.h" #include "idManager.h" +#include "protect.h" #ifndef PUBLIC_SERVER #include "language.h" @@ -208,7 +209,7 @@ void proto_recv_check_server(client_t *client, char *msg) sscanf(msg, "%s %d", cmd, &id); - delMsgInCheckFront(client->listCheck, id); + delMsgInCheckFront(client->listSendMsg, id); } void proto_send_init_server(int type, client_t *client, client_t *client2) @@ -307,6 +308,25 @@ void proto_send_event_client(int action) #endif +/* +static void debug_interval() +{ + static my_time_t lastEvent = 0; + my_time_t curentTime; + + if( lastEvent == 0 ) + { + lastEvent = getMyTime(); + } + + curentTime = getMyTime(); + + printf("time = %d\n", curentTime - lastEvent); + + lastEvent = getMyTime(); +} +*/ + void proto_recv_event_server(client_t *client, char *msg) { char cmd[STR_PROTO_SIZE]; @@ -315,10 +335,13 @@ void proto_recv_event_server(client_t *client, char *msg) assert( client != NULL ); assert( msg != NULL ); + //debug_interval(); + sscanf(msg, "%s %d", cmd, &action); proto_send_event_server(PROTO_SEND_ALL_SEES_TUX, client, client->tux, action); + refreshLastMove(client->protect); actionTux(client->tux, action); } @@ -572,17 +595,15 @@ void proto_recv_additem_client(char *msg) void proto_send_shot_server(int type, client_t *client, shot_t *p) { char msg[STR_PROTO_SIZE]; - int check_id; assert( p != NULL ); - check_id = getNewID(); - sprintf(msg, "shot %d %d %d %d %d %d %d %d %d %d\n", - p->id, p->x, p->y, p->px, p->py, p->position, p->gun, p->author_id, p->isCanKillAuthor, check_id); + sprintf(msg, "shot %d %d %d %d %d %d %d %d %d\n", + p->id, p->x, p->y, p->px, p->py, p->position, p->gun, p->author_id, p->isCanKillAuthor); //proto_send(type, client, msg); //proto_check(type, client, msg, check_id); - protoSendClient(type, client, msg, CHECK_FORNT_TYPE_CHECK, check_id); + protoSendClient(type, client, msg, CHECK_FORNT_TYPE_SIMPLE, CHECK_FRONT_ID_NONE); } #ifndef PUBLIC_SERVER @@ -590,15 +611,14 @@ void proto_send_shot_server(int type, client_t *client, shot_t *p) void proto_recv_shot_client(char *msg) { char cmd[STR_PROTO_SIZE]; - int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor, check_id; + int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor; shot_t *shot; assert( msg != NULL ); - sscanf(msg, "%s %d %d %d %d %d %d %d %d %d %d", - cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id, &isCanKillAuthor, &check_id); + sscanf(msg, "%s %d %d %d %d %d %d %d %d %d", + cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id, &isCanKillAuthor); - proto_send_check_client(check_id); if( ( shot = getObjectFromSpaceWithID(getCurrentArena()->spaceShot, shot_id) ) != NULL ) { diff --git a/src/publicServer.c b/src/publicServer.c index 7407f3f..4253e17 100644 --- a/src/publicServer.c +++ b/src/publicServer.c @@ -138,6 +138,7 @@ void eventPublicServer() */ lastActive = getMyTime(); + //fce_interval(); eventArena(arena); } diff --git a/src/server.c b/src/server.c index d328d73..dcc475c 100644 --- a/src/server.c +++ b/src/server.c @@ -16,7 +16,6 @@ #include "list.h" #include "tux.h" #include "network.h" -#include "buffer.h" #include "proto.h" #include "server.h" #include "assert.h" @@ -24,6 +23,7 @@ #include "arena.h" #include "net_multiplayer.h" #include "checkFront.h" +#include "protect.h" #ifndef PUBLIC_SERVER #include "screen_world.h" @@ -72,9 +72,11 @@ static void delZombieCLient(void *p_nothink) { thisClient = (client_t *) listClient->list[i]; - if( currentTime - thisClient->lastPing > SERVER_TIMEOUT ) + if( isDown(thisClient->protect) == TRUE ) { - proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT); + //proto_send_error_server(PROTO_SEND_ONE, thisClient, PROTO_ERROR_CODE_TIMEOUT); + proto_send_end_server(PROTO_SEND_ONE, thisClient); + eventMsgInCheckFront(thisClient); thisClient->status = NET_STATUS_ZOMBIE; } @@ -241,9 +243,9 @@ static client_t* newAnyClient() memset(new, 0, sizeof(client_t)); new->status = NET_STATUS_OK; - new->buffer = newList(); - new->lastPing = getMyTime(); - new->listCheck = newCheckFront(); + new->listRecvMsg = newList(); + new->listSendMsg = newCheckFront(); + new->protect = newProtect(); return new; } @@ -287,6 +289,8 @@ void destroyClient(client_t *p) assert( p != NULL ); + eventMsgInCheckFront(p); + #ifdef SUPPORT_NET_UNIX_UDP getSockUdpIp(p->socket_udp, str_ip, STR_IP_SIZE); printf("close connect %s %d\n", str_ip, getSockUdpPort(p->socket_udp) ); @@ -300,8 +304,10 @@ void destroyClient(client_t *p) closeSdlUdpSocket(p->socket_sdl_udp); #endif - destroyListItem(p->buffer, free); - destroyCheckFront(p->listCheck); + destroyListItem(p->listRecvMsg, free); + destroyCheckFront(p->listSendMsg); + + destroyProtect(p->protect); if( p->tux != NULL ) { @@ -381,7 +387,7 @@ static void addMsgClient(client_t *p, char *msg, int type, int id) if( p->status != NET_STATUS_ZOMBIE ) { - addMsgInCheckFront(p->listCheck, msg, type, id); + addMsgInCheckFront(p->listSendMsg, msg, type, id); } } @@ -565,10 +571,10 @@ static void eventClientBuffer(client_t *client) /* obsluha udalosti od clientov */ - //while( getBufferLine(client->buffer, line, STR_PROTO_SIZE) >= 0 ) - for( i = 0 ; i < client->buffer->count ; i++ ) + //while( getBufferLine(client->listRecvMsg, line, STR_PROTO_SIZE) >= 0 ) + for( i = 0 ; i < client->listRecvMsg->count ; i++ ) { - line = (char *)client->buffer->list[i]; + line = (char *)client->listRecvMsg->list[i]; #ifndef PUBLIC_SERVER if( isParamFlag("--recv") ) @@ -576,7 +582,8 @@ static void eventClientBuffer(client_t *client) printf("recv -> %s", line); } #endif - client->lastPing = getMyTime(); + + rereshLastPing(client->protect); if( strncmp(line, "hello", 5) == 0 ) { @@ -631,8 +638,8 @@ static void eventClientBuffer(client_t *client) } } - destroyListItem(client->buffer, free); - client->buffer = newList(); + destroyListItem(client->listRecvMsg, free); + client->listRecvMsg = newList(); } void eventClientListBuffer() @@ -672,7 +679,7 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) { sock_udp_t *sock_client; client_t *client; - char buffer[STR_SIZE]; + char listRecvMsg[STR_SIZE]; bool_t isCreateNewClient; int ret; @@ -681,9 +688,9 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) sock_client = newSockUdp(sock_server->proto); isCreateNewClient = FALSE; - memset(buffer, 0, STR_SIZE); + memset(listRecvMsg, 0, STR_SIZE); - ret = readUdpSocket(sock_server, sock_client, buffer, STR_SIZE-1); + ret = readUdpSocket(sock_server, sock_client, listRecvMsg, STR_SIZE-1); client = findUdpClient(sock_client); @@ -717,8 +724,8 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) return; } - //printf("add packet >>%s<<\n", buffer); - addList(client->buffer, strdup(buffer) ); + //printf("add packet >>%s<<\n", listRecvMsg); + addList(client->listRecvMsg, strdup(listRecvMsg) ); } int selectServerUdpSocket() @@ -770,15 +777,21 @@ int selectServerUdpSocket() //printf("select..\n"); +#ifdef PUBLIC_SERVER if( listClient->count == 0 ) { - ret = select(max_fd+1, &readfds, (fd_set *)NULL, &errorfds, &tv); + ret = select(max_fd+1, &readfds, (fd_set *)NULL, &errorfds, NULL); setServerTimer(); } else { ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); } +#endif + +#ifndef PUBLIC_SERVER + ret = select(max_fd+1, &readfds, (fd_set *)NULL,&errorfds, &tv); +#endif if( ret < 0 ) { @@ -843,7 +856,7 @@ void selectServerSdlUdpSocket() { sock_sdl_udp_t *sock_client; client_t *client; - char buffer[STR_PROTO_SIZE]; + char listRecvMsg[STR_PROTO_SIZE]; bool_t isCreateNewClient; int ret; @@ -853,9 +866,9 @@ void selectServerSdlUdpSocket() sock_client = newSdlSockUdp(); isCreateNewClient = FALSE; - memset(buffer, 0, STR_PROTO_SIZE); + memset(listRecvMsg, 0, STR_PROTO_SIZE); - ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, buffer, STR_PROTO_SIZE-1); + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, listRecvMsg, STR_PROTO_SIZE-1); if( ret < 0 ) { @@ -889,7 +902,7 @@ void selectServerSdlUdpSocket() return; } - addList(client->buffer, strdup(buffer) ); + addList(client->listRecvMsg, strdup(listRecvMsg) ); }while( ret > 0 ); } @@ -897,6 +910,8 @@ void selectServerSdlUdpSocket() void eventServer() { +#ifndef PUBLIC_SERVER + int count; #ifdef SUPPORT_NET_SDL_UDP @@ -909,6 +924,12 @@ void eventServer() }while( count > 0 ); #endif +#endif + +#ifdef PUBLIC_SERVER + selectServerUdpSocket(); +#endif + eventClientListBuffer(); eventTimer(listServerTimer); } diff --git a/src/space.c b/src/space.c index 96ac81b..0d3060a 100644 --- a/src/space.c +++ b/src/space.c @@ -331,9 +331,8 @@ void getObjectFromSpace(space_t *p, int x, int y, int w, int h, list_t *list) } } -void* getObjectFromSpaceWithID(space_t *space, int id) +int searchObjectInSpace(space_t *space, int id) { -#ifdef ERROR_SUPPORT int point_id; int x, y, w, h; int len; @@ -357,7 +356,7 @@ void* getObjectFromSpaceWithID(space_t *space, int id) if( max < 0 || point >= len || max < min ) { - return NULL; + return -1; } space->getStatus(space->list->list[point], &point_id, &x, &y, &w, &h); @@ -369,7 +368,7 @@ void* getObjectFromSpaceWithID(space_t *space, int id) if( point_id == id ) { - return space->list->list[point]; + return point; } if( id > point_id ) @@ -384,29 +383,15 @@ void* getObjectFromSpaceWithID(space_t *space, int id) continue; } } +} -#endif - -#ifndef ERROR_SUPPORT - int this_id, x, y, w, h; - void *this; - int i; - - for( i = 0 ; i < space->list->count ; i++ ) - { - this = space->list->list[i]; - assert( this != NULL ); - - space->getStatus(this, &this_id, &x, &y, &w, &h); +void* getObjectFromSpaceWithID(space_t *space, int id) +{ + int index; - if( this_id == id ) - { - return this; - } - } + index = searchObjectInSpace(space, id); - return NULL; -#endif + return ( index != -1 ? space->list->list[index] : NULL ); } int isConflictWithObjectFromSpace(space_t *p, int x, int y, int w, int h) @@ -495,7 +480,7 @@ void delObjectFromSpace(space_t *p, void *item) } } - index = searchListItem(p->list, item); + index = searchObjectInSpace(p, id); assert( index != -1 ); delList(p->list, index); } diff --git a/src/tux.c b/src/tux.c index 7b41798..ea083de 100644 --- a/src/tux.c +++ b/src/tux.c @@ -471,23 +471,6 @@ void eventConflictTuxWithShot(arena_t *arena) } } -static void interval() -{ - static my_time_t lastEvent = 0; - my_time_t curentTime; - - if( lastEvent == 0 ) - { - lastEvent = getMyTime(); - } - - curentTime = getMyTime(); - - printf("time = %d\n", curentTime - lastEvent); - - lastEvent = getMyTime(); -} - void moveTux(tux_t *tux, int n) { int px, py; -- cgit v1.2.3