summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororoborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-03-14 22:11:31 +0000
committeroroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-03-14 22:11:31 +0000
commitd9e19a0d5ba8a797e12052e29f9c3a945d2331bc (patch)
tree95e18dfbf1879b5edd973d81003a43955adb141b
parent593ac1e25c2053f06cb1b7e29785a695d3c57ad3 (diff)
- upravy v kode, lepsia synchronizacia
- clienty posielaju serveru "pingy", ak server isty cas ping od client anedostane tak ho dpoji ( nespojive UDP ) git-svn-id: http://opensvn.csie.org/tuxanci_ng@16 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
-rw-r--r--include/client.h3
-rw-r--r--include/proto.h6
-rw-r--r--include/server.h4
-rw-r--r--src/client.c23
-rw-r--r--src/net_multiplayer.c1
-rw-r--r--src/proto.c79
-rw-r--r--src/screen_world.c2
-rw-r--r--src/server.c15
-rw-r--r--src/tux.c29
-rw-r--r--src/udp.c1
10 files changed, 150 insertions, 13 deletions
diff --git a/include/client.h b/include/client.h
index 79887b1..979b5e8 100644
--- a/include/client.h
+++ b/include/client.h
@@ -3,11 +3,14 @@
#define MY_CLIENT
+#define CLIENT_TIMEOUT 2500
+
extern int initTcpClient(char *ip, int port);
extern int initUdpClient(char *ip, int port);
extern void sendServer(char *msg);
extern void eventServerBuffer();
extern void selectClientTcpSocket();
+extern void eventPingServer();
extern void selectClientUdpSocket();
extern void quitTcpClient();
extern void quitUdpClient();
diff --git a/include/proto.h b/include/proto.h
index a741148..7deb346 100644
--- a/include/proto.h
+++ b/include/proto.h
@@ -16,12 +16,18 @@ extern void proto_send_event_client(int action);
extern void proto_recv_event_server(client_t *client, char *msg);
extern void proto_send_newtux_server(client_t *client, tux_t *tux);
extern void proto_recv_newtux_client(char *msg);
+extern void proto_send_kill_server(tux_t *tux);
+extern void proto_recv_kill_client(char *msg);
+extern void proto_send_score_server(tux_t *tux);
+extern void proto_recv_score_client(char *msg);
extern void proto_send_deltux_server(client_t *client);
extern void proto_recv_deltux_client(char *msg);
extern void proto_send_additem_server(item_t *p);
extern void proto_recv_additem_client(char *msg);
extern void proto_send_context_client(tux_t *tux);
extern void proto_recv_context_server(client_t *client, char *msg);
+extern void proto_send_ping_client();
+extern void proto_recv_ping_server(client_t *client, char *msg);
extern void proto_send_end_client();
extern void proto_recv_end_server(client_t *client, char *msg);
extern void proto_send_end_server();
diff --git a/include/server.h b/include/server.h
index c7d4df9..328e2d3 100644
--- a/include/server.h
+++ b/include/server.h
@@ -9,12 +9,14 @@
#include "tcp.h"
#include "udp.h"
+#define SERVER_TIMEOUT 5000
+
typedef struct client_struct
{
sock_tcp_t *socket_tcp;
sock_udp_t *socket_udp;
int status;
-
+ Uint32 lastPing;
tux_t *tux;
buffer_t *buffer;
} client_t;
diff --git a/src/client.c b/src/client.c
index 530c0f3..dc9d324 100644
--- a/src/client.c
+++ b/src/client.c
@@ -18,6 +18,7 @@ static int protocolType;
static sock_tcp_t *sock_server_tcp;
static sock_udp_t *sock_server_udp;
static buffer_t *clientBuffer;
+static Uint32 lastPing;
static void initClient()
{
@@ -34,7 +35,7 @@ int initTcpClient(char *ip, int port)
return -1;
}
- printf("connect %s %d\n", ip, port);
+ printf("connect TCP %s %d\n", ip, port);
protocolType = NET_PROTOCOL_TYPE_TCP;
initClient();
@@ -50,8 +51,9 @@ int initUdpClient(char *ip, int port)
return -1;
}
- printf("connect %s %d\n", ip, port);
+ printf("connect UDP %s %d\n", ip, port);
protocolType = NET_PROTOCOL_TYPE_UDP;
+ lastPing = SDL_GetTicks();
initClient();
return 0;
@@ -143,6 +145,8 @@ void eventServerBuffer()
if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line);
if( strncmp(line, "deltux", 6) == 0 )proto_recv_deltux_client(line);
if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line);
+ if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line);
+ if( strncmp(line, "score", 5) == 0 )proto_recv_score_client(line);
if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line);
}
}
@@ -168,12 +172,27 @@ void selectClientTcpSocket()
}
}
+void eventPingServer()
+{
+ Uint32 currentTime;
+
+ currentTime = SDL_GetTicks();
+
+ if( currentTime - lastPing > CLIENT_TIMEOUT )
+ {
+ proto_send_ping_client();
+ lastPing = SDL_GetTicks();
+ }
+}
+
void selectClientUdpSocket()
{
fd_set readfds;
struct timeval tv;
int max_fd;
+ eventPingServer();
+
tv.tv_sec = 0;
tv.tv_usec = 0;
diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c
index cbf9cb8..c8f8965 100644
--- a/src/net_multiplayer.c
+++ b/src/net_multiplayer.c
@@ -121,6 +121,7 @@ void eventNetMultiplayer()
eventServerBuffer();
break;
case NET_PROTOCOL_TYPE_UDP :
+ eventPingServer();
selectClientUdpSocket();
eventServerBuffer();
break;
diff --git a/src/proto.c b/src/proto.c
index 2c2c5c4..ddfc51c 100644
--- a/src/proto.c
+++ b/src/proto.c
@@ -193,6 +193,66 @@ void proto_recv_newtux_client(char *msg)
tux->status = TUX_STATUS_ALIVE;
}
+void proto_send_kill_server(tux_t *tux)
+{
+ char msg[STR_SIZE];
+
+ assert( tux != NULL );
+
+ sprintf(msg, "kill %d\n", tux->id);
+
+ sendAllClient(msg);
+}
+
+void proto_recv_kill_client(char *msg)
+{
+ char cmd[STR_SIZE];
+ int id;
+ tux_t *tux;
+
+ assert( msg != NULL );
+
+ sscanf(msg, "%s %d", cmd, &id);
+
+ tux = getTuxID(getWorldArena()->listTux, id);
+
+ if( tux != NULL )
+ {
+ eventTuxIsDead(tux);
+ }
+}
+
+void proto_send_score_server(tux_t *tux)
+{
+ char msg[STR_SIZE];
+
+ assert( tux != NULL );
+
+ sprintf(msg, "score %d %d\n", tux->id, tux->score);
+
+ sendAllClient(msg);
+}
+
+void proto_recv_score_client(char *msg)
+{
+ char cmd[STR_SIZE];
+ int id, score;
+ tux_t *tux;
+
+ assert( msg != NULL );
+
+ sscanf(msg, "%s %d %d", cmd, &id, &score);
+
+ tux = getTuxID(getWorldArena()->listTux, id);
+
+ if( tux != NULL )
+ {
+ tux->score = score;
+ }
+
+ countRoundInc();
+}
+
void proto_send_deltux_server(client_t *client)
{
char msg[STR_SIZE];
@@ -293,19 +353,34 @@ void proto_recv_context_server(client_t *client, char *msg)
proto_send_newtux_server(NULL, client->tux);
}
+void proto_send_ping_client()
+{
+ char msg[STR_SIZE];
+ strcpy(msg, "ping\n");
+ sendServer(msg);
+}
+
+void proto_recv_ping_server(client_t *client, char *msg)
+{
+ assert( msg != NULL );
+ assert( client != NULL );
+
+ client->lastPing = SDL_GetTicks();
+}
+
void proto_send_end_client()
{
char msg[STR_SIZE];
strcpy(msg, "end\n");
sendServer(msg);
- printf("proto_send_end_client\n");
}
void proto_recv_end_server(client_t *client, char *msg)
{
assert( msg != NULL );
+ assert( client != NULL );
+
client->status = NET_STATUS_ZOMBIE;
- printf("proto_recv_end_server\n");
}
void proto_send_end_server()
diff --git a/src/screen_world.c b/src/screen_world.c
index 068ac08..fb9a559 100644
--- a/src/screen_world.c
+++ b/src/screen_world.c
@@ -104,9 +104,11 @@ void countRoundInc()
}
count++;
+ printf("count %d\n", count);
if( count >= max_count )
{
+ printf("count %d ending\n", count);
playSound("end", SOUND_GROUP_BASE);
addTimer(timer_endArena, NULL, TIMER_END_ARENA);
}
diff --git a/src/server.c b/src/server.c
index 419990d..6ff8491 100644
--- a/src/server.c
+++ b/src/server.c
@@ -34,7 +34,7 @@ int initTcpServer(int port)
initServer();
- printf("server listen port %d\n", port);
+ printf("server listen TCP port %d\n", port);
return 0;
}
@@ -52,7 +52,7 @@ int initUdpServer(int port)
initServer();
- printf("server listen port %d\n", port);
+ printf("server listen UDP port %d\n", port);
return 0;
}
@@ -68,6 +68,7 @@ static client_t* newAnyClient()
new->buffer = newBuffer(LIMIT_BUFFER);
new->tux = newTux();
new->tux->control = TUX_CONTROL_NET;
+ new->lastPing = SDL_GetTicks();
addList(getWorldArena()->listTux, new->tux);
return new;
@@ -269,6 +270,7 @@ static void eventClientBuffer(client_t *client)
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);
}
}
@@ -357,12 +359,21 @@ static client_t* findUdpClient(sock_udp_t *sock_udp)
static void delZombieCLient()
{
client_t *thisClient;
+ Uint32 currentTime;
int i;
+ currentTime = SDL_GetTicks();
+
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
+ if( currentTime - thisClient->lastPing > SERVER_TIMEOUT )
+ {
+ printf("client ping timeout\n");
+ thisClient->status = NET_STATUS_ZOMBIE;
+ }
+
if( thisClient->status == NET_STATUS_ZOMBIE )
{
proto_send_deltux_server(thisClient);
diff --git a/src/tux.c b/src/tux.c
index a39c3d2..b007a93 100644
--- a/src/tux.c
+++ b/src/tux.c
@@ -346,7 +346,8 @@ void eventTuxIsDead(tux_t *tux)
return;
}
- playSound("dead", SOUND_GROUP_BASE);
+ playSound("dead", SOUND_GROUP_BASE);
+ printf("tux id %d is dead\n", tux->id);
tux->status = TUX_STATUS_DEAD;
memset(tux->shot, 0, sizeof(int) * GUN_COUNT);
@@ -364,11 +365,22 @@ void eventTuxIsDead(tux_t *tux)
{
addTimer(timer_spawnTux, newInt(tux->id), TUX_TIME_SPAWN );
}
+
+ if( getNetTypeGame() == NET_GAME_TYPE_SERVER )
+ {
+ proto_send_kill_server(tux);
+ }
}
static void eventTuxIsDeadWIthShot(tux_t *tux, shot_t *shot)
{
shot->author->score++;
+
+ if( getNetTypeGame() == NET_GAME_TYPE_SERVER )
+ {
+ proto_send_score_server(shot->author);
+ }
+
countRoundInc();
eventTuxIsDead(tux);
}
@@ -378,10 +390,14 @@ void tuxTeleport(tux_t *tux)
int x, y;
playSound("teleport", SOUND_GROUP_BASE);
+ printf("tux id %d teleporting\n", tux->id);
+
+ if( getNetTypeGame() != NET_GAME_TYPE_CLIENT )
+ {
+ findFreeSpace(&x, &y, TUX_WIDTH, TUX_HEIGHT);
+ setTuxProportion(tux, x, y);
+ }
- findFreeSpace(&x, &y, TUX_WIDTH, TUX_HEIGHT);
- setTuxProportion(tux, x, y);
-
if( getNetTypeGame() == NET_GAME_TYPE_SERVER )
{
proto_send_newtux_server(NULL, tux);
@@ -420,7 +436,10 @@ void eventConflictTuxWithShot(list_t *listTux, list_t *listShot)
continue;
}
- eventTuxIsDeadWIthShot(thisTux, thisShot);
+ if( getNetTypeGame() != NET_GAME_TYPE_CLIENT )
+ {
+ eventTuxIsDeadWIthShot(thisTux, thisShot);
+ }
}
delListItem(listShot, i, destroyShot);
diff --git a/src/udp.c b/src/udp.c
index 0f86dc6..ee8a216 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -62,7 +62,6 @@ sock_udp_t* bindUdpSocket(int port)
return NULL;
}
-
return new;
}