diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-15 12:41:22 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-03-15 12:41:22 +0000 |
| commit | 6038f9434f1eb40caf7992ece9a3972e95a9fc11 (patch) | |
| tree | 6d99a66c5504ee894e15b215291f57e3e551a3e6 /src | |
| parent | d9e19a0d5ba8a797e12052e29f9c3a945d2331bc (diff) | |
- uprava par detialov v setovom mutiplayeri s portokolom UDP
( hlavne monznost synchronizacie dat od servera kazdnych 5 sekund )
git-svn-id: http://opensvn.csie.org/tuxanci_ng@17 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.c | 10 | ||||
| -rw-r--r-- | src/net_multiplayer.c | 2 | ||||
| -rw-r--r-- | src/server.c | 57 | ||||
| -rwxr-xr-x | src/tcp.c | 6 | ||||
| -rw-r--r-- | src/udp.c | 15 |
5 files changed, 80 insertions, 10 deletions
diff --git a/src/client.c b/src/client.c index dc9d324..5a96b00 100644 --- a/src/client.c +++ b/src/client.c @@ -23,6 +23,7 @@ static Uint32 lastPing; static void initClient() { clientBuffer = newBuffer( LIMIT_BUFFER ); + lastPing = SDL_GetTicks(); proto_send_hello_client(); } @@ -36,6 +37,7 @@ int initTcpClient(char *ip, int port) } printf("connect TCP %s %d\n", ip, port); + protocolType = NET_PROTOCOL_TYPE_TCP; initClient(); @@ -52,8 +54,8 @@ int initUdpClient(char *ip, int port) } printf("connect UDP %s %d\n", ip, port); + protocolType = NET_PROTOCOL_TYPE_UDP; - lastPing = SDL_GetTicks(); initClient(); return 0; @@ -138,7 +140,7 @@ void eventServerBuffer() while ( getBufferLine(clientBuffer, line, STR_SIZE) >= 0 ) { - //printf("dostal som %s", line); + printf("spracuvavam %s", line); if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line); if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line); @@ -222,7 +224,7 @@ void quitTcpClient() assert( sock_server_tcp != NULL ); closeTcpSocket(sock_server_tcp); - printf("quit conenct\n"); + printf("quit TCP conenct\n"); } void quitUdpClient() @@ -232,5 +234,5 @@ void quitUdpClient() assert( sock_server_udp != NULL ); closeUdpSocket(sock_server_udp); - printf("quit conenct\n"); + printf("quit UDP conenct\n"); } diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c index c8f8965..e3f5d93 100644 --- a/src/net_multiplayer.c +++ b/src/net_multiplayer.c @@ -105,10 +105,12 @@ void eventNetMultiplayer() case NET_PROTOCOL_TYPE_TCP : selectServerTcpSocket(); eventClientListBuffer(); + eventPeriodicSyncClient(); break; case NET_PROTOCOL_TYPE_UDP : selectServerUdpSocket(); eventClientListBuffer(); + eventPeriodicSyncClient(); break; } break; diff --git a/src/server.c b/src/server.c index 6ff8491..92d5eae 100644 --- a/src/server.c +++ b/src/server.c @@ -15,10 +15,12 @@ static int protocolType; static sock_tcp_t *sock_server_tcp; static sock_udp_t *sock_server_udp; static list_t *listClient; +static Uint32 lastSyncClient; void static initServer() { listClient = newList(); + lastSyncClient = SDL_GetTicks(); } int initTcpServer(int port) @@ -77,9 +79,13 @@ static client_t* newAnyClient() client_t* newTcpClient(sock_tcp_t *sock_tcp) { client_t *new; + char str_ip[STR_IP_SIZE]; assert( sock_tcp != NULL ); + getSockTcpIp(sock_tcp, str_ip); + printf("new client from %s:%d\n", str_ip, getSockTcpPort(sock_tcp)); + new = newAnyClient(); new->socket_tcp = sock_tcp; @@ -89,9 +95,12 @@ client_t* newTcpClient(sock_tcp_t *sock_tcp) client_t* newUdpClient(sock_udp_t *sock_udp) { client_t *new; - + char str_ip[STR_IP_SIZE]; + assert( sock_udp != NULL ); + getSockUdpIp(sock_udp, str_ip); + printf("new client from %s:%d\n", str_ip, getSockUdpPort(sock_udp)); new = newAnyClient(); new->socket_udp = sock_udp; @@ -186,9 +195,12 @@ static void eventCreateClient(client_t *client) item_t *thisItem; int i; + assert( client != NULL ); + proto_send_init_server(client); - proto_send_newtux_server(client, (tux_t *)(getWorldArena()->listTux->list[0]) ); + proto_send_newtux_server(client, + (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX]) ); for( i = 0 ; i < listClient->count; i++) { @@ -229,6 +241,7 @@ static void eventCreateNewUdpClient(sock_udp_t *socket_udp) client = newUdpClient( socket_udp ); addList(listClient, client); + eventCreateClient(client); } @@ -265,7 +278,7 @@ static void eventClientBuffer(client_t *client) while( getBufferLine(client->buffer, line, STR_SIZE) >= 0 ) { - //printf("dostal som %s", line); + printf("spracuvavam %s", line); if( strncmp(line, "hello", 5) == 0 )proto_recv_hello_server(client, line); if( strncmp(line, "event", 5) == 0 )proto_recv_event_server(client, line); @@ -347,7 +360,7 @@ static client_t* findUdpClient(sock_udp_t *sock_udp) { thisClient = (client_t *) listClient->list[i]; - if( thisClient->socket_udp->sockAddr.sin_port == sock_udp->sockAddr.sin_port ) + if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) ) { return thisClient; } @@ -387,11 +400,13 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) sock_udp_t *sock_client; client_t *client; char buffer[STR_SIZE]; + bool_t isCreateNewClient; int ret; assert( sock_server != NULL ); sock_client = newSockUdp(); + isCreateNewClient = FALSE; memset(buffer, 0, STR_SIZE); ret = readUdpSocket(sock_server, sock_client, buffer, STR_SIZE-1); @@ -402,6 +417,7 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) { eventCreateNewUdpClient(sock_client); client = findUdpClient(sock_client); + isCreateNewClient = TRUE; } if( client == NULL ) @@ -410,6 +426,11 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) return; } + if( isCreateNewClient == FALSE ) + { + destroySockUdp(sock_client); + } + if( ret <= 0 ) { client->status = NET_STATUS_ZOMBIE; @@ -446,6 +467,30 @@ void selectServerUdpSocket() } } +void eventPeriodicSyncClient() +{ + Uint32 currentTime; + + currentTime = SDL_GetTicks(); + + if( currentTime - lastSyncClient > SERVER_TIME_SYNC ) + { + client_t *thisClient; + int i; + + proto_send_newtux_server(NULL, + (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX])); + + for( i = 0 ; i < listClient->count; i++) + { + thisClient = (client_t *) listClient->list[i]; + proto_send_newtux_server(NULL, thisClient->tux); + } + + lastSyncClient = SDL_GetTicks(); + } +} + static void quitServer() { proto_send_end_server(); @@ -460,7 +505,7 @@ void quitTcpServer() assert( sock_server_tcp != NULL ); closeTcpSocket(sock_server_tcp); - printf("quit port\n"); + printf("quit TCP port\n"); } void quitUdpServer() @@ -470,5 +515,5 @@ void quitUdpServer() assert( sock_server_udp != NULL ); closeUdpSocket(sock_server_udp); - printf("quit port\n"); + printf("quit UDP port\n"); } @@ -95,6 +95,12 @@ void getSockTcpIp(sock_tcp_t *p, char *str_ip) strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); } +int getSockTcpPort(sock_tcp_t *p) +{ + assert( p != NULL ); + return htons(p->sockAddr.sin_port); +} + sock_tcp_t* connectTcpSocket(char *ip, int port) { sock_tcp_t *new; @@ -1,4 +1,5 @@ + #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> @@ -12,6 +13,8 @@ #define BUFSIZE 1000 +#include "main.h" + typedef struct struct_sock_udp_t { int sock; @@ -142,10 +145,22 @@ void getSockUdpIp(sock_udp_t *p, char *str_ip) strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); } +int getSockUdpPort(sock_udp_t *p) +{ + assert( p != NULL ); + return htons(p->sockAddr.sin_port); +} + + void closeUdpSocket(sock_udp_t *p) { + char str_ip[STR_IP_SIZE]; + assert( p != NULL ); + getSockUdpIp(p, str_ip); + printf("close connect from %s:%d\n", str_ip, htons(p->sockAddr.sin_port)); + close(p->sock); destroySockUdp(p); } |