i_net_win32.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "Precompiled.h"
  21. #include "globaldata.h"
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <string>
  26. #include <errno.h>
  27. #include "i_system.h"
  28. #include "d_event.h"
  29. #include "d_net.h"
  30. #include "m_argv.h"
  31. #include "doomstat.h"
  32. #include "i_net.h"
  33. #include "doomlib.h"
  34. void NetSend (void);
  35. qboolean NetListen (void);
  36. namespace {
  37. bool IsValidSocket( int socketDescriptor );
  38. int GetLastSocketError();
  39. /*
  40. ========================
  41. Returns true if the socket is valid. I made this function to help abstract the differences
  42. between WinSock (used on Xbox) and BSD sockets, which the PS3 follows more closely.
  43. ========================
  44. */
  45. bool IsValidSocket( int socketDescriptor ) {
  46. return false;
  47. }
  48. /*
  49. ========================
  50. Returns the last error reported by the platform's socket library.
  51. ========================
  52. */
  53. int GetLastSocketError() {
  54. return 0;
  55. }
  56. }
  57. //
  58. // NETWORKING
  59. //
  60. int DOOMPORT = 1002; // DHM - Nerve :: On original XBox, ports 1000 - 1255 saved you a byte on every packet. 360 too?
  61. unsigned long GetServerIP() {
  62. return ::g->sendaddress[::g->doomcom.consoleplayer].sin_addr.s_addr;
  63. }
  64. void (*netget) (void);
  65. void (*netsend) (void);
  66. //
  67. // UDPsocket
  68. //
  69. int UDPsocket (void)
  70. {
  71. int s;
  72. // allocate a socket
  73. s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  74. if ( !IsValidSocket( s ) ) {
  75. int err = GetLastSocketError();
  76. I_Error( "can't create socket, error %d", err );
  77. }
  78. return s;
  79. }
  80. //
  81. // BindToLocalPort
  82. //
  83. void BindToLocalPort( int s, int port )
  84. {
  85. }
  86. //
  87. // PacketSend
  88. //
  89. void PacketSend (void)
  90. {
  91. }
  92. //
  93. // PacketGet
  94. //
  95. void PacketGet (void)
  96. {
  97. }
  98. static int I_TrySetupNetwork(void)
  99. {
  100. // DHM - Moved to Session
  101. return 1;
  102. }
  103. //
  104. // I_InitNetwork
  105. //
  106. void I_InitNetwork (void)
  107. {
  108. //qboolean trueval = true;
  109. int i;
  110. int p;
  111. //int a = 0;
  112. // struct hostent* hostentry; // host information entry
  113. memset (&::g->doomcom, 0, sizeof(::g->doomcom) );
  114. // set up for network
  115. i = M_CheckParm ("-dup");
  116. if (i && i< ::g->myargc-1)
  117. {
  118. ::g->doomcom.ticdup = ::g->myargv[i+1][0]-'0';
  119. if (::g->doomcom.ticdup < 1)
  120. ::g->doomcom.ticdup = 1;
  121. if (::g->doomcom.ticdup > 9)
  122. ::g->doomcom.ticdup = 9;
  123. }
  124. else
  125. ::g->doomcom.ticdup = 1;
  126. if (M_CheckParm ("-extratic"))
  127. ::g->doomcom.extratics = 1;
  128. else
  129. ::g->doomcom.extratics = 0;
  130. p = M_CheckParm ("-port");
  131. if (p && p < ::g->myargc-1)
  132. {
  133. DOOMPORT = atoi (::g->myargv[p+1]);
  134. I_Printf ("using alternate port %i\n",DOOMPORT);
  135. }
  136. // parse network game options,
  137. // -net <::g->consoleplayer> <host> <host> ...
  138. i = M_CheckParm ("-net");
  139. if (!i || !I_TrySetupNetwork())
  140. {
  141. // single player game
  142. ::g->netgame = false;
  143. ::g->doomcom.id = DOOMCOM_ID;
  144. ::g->doomcom.numplayers = ::g->doomcom.numnodes = 1;
  145. ::g->doomcom.deathmatch = false;
  146. ::g->doomcom.consoleplayer = 0;
  147. return;
  148. }
  149. netsend = PacketSend;
  150. netget = PacketGet;
  151. #ifdef ID_ENABLE_DOOM_CLASSIC_NETWORKING
  152. ::g->netgame = true;
  153. {
  154. ++i; // skip the '-net'
  155. ::g->doomcom.numnodes = 0;
  156. ::g->doomcom.consoleplayer = atoi( ::g->myargv[i] );
  157. // skip the console number
  158. ++i;
  159. ::g->doomcom.numnodes = 0;
  160. for (; i < ::g->myargc; ++i)
  161. {
  162. ::g->sendaddress[::g->doomcom.numnodes].sin_family = AF_INET;
  163. ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons(DOOMPORT);
  164. // Pull out the port number.
  165. const std::string ipAddressWithPort( ::g->myargv[i] );
  166. const std::size_t colonPosition = ipAddressWithPort.find_last_of(':');
  167. std::string ipOnly;
  168. if( colonPosition != std::string::npos && colonPosition + 1 < ipAddressWithPort.size() ) {
  169. const std::string portOnly( ipAddressWithPort.substr( colonPosition + 1 ) );
  170. ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons( atoi( portOnly.c_str() ) );
  171. ipOnly = ipAddressWithPort.substr( 0, colonPosition );
  172. } else {
  173. // Assume the address doesn't include a port.
  174. ipOnly = ipAddressWithPort;
  175. }
  176. in_addr_t ipAddress = inet_addr( ipOnly.c_str() );
  177. if ( ipAddress == INADDR_NONE ) {
  178. I_Error( "Invalid IP Address: %s\n", ipOnly.c_str() );
  179. session->QuitMatch();
  180. common->AddDialog( GDM_OPPONENT_CONNECTION_LOST, DIALOG_ACCEPT, NULL, NULL, false );
  181. }
  182. ::g->sendaddress[::g->doomcom.numnodes].sin_addr.s_addr = ipAddress;
  183. ::g->doomcom.numnodes++;
  184. }
  185. ::g->doomcom.id = DOOMCOM_ID;
  186. ::g->doomcom.numplayers = ::g->doomcom.numnodes;
  187. }
  188. if ( globalNetworking ) {
  189. // Setup sockets
  190. ::g->insocket = UDPsocket ();
  191. BindToLocalPort (::g->insocket,htons(DOOMPORT));
  192. // PS3 call to enable non-blocking mode
  193. int nonblocking = 1; // Non-zero is nonblocking mode.
  194. setsockopt( ::g->insocket, SOL_SOCKET, SO_NBIO, &nonblocking, sizeof(nonblocking));
  195. ::g->sendsocket = UDPsocket ();
  196. I_Printf( "[+] Setting up sockets for player %d\n", DoomLib::GetPlayer() );
  197. }
  198. #endif
  199. }
  200. // DHM - Nerve
  201. void I_ShutdownNetwork( void ) {
  202. }
  203. void I_NetCmd (void)
  204. {
  205. if (::g->doomcom.command == CMD_SEND)
  206. {
  207. netsend ();
  208. }
  209. else if (::g->doomcom.command == CMD_GET)
  210. {
  211. netget ();
  212. }
  213. else
  214. I_Error ("Bad net cmd: %i\n",::g->doomcom.command);
  215. }