i_net_ps3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. // Sockets
  28. #include <sys/socket.h>
  29. #include <sys/types.h>
  30. #include <netex/errno.h>
  31. #include <netex/net.h>
  32. #include <arpa/inet.h>
  33. #include "i_system.h"
  34. #include "d_event.h"
  35. #include "d_net.h"
  36. #include "m_argv.h"
  37. #include "doomstat.h"
  38. #include "i_net.h"
  39. #include "doomlib.h"
  40. #include "../Main/Main.h"
  41. void NetSend (void);
  42. qboolean NetListen (void);
  43. namespace {
  44. bool IsValidSocket( int socketDescriptor );
  45. int GetLastSocketError();
  46. /*
  47. ========================
  48. Returns true if the socket is valid. I made this function to help abstract the differences
  49. between WinSock (used on Xbox) and BSD sockets, which the PS3 follows more closely.
  50. ========================
  51. */
  52. bool IsValidSocket( int socketDescriptor ) {
  53. return socketDescriptor >= 0;
  54. }
  55. /*
  56. ========================
  57. Returns the last error reported by the platform's socket library.
  58. ========================
  59. */
  60. int GetLastSocketError() {
  61. return sys_net_errno;
  62. }
  63. }
  64. //
  65. // NETWORKING
  66. //
  67. int DOOMPORT = 1002; // DHM - Nerve :: On original XBox, ports 1000 - 1255 saved you a byte on every packet. 360 too?
  68. unsigned long GetServerIP() {
  69. return ::g->sendaddress[::g->doomcom.consoleplayer].sin_addr.s_addr;
  70. }
  71. void (*netget) (void);
  72. void (*netsend) (void);
  73. //
  74. // UDPsocket
  75. //
  76. int UDPsocket (void)
  77. {
  78. int s;
  79. // allocate a socket
  80. s = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  81. if ( !IsValidSocket( s ) ) {
  82. int err = GetLastSocketError();
  83. I_Error( "can't create socket, error %d", err );
  84. }
  85. return s;
  86. }
  87. //
  88. // BindToLocalPort
  89. //
  90. void BindToLocalPort( int s, int port )
  91. {
  92. int v;
  93. struct sockaddr_in address;
  94. memset (&address, 0, sizeof(address));
  95. address.sin_family = AF_INET;
  96. address.sin_addr.s_addr = INADDR_ANY;
  97. address.sin_port = port;
  98. v = bind (s, (sockaddr*)&address, sizeof(address));
  99. //if (v == -1)
  100. //I_Error ("BindToPort: bind: %s", strerror(errno));
  101. }
  102. //
  103. // PacketSend
  104. //
  105. void PacketSend (void)
  106. {
  107. int c;
  108. doomdata_t sw;
  109. // byte swap
  110. sw.checksum = htonl(::g->netbuffer->checksum);
  111. sw.sourceDest = DoomLib::BuildSourceDest(::g->doomcom.remotenode);
  112. sw.player = ::g->netbuffer->player;
  113. sw.retransmitfrom = ::g->netbuffer->retransmitfrom;
  114. sw.starttic = ::g->netbuffer->starttic;
  115. sw.numtics = ::g->netbuffer->numtics;
  116. for (c=0 ; c< ::g->netbuffer->numtics ; c++)
  117. {
  118. sw.cmds[c].forwardmove = ::g->netbuffer->cmds[c].forwardmove;
  119. sw.cmds[c].sidemove = ::g->netbuffer->cmds[c].sidemove;
  120. sw.cmds[c].angleturn = htons(::g->netbuffer->cmds[c].angleturn);
  121. sw.cmds[c].consistancy = htons(::g->netbuffer->cmds[c].consistancy);
  122. sw.cmds[c].buttons = ::g->netbuffer->cmds[c].buttons;
  123. }
  124. // Send Socket
  125. {
  126. //DWORD num_sent;
  127. //if ( globalNetworking ) {
  128. // c = WSASendTo(::g->sendsocket, &buffer, 1, &num_sent, 0, (sockaddr*)&::g->sendaddress[::g->doomcom.remotenode],
  129. // sizeof(::g->sendaddress[::g->doomcom.remotenode]), 0, 0);
  130. //} else {
  131. c = DoomLib::Send( (char*)&sw, ::g->doomcom.datalength, (sockaddr_in*)&::g->sendaddress[::g->doomcom.remotenode], ::g->doomcom.remotenode );
  132. //}
  133. }
  134. }
  135. //
  136. // PacketGet
  137. //
  138. void PacketGet (void)
  139. {
  140. int i;
  141. int c;
  142. struct sockaddr_in fromaddress;
  143. int fromlen;
  144. doomdata_t sw;
  145. DWORD num_recieved; //, flags = 0;
  146. // Try and read a socket
  147. //buffer.buf = (char*)&sw;
  148. //buffer.len = sizeof(sw);
  149. fromlen = sizeof(fromaddress);
  150. //if ( globalNetworking ) {
  151. // c = WSARecvFrom(::g->insocket, &buffer, 1, &num_recieved, &flags, (struct sockaddr*)&fromaddress, &fromlen, 0, 0);
  152. //} else {
  153. c = DoomLib::Recv( (char*)&sw, &num_recieved );
  154. //}
  155. if ( c < 0 )
  156. {
  157. /*if ( globalNetworking ) {
  158. int err = WSAGetLastError();
  159. if (err != WSAEWOULDBLOCK)
  160. I_Error ("GetPacket: %s",strerror(errno));
  161. }*/
  162. ::g->doomcom.remotenode = -1; // no packet
  163. return;
  164. }
  165. // find remote node number
  166. /*for (i=0 ; i<::g->doomcom.numnodes ; i++)
  167. if ( fromaddress.sin_addr.s_addr == ::g->sendaddress[i].sin_addr.s_addr )
  168. break;
  169. if (i == ::g->doomcom.numnodes)
  170. {
  171. // packet is not from one of the ::g->players (new game broadcast)
  172. ::g->doomcom.remotenode = -1; // no packet
  173. return;
  174. }*/
  175. //if ( ::g->consoleplayer == 1 ) {
  176. //int x = 0;
  177. //}
  178. int source;
  179. int dest;
  180. DoomLib::GetSourceDest( sw.sourceDest, &source, &dest );
  181. i = source;
  182. //if ( ::g->consoleplayer == 1 ) {
  183. //if ( i == 2 ) {
  184. //int suck = 0;
  185. //}
  186. //}
  187. ::g->doomcom.remotenode = i; // good packet from a game player
  188. ::g->doomcom.datalength = (short)num_recieved;
  189. // byte swap
  190. ::g->netbuffer->checksum = ntohl(sw.checksum);
  191. ::g->netbuffer->player = sw.player;
  192. ::g->netbuffer->retransmitfrom = sw.retransmitfrom;
  193. ::g->netbuffer->starttic = sw.starttic;
  194. ::g->netbuffer->numtics = sw.numtics;
  195. for ( c = 0; c < ::g->netbuffer->numtics; c++ )
  196. {
  197. ::g->netbuffer->cmds[c].forwardmove = sw.cmds[c].forwardmove;
  198. ::g->netbuffer->cmds[c].sidemove = sw.cmds[c].sidemove;
  199. ::g->netbuffer->cmds[c].angleturn = ntohs(sw.cmds[c].angleturn);
  200. ::g->netbuffer->cmds[c].consistancy = ntohs(sw.cmds[c].consistancy);
  201. ::g->netbuffer->cmds[c].buttons = sw.cmds[c].buttons;
  202. }
  203. }
  204. static int I_TrySetupNetwork(void)
  205. {
  206. // DHM - Moved to Session
  207. return 1;
  208. }
  209. //
  210. // I_InitNetwork
  211. //
  212. void I_InitNetwork (void)
  213. {
  214. //qboolean trueval = true;
  215. int i;
  216. int p;
  217. //int a = 0;
  218. // struct hostent* hostentry; // host information entry
  219. memset (&::g->doomcom, 0, sizeof(::g->doomcom) );
  220. // set up for network
  221. i = M_CheckParm ("-dup");
  222. if (i && i< ::g->myargc-1)
  223. {
  224. ::g->doomcom.ticdup = ::g->myargv[i+1][0]-'0';
  225. if (::g->doomcom.ticdup < 1)
  226. ::g->doomcom.ticdup = 1;
  227. if (::g->doomcom.ticdup > 9)
  228. ::g->doomcom.ticdup = 9;
  229. }
  230. else
  231. ::g->doomcom.ticdup = 1;
  232. if (M_CheckParm ("-extratic"))
  233. ::g->doomcom.extratics = 1;
  234. else
  235. ::g->doomcom.extratics = 0;
  236. p = M_CheckParm ("-port");
  237. if (p && p < ::g->myargc-1)
  238. {
  239. DOOMPORT = atoi (::g->myargv[p+1]);
  240. I_Printf ("using alternate port %i\n",DOOMPORT);
  241. }
  242. // parse network game options,
  243. // -net <::g->consoleplayer> <host> <host> ...
  244. i = M_CheckParm ("-net");
  245. if (!i || !I_TrySetupNetwork())
  246. {
  247. // single player game
  248. ::g->netgame = false;
  249. ::g->doomcom.id = DOOMCOM_ID;
  250. ::g->doomcom.numplayers = ::g->doomcom.numnodes = 1;
  251. ::g->doomcom.deathmatch = false;
  252. ::g->doomcom.consoleplayer = 0;
  253. return;
  254. }
  255. netsend = PacketSend;
  256. netget = PacketGet;
  257. ::g->netgame = true;
  258. {
  259. ++i; // skip the '-net'
  260. ::g->doomcom.numnodes = 0;
  261. ::g->doomcom.consoleplayer = atoi( ::g->myargv[i] );
  262. // skip the console number
  263. ++i;
  264. ::g->doomcom.numnodes = 0;
  265. for (; i < ::g->myargc; ++i)
  266. {
  267. ::g->sendaddress[::g->doomcom.numnodes].sin_family = AF_INET;
  268. ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons(DOOMPORT);
  269. // Pull out the port number.
  270. const std::string ipAddressWithPort( ::g->myargv[i] );
  271. const std::size_t colonPosition = ipAddressWithPort.find_last_of(':');
  272. std::string ipOnly;
  273. if( colonPosition != std::string::npos && colonPosition + 1 < ipAddressWithPort.size() ) {
  274. const std::string portOnly( ipAddressWithPort.substr( colonPosition + 1 ) );
  275. ::g->sendaddress[::g->doomcom.numnodes].sin_port = htons( atoi( portOnly.c_str() ) );
  276. ipOnly = ipAddressWithPort.substr( 0, colonPosition );
  277. } else {
  278. // Assume the address doesn't include a port.
  279. ipOnly = ipAddressWithPort;
  280. }
  281. in_addr_t ipAddress = inet_addr( ipOnly.c_str() );
  282. if ( ipAddress == INADDR_NONE ) {
  283. I_Error( "Invalid IP Address: %s\n", ipOnly.c_str() );
  284. session->QuitMatch();
  285. common->AddDialog( GDM_OPPONENT_CONNECTION_LOST, DIALOG_ACCEPT, NULL, NULL, false );
  286. }
  287. ::g->sendaddress[::g->doomcom.numnodes].sin_addr.s_addr = ipAddress;
  288. ::g->doomcom.numnodes++;
  289. }
  290. ::g->doomcom.id = DOOMCOM_ID;
  291. ::g->doomcom.numplayers = ::g->doomcom.numnodes;
  292. }
  293. if ( globalNetworking ) {
  294. // Setup sockets
  295. ::g->insocket = UDPsocket ();
  296. BindToLocalPort (::g->insocket,htons(DOOMPORT));
  297. // PS3 call to enable non-blocking mode
  298. int nonblocking = 1; // Non-zero is nonblocking mode.
  299. setsockopt( ::g->insocket, SOL_SOCKET, SO_NBIO, &nonblocking, sizeof(nonblocking));
  300. ::g->sendsocket = UDPsocket ();
  301. I_Printf( "[+] Setting up sockets for player %d\n", DoomLib::GetPlayer() );
  302. }
  303. }
  304. // DHM - Nerve
  305. void I_ShutdownNetwork( void ) {
  306. if ( globalNetworking && gameLocal != NULL ) {
  307. int curPlayer = DoomLib::GetPlayer();
  308. for (int player = 0; player < gameLocal->Interface.GetNumPlayers(); ++player)
  309. {
  310. DoomLib::SetPlayer( player );
  311. if ( IsValidSocket( ::g->insocket ) ) {
  312. I_Printf( "[-] Shut down insocket for player %d\n", DoomLib::GetPlayer() );
  313. shutdown( ::g->insocket, SHUT_RDWR );
  314. socketclose( ::g->insocket );
  315. }
  316. if ( IsValidSocket( ::g->sendsocket ) ) {
  317. I_Printf( "[-] Shut down sendsocket for player %d\n", DoomLib::GetPlayer() );
  318. shutdown( ::g->sendsocket, SHUT_RDWR );
  319. socketclose( ::g->sendsocket );
  320. }
  321. }
  322. DoomLib::SetPlayer(curPlayer);
  323. globalNetworking = false;
  324. }
  325. }
  326. void I_NetCmd (void)
  327. {
  328. if (::g->doomcom.command == CMD_SEND)
  329. {
  330. netsend ();
  331. }
  332. else if (::g->doomcom.command == CMD_GET)
  333. {
  334. netget ();
  335. }
  336. else
  337. I_Error ("Bad net cmd: %i\n",::g->doomcom.command);
  338. }