diff options
| author | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-07-31 20:03:29 +0000 |
|---|---|---|
| committer | oroborus <oroborus@0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e> | 2008-07-31 20:03:29 +0000 |
| commit | bdd968b726362f55454853799ec7869cd14b9a4c (patch) | |
| tree | 1fed78c04d404ac03e57bab2b94cd4ea4343862e | |
| parent | 1050c84b401b58e8ad69a2e49edae5185b136f1f (diff) | |
- priprava kodu na podporu UDP a TCP sucastne
- upravy v kode, ktory spusta funkcie proto.c na zaklade prijatych packetov. ( na strane servera aj clienta )
git-svn-id: http://opensvn.csie.org/tuxanci_ng@185 0ee4d065-81f0-4d1e-b06c-ff20ad07cc3e
| -rw-r--r-- | config.h | 2 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/base/arena.c | 18 | ||||
| -rw-r--r-- | src/base/server.c | 147 | ||||
| -rw-r--r-- | src/base/server.h | 1 | ||||
| -rw-r--r-- | src/client/client.c | 69 | ||||
| -rwxr-xr-x | src/client/layer.c | 12 | ||||
| -rwxr-xr-x | src/net/tcp.c | 368 | ||||
| -rw-r--r-- | src/net/tcp.h | 35 |
9 files changed, 548 insertions, 106 deletions
@@ -1 +1 @@ -#define TUXANCI_VERSION "svn184" +#define TUXANCI_VERSION "svn185" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ede12bd..142608f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ SET ( WORKDIR ${CMAKE_SOURCE_DIR}/src ) SET ( SRC_base ${WORKDIR}/base/main ${WORKDIR}/base/shot ${WORKDIR}/base/space ${WORKDIR}/base/checkFront ${WORKDIR}/base/storage ${WORKDIR}/base/myTimer ${WORKDIR}/base/protect ${WORKDIR}/base/list ${WORKDIR}/base/arena ${WORKDIR}/base/server ${WORKDIR}/base/textFile ${WORKDIR}/base/gun ${WORKDIR}/base/arenaFile ${WORKDIR}/base/modules ${WORKDIR}/base/net_multiplayer ${WORKDIR}/base/index ${WORKDIR}/base/item ${WORKDIR}/base/tux ${WORKDIR}/base/director ${WORKDIR}/base/idManager ${WORKDIR}/base/homeDirector ${WORKDIR}/base/proto ${WORKDIR}/base/exportFunction -${WORKDIR}/net/udp ${WORKDIR}/net/dns +${WORKDIR}/net/udp ${WORKDIR}/net/tcp ${WORKDIR}/net/dns ) SET ( SRC_client diff --git a/src/base/arena.c b/src/base/arena.c index 4abf1f7..e5c90c0 100644 --- a/src/base/arena.c +++ b/src/base/arena.c @@ -150,11 +150,16 @@ static void action_drawShot(space_t *space, shot_t *shot, void *p) drawShot(shot); } +#if 0 + +#endif + void drawArena(arena_t *arena) { int screen_x, screen_y; tux_t *tux = NULL; int i, j; + //int count = 0; tux = getControlTux(TUX_CONTROL_KEYBOARD_RIGHT); @@ -162,27 +167,30 @@ void drawArena(arena_t *arena) { return; } -/* + screen_x = tux->x - WINDOW_SIZE_X/2; screen_y = tux->y - WINDOW_SIZE_Y/2; -*/ + getCenterScreen(&screen_x, &screen_y, tux->x, tux->y); for( i = screen_y/arena->background->h ; - i <= screen_y/arena->background->h + WINDOW_SIZE_Y/arena->background->h+1 ; i++ ) + i <= screen_y/arena->background->h + WINDOW_SIZE_Y/arena->background->h ; i++ ) { for( j = screen_x/arena->background->w ; - j <= screen_x/arena->background->w + WINDOW_SIZE_X/arena->background->w+1 ; j++ ) + j <= screen_x/arena->background->w + WINDOW_SIZE_X/arena->background->w ; j++ ) { addLayer(arena->background, j * arena->background->w, i * arena->background->h, 0, 0, arena->background->w, arena->background->h, -100); - } + //count++; + } } + //printf("arena count = %d\n", count); + actionSpaceFromLocation(arena->spaceTux, action_drawTux, NULL, screen_x, screen_y, WINDOW_SIZE_X, WINDOW_SIZE_Y); diff --git a/src/base/server.c b/src/base/server.c index ce4f848..8389d04 100644 --- a/src/base/server.c +++ b/src/base/server.c @@ -51,6 +51,57 @@ static list_t *listServerTimer; static time_t timeUpdate; static int maxClients; +typedef struct proto_cmd_server_struct +{ + char *name; + int len; + int tux; + void (*fce_proto)(client_t *,char *msg); +} proto_cmd_server_t; + +static proto_cmd_server_t proto_cmd_list[] = +{ + { .name = "hello", .len = 5, .tux = 0, .fce_proto = proto_recv_hello_server }, + { .name = "status", .len = 6, .tux = 0, .fce_proto = proto_recv_status_server }, +#ifdef PUBLIC_SERVER + { .name = "list", .len = 4, .tux = 0, .fce_proto = proto_recv_listscore_server }, +#endif + { .name = "event", .len = 5, .tux = 1, .fce_proto = proto_recv_event_server }, + { .name = "check", .len = 5, .tux = 1, .fce_proto = proto_recv_check_server }, + { .name = "module", .len = 6, .tux = 1, .fce_proto = proto_recv_module_server }, + { .name = "chat", .len = 4, .tux = 1, .fce_proto = proto_recv_chat_server }, + { .name = "ping", .len = 4, .tux = 1, .fce_proto = proto_recv_ping_server }, + { .name = "end", .len = 3, .tux = 1, .fce_proto = proto_recv_end_server }, + { .name = "", .len = 0, .tux = 0, .fce_proto = NULL }, +}; + +static proto_cmd_server_t* findCmdProto(client_t *client, char *msg) +{ + int len; + int i; + + len = strlen(msg); + + for( i = 0 ; proto_cmd_list[i].len != 0 ; i++ ) + { + proto_cmd_server_t *thisCmd; + + thisCmd = &proto_cmd_list[i]; + + if( len >= thisCmd->len && strncmp(msg, thisCmd->name, thisCmd->len) == 0 ) + { + if( thisCmd->tux && client->tux == NULL ) + { + return NULL; + } + + return thisCmd; + } + } + + return NULL; +} + static void startUpdateServer() { timeUpdate = time(NULL); @@ -61,7 +112,7 @@ time_t getUpdateServer() return time(NULL) - timeUpdate; } -static client_t* newUdpClient(sock_udp_t *sock_udp) +static client_t* newUdpClient(sock_udp_t *sock_udp, sock_udp_t *socket_udp_server) { client_t *new; char str_ip[STR_IP_SIZE]; @@ -80,6 +131,7 @@ static client_t* newUdpClient(sock_udp_t *sock_udp) new->listSeesShot = newList(); new->protect = newProtect(); new->socket_udp = sock_udp; + new->socket_udp_server = socket_udp_server; return new; } @@ -303,26 +355,7 @@ void sendClient(client_t *p, char *msg) } #endif - if( p->socket_udp->proto == PROTO_UDPv4 && - sock_server_udp != NULL && - sock_server_udp->proto == PROTO_UDPv4 ) - { - ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); - } - - if( p->socket_udp->proto == PROTO_UDPv6 && - sock_server_udp != NULL && - sock_server_udp->proto == PROTO_UDPv6 ) - { - ret = writeUdpSocket(sock_server_udp, p->socket_udp, msg, strlen(msg)); - } - - if( p->socket_udp->proto == PROTO_UDPv6 && - sock_server_udp_second != NULL && - sock_server_udp_second->proto == PROTO_UDPv6 ) - { - ret = writeUdpSocket(sock_server_udp_second, p->socket_udp, msg, strlen(msg)); - } + ret = writeUdpSocket(p->socket_udp_server, p->socket_udp, msg, strlen(msg)); } } @@ -487,14 +520,14 @@ void sendInfoCreateClient(client_t *client) } } -static void eventCreateNewUdpClient(sock_udp_t *socket_udp) +static void eventCreateNewUdpClient(sock_udp_t *socket_udp, sock_udp_t *socket_udp_server) { client_t *client; int offset; assert( socket_udp != NULL ); - client = newUdpClient( socket_udp ); + client = newUdpClient( socket_udp, socket_udp_server ); offset = addToIndex(listClientIndex, getSockUdpPort(client->socket_udp) ); @@ -508,6 +541,7 @@ static void eventCreateNewUdpClient(sock_udp_t *socket_udp) static void eventClientBuffer(client_t *client) { + proto_cmd_server_t* protoCmd; char *line; int i; @@ -518,6 +552,7 @@ static void eventClientBuffer(client_t *client) for( i = 0 ; i < client->listRecvMsg->count ; i++ ) { line = (char *)client->listRecvMsg->list[i]; + protoCmd = findCmdProto(client, line); #ifndef PUBLIC_SERVER if( isParamFlag("--recv") ) @@ -526,68 +561,12 @@ static void eventClientBuffer(client_t *client) } #endif - rereshLastPing(client->protect); - - if( strncmp(line, "hello", 5) == 0 ) + if( protoCmd != NULL ) { - proto_recv_hello_server(client, line); - continue; + protoCmd->fce_proto(client, line); + rereshLastPing(client->protect); } - - if( strncmp(line, "status", 6) == 0 ) - { - proto_recv_status_server(client, line); - continue; - } - -#ifdef PUBLIC_SERVER - if( strncmp(line, "list", 4) == 0 ) - { - proto_recv_listscore_server(client, line); - continue; - } -#endif - - if( client->tux != NULL ) - { - if( strncmp(line, "event", 5) == 0 ) - { - proto_recv_event_server(client, line); - continue; - } - - if( strncmp(line, "check", 5) == 0 ) - { - proto_recv_check_server(client, line); - continue; - } - - if( strncmp(line, "module", 7) == 0 ) - { - proto_recv_module_server(client, line); - continue; - } - - if( strncmp(line, "chat", 4) == 0 ) - { - proto_recv_chat_server(client, line); - continue; - } - - if( strncmp(line, "ping", 4) == 0 ) - { - proto_recv_ping_server(client, line); - continue; - } - - if( strncmp(line, "end", 3) == 0 ) - { - proto_recv_end_server(client, line); - continue; - } - } - - if( client->tux != NULL ) + else { proto_send_error_server(PROTO_SEND_ONE, client, PROTO_ERROR_CODE_BAD_COMMAND); } @@ -652,7 +631,7 @@ static void eventClientUdpSelect(sock_udp_t *sock_server) return; } - eventCreateNewUdpClient(sock_client); + eventCreateNewUdpClient(sock_client, sock_server); client = findUdpClient(sock_client); isCreateNewClient = TRUE; } diff --git a/src/base/server.h b/src/base/server.h index 0da4969..8eaf8d1 100644 --- a/src/base/server.h +++ b/src/base/server.h @@ -25,6 +25,7 @@ typedef struct client_struct { + sock_udp_t *socket_udp_server; sock_udp_t *socket_udp; int status; diff --git a/src/client/client.c b/src/client/client.c index cba5248..a0b59c4 100644 --- a/src/client/client.c +++ b/src/client/client.c @@ -37,6 +37,52 @@ static list_t *clientBuffer; static my_time_t lastPing; static my_time_t lastPingServerAlive; +typedef struct proto_cmd_client_struct +{ + char *name; + int len; + void (*fce_proto)(char *msg); +} proto_cmd_client_t; + +static proto_cmd_client_t proto_cmd_list[] = +{ + { .name = "error", .len = 5, .fce_proto = proto_recv_error_client }, + { .name = "init", .len = 4, .fce_proto = proto_recv_init_client }, + { .name = "event", .len = 5, .fce_proto = proto_recv_event_client }, + { .name = "newtux", .len = 6, .fce_proto = proto_recv_newtux_client }, + { .name = "del", .len = 3, .fce_proto = proto_recv_del_client }, + { .name = "additem", .len = 7, .fce_proto = proto_recv_additem_client }, + { .name = "shot", .len = 4, .fce_proto = proto_recv_shot_client }, + { .name = "kill", .len = 4, .fce_proto = proto_recv_kill_client }, + { .name = "module", .len = 6, .fce_proto = proto_recv_module_client }, + { .name = "chat", .len = 4, .fce_proto = proto_recv_chat_client }, + { .name = "ping", .len = 4, .fce_proto = proto_recv_ping_client }, + { .name = "end", .len = 3, .fce_proto = proto_recv_end_client }, + { .name = "", .len = 0, .fce_proto = NULL }, +}; + +static proto_cmd_client_t* findCmdProto(char *msg) +{ + int len; + int i; + + len = strlen(msg); + + for( i = 0 ; proto_cmd_list[i].len != 0 ; i++ ) + { + proto_cmd_client_t *thisCmd; + + thisCmd = &proto_cmd_list[i]; + + if( len >= thisCmd->len && strncmp(msg, thisCmd->name, thisCmd->len) == 0 ) + { + return thisCmd; + } + } + + return NULL; +} + static void initClient() { char name[STR_NAME_SIZE]; @@ -87,7 +133,7 @@ static int eventServerSelect() char buffer[STR_PROTO_SIZE]; int ret; - memset(buffer,0 ,STR_PROTO_SIZE); + memset(buffer, 0, STR_PROTO_SIZE); ret = readUdpSocket(sock_server_udp, sock_server_udp, buffer, STR_PROTO_SIZE-1); @@ -103,6 +149,7 @@ static int eventServerSelect() static void eventServerBuffer() { + proto_cmd_client_t *protoCmd; char *line; int i; @@ -113,6 +160,7 @@ static void eventServerBuffer() for( i = 0 ; i < clientBuffer->count ; i++ ) { line = (char *)clientBuffer->list[i]; + protoCmd = findCmdProto(line); #ifndef PUBLIC_SERVER if( isParamFlag("--recv") ) @@ -120,20 +168,11 @@ static void eventServerBuffer() 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, "module", 6) == 0 )proto_recv_module_client(line); - if( strncmp(line, "chat", 4) == 0 )proto_recv_chat_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(); + if( protoCmd != NULL ) + { + protoCmd->fce_proto(line); + lastPingServerAlive = getMyTime(); + } } destroyListItem(clientBuffer, free); diff --git a/src/client/layer.c b/src/client/layer.c index efac264..c0c9c03 100755 --- a/src/client/layer.c +++ b/src/client/layer.c @@ -108,6 +108,7 @@ void drawLayerCenter(int x, int y) layer_t *this; int screen_x, screen_y; int i; + //int count = 0; getCenterScreen(&screen_x, &screen_y, x, y); @@ -115,11 +116,20 @@ void drawLayerCenter(int x, int y) { this = (layer_t *)listLayer->list[i]; +/* if( (this->x - screen_x) + this->w < 0 || (this->x - screen_x) > WINDOW_SIZE_X || (this->y - screen_y) + this->h < 0 || (this->y - screen_y) > WINDOW_SIZE_Y ) { continue; } +*/ + if( this->x + this->w < screen_x || this->x > screen_x + WINDOW_SIZE_X || + this->y + this->h < screen_y || this->y > screen_y + WINDOW_SIZE_Y ) + { + continue; + } + + //count++; drawImage(this->image, this->x - screen_x, this->y - screen_y, @@ -129,6 +139,8 @@ void drawLayerCenter(int x, int y) //printf("%d %d\n", this->x - screen_x, this->y - screen_y); } + //printf("count = %d\n", count); + destroyListItem(listLayer, free); listLayer = newList(); } diff --git a/src/net/tcp.c b/src/net/tcp.c new file mode 100755 index 0000000..2df17b9 --- /dev/null +++ b/src/net/tcp.c @@ -0,0 +1,368 @@ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <sys/poll.h> +#include <sys/types.h> +#include <sys/time.h> +#include <sys/socket.h> +#include <sys/select.h> +#include <stdio.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <assert.h> +#include <netdb.h> + +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> + +#include "tcp.h" + +sock_tcp_t* newSockTcp(int proto) +{ + sock_tcp_t *new; + + new = malloc( sizeof(sock_tcp_t) ); + memset(new, 0, sizeof(sock_tcp_t)); + new->proto = proto; + + return new; +} + +void destroySockTcp(sock_tcp_t *p) +{ + assert( p != NULL ); + free(p); +} + +sock_tcp_t* bindTcpSocket(char *address, int port, int proto) +{ + sock_tcp_t *new; + int len; + int ret; + + assert( port > 0 && port < 65535 ); + + new = newSockTcp(proto); + + assert( new != NULL ); + + + if( new->proto == PROTO_TCPv4 ) + { + new->sock = socket(AF_INET, SOCK_STREAM, 0); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_TCPv6 ) + { + new->sock = socket(AF_INET6, SOCK_STREAM, 0); + } +#endif + + if( new->sock < 0 ) + { + fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); + destroySockTcp(new); + return NULL; + } + + if( new->proto == PROTO_TCPv4 ) + { + new->sockAddr.sin_family = AF_INET; + inet_pton(AF_INET, address, &(new->sockAddr.sin_addr)); + new->sockAddr.sin_port = htons(port); + + len = sizeof(new->sockAddr); + ret = bind(new->sock, (struct sockaddr *)&new->sockAddr, len); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_TCPv6 ) + { + new->sockAddr6.sin6_family = AF_INET6; + //new->sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); + inet_pton(AF_INET6, address, &(new->sockAddr6.sin6_addr)); + new->sockAddr6.sin6_port = htons(port); + + len = sizeof(new->sockAddr6); + ret = bind(new->sock, (struct sockaddr *)&new->sockAddr6, len); + } +#endif + + if( ret < 0 ) + { + fprintf(stderr, "Nemozem obsadit sietovy port %d\n", port); + destroySockTcp(new); + return NULL; + } + + listen(new->sock, 5); + + return new; +} + +sock_tcp_t* getTcpNewClient(sock_tcp_t *p) +{ + sock_tcp_t *new; + int client_len; + + assert( p != NULL ); + assert( p->sock >= 0 ); + + new = newSockTcp(p->proto); + + if( new->proto == PROTO_TCPv4 ) + { + client_len = sizeof(new->sockAddr); + + new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr, + (socklen_t *)&client_len); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_TCPv6 ) + { + client_len = sizeof(new->sockAddr6); + + new->sock = accept(p->sock, (struct sockaddr *)&new->sockAddr6, + (socklen_t *)&client_len); + } +#endif + + if( new->sock < 0 ) + { + //printf("XXX\n"); + destroySockTcp(new); + return NULL; + } + + return new; +} + +void getSockTcpIp(sock_tcp_t *p, char *str_ip, int len) +{ + assert( p != NULL ); + assert( str_ip != NULL ); + + + if( p->proto == PROTO_TCPv4 ) + { + inet_ntop(AF_INET, &(p->sockAddr.sin_addr), str_ip, len); + strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr)); + //printf("strcpy(str_ip, inet_ntoa(p->sockAddr.sin_addr));\n"); + } + +#ifdef SUPPORT_IPv6 + if( p->proto == PROTO_TCPv6 ) + { + inet_ntop(AF_INET6, &(p->sockAddr6.sin6_addr), str_ip, len); + } +#endif +} + +int getSockTcpPort(sock_tcp_t *p) +{ + assert( p != NULL ); + + if( p->proto == PROTO_TCPv4 ) + { + return htons(p->sockAddr.sin_port); + } + +#ifdef SUPPORT_IPv6 + if( p->proto == PROTO_TCPv6 ) + { + return htons(p->sockAddr6.sin6_port); + } +#endif + + assert( ! "bad proto !" ); + + return -1; +} + +sock_tcp_t* connectTcpSocket(char *ip, int port, int proto) +{ + sock_tcp_t *new; + int len; + int ret; + + assert( ip != NULL ); + assert( port > 0 && port < 65535 ); + + new = newSockTcp(proto); + + if( new->proto == PROTO_TCPv4 ) + { + new->sock = socket(AF_INET, SOCK_STREAM, 0); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_TCPv6 ) + { + new->sock = socket(AF_INET6, SOCK_STREAM, 0); + } +#endif + + if( new->sock < 0 ) + { + fprintf(stderr, "Nemozem vytvorit sietovy socket\n"); + destroySockTcp(new); + return NULL; + } + + if( new->proto == PROTO_TCPv4 ) + { + new->sockAddr.sin_family = AF_INET; + new->sockAddr.sin_addr.s_addr = inet_addr(ip); + new->sockAddr.sin_port = htons(port); + + len = sizeof(new->sockAddr); + ret = connect(new->sock, (struct sockaddr *)&new->sockAddr, len); + } + +#ifdef SUPPORT_IPv6 + if( new->proto == PROTO_TCPv6 ) + { + new->sockAddr6.sin6_family = AF_INET6; + inet_pton(AF_INET6, ip, &(new->sockAddr6.sin6_addr)); + new->sockAddr6.sin6_port = htons(port); + + len = sizeof(new->sockAddr6); + ret = connect(new->sock, (struct sockaddr *)&new->sockAddr6, len); + } +#endif + + if( ret < 0 ) + { + fprintf(stderr, "Nemozem sa pripojit na %s %d\n", ip, port); + destroySockTcp(new); + return NULL; + } + + + return new; +} + +int disableNagle(sock_tcp_t *p) +{ + int flag = 1; + + int result; + + result = setsockopt(p->sock, /* socket affected */ + IPPROTO_TCP, /* set option at TCP level */ + TCP_NODELAY, /* name of option */ + (char *) &flag, /* the cast is historical cruft */ + sizeof(int)); /* length of option value */ + + if( result < 0 ) + { + printf("disabled nagle error\n"); + } + else + { + printf("disabled nagle OK\n"); + } + + return result; +} + +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); +} + +#if 0 +#define MAX_CLIENTS 32 +#define BUF_SIZE 512 + +int main(int argc, char **argv) +{ + sock_tcp_t *sock; + sock_tcp_t *client[MAX_CLIENTS]; + int count_client; + int changed, i; + struct pollfd *tmp; + + sock = bindTcpSocket( "127.0.0.1", 2200, PROTO_TCPv4 ); + disableNagle(sock); + count_client = 0; + + for(;;) + { + tmp = malloc( (count_client+1) * sizeof(struct pollfd) ); + + tmp[0].fd = sock->sock; + tmp[0].events = POLLIN; + tmp[0].revents = 0; + + for( i = 0 ; i < count_client ; i++ ) + { + tmp[1+i].fd = client[i]->sock; + tmp[1+i].events = POLLIN; + tmp[1+i].revents = 0; + } + + changed = poll(tmp, count_client+1, 1000); + + if( tmp[0].revents & POLLIN != 0 ) + { + client[count_client] = getTcpNewClient(sock); + disableNagle(client[count_client]); + count_client++; + } + + for( i = 0 ; i < count_client ; i++ ) + { + if( tmp[1+i].revents & POLLIN != 0 ) + { + char buf[BUF_SIZE]; + int ret; + + ret = read(client[i]->sock, buf, BUF_SIZE); + + if( ret <= 0 ) + { + closeTcpSocket(client[i]); + + memmove(client + i, + client + (i+1), + ((count_client-1) - i) * sizeof(sock_tcp_t *) ); + + count_client--; + continue; + } + + write(client[i]->sock, buf, ret); + } + } + + free(tmp); + } +} +#endif + diff --git a/src/net/tcp.h b/src/net/tcp.h new file mode 100644 index 0000000..8c436c3 --- /dev/null +++ b/src/net/tcp.h @@ -0,0 +1,35 @@ + +#ifndef MY_TCP_H + +#define MY_TCP_H + +#include <netinet/in.h> + +#define SUPPORT_IPv6 + +#define PROTO_TCPv4 0 +#define PROTO_TCPv6 1 + +typedef struct struct_sock_tcp_t +{ + int sock; + int proto; + + struct sockaddr_in sockAddr; +#ifdef SUPPORT_IPv6 + struct sockaddr_in6 sockAddr6; +#endif +} sock_tcp_t; + +extern sock_tcp_t* newSockTcp(int proto); +extern void destroySockTcp(sock_tcp_t *p); +extern sock_tcp_t* bindTcpSocket(char *addresss, int port, int proto); +extern sock_tcp_t* getTcpNewClient(sock_tcp_t *p); +extern void getSockTcpIp(sock_tcp_t *p, char *str_ip, int len); +extern int getSockTcpPort(sock_tcp_t *p); +extern sock_tcp_t* connectTcpSocket(char *ip, int port, int proto); +extern int readTcpSocket(sock_tcp_t *p, void *address, int len); +extern int writeTcpSocket(sock_tcp_t *p, void *address, int len); +extern void closeTcpSocket(sock_tcp_t *p); + +#endif |