// netserv.c
// ============================================================================
-#include "libgame/platform.h"
-
-#if defined(NETWORK_AVALIABLE)
-
#include <fcntl.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
-#include "main.h"
-
#include "libgame/libgame.h"
#include "netserv.h"
+#include "main.h"
+
static int clients = 0;
static int onceonly = 0;
#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;
void NetworkServer(int port, int serveronly)
{
- int sl;
struct NetworkServerPlayerInfo *player;
int r;
unsigned int len;
#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)
{
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);
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))
{
if (player && !interrupt)
player = player->next;
}
- while (player && !interrupt);
}
}
-
-#endif /* NETWORK_AVALIABLE */