summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/server.h34
-rw-r--r--include/string_length.h1
-rw-r--r--include/tcp.h1
-rw-r--r--include/udp.h1
-rw-r--r--src/client.c10
-rw-r--r--src/net_multiplayer.c2
-rw-r--r--src/server.c57
-rwxr-xr-xsrc/tcp.c6
-rw-r--r--src/udp.c15
9 files changed, 103 insertions, 24 deletions
diff --git a/include/server.h b/include/server.h
index 328e2d3..c44b86b 100644
--- a/include/server.h
+++ b/include/server.h
@@ -9,7 +9,11 @@
#include "tcp.h"
#include "udp.h"
-#define SERVER_TIMEOUT 5000
+#define SERVER_TIMEOUT 5000
+#define SERVER_TIME_SYNC 5000
+
+#define SERVER_INDEX_ROOT_TUX 0
+
typedef struct client_struct
{
@@ -21,18 +25,20 @@ typedef struct client_struct
buffer_t *buffer;
} client_t;
-int initTcpServer(int port);
-int initUdpServer(int port);
-client_t* newTcpClient(sock_tcp_t *sock_tcp);
-client_t* newUdpClient(sock_udp_t *sock_udp);
-void destroyClient(client_t *p);
-void sendClient(client_t *p, char *msg);
-void sendAllClientBut(char *msg, client_t *p);
-void sendAllClient(char *msg);
-void eventClientListBuffer();
-void selectServerTcpSocket();
-void selectServerUdpSocket();
-void quitTcpServer();
-void quitUdpServer();
+extern int initTcpServer(int port);
+extern int initUdpServer(int port);
+extern client_t* newTcpClient(sock_tcp_t *sock_tcp);
+extern client_t* newUdpClient(sock_udp_t *sock_udp);
+extern void destroyClient(client_t *p);
+extern void sendClient(client_t *p, char *msg);
+extern void sendAllClientBut(char *msg, client_t *p);
+extern void sendAllClient(char *msg);
+extern void eventClientListBuffer();
+extern void selectServerTcpSocket();
+extern void selectServerUdpSocket();
+extern void eventPeriodicSyncClient();
+extern void quitTcpServer();
+extern void quitUdpServer();
+
#endif
diff --git a/include/string_length.h b/include/string_length.h
index b16271d..693af5b 100644
--- a/include/string_length.h
+++ b/include/string_length.h
@@ -7,5 +7,6 @@
#define STR_NUM_SIZE 8
#define STR_NAME_SIZE 32
#define STR_PATH_SIZE 4096
+#define STR_IP_SIZE 24
#endif
diff --git a/include/tcp.h b/include/tcp.h
index 03a7ec7..d33f0b0 100644
--- a/include/tcp.h
+++ b/include/tcp.h
@@ -16,6 +16,7 @@ extern void destroySockTcp(sock_tcp_t *p);
extern sock_tcp_t* bindTcpSocket(int port);
extern sock_tcp_t* getTcpNewClient(sock_tcp_t *p);
extern void getSockTcpIp(sock_tcp_t *p, char *str_ip);
+extern int getSockTcpPort(sock_tcp_t *p);
extern sock_tcp_t* connectTcpSocket(char *ip, int port);
extern int readTcpSocket(sock_tcp_t *p, void *address, int len);
extern int writeTcpSocket(sock_tcp_t *p, void *address, int len);
diff --git a/include/udp.h b/include/udp.h
index 8619805..8c63e10 100644
--- a/include/udp.h
+++ b/include/udp.h
@@ -18,6 +18,7 @@ extern sock_udp_t* connectUdpSocket(char *address, int port);
extern int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len);
extern int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len);
extern void getSockUdpIp(sock_udp_t *p, char *str_ip);
+extern int getSockUdpPort(sock_udp_t *p);
extern void closeUdpSocket(sock_udp_t *p);
#endif
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");
}
diff --git a/src/tcp.c b/src/tcp.c
index 4558095..87b548b 100755
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -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;
diff --git a/src/udp.c b/src/udp.c
index ee8a216..58e7637 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -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);
}