summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/Makefile9
-rwxr-xr-xsrc/Makefile-publicServer2
-rw-r--r--src/client.c126
-rw-r--r--src/net_multiplayer.c171
-rw-r--r--src/proto.c14
-rw-r--r--src/sdl_udp.c209
-rw-r--r--src/server.c321
7 files changed, 690 insertions, 162 deletions
diff --git a/src/Makefile b/src/Makefile
index 2ecb449..11f664f 100755
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,10 +2,10 @@
#CC = /usr/local/gcc/bin/gcc
CC = gcc
-CFLAGS = -g -O0 -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
+CFLAGS = -g -O0 -DSUPPORT_NET_SDL_UDP -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
#CFLAGS = -O2 -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
-LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer
+LIBS = `sdl-config --libs` -lSDL_image -lSDL_ttf -lSDL_mixer -lSDL_net
FILES = interface.o font.o list.o image.o layer.o director.o configFile.o tux.o main.o \
screen.o screen_world.o wall.o shot.o myTimer.o panel.o net_multiplayer.o network.o \
@@ -13,7 +13,7 @@ FILES = interface.o font.o list.o image.o layer.o director.o configFile.o tux.o
item.o widget_label.o screen_mainMenu.o widget_button.o screen_analyze.o widget_textfield.o \
widget_check.o widget_image.o screen_setting.o screen_gameType.o screen_choiceArena.o \
widget_buttonimage.o screen_credits.o teleport.o pipe.o homeDirector.o screen_table.o \
- tcp.o udp.o proto.o server.o client.o
+ tcp.o udp.o sdl_udp.o proto.o server.o client.o
tuxanci-ng: $(FILES)
$(CC) $(CFLAGS) $(LIBS) -o tuxanci-ng $(FILES)
@@ -72,6 +72,9 @@ tcp.o: tcp.c ../include/tcp.h
udp.o: udp.c ../include/udp.h
$(CC) $(CFLAGS) -o udp.o -c udp.c
+sdl_udp.o: sdl_udp.c ../include/sdl_udp.h
+ $(CC) $(CFLAGS) -o sdl_udp.o -c sdl_udp.c
+
proto.o: proto.c ../include/proto.h
$(CC) $(CFLAGS) -o proto.o -c proto.c
diff --git a/src/Makefile-publicServer b/src/Makefile-publicServer
index bb4b12b..243399f 100755
--- a/src/Makefile-publicServer
+++ b/src/Makefile-publicServer
@@ -4,7 +4,7 @@ CC = gcc
DISTDIR:=/usr/
-CFLAGS = -g -O0 -DBUBLIC_SERVER -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
+CFLAGS = -g -O0 -DBUBLIC_SERVER -DSUPPORT_NET_UNIX_UDP -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
#CFLAGS = -O2 -DDISTDIR=\"$(DISTDIR)\" `sdl-config --cflags` -I../include -Wall
LIBS =
diff --git a/src/client.c b/src/client.c
index 4992b40..a238373 100644
--- a/src/client.c
+++ b/src/client.c
@@ -9,14 +9,26 @@
#include "buffer.h"
#include "net_multiplayer.h"
#include "screen_world.h"
-#include "tcp.h"
-#include "udp.h"
#include "proto.h"
#include "client.h"
static int protocolType;
+
+#ifdef SUPPORT_NET_UNIX_TCP
+#include "tcp.h"
static sock_tcp_t *sock_server_tcp;
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+#include "udp.h"
static sock_udp_t *sock_server_udp;
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+#include "sdl_udp.h"
+static sock_sdl_udp_t *sock_server_sdl_udp;
+#endif
+
static buffer_t *clientBuffer;
static my_time_t lastPing;
@@ -27,6 +39,8 @@ static void initClient()
proto_send_hello_client();
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
int initTcpClient(char *ip, int port)
{
sock_server_tcp = connectTcpSocket(ip, port);
@@ -44,6 +58,10 @@ int initTcpClient(char *ip, int port)
return 0;
}
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
int initUdpClient(char *ip, int port)
{
sock_server_udp = connectUdpSocket(ip, port);
@@ -61,21 +79,46 @@ int initUdpClient(char *ip, int port)
return 0;
}
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+
+int initSdlUdpClient(char *ip, int port)
+{
+ sock_server_sdl_udp = connectSdlUdpSocket(ip, port);
+
+ if( sock_server_sdl_udp == NULL )
+ {
+ return -1;
+ }
+
+ printf("connect UDP %s %d\n", ip, port);
+
+ protocolType = NET_PROTOCOL_TYPE_UDP;
+ initClient();
+
+ return 0;
+}
+
+#endif
+
void sendServer(char *msg)
{
int ret;
assert( msg != NULL );
- if( protocolType == NET_PROTOCOL_TYPE_TCP )
- {
- ret = writeTcpSocket(sock_server_tcp, msg, strlen(msg));
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ ret = writeTcpSocket(sock_server_tcp, msg, strlen(msg));
+#endif
- if( protocolType == NET_PROTOCOL_TYPE_UDP )
- {
- ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
- }
+#ifdef SUPPORT_NET_UNIX_UDP
+ ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+ ret = writeSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, msg, strlen(msg));
+#endif
if ( ret == 0 )
{
@@ -97,16 +140,22 @@ static void eventServerSelect()
memset(buffer,0 ,STR_SIZE);
- if( protocolType == NET_PROTOCOL_TYPE_TCP )
- {
- ret = readTcpSocket(sock_server_tcp, buffer, STR_SIZE-1);
+#ifdef SUPPORT_NET_UNIX_TCP
+ ret = readTcpSocket(sock_server_tcp, buffer, STR_SIZE-1);
+#endif
- }
+#ifdef SUPPORT_NET_UNIX_UDP
+ ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_SIZE-1);
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+ ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_SIZE-1);
- if( protocolType == NET_PROTOCOL_TYPE_UDP )
+ if( ret < 0 )
{
- ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_SIZE-1);
+ return;
}
+#endif
if( ret == 0 )
{
@@ -153,6 +202,8 @@ void eventServerBuffer()
}
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
void selectClientTcpSocket()
{
fd_set readfds;
@@ -174,6 +225,10 @@ void selectClientTcpSocket()
}
}
+#endif
+
+#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP
+
void eventPingServer()
{
my_time_t currentTime;
@@ -187,14 +242,16 @@ void eventPingServer()
}
}
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
void selectClientUdpSocket()
{
fd_set readfds;
struct timeval tv;
int max_fd;
- eventPingServer();
-
tv.tv_sec = 0;
tv.tv_usec = 0;
@@ -210,6 +267,17 @@ void selectClientUdpSocket()
}
}
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+
+void selectClientSdlUdpSocket()
+{
+ eventServerSelect();
+}
+
+#endif
+
static void quitClient()
{
proto_send_end_client();
@@ -217,6 +285,8 @@ static void quitClient()
destroyBuffer(clientBuffer);
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
void quitTcpClient()
{
quitClient();
@@ -227,6 +297,10 @@ void quitTcpClient()
printf("quit TCP conenct\n");
}
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
void quitUdpClient()
{
quitClient();
@@ -236,3 +310,19 @@ void quitUdpClient()
printf("quit UDP conenct\n");
}
+
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+
+void quitSdlUdpClient()
+{
+ quitClient();
+
+ assert( sock_server_sdl_udp != NULL );
+ closeSdlUdpSocket(sock_server_sdl_udp);
+
+ printf("quit UDP conenct\n");
+}
+
+#endif
diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c
index 410ee99..84cb28d 100644
--- a/src/net_multiplayer.c
+++ b/src/net_multiplayer.c
@@ -15,6 +15,7 @@
#include "server.h"
#include "tcp.h"
#include "udp.h"
+#include "sdl_udp.h"
#ifndef BUBLIC_SERVER
#include "screen.h"
@@ -41,49 +42,58 @@ int initNetMuliplayer(int type, char *ip, int port)
break;
case NET_GAME_TYPE_SERVER :
- switch( netProtoType )
+#ifdef SUPPORT_NET_UNIX_TCP
+ if( initTcpServer(port) != 0 )
{
- case NET_PROTOCOL_TYPE_TCP :
- if( initTcpServer(port) != 0 )
- {
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
- }
- break;
- case NET_PROTOCOL_TYPE_UDP :
- if( initUdpServer(port) != 0 )
- {
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
- }
- break;
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
}
-
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ if( initUdpServer(port) != 0 )
+ {
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
+ }
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ if( initSdlUdpServer(port) != 0 )
+ {
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
+ }
+#endif
break;
#ifndef BUBLIC_SERVER
case NET_GAME_TYPE_CLIENT :
- switch( netProtoType )
+#ifdef SUPPORT_NET_UNIX_TCP
+ if( initTcpClient(ip, port) != 0 )
+ {
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
+ }
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ if( initUdpClient(ip, port) != 0 )
{
- case NET_PROTOCOL_TYPE_TCP :
- if( initTcpClient(ip, port) != 0 )
- {
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
- }
- break;
- case NET_PROTOCOL_TYPE_UDP :
- if( initUdpClient(ip, port) != 0 )
- {
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
- }
- break;
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
}
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ if( initSdlUdpClient(ip, port) != 0 )
+ {
+ fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
+ netGameType = NET_GAME_TYPE_NONE;
+ return -1;
+ }
+#endif
break;
#endif
default :
@@ -102,37 +112,40 @@ void eventNetMultiplayer()
break;
case NET_GAME_TYPE_SERVER :
- switch( netProtoType )
- {
- case NET_PROTOCOL_TYPE_TCP :
- selectServerTcpSocket();
- eventClientListBuffer();
- eventPeriodicSyncClient();
- break;
- case NET_PROTOCOL_TYPE_UDP :
- selectServerUdpSocket();
- eventClientListBuffer();
- eventPeriodicSyncClient();
- break;
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ selectServerTcpSocket();
+ eventClientListBuffer();
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ selectServerUdpSocket();
+ eventClientListBuffer();
+ eventPeriodicSyncClient();
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ selectServerSdlUdpSocket();
+ eventClientListBuffer();
+ eventPeriodicSyncClient();
+#endif
break;
#ifndef BUBLIC_SERVER
case NET_GAME_TYPE_CLIENT :
- switch( netProtoType )
- {
- case NET_PROTOCOL_TYPE_TCP :
- selectClientTcpSocket();
- eventServerBuffer();
- break;
- case NET_PROTOCOL_TYPE_UDP :
- eventPingServer();
- selectClientUdpSocket();
- eventServerBuffer();
- break;
- }
- break;
+#ifdef SUPPORT_NET_UNIX_TCP
+ selectClientTcpSocket();
+ eventServerBuffer();
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ eventPingServer();
+ selectClientUdpSocket();
+ eventServerBuffer();
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ eventPingServer();
+ selectClientSdlUdpSocket();
+ eventServerBuffer();
#endif
+#endif
+ break;
default :
assert( ! "Premenna netGameType ma zlu hodnotu !" );
break;
@@ -147,28 +160,28 @@ void quitNetMultiplayer()
break;
case NET_GAME_TYPE_SERVER :
- switch( netProtoType )
- {
- case NET_PROTOCOL_TYPE_TCP :
- quitTcpServer();
- break;
- case NET_PROTOCOL_TYPE_UDP :
- quitUdpServer();
- break;
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ quitTcpServer();
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ quitUdpServer();
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ quitSdlUdpServer();
+#endif
break;
#ifndef BUBLIC_SERVER
case NET_GAME_TYPE_CLIENT :
- switch( netProtoType )
- {
- case NET_PROTOCOL_TYPE_TCP :
- quitTcpClient();
- break;
- case NET_PROTOCOL_TYPE_UDP :
- quitUdpClient();
- break;
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ quitTcpClient();
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
+ quitUdpClient();
+#endif
+#ifdef SUPPORT_NET_SDL_UDP
+ quitSdlUdpClient();
+#endif
break;
#endif
diff --git a/src/proto.c b/src/proto.c
index 01a4ad2..a852eb9 100644
--- a/src/proto.c
+++ b/src/proto.c
@@ -158,8 +158,8 @@ void proto_send_newtux_server(client_t *client, tux_t *tux)
assert( tux != NULL );
- sprintf(msg, "newtux %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n",
- tux->id ,tux->x, tux->y, tux->position ,tux->frame, tux->score, tux->name,
+ sprintf(msg, "newtux %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d\n",
+ tux->id ,tux->x, tux->y, tux->status, tux->position ,tux->frame, tux->score, tux->name,
tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE],
tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY], tux->shot[GUN_LASSER],
tux->shot[GUN_MINE], tux->shot[GUN_BOMBBALL],
@@ -181,7 +181,7 @@ void proto_recv_newtux_client(char *msg)
{
char cmd[STR_SIZE];
char name[STR_NAME_SIZE];
- int id, x, y, position, frame, score;
+ int id, x, y, status, position, frame, score;
int myGun, myBonus;
int gun1, gun2, gun3, gun4, gun5, gun6, gun7;
int time1, time2;
@@ -189,8 +189,8 @@ void proto_recv_newtux_client(char *msg)
assert( msg != NULL );
- sscanf(msg, "%s %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d",
- cmd, &id, &x, &y, &position, &frame, &score, name,
+ sscanf(msg, "%s %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d",
+ cmd, &id, &x, &y, &status, &position, &frame, &score, name,
&myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2);
tux = getTuxID(getWorldArena()->listTux, id);
@@ -205,6 +205,7 @@ void proto_recv_newtux_client(char *msg)
tux->id = id;
tux->x = x;
tux->y = y;
+ tux->status = status;
tux->position = position;
tux->frame = frame;
tux->score = score;
@@ -220,7 +221,6 @@ void proto_recv_newtux_client(char *msg)
tux->shot[GUN_BOMBBALL] = gun7;
tux->bonus_time = time1;
tux->pickup_time = time2;
- tux->status = TUX_STATUS_ALIVE;
}
#endif
@@ -421,7 +421,9 @@ void proto_recv_ping_server(client_t *client, char *msg)
assert( msg != NULL );
assert( client != NULL );
+#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP
client->lastPing = getMyTime();
+#endif
}
#ifndef BUBLIC_SERVER
diff --git a/src/sdl_udp.c b/src/sdl_udp.c
new file mode 100644
index 0000000..a8eae7a
--- /dev/null
+++ b/src/sdl_udp.c
@@ -0,0 +1,209 @@
+
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include "sdl_udp.h"
+
+#define BUFSIZE 1000
+
+
+sock_sdl_udp_t* newSdlSockUdp()
+{
+ sock_sdl_udp_t *new;
+
+ new = malloc( sizeof(sock_sdl_udp_t) );
+ memset(new, 0, sizeof(sock_sdl_udp_t));
+
+ if( !( new->packet = SDLNet_AllocPacket(512) ) )
+ {
+ fprintf(stderr, "nemozem alokovat pamat pre packet !\n");
+ destroySockSdlUdp(new);
+ }
+
+ return new;
+}
+
+void destroySockSdlUdp(sock_sdl_udp_t *p)
+{
+ assert( p != NULL );
+
+ SDLNet_FreePacket(p->packet);
+ free(p);
+}
+
+sock_sdl_udp_t* bindSdlUdpSocket(int port)
+{
+ sock_sdl_udp_t *new;
+
+ assert( port > 0 && port < 65535 );
+
+ new = newSdlSockUdp();
+
+ assert( new != NULL );
+
+ if( SDLNet_Init() < 0 )
+ {
+ fprintf(stderr, "nemozem initcializovat SDLnet !\n");
+ destroySockSdlUdp(new);
+ return NULL;
+ }
+
+ if( !( new->sock = SDLNet_UDP_Open(port) ) )
+ {
+ fprintf(stderr, "nemozem otovrit socket !\n");
+ destroySockSdlUdp(new);
+ return NULL;
+ }
+
+ new->type = SDL_UDP_SERVER;
+
+ return new;
+}
+
+sock_sdl_udp_t* connectSdlUdpSocket(char *address, int port)
+{
+ sock_sdl_udp_t *new;
+
+ assert( address != NULL );
+ assert( port > 0 && port < 65536 );
+
+ new = newSdlSockUdp();
+
+ assert( new != NULL );
+
+ /* Initialize SDL_net */
+ if( SDLNet_Init() < 0 )
+ {
+ fprintf(stderr, "nemozem initcializovat SDLnet !\n");
+ destroySockSdlUdp(new);
+ return NULL;
+ }
+
+ /* Open a socket on random port */
+ if( !( new->sock = SDLNet_UDP_Open(0) ) )
+ {
+ fprintf(stderr, "nemozem otovrit socket !\n");
+ destroySockSdlUdp(new);
+ return NULL;
+ }
+
+ /* Resolve server name */
+ if( SDLNet_ResolveHost(&(new->srvadd), address, port) )
+ {
+ fprintf(stderr, "nemozem sa pripojit na sokcet !\n");
+ destroySockSdlUdp(new);
+ return NULL;
+ }
+
+ new->type = SDL_UDP_CLIENT;
+
+ return new;
+}
+
+int readSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len)
+{
+ int x;
+
+ if( SDLNet_UDP_Recv(src->sock, dst->packet) )
+ {
+ x = (dst->packet->len > len ? len : dst->packet->len);
+ memcpy(address, dst->packet->data, x);
+ return x;
+ }
+
+ return -1;
+}
+
+int writeSdlUdpSocket(sock_sdl_udp_t *src, sock_sdl_udp_t *dst, void *address, int len)
+{
+ assert( src != NULL );
+ assert( address != NULL );
+
+ memcpy(dst->packet->data, address, len);
+ dst->packet->len = len;
+
+ if( src->type == SDL_UDP_CLIENT )
+ {
+ dst->packet->address.host = src->srvadd.host;
+ dst->packet->address.port = src->srvadd.port;
+ }
+
+ SDLNet_UDP_Send(src->sock, -1, dst->packet);
+
+ return len;
+}
+
+int getSockSdlUdpPort(sock_sdl_udp_t *p)
+{
+ assert( p != NULL );
+
+ return p->packet->address.port;
+}
+
+
+void closeSdlUdpSocket(sock_sdl_udp_t *p)
+{
+ assert( p != NULL );
+
+ SDLNet_UDP_Close(p->sock);
+
+ printf("close connect\n");
+
+ destroySockSdlUdp(p);
+}
+
+#if 0
+int main(int argc, char *argv[])
+{
+ sock_sdl_udp_t *sock;
+ char buf[BUFSIZE];
+ int ret;
+
+ if( strcmp(argv[1], "s") == 0 )
+ {
+ sock_sdl_udp_t *cli;
+
+ sock = bindSdlUdpSocket( atoi(argv[2]) );
+ cli = newSdlSockUdp();
+
+ while(1)
+ {
+ memset(buf, 0, BUFSIZE);
+
+ ret = readSdlUdpSocket(sock, cli, buf, BUFSIZE);
+
+ if( ret > 0 )
+ {
+ writeSdlUdpSocket(sock, cli, buf, ret);
+ }
+ }
+ }
+
+ if( strcmp(argv[1], "c") == 0 )
+ {
+ sock = connectSdlUdpSocket("127.0.0.1", atoi(argv[2]));
+
+ strcpy(buf, "Hello world !\n");
+ writeSdlUdpSocket(sock, sock, buf, strlen(buf));
+
+ while(1)
+ {
+ memset(buf, 0, BUFSIZE);
+ ret = readSdlUdpSocket(sock, sock, buf, BUFSIZE);
+
+ if( ret > 0 )
+ {
+ printf("buf = %s", buf);
+ }
+ }
+
+ closeSdlUdpSocket(sock);
+ }
+
+ return 0;
+}
+
+#endif
diff --git a/src/server.c b/src/server.c
index 6e3ec99..39aaca6 100644
--- a/src/server.c
+++ b/src/server.c
@@ -3,8 +3,6 @@
#include "tux.h"
#include "network.h"
#include "buffer.h"
-#include "tcp.h"
-#include "udp.h"
#include "proto.h"
#include "server.h"
#include "assert.h"
@@ -20,8 +18,22 @@
#endif
static int protocolType;
+
+#ifdef SUPPORT_NET_UNIX_TCP
+#include "tcp.h"
static sock_tcp_t *sock_server_tcp;
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+#include "udp.h"
static sock_udp_t *sock_server_udp;
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+#include "sdl_udp.h"
+static sock_sdl_udp_t *sock_server_sdl_udp;
+#endif
+
static list_t *listClient;
static my_time_t lastSyncClient;
@@ -31,6 +43,7 @@ void static initServer()
lastSyncClient = getMyTime();
}
+#ifdef SUPPORT_NET_UNIX_TCP
int initTcpServer(int port)
{
protocolType = NET_PROTOCOL_TYPE_TCP;
@@ -48,7 +61,9 @@ int initTcpServer(int port)
return 0;
}
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
int initUdpServer(int port)
{
protocolType = NET_PROTOCOL_TYPE_UDP;
@@ -66,6 +81,27 @@ int initUdpServer(int port)
return 0;
}
+#endif
+
+#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 )
+ {
+ return -1;
+ }
+
+ initServer();
+
+ printf("server listen UDP port %d\n", port);
+
+ return 0;
+}
+#endif
static client_t* newAnyClient()
{
@@ -78,12 +114,12 @@ static client_t* newAnyClient()
new->buffer = newBuffer(LIMIT_BUFFER);
new->tux = newTux();
new->tux->control = TUX_CONTROL_NET;
- new->lastPing = getMyTime();
addList(getWorldArena()->listTux, new->tux);
return new;
}
+#ifdef SUPPORT_NET_UNIX_TCP
client_t* newTcpClient(sock_tcp_t *sock_tcp)
{
client_t *new;
@@ -99,21 +135,39 @@ client_t* newTcpClient(sock_tcp_t *sock_tcp)
return new;
}
+#endif
+#ifdef SUPPORT_NET_UNIX_UDP
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));
+ printf("new client from %d\n", getSockUdpPort(sock_udp));
new = newAnyClient();
+ new->lastPing = getMyTime();
new->socket_udp = sock_udp;
return new;
}
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+client_t* newSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp)
+{
+ client_t *new;
+
+ assert( sock_sdl_udp != NULL );
+
+ printf("new client from %d\n", getSockSdlUdpPort(sock_sdl_udp));
+ new = newAnyClient();
+ new->lastPing = getMyTime();
+ new->socket_sdl_udp = sock_sdl_udp;
+
+ return new;
+}
+#endif
void destroyClient(client_t *p)
{
@@ -121,15 +175,17 @@ void destroyClient(client_t *p)
assert( p != NULL );
- if( p->socket_tcp != NULL )
- {
- closeTcpSocket(p->socket_tcp);
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ closeTcpSocket(p->socket_tcp);
+#endif
- if( p->socket_udp != NULL )
- {
- closeUdpSocket(p->socket_udp);
- }
+#ifdef SUPPORT_NET_UNIX_UDP
+ closeUdpSocket(p->socket_udp);
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+ closeSdlUdpSocket(p->socket_sdl_udp);
+#endif
destroyBuffer(p->buffer);
index = searchListItem(getWorldArena()->listTux, p->tux);
@@ -147,15 +203,17 @@ void sendClient(client_t *p, char *msg)
{
int ret;
- if( protocolType == NET_PROTOCOL_TYPE_TCP )
- {
- ret = writeTcpSocket(p->socket_tcp, msg, strlen(msg));
- }
-
- if( protocolType == NET_PROTOCOL_TYPE_UDP )
- {
- ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg));
- }
+#ifdef SUPPORT_NET_UNIX_TCP
+ ret = writeTcpSocket(p->socket_tcp, msg, strlen(msg));
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+ ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg));
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+ ret = writeSdlUdpSocket(sock_server_sdl_udp, p->socket_sdl_udp, msg, strlen(msg));
+#endif
if( ret == 0 )
{
@@ -231,6 +289,8 @@ static void eventCreateClient(client_t *client)
}
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
{
client_t *client;
@@ -243,6 +303,10 @@ static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
eventCreateClient(client);
}
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
static void eventCreateNewUdpClient(sock_udp_t *socket_udp)
{
client_t *client;
@@ -255,6 +319,26 @@ static void eventCreateNewUdpClient(sock_udp_t *socket_udp)
eventCreateClient(client);
}
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+
+static void eventCreateNewSdlUdpClient(sock_sdl_udp_t *socket_sdl_udp)
+{
+ client_t *client;
+
+ assert( socket_sdl_udp != NULL );
+
+ client = newSdlUdpClient( socket_sdl_udp );
+ addList(listClient, client);
+
+ eventCreateClient(client);
+}
+
+#endif
+
+#ifdef SUPPORT_NET_UNIX_TCP
+
static void eventClientTcpSelect(client_t *client)
{
char buffer[STR_SIZE];
@@ -278,6 +362,8 @@ static void eventClientTcpSelect(client_t *client)
}
}
+#endif
+
static void eventClientBuffer(client_t *client)
{
char line[STR_SIZE];
@@ -310,6 +396,8 @@ void eventClientListBuffer()
}
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
void selectServerTcpSocket()
{
fd_set readfds;
@@ -361,23 +449,9 @@ void selectServerTcpSocket()
}
}
-static client_t* findUdpClient(sock_udp_t *sock_udp)
-{
- int i;
- client_t *thisClient;
-
- for( i = 0 ; i < listClient->count; i++)
- {
- thisClient = (client_t *) listClient->list[i];
-
- if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) )
- {
- return thisClient;
- }
- }
+#endif
- return NULL;
-}
+#if defined SUPPORT_NET_UNIX_UDP || defined SUPPORT_NET_SDL_UDP
static void delZombieCLient()
{
@@ -405,6 +479,66 @@ static void delZombieCLient()
}
}
+void eventPeriodicSyncClient()
+{
+ my_time_t currentTime;
+
+ currentTime = getMyTime();
+
+ if( currentTime - lastSyncClient > SERVER_TIME_SYNC )
+ {
+ client_t *thisClientInfo;
+ client_t *thisClientSend;
+ tux_t *thisTux;
+ int i, j;
+
+#ifndef BUBLIC_SERVER
+ proto_send_newtux_server(NULL,
+ (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX]));
+#endif
+ for( i = 0 ; i < listClient->count; i++)
+ {
+ thisClientInfo = (client_t *) listClient->list[i];
+
+ for( j = 0 ; j < listClient->count; j++)
+ {
+ thisClientSend = (client_t *) listClient->list[j];
+ thisTux = thisClientInfo->tux;
+
+ if( thisClientSend != thisClientInfo &&
+ thisTux->status == TUX_STATUS_ALIVE )
+ {
+ proto_send_newtux_server(thisClientSend, thisTux);
+ }
+ }
+ }
+
+ lastSyncClient = getMyTime();
+ }
+}
+
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
+static client_t* findUdpClient(sock_udp_t *sock_udp)
+{
+ int i;
+ client_t *thisClient;
+
+ for( i = 0 ; i < listClient->count; i++)
+ {
+ thisClient = (client_t *) listClient->list[i];
+
+ if( getSockUdpPort(thisClient->socket_udp) == getSockUdpPort(sock_udp) )
+ {
+ return thisClient;
+ }
+ }
+
+ return NULL;
+}
+
static void eventClientUdpSelect(sock_udp_t *sock_server)
{
sock_udp_t *sock_client;
@@ -484,31 +618,86 @@ void selectServerUdpSocket()
}
}
-void eventPeriodicSyncClient()
-{
- my_time_t currentTime;
+#endif
- currentTime = getMyTime();
+#ifdef SUPPORT_NET_SDL_UDP
- if( currentTime - lastSyncClient > SERVER_TIME_SYNC )
+static client_t* findSdlUdpClient(sock_sdl_udp_t *sock_sdl_udp)
+{
+ int i;
+ client_t *thisClient;
+
+ for( i = 0 ; i < listClient->count; i++)
{
- client_t *thisClient;
- int i;
-
-#ifndef BUBLIC_SERVER
- proto_send_newtux_server(NULL,
- (tux_t *)(getWorldArena()->listTux->list[SERVER_INDEX_ROOT_TUX]));
-#endif
- for( i = 0 ; i < listClient->count; i++)
+ thisClient = (client_t *) listClient->list[i];
+
+ if( getSockSdlUdpPort(thisClient->socket_sdl_udp) == getSockSdlUdpPort(sock_sdl_udp) )
{
- thisClient = (client_t *) listClient->list[i];
- proto_send_newtux_server(NULL, thisClient->tux);
+ return thisClient;
}
+ }
- lastSyncClient = getMyTime();
+ return NULL;
+}
+
+void selectServerSdlUdpSocket()
+{
+ sock_sdl_udp_t *sock_client;
+ client_t *client;
+ char buffer[STR_SIZE];
+ bool_t isCreateNewClient;
+ int ret;
+
+ assert( sock_server_sdl_udp != NULL );
+
+ delZombieCLient();
+
+ sock_client = newSdlSockUdp();
+ isCreateNewClient = FALSE;
+
+ memset(buffer, 0, STR_SIZE);
+ ret = readSdlUdpSocket(sock_server_sdl_udp, sock_client, buffer, STR_SIZE-1);
+
+ if( ret < 0 )
+ {
+ return;
+ }
+
+ client = findSdlUdpClient(sock_client);
+
+ if( client == NULL )
+ {
+ eventCreateNewSdlUdpClient(sock_client);
+ client = findSdlUdpClient(sock_client);
+ isCreateNewClient = TRUE;
+ }
+
+ if( client == NULL )
+ {
+ fprintf(stderr, "Client total not found !\n");
+ return;
+ }
+
+ if( isCreateNewClient == FALSE )
+ {
+ destroySockSdlUdp(sock_client);
+ }
+
+ if( ret <= 0 )
+ {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
+
+ if( addBuffer(client->buffer, buffer, ret) != 0 )
+ {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
}
}
+#endif
+
static void quitServer()
{
proto_send_end_server();
@@ -516,6 +705,8 @@ static void quitServer()
destroyListItem(listClient, destroyClient);
}
+#ifdef SUPPORT_NET_UNIX_TCP
+
void quitTcpServer()
{
quitServer();
@@ -526,6 +717,10 @@ void quitTcpServer()
printf("quit TCP port\n");
}
+#endif
+
+#ifdef SUPPORT_NET_UNIX_UDP
+
void quitUdpServer()
{
quitServer();
@@ -535,3 +730,19 @@ void quitUdpServer()
printf("quit UDP port\n");
}
+
+#endif
+
+#ifdef SUPPORT_NET_SDL_UDP
+
+void quitSdlUdpServer()
+{
+ quitServer();
+
+ assert( sock_server_sdl_udp != NULL );
+ closeSdlUdpSocket(sock_server_sdl_udp);
+
+ printf("quit UDP port\n");
+}
+
+#endif