i_net_xbox.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <errno.h>
  26. #include "i_system.h"
  27. #include "d_event.h"
  28. #include "d_net.h"
  29. #include "m_argv.h"
  30. #include "doomstat.h"
  31. #include "i_net.h"
  32. #include "doomlib.h"
  33. #include "../Main/Main.h"
  34. void NetSend (void);
  35. qboolean NetListen (void);
  36. //
  37. // NETWORKING
  38. //
  39. int DOOMPORT = 1002; // DHM - Nerve :: On original XBox, ports 1000 - 1255 saved you a byte on every packet. 360 too?
  40. unsigned long GetServerIP() { return ::g->sendaddress[::g->doomcom.consoleplayer].sin_addr.s_addr; }
  41. void (*netget) (void);
  42. void (*netsend) (void);
  43. //
  44. // UDPsocket
  45. //
  46. int UDPsocket (void)
  47. {
  48. int s;
  49. // allocate a socket
  50. s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  51. if (s == INVALID_SOCKET) {
  52. int err = WSAGetLastError ();
  53. I_Error ("can't create socket: %s",strerror(errno));
  54. }
  55. return s;
  56. }
  57. //
  58. // BindToLocalPort
  59. //
  60. void BindToLocalPort( int s, int port )
  61. {
  62. int v;
  63. struct sockaddr_in address;
  64. memset (&address, 0, sizeof(address));
  65. address.sin_family = AF_INET;
  66. address.sin_addr.s_addr = INADDR_ANY;
  67. address.sin_port = port;
  68. v = bind (s, (sockaddr*)&address, sizeof(address));
  69. //if (v == -1)
  70. //I_Error ("BindToPort: bind: %s", strerror(errno));
  71. }
  72. //
  73. // PacketSend
  74. //
  75. void PacketSend (void)
  76. {
  77. int c;
  78. doomdata_t sw;
  79. // byte swap
  80. sw.checksum = htonl(::g->netbuffer->checksum);
  81. sw.sourceDest = DoomLib::BuildSourceDest(::g->doomcom.remotenode);
  82. sw.player = ::g->netbuffer->player;
  83. sw.retransmitfrom = ::g->netbuffer->retransmitfrom;
  84. sw.starttic = ::g->netbuffer->starttic;
  85. sw.numtics = ::g->netbuffer->numtics;
  86. for (c=0 ; c< ::g->netbuffer->numtics ; c++)
  87. {
  88. sw.cmds[c].forwardmove = ::g->netbuffer->cmds[c].forwardmove;
  89. sw.cmds[c].sidemove = ::g->netbuffer->cmds[c].sidemove;
  90. sw.cmds[c].angleturn = htons(::g->netbuffer->cmds[c].angleturn);
  91. //sw.cmds[c].consistancy = htons(::g->netbuffer->cmds[c].consistancy);
  92. sw.cmds[c].buttons = ::g->netbuffer->cmds[c].buttons;
  93. }
  94. // Send Socket
  95. {
  96. //DWORD num_sent;
  97. WSABUF buffer;
  98. buffer.buf = (char*)&sw;
  99. buffer.len = ::g->doomcom.datalength;
  100. //if ( globalNetworking ) {
  101. // c = WSASendTo(::g->sendsocket, &buffer, 1, &num_sent, 0, (sockaddr*)&::g->sendaddress[::g->doomcom.remotenode],
  102. // sizeof(::g->sendaddress[::g->doomcom.remotenode]), 0, 0);
  103. //} else {
  104. c = DoomLib::Send( buffer.buf, buffer.len, (sockaddr_in*)&::g->sendaddress[::g->doomcom.remotenode], ::g->doomcom.remotenode );
  105. //}
  106. }
  107. }
  108. //
  109. // PacketGet
  110. //
  111. void PacketGet (void)
  112. {
  113. int i;
  114. int c;
  115. struct sockaddr_in fromaddress;
  116. int fromlen;
  117. doomdata_t sw;
  118. WSABUF buffer;
  119. DWORD num_recieved, flags = 0;
  120. // Try and read a socket
  121. buffer.buf = (char*)&sw;
  122. buffer.len = sizeof(sw);
  123. fromlen = sizeof(fromaddress);
  124. //if ( globalNetworking ) {
  125. // c = WSARecvFrom(::g->insocket, &buffer, 1, &num_recieved, &flags, (struct sockaddr*)&fromaddress, &fromlen, 0, 0);
  126. //} else {
  127. c = DoomLib::Recv( (char*)&sw, &num_recieved );
  128. //}
  129. if (c == SOCKET_ERROR )
  130. {
  131. /*if ( globalNetworking ) {
  132. int err = WSAGetLastError();
  133. if (err != WSAEWOULDBLOCK)
  134. I_Error ("GetPacket: %s",strerror(errno));
  135. }*/
  136. ::g->doomcom.remotenode = -1; // no packet
  137. return;
  138. }
  139. // find remote node number
  140. /*for (i=0 ; i<::g->doomcom.numnodes ; i++)
  141. if ( fromaddress.sin_addr.s_addr == ::g->sendaddress[i].sin_addr.s_addr )
  142. break;
  143. if (i == ::g->doomcom.numnodes)
  144. {
  145. // packet is not from one of the ::g->players (new game broadcast)
  146. ::g->doomcom.remotenode = -1; // no packet
  147. return;
  148. }*/
  149. //if ( ::g->consoleplayer == 1 ) {
  150. //int x = 0;
  151. //}
  152. int source;
  153. int dest;
  154. DoomLib::GetSourceDest( sw.sourceDest, &source, &dest );
  155. i = source;
  156. //if ( ::g->consoleplayer == 1 ) {
  157. //if ( i == 2 ) {
  158. //int suck = 0;
  159. //}
  160. //}
  161. ::g->doomcom.remotenode = i; // good packet from a game player
  162. ::g->doomcom.datalength = (short)num_recieved;
  163. // byte swap
  164. ::g->netbuffer->checksum = ntohl(sw.checksum);
  165. ::g->netbuffer->player = sw.player;
  166. ::g->netbuffer->retransmitfrom = sw.retransmitfrom;
  167. ::g->netbuffer->starttic = sw.starttic;
  168. ::g->netbuffer->numtics = sw.numtics;
  169. for (c=0 ; c< ::g->netbuffer->numtics ; c++)
  170. {
  171. ::g->netbuffer->cmds[c].forwardmove = sw.cmds[c].forwardmove;
  172. ::g->netbuffer->cmds[c].sidemove = sw.cmds[c].sidemove;
  173. ::g->netbuffer->cmds[c].angleturn = ntohs(sw.cmds[c].angleturn);
  174. //::g->netbuffer->cmds[c].consistancy = ntohs(sw.cmds[c].consistancy);
  175. ::g->netbuffer->cmds[c].buttons = sw.cmds[c].buttons;
  176. }
  177. }
  178. static int I_TrySetupNetwork(void)
  179. {
  180. // DHM - Moved to Session
  181. return 1;
  182. }
  183. //
  184. // I_InitNetwork
  185. //
  186. void I_InitNetwork (void)
  187. {
  188. qboolean trueval = true;
  189. int i;
  190. int p;
  191. int a = 0;
  192. // struct hostent* hostentry; // host information entry
  193. memset (&::g->doomcom, 0, sizeof(::g->doomcom) );
  194. // set up for network
  195. i = M_CheckParm ("-dup");
  196. if (i && i< ::g->myargc-1)
  197. {
  198. ::g->doomcom.ticdup = ::g->myargv[i+1][0]-'0';
  199. if (::g->doomcom.ticdup < 1)
  200. ::g->doomcom.ticdup = 1;
  201. if (::g->doomcom.ticdup > 9)
  202. ::g->doomcom.ticdup = 9;
  203. }
  204. else
  205. ::g->doomcom.ticdup = 1;
  206. if (M_CheckParm ("-extratic"))
  207. ::g->doomcom.extratics = 1;
  208. else
  209. ::g->doomcom.extratics = 0;
  210. p = M_CheckParm ("-port");
  211. if (p && p<::g->myargc-1)
  212. {
  213. DOOMPORT = atoi (::g->myargv[p+1]);
  214. I_Printf ("using alternate port %i\n",DOOMPORT);
  215. }
  216. // parse network game options,
  217. // -net <::g->consoleplayer> <host> <host> ...
  218. i = M_CheckParm ("-net");
  219. if (!i || !I_TrySetupNetwork())
  220. {
  221. // single player game
  222. ::g->netgame = false;
  223. ::g->doomcom.id = DOOMCOM_ID;
  224. ::g->doomcom.numplayers = ::g->doomcom.numnodes = 1;
  225. ::g->doomcom.deathmatch = false;
  226. ::g->doomcom.consoleplayer = 0;
  227. return;
  228. }
  229. netsend = PacketSend;
  230. netget = PacketGet;
  231. ::g->netgame = true;
  232. {
  233. ++i; // skip the '-net'
  234. ::g->doomcom.numnodes = 0;
  235. ::g->doomcom.consoleplayer = atoi( ::g->myargv[i] );
  236. // skip the console number
  237. ++i;
  238. ::g->doomcom.numnodes = 0;
  239. for (; i < ::g->myargc; ++i)
  240. {
  241. ::g->sendaddress[::g->doomcom.numnodes].sin_family = AF_INET;
  242. ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons(DOOMPORT);
  243. ::g->sendaddress[::g->doomcom.numnodes].sin_addr.s_addr = inet_addr (::g->myargv[i]);//39, 17
  244. ::g->doomcom.numnodes++;
  245. }
  246. ::g->doomcom.id = DOOMCOM_ID;
  247. ::g->doomcom.numplayers = ::g->doomcom.numnodes;
  248. }
  249. if ( globalNetworking ) {
  250. // Setup sockets
  251. ::g->insocket = UDPsocket ();
  252. BindToLocalPort (::g->insocket,htons(DOOMPORT));
  253. ioctlsocket (::g->insocket, FIONBIO, (u_long*)&trueval);
  254. ::g->sendsocket = UDPsocket ();
  255. I_Printf( "[+] Setting up sockets for player %d\n", DoomLib::GetPlayer() );
  256. }
  257. }
  258. // DHM - Nerve
  259. void I_ShutdownNetwork( void ) {
  260. if ( globalNetworking ) {
  261. int curPlayer = DoomLib::GetPlayer();
  262. for (int player = 0; player < App->Game->Interface.GetNumPlayers(); ++player)
  263. {
  264. DoomLib::SetPlayer( player );
  265. if ( ::g->insocket != INVALID_SOCKET ) {
  266. I_Printf( "[-] Shut down insocket for player %d\n", DoomLib::GetPlayer() );
  267. shutdown( ::g->insocket, SD_BOTH );
  268. closesocket( ::g->insocket );
  269. }
  270. if ( ::g->sendsocket != INVALID_SOCKET ) {
  271. I_Printf( "[-] Shut down sendsocket for player %d\n", DoomLib::GetPlayer() );
  272. shutdown( ::g->sendsocket, SD_BOTH );
  273. closesocket( ::g->sendsocket );
  274. }
  275. }
  276. DoomLib::SetPlayer(curPlayer);
  277. globalNetworking = false;
  278. }
  279. }
  280. void I_NetCmd (void)
  281. {
  282. if (::g->doomcom.command == CMD_SEND)
  283. {
  284. netsend ();
  285. }
  286. else if (::g->doomcom.command == CMD_GET)
  287. {
  288. netget ();
  289. }
  290. else
  291. I_Error ("Bad net cmd: %i\n",::g->doomcom.command);
  292. }