Networking.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "../Main/Main.h"
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include "tech5/engine/sys/sys_lobby.h"
  25. bool useTech5Packets = true;
  26. #if 1
  27. struct networkitem
  28. {
  29. int source;
  30. int size;
  31. char buffer[256*64];
  32. };
  33. std::queue< networkitem > networkstacks[4];
  34. //sockaddr_in sockaddrs[4];
  35. int DoomLibRecv( char* buff, DWORD *numRecv )
  36. {
  37. //int player = DoomInterface::CurrentPlayer();
  38. int player = ::g->consoleplayer;
  39. if (networkstacks[player].empty())
  40. return -1;
  41. networkitem item = networkstacks[player].front();
  42. memcpy( buff, item.buffer, item.size );
  43. *numRecv = item.size;
  44. //*source = sockaddrs[item.source];
  45. networkstacks[player].pop();
  46. return 1;
  47. }
  48. void I_Printf(char *error, ...);
  49. int DoomLibSend( const char* buff, DWORD size, sockaddr_in *target, int toNode )
  50. {
  51. int i;
  52. i = DoomLib::RemoteNodeToPlayerIndex( toNode );
  53. //I_Printf( "DoomLibSend %d --> %d: %d\n", ::g->consoleplayer, i, size );
  54. networkitem item;
  55. item.source = DoomInterface::CurrentPlayer();
  56. item.size = size;
  57. memcpy( item.buffer, buff, size );
  58. networkstacks[i].push( item );
  59. return 1;
  60. }
  61. int DoomLibSendRemote()
  62. {
  63. if ( gameLocal == NULL ) {
  64. return 0;
  65. }
  66. int curPlayer = DoomLib::GetPlayer();
  67. for (int player = 0; player < gameLocal->Interface.GetNumPlayers(); ++player)
  68. {
  69. DoomLib::SetPlayer( player );
  70. for( int i = 0; i < 4; i++ ) {
  71. //Check if it is remote
  72. int node = DoomLib::PlayerIndexToRemoteNode( i );
  73. if ( ::g->sendaddress[node].sin_addr.s_addr == ::g->sendaddress[0].sin_addr.s_addr ) {
  74. continue;
  75. }
  76. while(!networkstacks[i].empty()) {
  77. networkitem item = networkstacks[i].front();
  78. int c;
  79. //WSABUF buffer;
  80. //DWORD num_sent;
  81. //buffer.buf = (char*)&item.buffer;
  82. //buffer.len = item.size;
  83. if ( useTech5Packets ) {
  84. idLobby & lobby = static_cast< idLobby & >( session->GetGameLobbyBase() );
  85. lobbyUser_t * user = lobby.GetLobbyUser( i );
  86. if ( user != NULL ) {
  87. lobby.SendConnectionLess( user->address, idLobby::OOB_GENERIC_GAME_DATA, (const byte *)(&item.buffer[0] ), item.size );
  88. }
  89. } else {
  90. c = sendto( ::g->sendsocket, &item.buffer, item.size, MSG_DONTWAIT, (sockaddr*)&::g->sendaddress[node], sizeof(::g->sendaddress[node]) );
  91. //c = WSASendTo(::g->sendsocket, &buffer, 1, &num_sent, 0, (sockaddr*)&::g->sendaddress[node], sizeof(::g->sendaddress[node]), 0, 0);
  92. }
  93. networkstacks[i].pop();
  94. }
  95. }
  96. }
  97. DoomLib::SetPlayer(curPlayer);
  98. return 1;
  99. }
  100. void DL_InitNetworking( DoomInterface *pdi )
  101. {
  102. // DHM - Nerve :: Clear out any old splitscreen packets that may be lingering.
  103. for ( int i = 0; i<4; i++ ) {
  104. while ( !networkstacks[i].empty() ) {
  105. networkstacks[i].pop();
  106. }
  107. }
  108. /*sockaddrs[0].sin_addr.s_addr = inet_addr("0.0.0.1" );
  109. sockaddrs[1].sin_addr.s_addr = inet_addr("0.0.0.2" );
  110. sockaddrs[2].sin_addr.s_addr = inet_addr("0.0.0.3" );
  111. sockaddrs[3].sin_addr.s_addr = inet_addr("0.0.0.4" );*/
  112. pdi->SetNetworking( DoomLibRecv, DoomLibSend, DoomLibSendRemote );
  113. }
  114. #else
  115. void DL_InitNetworking( DoomInterface *pdi ) {
  116. }
  117. #endif