X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fnetserv.c;h=02a014fd86f3757a6b3891bbb302b7cfb2007d1c;hb=014f8db5b98964dddaf817ce5cd8cf7930bc7ae4;hp=6555e0497922502843b7ef909c34cfc69be28bf1;hpb=3ff2e8a0b5c27b99a9920bdf5ed82bc41bf40181;p=rocksndiamonds.git diff --git a/src/netserv.c b/src/netserv.c index 6555e049..02a014fd 100644 --- a/src/netserv.c +++ b/src/netserv.c @@ -47,7 +47,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; @@ -433,6 +434,13 @@ static void Handle_OP_MOVE_PLAYER(struct NetworkServerPlayerInfo *player) ServerFrameCounter++; } +void ExitNetworkServer(int exit_value) +{ + Error(ERR_NETWORK_SERVER, "exiting network server"); + + exit(exit_value); +} + /* the following is not used for a standalone server; the pointer points to an integer containing the port-number */ int NetworkServerThread(void *ptr) @@ -445,7 +453,6 @@ int NetworkServerThread(void *ptr) void NetworkServer(int port, int serveronly) { - int sl; struct NetworkServerPlayerInfo *player; int r; unsigned int len; @@ -458,6 +465,10 @@ void NetworkServer(int port, int serveronly) if (port == 0) port = DEFAULT_SERVER_PORT; + // if only running the network server, exit on Ctrl-C + if (serveronly) + signal(SIGINT, ExitNetworkServer); + if (!serveronly) onceonly = 1; @@ -473,14 +484,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(); - lfd = SDLNet_TCP_Open(&ip); - if (!lfd) - Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed"); + if ((lfd = SDLNet_TCP_Open(&ip)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed: %s"), + SDLNet_GetError(); - fds = SDLNet_AllocSocketSet(MAX_PLAYERS+1); - SDLNet_TCP_AddSocket(fds, lfd); + if (SDLNet_TCP_AddSocket(fds, lfd) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); + + if ((udp = SDLNet_UDP_Open(port)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_UDP_Open() failed: %s", + SDLNet_GetError()); + + if (SDLNet_UDP_AddSocket(fds, udp) == -1) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_AddSocket() failed: %s"), + SDLNet_GetError(); if (options.verbose) { @@ -496,22 +521,15 @@ void NetworkServer(int port, int serveronly) for (player = first_player; player; player = player->next) flushuser(player); - if ((sl = SDLNet_CheckSockets(fds, 500000)) < 1) - { - Error(ERR_NETWORK_SERVER, "SDLNet_CheckSockets failed: %s", - SDLNet_GetError()); - perror("SDLNet_CheckSockets"); - } - - if (sl < 0) - continue; - - if (sl == 0) + // wait for 100 ms for activity on open network sockets + 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); @@ -520,9 +538,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)) { @@ -621,7 +655,6 @@ void NetworkServer(int port, int serveronly) if (player && !interrupt) player = player->next; } - while (player && !interrupt); } }