X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fnetserv.c;h=963a862b229b345067ca0e64b2ec3a5e3f4d475c;hb=5ef9148d1f605af02e872264519eda261a3e88ca;hp=7c38ceed788b44a2f5921dc6a9c011c2a134cc73;hpb=fe81860be6d728f992a7ce5af51d6b7e84e2136e;p=rocksndiamonds.git diff --git a/src/netserv.c b/src/netserv.c index 7c38ceed..963a862b 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -9,20 +9,16 @@ // netserv.c // ============================================================================ -#include "libgame/platform.h" - -#if defined(NETWORK_AVALIABLE) - #include #include #include #include -#include "main.h" - #include "libgame/libgame.h" #include "netserv.h" +#include "main.h" + static int clients = 0; static int onceonly = 0; @@ -47,7 +43,8 @@ static struct NetworkServerPlayerInfo *first_player = NULL; #define NEXT(player) ((player)->next ? (player)->next : first_player) /* TODO: peer address */ -static TCPsocket lfd; /* listening socket */ +static TCPsocket lfd; /* listening TCP socket */ +static UDPsocket udp; /* listening UDP socket */ static SDLNet_SocketSet fds; /* socket set */ static unsigned char realbuffer[512], *buffer = realbuffer + 4; @@ -132,6 +129,7 @@ static void RemovePlayer(struct NetworkServerPlayerInfo *player) free(player); clients--; +#if 0 /* do not terminate network server if last player disconnected */ if (onceonly && clients == 0) { if (options.verbose) @@ -141,6 +139,7 @@ static void RemovePlayer(struct NetworkServerPlayerInfo *player) } exit(0); } +#endif } static void AddPlayer(TCPsocket fd) @@ -483,14 +482,28 @@ void NetworkServer(int port, int serveronly) #endif if (SDLNet_ResolveHost(&ip, NULL, port) == -1) - Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_ResolveHost() failed"); + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_ResolveHost() failed: %s", + SDLNet_GetError()); + + if ((fds = SDLNet_AllocSocketSet(MAX_PLAYERS + 1 + 1)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_AllocSocketSet() failed: %s"), + SDLNet_GetError(); + + if ((lfd = SDLNet_TCP_Open(&ip)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed: %s"), + SDLNet_GetError(); + + if (SDLNet_TCP_AddSocket(fds, lfd) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); - lfd = SDLNet_TCP_Open(&ip); - if (!lfd) - Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed"); + if ((udp = SDLNet_UDP_Open(port)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_UDP_Open() failed: %s", + SDLNet_GetError()); - fds = SDLNet_AllocSocketSet(MAX_PLAYERS+1); - SDLNet_TCP_AddSocket(fds, lfd); + if (SDLNet_UDP_AddSocket(fds, udp) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); if (options.verbose) { @@ -510,9 +523,11 @@ void NetworkServer(int port, int serveronly) if (SDLNet_CheckSockets(fds, 100) < 1) continue; - /* accept incoming connections */ + /* accept incoming TCP connections */ if (SDLNet_SocketReady(lfd)) { + Error(ERR_DEBUG, "got TCP packet"); + TCPsocket newsock; newsock = SDLNet_TCP_Accept(lfd); @@ -521,9 +536,25 @@ void NetworkServer(int port, int serveronly) AddPlayer(newsock); } + /* accept incoming UDP packets */ + if (SDLNet_SocketReady(udp)) + { + Error(ERR_DEBUG, "got UDP packet"); + + static UDPpacket packet; + + int num_packets = SDLNet_UDP_Recv(udp, &packet); + + if (num_packets == 1) + { + // bounce packet + SDLNet_UDP_Send(udp, -1, &packet); + } + } + player = first_player; - do + while (player && !interrupt) { if (SDLNet_SocketReady(player->fd)) { @@ -622,8 +653,5 @@ void NetworkServer(int port, int serveronly) if (player && !interrupt) player = player->next; } - while (player && !interrupt); } } - -#endif /* NETWORK_AVALIABLE */