From 090ca3434016213e9d0b84a3fa0d629fb47ff6a3 Mon Sep 17 00:00:00 2001 From: oroborus Date: Fri, 22 Aug 2008 18:47:31 +0000 Subject: - podpora TCP a UDP je v makrach SUPPORT_TCP a SUPPORT_UDP - pri kompilacii musi existovat prave jedno makro. makro SUPPORT_UDP kompilovat s udp_server.c udp.c makro SUPPORT_TCP kompilovat s tcp_server.c tcp.c git-svn-id: http://opensvn.csie.org/tuxanci_ng@240 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e --- src/client/client.c | 93 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 25 deletions(-) (limited to 'src/client') diff --git a/src/client/client.c b/src/client/client.c index 4637d01..66c5700 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -25,13 +25,23 @@ #include "screen_analyze.h" #include "screen_world.h" +#include "buffer.h" +#ifdef SUPPORT_UDP #include "udp.h" +#endif + +#ifdef SUPPORT_TCP #include "tcp.h" -#include "buffer.h" +#endif +#ifdef SUPPORT_UDP static sock_udp_t *sock_server_udp; +#endif + +#ifdef SUPPORT_TCP static sock_tcp_t *sock_server_tcp; +#endif static list_t *listRecvMsg; @@ -47,6 +57,7 @@ static int traffic_down; static int traffic_up; #endif + typedef struct proto_cmd_client_struct { char *name; @@ -93,6 +104,8 @@ static proto_cmd_client_t* findCmdProto(char *msg) return NULL; } +#ifdef SUPPORT_UDP + static int initUdpClient(char *ip, int port, int proto) { sock_server_udp = connectUdpSocket(ip, port, proto); @@ -107,6 +120,10 @@ static int initUdpClient(char *ip, int port, int proto) return 0; } +#endif + +#ifdef SUPPORT_TCP + static int initTcpClient(char *ip, int port, int proto) { sock_server_tcp = connectTcpSocket(ip, port, proto); @@ -125,6 +142,8 @@ static int initTcpClient(char *ip, int port, int proto) return 0; } +#endif + int initClient(char *ip, int port, int proto) { char name[STR_NAME_SIZE]; @@ -143,35 +162,23 @@ int initClient(char *ip, int port, int proto) clientRecvBuffer = newBuffer(CLIENT_BUFFER_LIMIT); clientSendBuffer = newBuffer(CLIENT_BUFFER_LIMIT); - if( ! isParamFlag("--udp") && ! isParamFlag("--tcp") ) - { - ret = initUdpClient(ip, port, proto); +#ifdef SUPPORT_UDP + ret = initUdpClient(ip, port, proto); - if( ret < 0 ) - { - return -1; - } - } - - if( isParamFlag("--udp") && ! isParamFlag("--tcp") ) + if( ret < 0 ) { - ret = initUdpClient(ip, port, proto); - - if( ret < 0 ) - { - return -1; - } + return -1; } +#endif - if( isParamFlag("--tcp") && ! isParamFlag("--udp") ) - { - ret = initTcpClient(ip, port, proto); +#ifdef SUPPORT_TCP + ret = initTcpClient(ip, port, proto); - if( ret < 0 ) - { - return -1; - } + if( ret < 0 ) + { + return -1; } +#endif getSettingNameRight(name); proto_send_hello_client(name); @@ -186,6 +193,8 @@ static void errorWithServer() setWorldEnd(); } +#ifdef SUPPORT_TCP + static void sendBuffer() { void *data; @@ -212,6 +221,8 @@ static void sendBuffer() cutBuffer(clientSendBuffer, res); } +#endif + void sendServer(char *msg) { int ret; @@ -230,16 +241,20 @@ void sendServer(char *msg) traffic_up += strlen(msg); #endif +#ifdef SUPPORT_UDP if( sock_server_udp != NULL ) { ret = writeUdpSocket(sock_server_udp, sock_server_udp, msg, strlen(msg)); } +#endif +#ifdef SUPPORT_TCP if( sock_server_tcp != NULL ) { //ret = writeTcpSocket(sock_server_tcp, msg, strlen(msg)); ret = addBuffer(clientSendBuffer, msg, strlen(msg)); } +#endif if( ret < 0 ) { @@ -255,15 +270,19 @@ static int eventServerSelect() memset(buffer, 0, STR_PROTO_SIZE); +#ifdef SUPPORT_UDP if( sock_server_udp != NULL ) { ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); } +#endif +#ifdef SUPPORT_TCP if( sock_server_tcp != NULL ) { ret = readTcpSocket(sock_server_tcp, buffer, STR_PROTO_SIZE-1); } +#endif if( ret < 0 ) { @@ -337,6 +356,8 @@ static void eventPingServer() } } +#ifdef SUPPORT_UDP + static bool_t isServerAlive() { my_time_t currentTime; @@ -351,6 +372,8 @@ static bool_t isServerAlive() return TRUE; } +#endif + static void selectClientSocket() { fd_set readfds; @@ -359,6 +382,7 @@ static void selectClientSocket() int sock; bool_t isNext; +#ifdef SUPPORT_UDP if( sock_server_udp != NULL ) { if( isServerAlive() == FALSE ) @@ -367,6 +391,7 @@ static void selectClientSocket() return; } } +#endif do{ isNext = FALSE; @@ -376,16 +401,19 @@ static void selectClientSocket() FD_ZERO(&readfds); +#ifdef SUPPORT_UDP if( sock_server_udp != NULL ) { sock = sock_server_udp->sock; } +#endif +#ifdef SUPPORT_TCP if( sock_server_tcp != NULL ) { sock = sock_server_tcp->sock; } - +#endif FD_SET(sock, &readfds); max_fd = sock; select(max_fd+1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &tv); @@ -433,9 +461,14 @@ void eventClient() eventPingServer(); selectClientSocket(); eventClientWorkRecvList(); + +#ifdef SUPPORT_TCP sendBuffer(); +#endif } +#ifdef SUPPORT_UDP + static void quitUdpClient() { assert( sock_server_udp != NULL ); @@ -445,6 +478,10 @@ static void quitUdpClient() #endif } +#endif + +#ifdef SUPPORT_TCP + static void quitTcpClient() { assert( sock_server_tcp != NULL ); @@ -454,6 +491,8 @@ static void quitTcpClient() #endif } +#endif + void quitClient() { proto_send_end_client(); @@ -464,14 +503,18 @@ void quitClient() destroyBuffer(clientRecvBuffer); destroyBuffer(clientSendBuffer); +#ifdef SUPPORT_UDP if( sock_server_udp != NULL ) { quitUdpClient(); } +#endif +#ifdef SUPPORT_TCP if( sock_server_tcp != NULL ) { quitTcpClient(); } +#endif } -- cgit v1.2.3