diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-20 20:22:41 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-20 20:22:41 +0000 |
| commit | 1e1d106a545a9cadc517de1a0ad931f1a8fcc2fa (patch) | |
| tree | 1c6720eb14074cd0be263a75f1c547b83bfaa420 /src | |
| parent | dbf67486087dea9c501014247a82244a44213582 (diff) | |
- uprava sietoveho protokola
- novy prikaz to prtokolu "status"
- moznost nastavit maximalny pocet clientov pri serverti ./publicserver --maxclient <max clients>
- drobne upravy v kode
git-svn-id: http://opensvn.csie.org/tuxanci_ng@30 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src')
| -rwxr-xr-x | src/Makefile | 3 | ||||
| -rw-r--r-- | src/client.c | 20 | ||||
| -rw-r--r-- | src/proto.c | 98 | ||||
| -rw-r--r-- | src/publicServer.c | 2 | ||||
| -rw-r--r-- | src/screen_world.c | 8 | ||||
| -rw-r--r-- | src/server.c | 117 | ||||
| -rw-r--r-- | src/tux.c | 4 |
7 files changed, 164 insertions, 88 deletions
diff --git a/src/Makefile b/src/Makefile index 7160cb5..405812f 100755 --- a/src/Makefile +++ b/src/Makefile @@ -3,7 +3,7 @@ CC = gcc #CFLAGS = -O2 -DSUPPORT_NET_SDL_UDP -I../include -Wall `sdl-config --cflags` -CFLAGS = -O2 -I../include -Wall `sdl-config --cflags` +CFLAGS = -g -O0 -I../include -Wall `sdl-config --cflags` #CFLAGS = -O2 -DDESTDIR=\"$(DESTDIR)\" `sdl-config --cflags` -I../include -Wall LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net @@ -84,6 +84,7 @@ server.o: server.c ../include/server.h client.o: client.c ../include/client.h $(CC) $(CFLAGS) -o client.o -c client.c + network.o: network.c ../include/network.h $(CC) $(CFLAGS) -o network.o -c network.c diff --git a/src/client.c b/src/client.c index 8ca55e1..f525cfe 100644 --- a/src/client.c +++ b/src/client.c @@ -36,10 +36,14 @@ static my_time_t lastPingServerAlive; static void initClient() { + char name[STR_NAME_SIZE]; + clientBuffer = newBuffer( LIMIT_BUFFER ); lastPing = getMyTime(); lastPingServerAlive = getMyTime(); - proto_send_hello_client(); + + getSettingNameRight(name); + proto_send_hello_client(name); } #ifdef SUPPORT_NET_UNIX_TCP @@ -142,13 +146,13 @@ void sendServer(char *msg) static int eventServerSelect() { - char buffer[STR_SIZE]; + char buffer[STR_PROTO_SIZE]; int ret; - memset(buffer,0 ,STR_SIZE); + memset(buffer,0 ,STR_PROTO_SIZE); #ifdef SUPPORT_NET_UNIX_TCP - ret = readTcpSocket(sock_server_tcp, buffer, STR_SIZE-1); + ret = readTcpSocket(sock_server_tcp, buffer, STR_PROTO_SIZE-1); if( ret == 0 ) { @@ -166,11 +170,11 @@ static int eventServerSelect() #endif #ifdef SUPPORT_NET_UNIX_UDP - ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_SIZE-1); + ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); #endif #ifdef SUPPORT_NET_SDL_UDP - ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_SIZE-1); + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_PROTO_SIZE-1); if( ret < 0 ) { @@ -190,13 +194,13 @@ static int eventServerSelect() void eventServerBuffer() { - char line[STR_SIZE]; + char line[STR_PROTO_SIZE]; /* obsluha udalosti od servera */ assert( clientBuffer != NULL ); - while ( getBufferLine(clientBuffer, line, STR_SIZE) >= 0 ) + while ( getBufferLine(clientBuffer, line, STR_PROTO_SIZE) >= 0 ) { #ifdef DEBUG_CLIENT_RECV printf("recv server msg->%s", line); diff --git a/src/proto.c b/src/proto.c index 77ce16f..e2ade65 100644 --- a/src/proto.c +++ b/src/proto.c @@ -62,11 +62,11 @@ static void proto_send(int type, client_t *client, char *msg) #ifndef BUBLIC_SERVER -void proto_send_hello_client() +void proto_send_hello_client(char *name) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; - strcpy(msg, "hello\n"); + sprintf(msg, "hello %s\n", name); sendServer(msg); } @@ -74,11 +74,48 @@ void proto_send_hello_client() void proto_recv_hello_server(client_t *client, char *msg) { + char cmd[STR_PROTO_SIZE]; + char name[STR_NAME_SIZE]; + + assert( client != NULL ); + + sscanf(msg, "%s %s\n", cmd, name); + + client->tux = newTux(); + client->tux->control = TUX_CONTROL_NET; + addList(getWorldArena()->listTux, client->tux); + + if( strlen(name) > STR_NAME_SIZE-1 ) + { + name[STR_NAME_SIZE-1] = '\0'; + } + + strcpy(client->tux->name, name); + sendInfoCreateClient(client); +} + +void proto_send_status_server(int type, client_t *client) +{ + char msg[STR_PROTO_SIZE]; + + sprintf(msg, "version: %s\n" + "clients: %d\n" + "maxclients: %d\n" + "uptime: %d\n", + TUXANCI_NG_VERSION, getListServerClient()->count, + getServerMaxClients(), getMyTime() ); + + proto_send(type, client, msg); +} + +void proto_recv_status_server(client_t *client, char *msg) +{ + proto_send_status_server(PROTO_SEND_ONE, client); } void proto_send_init_server(int type, client_t *client, client_t *client2) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; int n = 9999; assert( client2 != NULL ); @@ -98,8 +135,8 @@ void proto_send_init_server(int type, client_t *client, client_t *client2) void proto_recv_init_client(char *msg) { - char cmd[STR_SIZE]; - char arena_name[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; + char arena_name[STR_PROTO_SIZE]; tux_t *tux; int id; int x, y, n; @@ -117,9 +154,10 @@ void proto_recv_init_client(char *msg) tux->x = x; tux->y = y; tux->control = TUX_CONTROL_KEYBOARD_RIGHT; + getSettingNameRight(tux->name); - proto_send_context_client(tux); +// proto_send_context_client(tux); addList(getWorldArena()->listTux, tux); } @@ -127,7 +165,7 @@ void proto_recv_init_client(char *msg) void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( tux != NULL ); @@ -140,7 +178,7 @@ void proto_send_event_server(int type, client_t *client, tux_t *tux, int action) void proto_recv_event_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int id, action; tux_t *tux; @@ -162,7 +200,7 @@ void proto_recv_event_client(char *msg) void proto_send_event_client(int action) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; sprintf(msg, "event %d\n", action); @@ -173,7 +211,7 @@ void proto_send_event_client(int action) void proto_recv_event_server(client_t *client, char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int action; assert( client != NULL ); @@ -188,7 +226,7 @@ void proto_recv_event_server(client_t *client, char *msg) void proto_send_newtux_server(int type, client_t *client, tux_t *tux) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( tux != NULL ); @@ -206,7 +244,7 @@ void proto_send_newtux_server(int type, client_t *client, tux_t *tux) void proto_recv_newtux_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; char name[STR_NAME_SIZE]; int id, x, y, status, position, frame, score; int myGun, myBonus; @@ -254,7 +292,7 @@ void proto_recv_newtux_client(char *msg) void proto_send_kill_server(int type, client_t *client, tux_t *tux) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( tux != NULL ); @@ -267,7 +305,7 @@ void proto_send_kill_server(int type, client_t *client, tux_t *tux) void proto_recv_kill_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int id; tux_t *tux; @@ -287,7 +325,7 @@ void proto_recv_kill_client(char *msg) void proto_send_score_server(int type, client_t *client, tux_t *tux) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( tux != NULL ); @@ -300,7 +338,7 @@ void proto_send_score_server(int type, client_t *client, tux_t *tux) void proto_recv_score_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int id, score; tux_t *tux; @@ -322,7 +360,7 @@ void proto_recv_score_client(char *msg) void proto_send_deltux_server(int type, client_t *client, client_t *client2) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( client2 != NULL ); @@ -335,7 +373,7 @@ void proto_send_deltux_server(int type, client_t *client, client_t *client2) void proto_recv_deltux_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int id; tux_t *tux; @@ -358,7 +396,7 @@ void proto_recv_deltux_client(char *msg) void proto_send_additem_server(int type, client_t *client, item_t *p) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; int id = -1; assert( p != NULL ); @@ -378,7 +416,7 @@ void proto_send_additem_server(int type, client_t *client, item_t *p) void proto_recv_additem_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int type, x, y, count, frame, id; item_t *item; @@ -404,7 +442,7 @@ void proto_recv_additem_client(char *msg) void proto_send_shot_server(int type, client_t *client, shot_t *p) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; int id = -1; assert( p != NULL ); @@ -424,7 +462,7 @@ void proto_send_shot_server(int type, client_t *client, shot_t *p) void proto_recv_shot_client(char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; int x, y, px, py, position, gun, id, isCanKillAuthor; shot_t *shot; @@ -449,11 +487,12 @@ void proto_recv_shot_client(char *msg) #endif +/* #ifndef BUBLIC_SERVER void proto_send_context_client(tux_t *tux) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; assert( tux != NULL ); @@ -466,7 +505,7 @@ void proto_send_context_client(tux_t *tux) void proto_recv_context_server(client_t *client, char *msg) { - char cmd[STR_SIZE]; + char cmd[STR_PROTO_SIZE]; char name[STR_NAME_SIZE]; assert( client != NULL ); @@ -478,12 +517,13 @@ void proto_recv_context_server(client_t *client, char *msg) // proto_send_newtux_server(NULL, client->tux); } +*/ #ifndef BUBLIC_SERVER void proto_send_ping_client() { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; strcpy(msg, "ping\n"); sendServer(msg); } @@ -504,7 +544,7 @@ void proto_recv_ping_server(client_t *client, char *msg) void proto_send_end_client() { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; strcpy(msg, "end\n"); sendServer(msg); } @@ -521,7 +561,7 @@ void proto_recv_end_server(client_t *client, char *msg) void proto_send_ping_server(int type, client_t *client) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; strcpy(msg, "ping\n"); proto_send(type, client, msg); } @@ -541,7 +581,7 @@ void proto_recv_ping_client(char *msg) void proto_send_end_server(int type, client_t *client) { - char msg[STR_SIZE]; + char msg[STR_PROTO_SIZE]; strcpy(msg, "end\n"); proto_send(type, client, msg); } diff --git a/src/publicServer.c b/src/publicServer.c index 0ba1fc6..2f07b3d 100644 --- a/src/publicServer.c +++ b/src/publicServer.c @@ -51,6 +51,8 @@ int initPublicServer() return -1; } + setServerMaxClients( atoi( getParamElse("--maxclients", "100") )); + return 0; } diff --git a/src/screen_world.c b/src/screen_world.c index f9ef868..d4540b1 100644 --- a/src/screen_world.c +++ b/src/screen_world.c @@ -282,13 +282,13 @@ static void control_keyboard_right(tux_t *tux) actionTux(tux, TUX_DOWN); } - if( mapa[(SDLKey)SDLK_LCTRL] == SDL_PRESSED ) + if( mapa[(SDLKey)SDLK_KP0] == SDL_PRESSED ) { netAction(tux, TUX_SHOT); actionTux(tux, TUX_SHOT); } - if( mapa[(SDLKey)SDLK_LSHIFT] == SDL_PRESSED ) + if( mapa[(SDLKey)SDLK_KP1] == SDL_PRESSED ) { if( tux->isCanSwitchGun == TRUE ) { @@ -389,10 +389,10 @@ void eventWorld() eventMoveListShot(arena->listShot); eventListItem(arena->listItem); - eventListTux(arena->listTux); - eventTimer(); eventNetMultiplayer(); + eventListTux(arena->listTux); + eventTimer(); eventEnd(); eventEsc(); diff --git a/src/server.c b/src/server.c index b66d97e..91e26c0 100644 --- a/src/server.c +++ b/src/server.c @@ -28,8 +28,6 @@ #include "publicServer.h" #endif -static int protocolType; - #ifdef SUPPORT_NET_UNIX_TCP #include "tcp.h" static sock_tcp_t *sock_server_tcp; @@ -47,18 +45,18 @@ static sock_sdl_udp_t *sock_server_sdl_udp; static list_t *listClient; static my_time_t lastSyncClient; +static int maxClients; void static initServer() { listClient = newList(); lastSyncClient = getMyTime(); + setServerMaxClients(SERVER_MAX_CLIENTS); } #ifdef SUPPORT_NET_UNIX_TCP int initTcpServer(int port) { - protocolType = NET_PROTOCOL_TYPE_TCP; - sock_server_tcp = bindTcpSocket(port); if( sock_server_tcp == NULL ) @@ -77,8 +75,6 @@ int initTcpServer(int port) #ifdef SUPPORT_NET_UNIX_UDP int initUdpServer(int port) { - protocolType = NET_PROTOCOL_TYPE_UDP; - sock_server_udp = bindUdpSocket(port); if( sock_server_udp == NULL ) @@ -97,8 +93,6 @@ int initUdpServer(int port) #ifdef SUPPORT_NET_SDL_UDP int initSdlUdpServer(int port) { - protocolType = NET_PROTOCOL_TYPE_UDP; - sock_server_sdl_udp = bindSdlUdpSocket(port); if( sock_server_sdl_udp == NULL ) @@ -123,9 +117,6 @@ static client_t* newAnyClient() new->status = NET_STATUS_OK; new->buffer = newBuffer(LIMIT_BUFFER); - new->tux = newTux(); - new->tux->control = TUX_CONTROL_NET; - addList(getWorldArena()->listTux, new->tux); return new; } @@ -199,18 +190,37 @@ void destroyClient(client_t *p) #endif destroyBuffer(p->buffer); - index = searchListItem(getWorldArena()->listTux, p->tux); - delListItem(getWorldArena()->listTux, index, destroyTux); + + if( p->tux != NULL ) + { + index = searchListItem(getWorldArena()->listTux, p->tux); + delListItem(getWorldArena()->listTux, index, destroyTux); + } free(p); } +list_t* getListServerClient() +{ + return listClient; +} + +int getServerMaxClients() +{ + return maxClients; +} + +void setServerMaxClients(int n) +{ + maxClients = n; +} + void sendClient(client_t *p, char *msg) { assert( p != NULL ); assert( msg != NULL ); - if( p->status == NET_STATUS_OK ) + if( p->status != NET_STATUS_ZOMBIE ) { int ret; @@ -265,7 +275,7 @@ void sendAllClient(char *msg) sendAllClientBut(msg, NULL); } -static void eventCreateClient(client_t *client) +void sendInfoCreateClient(client_t *client) { client_t *thisClient; tux_t *thisTux; @@ -310,8 +320,6 @@ static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp) client = newTcpClient( getTcpNewClient(socket_tcp) ); addList(listClient, client); - - eventCreateClient(client); } #endif @@ -326,8 +334,6 @@ static void eventCreateNewUdpClient(sock_udp_t *socket_udp) client = newUdpClient( socket_udp ); addList(listClient, client); - - eventCreateClient(client); } #endif @@ -342,8 +348,6 @@ static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp) client = newSdlUdpClient( socket_sdl_udp ); addList(listClient, client); - - eventCreateClient(client); } #endif @@ -352,13 +356,13 @@ static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp) static void eventClientTcpSelect(client_t *client) { - char buffer[STR_SIZE]; + char buffer[STR_PROTO_SIZE]; int ret; assert( client != NULL ); memset(buffer, 0, STR_SIZE); - ret = readTcpSocket(client->socket_tcp, buffer, STR_SIZE-1); + ret = readTcpSocket(client->socket_tcp, buffer, STR_PROTO_SIZE-1); if( ret <= 0 ) { @@ -377,23 +381,27 @@ static void eventClientTcpSelect(client_t *client) static void eventClientBuffer(client_t *client) { - char line[STR_SIZE]; + char line[STR_PROTO_SIZE]; assert( client != NULL ); /* obsluha udalosti od clientov */ - while( getBufferLine(client->buffer, line, STR_SIZE) >= 0 ) + while( getBufferLine(client->buffer, line, STR_PROTO_SIZE) >= 0 ) { #ifdef DEBUG_SERVER_RECV printf("recv client msg->%s", line); #endif if( strncmp(line, "hello", 5) == 0 )proto_recv_hello_server(client, line); - if( strncmp(line, "event", 5) == 0 )proto_recv_event_server(client, line); - if( strncmp(line, "context", 7) == 0 )proto_recv_context_server(client, line); - if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_server(client, line); - if( strncmp(line, "end", 3) == 0 )proto_recv_end_server(client, line); + if( strncmp(line, "status", 6) == 0 )proto_recv_status_server(client, line); + + if( client->tux != NULL ) + { + if( strncmp(line, "event", 5) == 0 )proto_recv_event_server(client, line); + if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_server(client, line); + if( strncmp(line, "end", 3) == 0 )proto_recv_end_server(client, line); + } } } @@ -456,7 +464,11 @@ void selectServerTcpSocket() if( thisClient->status == NET_STATUS_ZOMBIE ) { - proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient); + if( thisClient->tux != NULL ) + { + proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient); + } + delListItem(listClient, i, destroyClient); } } @@ -486,7 +498,11 @@ static void delZombieCLient() if( thisClient->status == NET_STATUS_ZOMBIE ) { - proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient); + if( thisClient->tux != NULL ) + { + proto_send_deltux_server(PROTO_SEND_ALL, NULL, thisClient); + } + delListItem(listClient, i, destroyClient); } } @@ -504,26 +520,33 @@ void eventPeriodicSyncClient() client_t *thisClientSend; tux_t *thisTux; int i, j; - - proto_send_ping_server(PROTO_SEND_ALL, NULL); -#ifndef BUBLIC_SERVER - proto_send_newtux_server(PROTO_SEND_ALL, NULL, - (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX])); -#endif for( i = 0 ; i < listClient->count; i++) { - thisClientInfo = (client_t *) listClient->list[i]; + thisClientSend = (client_t *) listClient->list[i]; + + if( thisClientSend->tux != NULL ) + { + proto_send_ping_server(PROTO_SEND_ONE, thisClientSend); + +#ifndef BUBLIC_SERVER + proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend, + (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX])); +#endif + } for( j = 0 ; j < listClient->count; j++) { - thisClientSend = (client_t *) listClient->list[j]; + thisClientInfo = (client_t *) listClient->list[j]; thisTux = thisClientInfo->tux; - if( thisClientSend != thisClientInfo && + if( thisTux != NULL && + thisClientSend->tux != NULL && + thisClientSend != thisClientInfo && thisTux->status == TUX_STATUS_ALIVE ) { - proto_send_newtux_server(PROTO_SEND_ONE, thisClientSend, thisTux); + proto_send_newtux_server(PROTO_SEND_ONE, + thisClientSend, thisTux); } } } @@ -575,6 +598,12 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) if( client == NULL ) { + if( listClient->count+1 > maxClients ) + { + destroySockUdp(sock_client); + return; + } + eventCreateNewUdpClient(sock_client); client = findUdpClient(sock_client); isCreateNewClient = TRUE; @@ -666,7 +695,7 @@ void selectServerSdlUdpSocket() { sock_sdl_udp_t *sock_client; client_t *client; - char buffer[STR_SIZE]; + char buffer[STR_PROTO_SIZE]; bool_t isCreateNewClient; int ret; @@ -678,9 +707,9 @@ void selectServerSdlUdpSocket() sock_client = newSdlSockUdp(); isCreateNewClient = FALSE; - memset(buffer, 0, STR_SIZE); + memset(buffer, 0, STR_PROTO_SIZE); - ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, buffer, STR_SIZE-1); + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, buffer, STR_PROTO_SIZE-1); if( ret < 0 ) { @@ -679,8 +679,8 @@ static void eventBonus(tux_t *tux) tux->bonus_time--; } - if( tux->bonus_time == 0 ) - { + if( tux->bonus_time == 0 && getNetTypeGame() != NET_GAME_TYPE_CLIENT ) + { if( tux->bonus == BONUS_GHOST ) { int x, y, w, h; |