sys_dedicated_server_search.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #pragma hdrstop
  21. #include "../idlib/precompiled.h"
  22. #include "sys_lobby_backend.h"
  23. #include "sys_dedicated_server_search.h"
  24. /*
  25. ========================
  26. idDedicatedServerSearch::idDedicatedServerSearch
  27. ========================
  28. */
  29. idDedicatedServerSearch::idDedicatedServerSearch() :
  30. callback( NULL ) {
  31. }
  32. /*
  33. ========================
  34. idDedicatedServerSearch::~idDedicatedServerSearch
  35. ========================
  36. */
  37. idDedicatedServerSearch::~idDedicatedServerSearch() {
  38. if ( callback != NULL ) {
  39. delete callback;
  40. }
  41. }
  42. /*
  43. ========================
  44. idDedicatedServerSearch::StartSearch
  45. ========================
  46. */
  47. void idDedicatedServerSearch::StartSearch( const idCallback & cb ) {
  48. Clear();
  49. callback = cb.Clone();
  50. }
  51. /*
  52. ========================
  53. idDedicatedServerSearch::Clear
  54. ========================
  55. */
  56. void idDedicatedServerSearch::Clear() {
  57. if ( callback != NULL ) {
  58. delete callback;
  59. callback = NULL;
  60. }
  61. list.Clear();
  62. }
  63. /*
  64. ========================
  65. idDedicatedServerSearch::Clear
  66. ========================
  67. */
  68. void idDedicatedServerSearch::HandleQueryAck( lobbyAddress_t & addr, idBitMsg & msg ) {
  69. bool found = false;
  70. // Find the server this ack belongs to
  71. for ( int i = 0; i < list.Num(); i++ ) {
  72. serverInfoDedicated_t & query = list[i];
  73. if ( query.addr.Compare( addr ) ) {
  74. // Found the server
  75. found = true;
  76. bool canJoin = msg.ReadBool();
  77. if ( !canJoin ) {
  78. // If we can't join this server, then remove it
  79. list.RemoveIndex( i-- );
  80. break;
  81. }
  82. query.serverInfo.Read( msg );
  83. query.connectedPlayers.Clear();
  84. for ( int i = 0; i < query.serverInfo.numPlayers; i++ ) {
  85. idStr user;
  86. msg.ReadString( user );
  87. query.connectedPlayers.Append( user );
  88. }
  89. break;
  90. }
  91. }
  92. if ( !found ) {
  93. bool canJoin = msg.ReadBool();
  94. if ( canJoin ) {
  95. serverInfoDedicated_t newServer;
  96. newServer.addr = addr;
  97. newServer.serverInfo.Read( msg );
  98. if ( newServer.serverInfo.serverName.IsEmpty() ) {
  99. newServer.serverInfo.serverName = addr.ToString();
  100. }
  101. newServer.connectedPlayers.Clear();
  102. for ( int i = 0; i < newServer.serverInfo.numPlayers; i++ ) {
  103. idStr user;
  104. msg.ReadString( user );
  105. newServer.connectedPlayers.Append( user );
  106. }
  107. list.Append( newServer );
  108. }
  109. }
  110. if ( callback != NULL ) {
  111. callback->Call();
  112. }
  113. }
  114. /*
  115. ========================
  116. idDedicatedServerSearch::GetAddrAtIndex
  117. ========================
  118. */
  119. bool idDedicatedServerSearch::GetAddrAtIndex( netadr_t & addr, int i ) {
  120. if ( i >= 0 && i < list.Num() ) {
  121. addr = list[i].addr.netAddr;
  122. return true;
  123. }
  124. return false;
  125. }
  126. /*
  127. ========================
  128. idDedicatedServerSearch::DescribeServerAtIndex
  129. ========================
  130. */
  131. const serverInfo_t * idDedicatedServerSearch::DescribeServerAtIndex( int i ) const {
  132. if ( i >= 0 && i < list.Num() ) {
  133. return &list[i].serverInfo;
  134. }
  135. return NULL;
  136. }
  137. /*
  138. ========================
  139. idDedicatedServerSearch::GetServerPlayersAtIndex
  140. ========================
  141. */
  142. const idList< idStr > * idDedicatedServerSearch::GetServerPlayersAtIndex( int i ) const {
  143. if ( i >= 0 && i < list.Num() ) {
  144. return &list[i].connectedPlayers;
  145. }
  146. return NULL;
  147. }
  148. /*
  149. ========================
  150. idDedicatedServerSearch::NumServers
  151. ========================
  152. */
  153. int idDedicatedServerSearch::NumServers() const {
  154. return list.Num();
  155. }