X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fnetserv.c;h=02a014fd86f3757a6b3891bbb302b7cfb2007d1c;hb=a71a2ef6b05438e43b6c4f8d475a3010e43b9beb;hp=7c38ceed788b44a2f5921dc6a9c011c2a134cc73;hpb=fe81860be6d728f992a7ce5af51d6b7e84e2136e;p=rocksndiamonds.git diff --git a/src/netserv.c b/src/netserv.c index 7c38ceed..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; @@ -483,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()); - lfd = SDLNet_TCP_Open(&ip); - if (!lfd) - Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_TCP_Open() failed"); + if ((fds = SDLNet_AllocSocketSet(MAX_PLAYERS + 1 + 1)) == NULL) + Error(ERR_EXIT_NETWORK_SERVER, "SDLNet_AllocSocketSet() failed: %s"), + SDLNet_GetError(); - fds = SDLNet_AllocSocketSet(MAX_PLAYERS+1); - SDLNet_TCP_AddSocket(fds, lfd); + 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(); + + 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) { @@ -510,9 +525,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 +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)) { @@ -622,7 +655,6 @@ void NetworkServer(int port, int serveronly) if (player && !interrupt) player = player->next; } - while (player && !interrupt); } }