diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-06-28 16:38:35 +0000 |
| commit | 2d776fcd61be168e1ff0ff2e4a5e71835e7cea19 (patch) | |
| tree | 7cf274897bf540d5332d446f583ffd066e4e2e1d /src/client/client.c | |
| parent | b877ae23ac5d380ab3aa48d7724045cf4883b186 (diff) | |
- prekopanie adresarovej struktury, prva cast
git-svn-id: http://opensvn.csie.org/tuxanci_ng@77 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
Diffstat (limited to 'src/client/client.c')
| -rw-r--r-- | src/client/client.c | 305 |
1 files changed, 305 insertions, 0 deletions
diff --git a/src/client/client.c b/src/client/client.c new file mode 100644 index 0000000..975c9da --- /dev/null +++ b/src/client/client.c @@ -0,0 +1,305 @@ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include <fcntl.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/time.h> +#include <sys/socket.h> +#include <sys/select.h> + +#include "base/main.h" +#include "base/list.h" +#include "base/tux.h" +#include "base/proto.h" + +#include "client/client.h" +#include "client/language.h" + +#include "screen/screen_analyze.h" +#include "screen/screen_world.h" + +static int protocolType; + +#ifdef SUPPORT_NET_UNIX_UDP +#include "net_unix/udp.h" +static sock_udp_t *sock_server_udp; +#endif + +#ifdef SUPPORT_NET_SDL_UDP +#include "net_sdl/sdl_udp.h" +static sock_sdl_udp_t *sock_server_sdl_udp; +#endif + +static list_t *clientBuffer; +static my_time_t lastPing; +static my_time_t lastPingServerAlive; + +static void initClient() +{ + char name[STR_NAME_SIZE]; + + clientBuffer = newList(); + lastPing = getMyTime(); + lastPingServerAlive = getMyTime(); + + getSettingNameRight(name); + proto_send_hello_client(name); +} + +#ifdef SUPPORT_NET_UNIX_UDP + +int initUdpClient(char *ip, int port, int proto) +{ + sock_server_udp = connectUdpSocket(ip, port, proto); + + if( sock_server_udp == NULL ) + { + return -1; + } + + printf("connect UDP %s %d\n", ip, port); + + protocolType = NET_PROTOCOL_TYPE_UDP; + initClient(); + + 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 ); + +#ifndef PUBLIC_SERVER + if( isParamFlag("--send") ) + { + printf("send -> %s", msg); + } +#endif + +#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 +} + +static int eventServerSelect() +{ + char buffer[STR_PROTO_SIZE]; + int ret; + + memset(buffer,0 ,STR_PROTO_SIZE); + +#ifdef SUPPORT_NET_UNIX_UDP + ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); +#endif + +#ifdef SUPPORT_NET_SDL_UDP + ret = readSdlUdpSocket(sock_server_sdl_udp, sock_server_sdl_udp, buffer, STR_PROTO_SIZE-1); + + if( ret < 0 ) + { + return ret; + } +#endif + + addList(clientBuffer, strdup(buffer) ); +/* + if( addBuffer(clientBuffer, buffer, ret) != 0 ) + { + fprintf(stderr, "chyba, nemozem zapisovat do mojho buffera !\n"); + setWorldEnd(); + return ret; + } +*/ + return ret; +} + +void eventServerBuffer() +{ + char *line; + int i; + + /* obsluha udalosti od servera */ + + assert( clientBuffer != NULL ); + + for( i = 0 ; i < clientBuffer->count ; i++ ) + { + line = (char *)clientBuffer->list[i]; + +#ifndef PUBLIC_SERVER + if( isParamFlag("--recv") ) + { + printf("recv -> %s", line); + } +#endif + if( strncmp(line, "error", 5) == 0 )proto_recv_error_client(line); + if( strncmp(line, "init", 4) == 0 )proto_recv_init_client(line); + if( strncmp(line, "event", 5) == 0 )proto_recv_event_client(line); + if( strncmp(line, "newtux", 6) == 0 )proto_recv_newtux_client(line); + if( strncmp(line, "del", 3) == 0 )proto_recv_del_client(line); + if( strncmp(line, "additem", 7) == 0 )proto_recv_additem_client(line); + if( strncmp(line, "shot", 4) == 0 )proto_recv_shot_client(line); + if( strncmp(line, "kill", 4) == 0 )proto_recv_kill_client(line); + if( strncmp(line, "ping", 4) == 0 )proto_recv_ping_client(line); + if( strncmp(line, "end", 3) == 0 )proto_recv_end_client(line); + + lastPingServerAlive = getMyTime(); + } + + destroyListItem(clientBuffer, free); + clientBuffer = newList(); +} + +void eventPingServer() +{ + my_time_t currentTime; + + currentTime = getMyTime(); + + if( currentTime - lastPing > CLIENT_TIMEOUT ) + { + proto_send_ping_client(); + lastPing = getMyTime(); + } +} + +bool_t isServerAlive() +{ + my_time_t currentTime; + + currentTime = getMyTime(); + + if( currentTime - lastPingServerAlive > SERVER_TIMEOUT_ALIVE ) + { + return FALSE; + } + + return TRUE; +} + +#ifdef SUPPORT_NET_UNIX_UDP + +void selectClientUdpSocket() +{ + fd_set readfds; + struct timeval tv; + int max_fd; + bool_t isNext; + + if( isServerAlive() == FALSE ) + { + fprintf(stderr, "server neodpoveda !\n"); + setWorldEnd(); + return; + } + + do{ + isNext = FALSE; + + 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(); + isNext = TRUE; + } + + }while( isNext == TRUE ); + +} + +#endif + +#ifdef SUPPORT_NET_SDL_UDP + +void selectClientSdlUdpSocket() +{ + int ret; + + if( isServerAlive() == FALSE ) + { + setMsgToAnalyze(getMyText("ERROR_SERVER_DONT_ALIVE")); + setWorldEnd(); + return; + } + + do{ + ret = eventServerSelect(); + }while( ret > 0); +} + +#endif + +static void quitClient() +{ + proto_send_end_client(); + assert( clientBuffer != NULL ); + destroyListItem(clientBuffer, free); +} + +#ifdef SUPPORT_NET_UNIX_UDP + +void quitUdpClient() +{ + quitClient(); + + assert( sock_server_udp != NULL ); + closeUdpSocket(sock_server_udp); + + 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 |