summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororoborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-03-14 19:31:47 +0000
committeroroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e>2008-03-14 19:31:47 +0000
commit593ac1e25c2053f06cb1b7e29785a695d3c57ad3 (patch)
tree15f6bb50abedc884122d40b98349e35d702b7729
parenta4e34c257a4f5966ce699f48d30b3528bdaa45e4 (diff)
- podpora UDP
git-svn-id: http://opensvn.csie.org/tuxanci_ng@15 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
-rw-r--r--include/client.h3
-rw-r--r--include/proto.h6
-rw-r--r--include/server.h24
-rw-r--r--src/client.c133
-rw-r--r--src/net_multiplayer.c90
-rw-r--r--src/proto.c74
-rw-r--r--src/server.c218
-rwxr-xr-xsrc/tcp.c20
-rw-r--r--src/udp.c26
-rw-r--r--src/widget_label.c6
10 files changed, 520 insertions, 80 deletions
diff --git a/include/client.h b/include/client.h
index ba04e61..79887b1 100644
--- a/include/client.h
+++ b/include/client.h
@@ -4,9 +4,12 @@
#define MY_CLIENT
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 selectClientUdpSocket();
extern void quitTcpClient();
+extern void quitUdpClient();
#endif
diff --git a/include/proto.h b/include/proto.h
index 5813165..a741148 100644
--- a/include/proto.h
+++ b/include/proto.h
@@ -6,6 +6,8 @@
#include "tux.h"
#include "server.h"
+extern void proto_send_hello_client();
+extern void proto_recv_hello_server(client_t *client, char *msg);
extern void proto_send_init_server(client_t *client);
extern void proto_recv_init_client(char *msg);
extern void proto_send_event_server(tux_t *tux, int action, client_t *client);
@@ -20,6 +22,10 @@ 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_end_client();
+extern void proto_recv_end_server(client_t *client, char *msg);
+extern void proto_send_end_server();
+extern void proto_recv_end_client(char *msg);
#endif
diff --git a/include/server.h b/include/server.h
index d7e1313..c7d4df9 100644
--- a/include/server.h
+++ b/include/server.h
@@ -19,16 +19,18 @@ typedef struct client_struct
buffer_t *buffer;
} client_t;
-extern int initTcpServer(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 quitTcpServer();
-
+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();
#endif
diff --git a/src/client.c b/src/client.c
index a885b65..530c0f3 100644
--- a/src/client.c
+++ b/src/client.c
@@ -1,4 +1,8 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
#include "list.h"
#include "tux.h"
#include "network.h"
@@ -15,6 +19,12 @@ static sock_tcp_t *sock_server_tcp;
static sock_udp_t *sock_server_udp;
static buffer_t *clientBuffer;
+static void initClient()
+{
+ clientBuffer = newBuffer( LIMIT_BUFFER );
+ proto_send_hello_client();
+}
+
int initTcpClient(char *ip, int port)
{
sock_server_tcp = connectTcpSocket(ip, port);
@@ -25,7 +35,24 @@ int initTcpClient(char *ip, int port)
}
printf("connect %s %d\n", ip, port);
- clientBuffer = newBuffer( LIMIT_BUFFER );
+ protocolType = NET_PROTOCOL_TYPE_TCP;
+ initClient();
+
+ return 0;
+}
+
+int initUdpClient(char *ip, int port)
+{
+ sock_server_udp = connectUdpSocket(ip, port);
+
+ if( sock_server_udp == NULL )
+ {
+ return -1;
+ }
+
+ printf("connect %s %d\n", ip, port);
+ protocolType = NET_PROTOCOL_TYPE_UDP;
+ initClient();
return 0;
}
@@ -34,29 +61,32 @@ void sendServer(char *msg)
{
int ret;
+ assert( msg != NULL );
+
if( protocolType == NET_PROTOCOL_TYPE_TCP )
{
ret = writeTcpSocket(sock_server_tcp, msg, strlen(msg));
-
- if ( ret == 0 )
- {
- fprintf(stderr, "server uzatvoril sietovy socket\n");
- setWorldEnd();
- }
-
- if ( ret < 0 )
- {
- fprintf(stderr, "nastala chyba pri poslani spravy serveru\n");
- setWorldEnd();
- }
}
if( protocolType == NET_PROTOCOL_TYPE_UDP )
{
+ ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg));
+ }
+
+ if ( ret == 0 )
+ {
+ fprintf(stderr, "server uzatvoril sietovy socket\n");
+ setWorldEnd();
+ }
+
+ if ( ret < 0 )
+ {
+ fprintf(stderr, "nastala chyba pri poslani spravy serveru\n");
+ setWorldEnd();
}
}
-static void eventServerTcpSelect()
+static void eventServerSelect()
{
char buffer[STR_SIZE];
int ret;
@@ -67,23 +97,25 @@ static void eventServerTcpSelect()
{
ret = readTcpSocket(sock_server_tcp, buffer, STR_SIZE-1);
- if( ret == 0 )
- {
- fprintf(stderr, "server uzatovril sietovy socket\n");
- setWorldEnd();
- return;
- }
-
- if( ret < 0 )
- {
- fprintf(stderr, "chyba, spojenie prerusene \n");
- setWorldEnd();
- return;
- }
}
if( protocolType == NET_PROTOCOL_TYPE_UDP )
{
+ ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_SIZE-1);
+ }
+
+ if( ret == 0 )
+ {
+ fprintf(stderr, "server uzatovril sietovy socket\n");
+ setWorldEnd();
+ return;
+ }
+
+ if( ret < 0 )
+ {
+ fprintf(stderr, "chyba, spojenie prerusene \n");
+ setWorldEnd();
+ return;
}
if( addBuffer(clientBuffer, buffer, ret) != 0 )
@@ -100,6 +132,8 @@ void eventServerBuffer()
/* obsluha udalosti od servera */
+ assert( clientBuffer != NULL );
+
while ( getBufferLine(clientBuffer, line, STR_SIZE) >= 0 )
{
//printf("dostal som %s", line);
@@ -109,6 +143,7 @@ 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, "end", 3) == 0 )proto_recv_end_client(line);
}
}
@@ -129,14 +164,54 @@ void selectClientTcpSocket()
if( FD_ISSET(sock_server_tcp->sock, &readfds) )
{
- eventServerTcpSelect(sock_server_tcp);
+ eventServerSelect();
+ }
+}
+
+void selectClientUdpSocket()
+{
+ fd_set readfds;
+ struct timeval tv;
+ int max_fd;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&readfds);
+ FD_SET(sock_server_udp->sock, &readfds);
+ max_fd = sock_server_udp->sock;
+
+ select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
+
+ if( FD_ISSET(sock_server_udp->sock, &readfds) )
+ {
+ eventServerSelect();
}
}
+static void quitClient()
+{
+ proto_send_end_client();
+ assert( clientBuffer != NULL );
+ destroyBuffer(clientBuffer);
+}
+
void quitTcpClient()
{
+ quitClient();
+
+ assert( sock_server_tcp != NULL );
closeTcpSocket(sock_server_tcp);
- destroyBuffer(clientBuffer);
+
printf("quit conenct\n");
}
+void quitUdpClient()
+{
+ quitClient();
+
+ assert( sock_server_udp != NULL );
+ closeUdpSocket(sock_server_udp);
+
+ printf("quit conenct\n");
+}
diff --git a/src/net_multiplayer.c b/src/net_multiplayer.c
index 2cfc898..cbf9cb8 100644
--- a/src/net_multiplayer.c
+++ b/src/net_multiplayer.c
@@ -23,6 +23,7 @@
#include "udp.h"
static int netGameType;
+static int netProtoType = NET_PROTOCOL_TYPE_UDP;
int getNetTypeGame()
{
@@ -39,20 +40,47 @@ int initNetMuliplayer(int type, char *ip, int port)
break;
case NET_GAME_TYPE_SERVER :
- if( initTcpServer(port) != 0 )
+ switch( netProtoType )
{
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre server !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
+ 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;
}
+
break;
case NET_GAME_TYPE_CLIENT :
- if( initTcpClient(ip, port) != 0 )
+ switch( netProtoType )
{
- fprintf(stderr, "Neomzem inicalizovat sietovu hru pre clienta !\n");
- netGameType = NET_GAME_TYPE_NONE;
- return -1;
+ 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;
}
break;
@@ -72,13 +100,31 @@ void eventNetMultiplayer()
break;
case NET_GAME_TYPE_SERVER :
- selectServerTcpSocket();
- eventClientListBuffer();
+ switch( netProtoType )
+ {
+ case NET_PROTOCOL_TYPE_TCP :
+ selectServerTcpSocket();
+ eventClientListBuffer();
+ break;
+ case NET_PROTOCOL_TYPE_UDP :
+ selectServerUdpSocket();
+ eventClientListBuffer();
+ break;
+ }
break;
case NET_GAME_TYPE_CLIENT :
- selectClientTcpSocket();
- eventServerBuffer();
+ switch( netProtoType )
+ {
+ case NET_PROTOCOL_TYPE_TCP :
+ selectClientTcpSocket();
+ eventServerBuffer();
+ break;
+ case NET_PROTOCOL_TYPE_UDP :
+ selectClientUdpSocket();
+ eventServerBuffer();
+ break;
+ }
break;
default :
@@ -95,11 +141,27 @@ void quitNetMultiplayer()
break;
case NET_GAME_TYPE_SERVER :
- quitTcpServer();
+ switch( netProtoType )
+ {
+ case NET_PROTOCOL_TYPE_TCP :
+ quitTcpServer();
+ break;
+ case NET_PROTOCOL_TYPE_UDP :
+ quitUdpServer();
+ break;
+ }
break;
case NET_GAME_TYPE_CLIENT :
- quitTcpClient();
+ switch( netProtoType )
+ {
+ case NET_PROTOCOL_TYPE_TCP :
+ quitTcpClient();
+ break;
+ case NET_PROTOCOL_TYPE_UDP :
+ quitUdpClient();
+ break;
+ }
break;
default :
diff --git a/src/proto.c b/src/proto.c
index 8304c6d..2c2c5c4 100644
--- a/src/proto.c
+++ b/src/proto.c
@@ -1,4 +1,7 @@
+#include <stdio.h>
+#include <assert.h>
+
#include "list.h"
#include "tux.h"
#include "item.h"
@@ -13,11 +16,25 @@
#include "server.h"
#include "proto.h"
+void proto_send_hello_client()
+{
+ char msg[STR_SIZE];
+
+ strcpy(msg, "hello\n");
+ sendServer(msg);
+}
+
+void proto_recv_hello_server(client_t *client, char *msg)
+{
+}
+
void proto_send_init_server(client_t *client)
{
char msg[STR_SIZE];
int n;
+ assert( client != NULL );
+
getSettingCountRound(&n);
sprintf(msg, "init %d %d %d %d %s\n",
@@ -35,6 +52,8 @@ void proto_recv_init_client(char *msg)
int id;
int x, y, n;
+ assert( msg != NULL );
+
sscanf(msg, "%s %d %d %d %d %s\n",
cmd, &id, &x, &y, &n, arena_name);
@@ -56,6 +75,8 @@ void proto_send_event_server(tux_t *tux, int action, client_t *client)
{
char msg[STR_SIZE];
+ assert( tux != NULL );
+
sprintf(msg, "event %d %d\n", tux->id, action);
sendAllClientBut(msg, client);
@@ -67,6 +88,8 @@ void proto_recv_event_client(char *msg)
int id, action;
tux_t *tux;
+ assert( msg != NULL );
+
sscanf(msg, "%s %d %d", cmd, &id, &action);
tux = getTuxID(getWorldArena()->listTux, id);
@@ -91,6 +114,9 @@ void proto_recv_event_server(client_t *client, char *msg)
char cmd[STR_SIZE];
int action;
+ assert( client != NULL );
+ assert( msg != NULL );
+
sscanf(msg, "%s %d", cmd, &action);
actionTux(client->tux, action);
@@ -102,6 +128,8 @@ void proto_send_newtux_server(client_t *client, tux_t *tux)
{
char msg[STR_SIZE];
+ 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,
tux->gun, tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE],
@@ -129,6 +157,8 @@ void proto_recv_newtux_client(char *msg)
int time1, time2;
tux_t *tux;
+ 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,
&myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6, &gun7, &time1, &time2);
@@ -167,6 +197,8 @@ void proto_send_deltux_server(client_t *client)
{
char msg[STR_SIZE];
+ assert( client != NULL );
+
sprintf(msg, "deltux %d\n", client->tux->id);
sendAllClientBut(msg, client);
@@ -178,6 +210,8 @@ void proto_recv_deltux_client(char *msg)
int id;
tux_t *tux;
+ assert( msg != NULL );
+
sscanf(msg, "%s %d\n", cmd, &id);
tux = getTuxID(getWorldArena()->listTux, id);
@@ -196,6 +230,8 @@ void proto_send_additem_server(item_t *p)
char msg[STR_SIZE];
int id = -1;
+ assert( p != NULL );
+
if( p->author != NULL )
{
id = p->author->id;
@@ -213,6 +249,8 @@ void proto_recv_additem_client(char *msg)
int type, x, y, count, frame, id;
item_t *item;
+ assert( msg != NULL );
+
sscanf(msg, "%s %d %d %d %d %d %d\n", cmd, &type, &x, &y, &count, &frame, &id);
item = newItem(x, y, type, getTuxID(getWorldArena()->listTux, id));
@@ -233,6 +271,8 @@ void proto_send_context_client(tux_t *tux)
{
char msg[STR_SIZE];
+ assert( tux != NULL );
+
sprintf(msg, "context %s\n", tux->name);
sendServer(msg);
@@ -243,10 +283,42 @@ void proto_recv_context_server(client_t *client, char *msg)
char cmd[STR_SIZE];
char name[STR_NAME_SIZE];
+ assert( client != NULL );
+ assert( msg != NULL );
+
sscanf(msg, "%s %s\n", cmd, name);
strcpy(client->tux->name, name);
proto_send_newtux_server(NULL, client->tux);
}
-
+
+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 );
+ client->status = NET_STATUS_ZOMBIE;
+ printf("proto_recv_end_server\n");
+}
+
+void proto_send_end_server()
+{
+ char msg[STR_SIZE];
+ strcpy(msg, "end\n");
+ sendAllClient(msg);
+}
+
+void proto_recv_end_client(char *msg)
+{
+ assert( msg != NULL );
+
+ setWorldEnd();
+}
+
diff --git a/src/server.c b/src/server.c
index 5a04e91..419990d 100644
--- a/src/server.c
+++ b/src/server.c
@@ -16,18 +16,42 @@ static sock_tcp_t *sock_server_tcp;
static sock_udp_t *sock_server_udp;
static list_t *listClient;
+void static initServer()
+{
+ listClient = newList();
+}
+
int initTcpServer(int port)
{
protocolType = NET_PROTOCOL_TYPE_TCP;
sock_server_tcp = bindTcpSocket(port);
- listClient = newList();
if( sock_server_tcp == NULL )
{
return -1;
}
+ initServer();
+
+ printf("server listen port %d\n", port);
+
+ return 0;
+}
+
+int initUdpServer(int port)
+{
+ protocolType = NET_PROTOCOL_TYPE_UDP;
+
+ sock_server_udp = bindUdpSocket(port);
+
+ if( sock_server_udp == NULL )
+ {
+ return -1;
+ }
+
+ initServer();
+
printf("server listen port %d\n", port);
return 0;
@@ -53,6 +77,8 @@ client_t* newTcpClient(sock_tcp_t *sock_tcp)
{
client_t *new;
+ assert( sock_tcp != NULL );
+
new = newAnyClient();
new->socket_tcp = sock_tcp;
@@ -63,6 +89,8 @@ client_t* newUdpClient(sock_udp_t *sock_udp)
{
client_t *new;
+ assert( sock_udp != NULL );
+
new = newAnyClient();
new->socket_udp = sock_udp;
@@ -73,6 +101,8 @@ void destroyClient(client_t *p)
{
int index;
+ assert( p != NULL );
+
if( p->socket_tcp != NULL )
{
closeTcpSocket(p->socket_tcp);
@@ -92,29 +122,33 @@ void destroyClient(client_t *p)
void sendClient(client_t *p, char *msg)
{
+ assert( p != NULL );
+ assert( msg != NULL );
+
if( p->status == NET_STATUS_OK )
{
int ret;
if( protocolType == NET_PROTOCOL_TYPE_TCP )
{
- ret = writeTcpSocket(p->socket_tcp, msg, strlen(msg));
-
- if( ret == 0 )
- {
- fprintf(stderr, "client sa odpojil\n");
- p->status = NET_STATUS_ZOMBIE;
- }
-
- if( ret < 0 )
- {
- fprintf(stderr, "nastala chyba pri posielanie spravy clientovy\n");
- p->status = NET_STATUS_ZOMBIE;
- }
+ 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));
+ }
+
+ if( ret == 0 )
+ {
+ fprintf(stderr, "client sa odpojil\n");
+ p->status = NET_STATUS_ZOMBIE;
+ }
+
+ if( ret < 0 )
+ {
+ fprintf(stderr, "nastala chyba pri posielanie spravy clientovy\n");
+ p->status = NET_STATUS_ZOMBIE;
}
}
}
@@ -124,6 +158,8 @@ void sendAllClientBut(char *msg, client_t *p)
client_t *thisClient;
int i;
+ assert( msg != NULL );
+
for( i = 0 ; i < listClient->count; i++)
{
thisClient = (client_t *) listClient->list[i];
@@ -137,20 +173,18 @@ void sendAllClientBut(char *msg, client_t *p)
void sendAllClient(char *msg)
{
+ assert( msg != NULL );
+
sendAllClientBut(msg, NULL);
}
-static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
+static void eventCreateClient(client_t *client)
{
- client_t *client;
client_t *thisClient;
tux_t *thisTux;
item_t *thisItem;
int i;
- client = newTcpClient( getTcpNewClient(socket_tcp) );
- addList(listClient, client);
-
proto_send_init_server(client);
proto_send_newtux_server(client, (tux_t *)(getWorldArena()->listTux->list[0]) );
@@ -174,6 +208,29 @@ static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
}
}
+static void eventCreateNewTcpClient(sock_tcp_t *socket_tcp)
+{
+ client_t *client;
+
+ assert( socket_tcp != NULL );
+
+ client = newTcpClient( getTcpNewClient(socket_tcp) );
+ addList(listClient, client);
+
+ eventCreateClient(client);
+}
+
+static void eventCreateNewUdpClient(sock_udp_t *socket_udp)
+{
+ client_t *client;
+
+ assert( socket_udp != NULL );
+
+ client = newUdpClient( socket_udp );
+ addList(listClient, client);
+ eventCreateClient(client);
+}
+
static void eventClientTcpSelect(client_t *client)
{
char buffer[STR_SIZE];
@@ -201,14 +258,18 @@ static void eventClientBuffer(client_t *client)
{
char line[STR_SIZE];
+ assert( client != NULL );
+
/* obsluha udalosti od clientov */
while( getBufferLine(client->buffer, line, STR_SIZE) >= 0 )
{
//printf("dostal som %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);
if( strncmp(line, "context", 7) == 0 )proto_recv_context_server(client, line);
+ if( strncmp(line, "end", 3) == 0 )proto_recv_end_server(client, line);
}
}
@@ -275,11 +336,128 @@ void selectServerTcpSocket()
}
}
-void quitTcpServer()
+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( thisClient->socket_udp->sockAddr.sin_port == sock_udp->sockAddr.sin_port )
+ {
+ return thisClient;
+ }
+ }
+
+ return NULL;
+}
+
+static void delZombieCLient()
{
+ client_t *thisClient;
+ int i;
+
+ for( i = 0 ; i < listClient->count; i++)
+ {
+ thisClient = (client_t *) listClient->list[i];
+
+ if( thisClient->status == NET_STATUS_ZOMBIE )
+ {
+ proto_send_deltux_server(thisClient);
+ delListItem(listClient, i, destroyClient);
+ }
+ }
+}
+
+static void eventClientUdpSelect(sock_udp_t *sock_server)
+{
+ sock_udp_t *sock_client;
+ client_t *client;
+ char buffer[STR_SIZE];
+ int ret;
+
+ assert( sock_server != NULL );
+
+ sock_client = newSockUdp();
+
+ memset(buffer, 0, STR_SIZE);
+ ret = readUdpSocket(sock_server, sock_client, buffer, STR_SIZE-1);
+
+ client = findUdpClient(sock_client);
+
+ if( client == NULL )
+ {
+ eventCreateNewUdpClient(sock_client);
+ client = findUdpClient(sock_client);
+ }
+
+ if( client == NULL )
+ {
+ fprintf(stderr, "Client total not found !\n");
+ return;
+ }
+
+ if( ret <= 0 )
+ {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
+
+ if( addBuffer(client->buffer, buffer, ret) != 0 )
+ {
+ client->status = NET_STATUS_ZOMBIE;
+ return;
+ }
+}
+
+void selectServerUdpSocket()
+{
+ fd_set readfds;
+ struct timeval tv;
+ int max_fd;
+
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+
+ FD_ZERO(&readfds);
+ FD_SET(sock_server_udp->sock, &readfds);
+ max_fd = sock_server_udp->sock;
+
+ delZombieCLient();
+
+ select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv);
+
+ if( FD_ISSET(sock_server_udp->sock, &readfds) )
+ {
+ eventClientUdpSelect(sock_server_udp);
+ }
+}
+
+static void quitServer()
+{
+ proto_send_end_server();
+ assert( listClient != NULL );
destroyListItem(listClient, destroyClient);
+}
+
+void quitTcpServer()
+{
+ quitServer();
+
+ assert( sock_server_tcp != NULL );
closeTcpSocket(sock_server_tcp);
+
printf("quit port\n");
}
+void quitUdpServer()
+{
+ quitServer();
+
+ assert( sock_server_udp != NULL );
+ closeUdpSocket(sock_server_udp);
+ printf("quit port\n");
+}
diff --git a/src/tcp.c b/src/tcp.c
index bcd7764..4558095 100755
--- a/src/tcp.c
+++ b/src/tcp.c
@@ -27,6 +27,7 @@ sock_tcp_t* newSockTcp()
void destroySockTcp(sock_tcp_t *p)
{
+ assert( p != NULL );
free(p);
}
@@ -39,6 +40,8 @@ sock_tcp_t* bindTcpSocket(int port)
new = newSockTcp();
+ assert( new != NULL );
+
new->sock = socket(AF_INET, SOCK_STREAM, 0);
if( new->sock < 0 )
@@ -71,7 +74,8 @@ sock_tcp_t* getTcpNewClient(sock_tcp_t *p)
sock_tcp_t *new;
int client_len;
- assert( new->sock >= 0 );
+ assert( p != NULL );
+ assert( p->sock >= 0 );
new = newSockTcp();
@@ -85,6 +89,9 @@ sock_tcp_t* getTcpNewClient(sock_tcp_t *p)
void getSockTcpIp(sock_tcp_t *p, char *str_ip)
{
+ assert( p != NULL );
+ assert( str_ip != NULL );
+
strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
}
@@ -93,6 +100,7 @@ sock_tcp_t* connectTcpSocket(char *ip, int port)
sock_tcp_t *new;
int len;
+ assert( ip != NULL );
assert( port > 0 && port < 65535 );
new = newSockTcp();
@@ -124,16 +132,24 @@ sock_tcp_t* connectTcpSocket(char *ip, int port)
int readTcpSocket(sock_tcp_t *p, void *address, int len)
{
+ assert( p != NULL );
+ assert( address != NULL );
+
return read(p->sock, address, len);
}
int writeTcpSocket(sock_tcp_t *p, void *address, int len)
{
+ assert( p != NULL );
+ assert( address != NULL );
+
return write(p->sock, address, len);
}
void closeTcpSocket(sock_tcp_t *p)
{
+ assert( p != NULL );
+
close(p->sock);
destroySockTcp(p);
}
@@ -207,4 +223,6 @@ int test_tcp_main(int argc, char **argv)
closeTcpSocket(sock);
}
+
+ return 0;
}
diff --git a/src/udp.c b/src/udp.c
index 72a7448..0f86dc6 100644
--- a/src/udp.c
+++ b/src/udp.c
@@ -8,6 +8,7 @@
#include <string.h>
#include <stdio.h>
#include <netdb.h>
+#include <assert.h>
#define BUFSIZE 1000
@@ -29,6 +30,7 @@ sock_udp_t* newSockUdp()
void destroySockUdp(sock_udp_t *p)
{
+ assert( p != NULL );
free(p);
}
@@ -36,8 +38,12 @@ sock_udp_t* bindUdpSocket(int port)
{
sock_udp_t *new;
+ assert( port > 0 && port < 65535 );
+
new = newSockUdp();
+ assert( new != NULL );
+
if( ( new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) < 0 )
{
fprintf(stderr, "nemozem obsadit sokcet !\n");
@@ -65,8 +71,13 @@ sock_udp_t* connectUdpSocket(char *address, int port)
struct hostent *host;
sock_udp_t *new;
+ assert( address != NULL );
+ assert( port > 0 && port < 65536 );
+
new = newSockUdp();
+ assert( new != NULL );
+
if( ( new->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) < 0 )
{
fprintf(stderr, "nemozem sa pripojit na sokcet !\n");
@@ -87,6 +98,10 @@ int readUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
int addrlen;
int size;
+ assert( src != NULL );
+ assert( dst != NULL );
+ assert( address != NULL );
+
addrlen = sizeof(src->sockAddr);
if( ( size = recvfrom(src->sock, address, len, 0,
@@ -106,7 +121,11 @@ int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
addrlen = sizeof(src->sockAddr);
- if( ( size = sendto(src->sock, address, len, 0,
+ assert( src != NULL );
+ assert( dst != NULL );
+ assert( address != NULL );
+
+ if( ( size = sendto(src->sock, address, len, 0,
(struct sockaddr *)&dst->sockAddr, (socklen_t)addrlen) ) < 0 )
{
fprintf(stderr, "nemozem nacitat sokcet !\n");
@@ -118,11 +137,16 @@ int writeUdpSocket(sock_udp_t *src, sock_udp_t *dst, void *address, int len)
void getSockUdpIp(sock_udp_t *p, char *str_ip)
{
+ assert( p != NULL );
+ assert( str_ip != NULL );
+
strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));
}
void closeUdpSocket(sock_udp_t *p)
{
+ assert( p != NULL );
+
close(p->sock);
destroySockUdp(p);
}
diff --git a/src/widget_label.c b/src/widget_label.c
index 9cb9718..15948a2 100644
--- a/src/widget_label.c
+++ b/src/widget_label.c
@@ -23,13 +23,13 @@ void drawWidgetLabel(widget_label_t *p)
switch( p->bind )
{
case WIDGET_LABEL_RIGHT :
- drawFont(p->text, p->x+p->w, p->y, COLOR_WHITE);
+ drawFont(p->text, p->x+p->w, p->y + p->h/2, COLOR_WHITE);
break;
case WIDGET_LABEL_LEFT :
- drawFont(p->text, p->x, p->y, COLOR_WHITE);
+ drawFont(p->text, p->x, p->y + p->h/2, COLOR_WHITE);
break;
case WIDGET_LABEL_CENTER :
- drawFont(p->text, p->x-p->w/2, p->y, COLOR_WHITE);
+ drawFont(p->text, p->x-p->w/2, p->y + p->h/2, COLOR_WHITE);
break;
}
}