diff options
-rw-r--r-- | configure.in | 16 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/i_net.c | 243 |
3 files changed, 124 insertions, 139 deletions
diff --git a/configure.in b/configure.in index 74b58d21..3d9011d5 100644 --- a/configure.in +++ b/configure.in @@ -15,17 +15,25 @@ AM_PATH_SDL(1.1.3) AC_CHECK_LIB(SDL_mixer,Mix_LoadMUS,[ dnl AC_DEFINE(HAVE_LIBSDL_MIXER) - MIXER_LIBS="$MIXER_LIBS -lSDL_mixer" + SDLMIXER_LIBS="$SDLMIXER_LIBS -lSDL_mixer" ],echo "*** Compiling without SDL_mixer installed probably won't work" -,$SDL_LIBS $MIXER_LIBS) +,$SDL_LIBS $SDLMIXER_LIBS) + +AC_CHECK_LIB(SDL_net,SDLNet_UDP_Send,[ + SDLNET_LIBS="$SDLNET_LIBS -lSDL_net" +],echo "*** Compiling without SDL_net installed probably won't work" +,$SDL_LIBS $SDLNET_LIBS) AM_INIT_AUTOMAKE(AC_PACKAGE_TARNAME,AC_PACKAGE_VERSION) AM_CONFIG_HEADER(config.h:config.hin) -AC_SUBST(MIXER_CFLAGS) -AC_SUBST(MIXER_LIBS) +AC_SUBST(SDLMIXER_CFLAGS) +AC_SUBST(SDLMIXER_LIBS) + +AC_SUBST(SDLNET_CFLAGS) +AC_SUBST(SDLNET_LIBS) AC_OUTPUT([ Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 96a762a1..dd0b8130 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,8 @@ bindir = $(prefix)/bin -CFLAGS = @CFLAGS@ @SDL_CFLAGS@ @MIXER_CFLAGS@ -DNORMALUNIX -Wall -LDFLAGS = @LDFLAGS@ @SDL_LIBS@ @MIXER_LIBS@ +CFLAGS = @CFLAGS@ @SDL_CFLAGS@ @SDLMIXER_CFLAGS@ @SDLNET_CFLAGS@ -DNORMALUNIX -Wall +LDFLAGS = @LDFLAGS@ @SDL_LIBS@ @SDLMIXER_LIBS@ @SDLNET_LIBS@ chocolate_doom_SOURCES=\ am_map.c d_think.h i_video.c p_floor.c p_tick.c r_things.h \ diff --git a/src/i_net.c b/src/i_net.c index e54b7b31..11ed335d 100644 --- a/src/i_net.c +++ b/src/i_net.c @@ -1,7 +1,7 @@ // Emacs style mode select -*- C++ -*- //----------------------------------------------------------------------------- // -// $Id: i_net.c 37 2005-08-04 18:42:15Z fraggle $ +// $Id: i_net.c 52 2005-08-12 16:54:15Z fraggle $ // // Copyright(C) 1993-1996 Id Software, Inc. // Copyright(C) 2005 Simon Howard @@ -22,6 +22,9 @@ // 02111-1307, USA. // // $Log$ +// Revision 1.5 2005/08/12 16:54:15 fraggle +// Port network code to use SDL_net +// // Revision 1.4 2005/08/04 18:42:15 fraggle // Silence compiler warnings // @@ -40,19 +43,13 @@ //----------------------------------------------------------------------------- static const char -rcsid[] = "$Id: i_net.c 37 2005-08-04 18:42:15Z fraggle $"; +rcsid[] = "$Id: i_net.c 52 2005-08-12 16:54:15Z fraggle $"; #include <stdlib.h> #include <string.h> #include <stdio.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <errno.h> -#include <unistd.h> -#include <netdb.h> -#include <sys/ioctl.h> +#include <SDL_net.h> #include "i_system.h" #include "d_event.h" @@ -75,51 +72,61 @@ boolean NetListen (void); // NETWORKING // -int DOOMPORT = (IPPORT_USERRESERVED +0x1d ); +int DOOMPORT = 8626; -int sendsocket; -int insocket; +static UDPsocket udpsocket; +static UDPpacket *packet; -struct sockaddr_in sendaddress[MAXNETNODES]; +static IPaddress sendaddress[MAXNETNODES]; void (*netget) (void); void (*netsend) (void); -// -// UDPsocket -// -int UDPsocket (void) +unsigned short host_to_net16(unsigned int value) { - int s; - - // allocate a socket - s = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (s<0) - I_Error ("can't create socket: %s",strerror(errno)); - - return s; + union + { + unsigned short s; + char b[2]; + } data; + + SDLNet_Write16(value, data.b); + + return data.s; } -// -// BindToLocalPort -// -void -BindToLocalPort -( int s, - int port ) +unsigned short net_to_host16(unsigned int value) { - int v; - struct sockaddr_in address; - - memset (&address, 0, sizeof(address)); - address.sin_family = AF_INET; - address.sin_addr.s_addr = INADDR_ANY; - address.sin_port = port; - - v = bind (s, (void *)&address, sizeof(address)); - if (v == -1) - I_Error ("BindToPort: bind: %s", strerror(errno)); + unsigned short s = value; + + return SDLNet_Read16(&s); +} + +unsigned long host_to_net32(unsigned int value) +{ + union + { + unsigned long l; + char b[4]; + } data; + + SDLNet_Write32(value, data.b); + + return data.l; +} + +unsigned long net_to_host32(unsigned int value) +{ + union + { + unsigned long l; + char b[4]; + } data; + + data.l = value; + + return SDLNet_Read32(data.b); } @@ -129,31 +136,34 @@ BindToLocalPort void PacketSend (void) { int c; - doomdata_t sw; + doomdata_t *sw; + + sw = (doomdata_t *) packet->data; // byte swap - sw.checksum = htonl(netbuffer->checksum); - sw.player = netbuffer->player; - sw.retransmitfrom = netbuffer->retransmitfrom; - sw.starttic = netbuffer->starttic; - sw.numtics = netbuffer->numtics; + sw->checksum = host_to_net32(netbuffer->checksum); + sw->player = netbuffer->player; + sw->retransmitfrom = netbuffer->retransmitfrom; + sw->starttic = netbuffer->starttic; + sw->numtics = netbuffer->numtics; + for (c=0 ; c< netbuffer->numtics ; c++) { - sw.cmds[c].forwardmove = netbuffer->cmds[c].forwardmove; - sw.cmds[c].sidemove = netbuffer->cmds[c].sidemove; - sw.cmds[c].angleturn = htons(netbuffer->cmds[c].angleturn); - sw.cmds[c].consistancy = htons(netbuffer->cmds[c].consistancy); - sw.cmds[c].chatchar = netbuffer->cmds[c].chatchar; - sw.cmds[c].buttons = netbuffer->cmds[c].buttons; + sw->cmds[c].forwardmove = netbuffer->cmds[c].forwardmove; + sw->cmds[c].sidemove = netbuffer->cmds[c].sidemove; + sw->cmds[c].angleturn = host_to_net16(netbuffer->cmds[c].angleturn); + sw->cmds[c].consistancy = host_to_net16(netbuffer->cmds[c].consistancy); + sw->cmds[c].chatchar = netbuffer->cmds[c].chatchar; + sw->cmds[c].buttons = netbuffer->cmds[c].buttons; + } + + packet->len = doomcom->datalength; + packet->address = sendaddress[doomcom->remotenode]; + + if (!SDLNet_UDP_Send(udpsocket, -1, packet)) + { + I_Error("Error sending packet: %s", SDLNet_GetError()); } - - //printf ("sending %i\n",gametic); - c = sendto (sendsocket , &sw, doomcom->datalength - ,0,(void *)&sendaddress[doomcom->remotenode] - ,sizeof(sendaddress[doomcom->remotenode])); - - // if (c == -1) - // I_Error ("SendPacket error: %s",strerror(errno)); } @@ -164,31 +174,26 @@ void PacketGet (void) { int i; int c; - struct sockaddr_in fromaddress; - int fromlen; - doomdata_t sw; + doomdata_t *sw; + int packets_read; - fromlen = sizeof(fromaddress); - c = recvfrom (insocket, &sw, sizeof(sw), 0 - , (struct sockaddr *)&fromaddress, &fromlen ); - if (c == -1 ) + packets_read = SDLNet_UDP_Recv(udpsocket, packet); + + if (packets_read < 0) { - if (errno != EWOULDBLOCK) - I_Error ("GetPacket: %s",strerror(errno)); - doomcom->remotenode = -1; // no packet - return; + I_Error("Error reading packet: %s\n", SDLNet_GetError()); } + if (packets_read == 0) { - static int first=1; - if (first) - printf("len=%d:p=[0x%x 0x%x] \n", c, *(int*)&sw, *((int*)&sw+1)); - first = 0; + doomcom->remotenode = -1; + return; } // find remote node number for (i=0 ; i<doomcom->numnodes ; i++) - if ( fromaddress.sin_addr.s_addr == sendaddress[i].sin_addr.s_addr ) + if (packet->address.host == sendaddress[i].host + && packet->address.port == sendaddress[i].port) break; if (i == doomcom->numnodes) @@ -198,57 +203,37 @@ void PacketGet (void) return; } - doomcom->remotenode = i; // good packet from a game player - doomcom->datalength = c; + doomcom->remotenode = i; // good packet from a game player + doomcom->datalength = packet->len; + + sw = (doomdata_t *) packet->data; // byte swap - netbuffer->checksum = ntohl(sw.checksum); - netbuffer->player = sw.player; - netbuffer->retransmitfrom = sw.retransmitfrom; - netbuffer->starttic = sw.starttic; - netbuffer->numtics = sw.numtics; + netbuffer->checksum = net_to_host32(sw->checksum); + netbuffer->player = sw->player; + netbuffer->retransmitfrom = sw->retransmitfrom; + netbuffer->starttic = sw->starttic; + netbuffer->numtics = sw->numtics; for (c=0 ; c< netbuffer->numtics ; c++) { - netbuffer->cmds[c].forwardmove = sw.cmds[c].forwardmove; - netbuffer->cmds[c].sidemove = sw.cmds[c].sidemove; - netbuffer->cmds[c].angleturn = ntohs(sw.cmds[c].angleturn); - netbuffer->cmds[c].consistancy = ntohs(sw.cmds[c].consistancy); - netbuffer->cmds[c].chatchar = sw.cmds[c].chatchar; - netbuffer->cmds[c].buttons = sw.cmds[c].buttons; + netbuffer->cmds[c].forwardmove = sw->cmds[c].forwardmove; + netbuffer->cmds[c].sidemove = sw->cmds[c].sidemove; + netbuffer->cmds[c].angleturn = net_to_host16(sw->cmds[c].angleturn); + netbuffer->cmds[c].consistancy = net_to_host16(sw->cmds[c].consistancy); + netbuffer->cmds[c].chatchar = sw->cmds[c].chatchar; + netbuffer->cmds[c].buttons = sw->cmds[c].buttons; } } - -int GetLocalAddress (void) -{ - char hostname[1024]; - struct hostent* hostentry; // host information entry - int v; - - // get local address - v = gethostname (hostname, sizeof(hostname)); - if (v == -1) - I_Error ("GetLocalAddress : gethostname: errno %d",errno); - - hostentry = gethostbyname (hostname); - if (!hostentry) - I_Error ("GetLocalAddress : gethostbyname: couldn't get local host"); - - return *(int *)hostentry->h_addr_list[0]; -} - - // // I_InitNetwork // void I_InitNetwork (void) { - boolean trueval = true; int i; int p; - struct hostent* hostentry; // host information entry doomcom = malloc (sizeof (*doomcom) ); memset (doomcom, 0, sizeof(*doomcom) ); @@ -300,25 +285,18 @@ void I_InitNetwork (void) doomcom->consoleplayer = myargv[i+1][0]-'1'; doomcom->numnodes = 1; // this node for sure + + SDLNet_Init(); i++; while (++i < myargc && myargv[i][0] != '-') { - sendaddress[doomcom->numnodes].sin_family = AF_INET; - sendaddress[doomcom->numnodes].sin_port = htons(DOOMPORT); - if (myargv[i][0] == '.') - { - sendaddress[doomcom->numnodes].sin_addr.s_addr - = inet_addr (myargv[i]+1); - } - else - { - hostentry = gethostbyname (myargv[i]); - if (!hostentry) - I_Error ("gethostbyname: couldn't find %s", myargv[i]); - sendaddress[doomcom->numnodes].sin_addr.s_addr - = *(int *)hostentry->h_addr_list[0]; - } + if (SDLNet_ResolveHost(&sendaddress[doomcom->numnodes], + myargv[i], DOOMPORT)) + { + I_Error("Unable to resolve %s", myargv[i]); + } + doomcom->numnodes++; } @@ -326,11 +304,10 @@ void I_InitNetwork (void) doomcom->numplayers = doomcom->numnodes; // build message to receive - insocket = UDPsocket (); - BindToLocalPort (insocket,htons(DOOMPORT)); - ioctl (insocket, FIONBIO, &trueval); - sendsocket = UDPsocket (); + udpsocket = SDLNet_UDP_Open(DOOMPORT); + + packet = SDLNet_AllocPacket(5000); } |