123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- #include "Precompiled.h"
- #include "globaldata.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <string>
- #include <errno.h>
- #include "i_system.h"
- #include "d_event.h"
- #include "d_net.h"
- #include "m_argv.h"
- #include "doomstat.h"
- #include "i_net.h"
- #include "doomlib.h"
- void NetSend (void);
- qboolean NetListen (void);
- namespace {
- bool IsValidSocket( int socketDescriptor );
- int GetLastSocketError();
-
- bool IsValidSocket( int socketDescriptor ) {
- return false;
- }
-
- int GetLastSocketError() {
- return 0;
- }
- }
- int DOOMPORT = 1002;
- unsigned long GetServerIP() {
- return ::g->sendaddress[::g->doomcom.consoleplayer].sin_addr.s_addr;
- }
- void (*netget) (void);
- void (*netsend) (void);
- int UDPsocket (void)
- {
- int s;
-
- s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if ( !IsValidSocket( s ) ) {
- int err = GetLastSocketError();
- I_Error( "can't create socket, error %d", err );
- }
- return s;
- }
- void BindToLocalPort( int s, int port )
- {
- }
- void PacketSend (void)
- {
- }
- void PacketGet (void)
- {
- }
- static int I_TrySetupNetwork(void)
- {
-
- return 1;
- }
- void I_InitNetwork (void)
- {
-
- int i;
- int p;
-
-
- memset (&::g->doomcom, 0, sizeof(::g->doomcom) );
-
- i = M_CheckParm ("-dup");
- if (i && i< ::g->myargc-1)
- {
- ::g->doomcom.ticdup = ::g->myargv[i+1][0]-'0';
- if (::g->doomcom.ticdup < 1)
- ::g->doomcom.ticdup = 1;
- if (::g->doomcom.ticdup > 9)
- ::g->doomcom.ticdup = 9;
- }
- else
- ::g->doomcom.ticdup = 1;
- if (M_CheckParm ("-extratic"))
- ::g->doomcom.extratics = 1;
- else
- ::g->doomcom.extratics = 0;
- p = M_CheckParm ("-port");
- if (p && p < ::g->myargc-1)
- {
- DOOMPORT = atoi (::g->myargv[p+1]);
- I_Printf ("using alternate port %i\n",DOOMPORT);
- }
-
-
- i = M_CheckParm ("-net");
- if (!i || !I_TrySetupNetwork())
- {
-
- ::g->netgame = false;
- ::g->doomcom.id = DOOMCOM_ID;
- ::g->doomcom.numplayers = ::g->doomcom.numnodes = 1;
- ::g->doomcom.deathmatch = false;
- ::g->doomcom.consoleplayer = 0;
- return;
- }
- netsend = PacketSend;
- netget = PacketGet;
- #ifdef ID_ENABLE_DOOM_CLASSIC_NETWORKING
- ::g->netgame = true;
- {
- ++i;
- ::g->doomcom.numnodes = 0;
- ::g->doomcom.consoleplayer = atoi( ::g->myargv[i] );
-
- ++i;
- ::g->doomcom.numnodes = 0;
- for (; i < ::g->myargc; ++i)
- {
- ::g->sendaddress[::g->doomcom.numnodes].sin_family = AF_INET;
- ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons(DOOMPORT);
-
-
- const std::string ipAddressWithPort( ::g->myargv[i] );
- const std::size_t colonPosition = ipAddressWithPort.find_last_of(':');
- std::string ipOnly;
- if( colonPosition != std::string::npos && colonPosition + 1 < ipAddressWithPort.size() ) {
- const std::string portOnly( ipAddressWithPort.substr( colonPosition + 1 ) );
- ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons( atoi( portOnly.c_str() ) );
- ipOnly = ipAddressWithPort.substr( 0, colonPosition );
- } else {
-
- ipOnly = ipAddressWithPort;
- }
- in_addr_t ipAddress = inet_addr( ipOnly.c_str() );
- if ( ipAddress == INADDR_NONE ) {
- I_Error( "Invalid IP Address: %s\n", ipOnly.c_str() );
- session->QuitMatch();
- common->AddDialog( GDM_OPPONENT_CONNECTION_LOST, DIALOG_ACCEPT, NULL, NULL, false );
- }
- ::g->sendaddress[::g->doomcom.numnodes].sin_addr.s_addr = ipAddress;
- ::g->doomcom.numnodes++;
- }
-
- ::g->doomcom.id = DOOMCOM_ID;
- ::g->doomcom.numplayers = ::g->doomcom.numnodes;
- }
- if ( globalNetworking ) {
-
- ::g->insocket = UDPsocket ();
- BindToLocalPort (::g->insocket,htons(DOOMPORT));
-
-
- int nonblocking = 1;
- setsockopt( ::g->insocket, SOL_SOCKET, SO_NBIO, &nonblocking, sizeof(nonblocking));
- ::g->sendsocket = UDPsocket ();
- I_Printf( "[+] Setting up sockets for player %d\n", DoomLib::GetPlayer() );
- }
- #endif
- }
- void I_ShutdownNetwork( void ) {
-
- }
- void I_NetCmd (void)
- {
- if (::g->doomcom.command == CMD_SEND)
- {
- netsend ();
- }
- else if (::g->doomcom.command == CMD_GET)
- {
- netget ();
- }
- else
- I_Error ("Bad net cmd: %i\n",::g->doomcom.command);
- }
|