gamesideservercontrol.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Code/Commando/gamesideservercontrol.cpp $*
  25. * *
  26. * $Author:: Bhayes $*
  27. * *
  28. * $Modtime:: 3/19/02 4:37p $*
  29. * *
  30. * $Revision:: 9 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "always.h"
  36. #include "gamesideservercontrol.h"
  37. #include "registry.h"
  38. #include "_globals.h"
  39. #include "consolefunction.h"
  40. #include "gamedata.h"
  41. #include "servercontrol.h"
  42. #include "gamemode.h"
  43. #include "buildnum.h"
  44. #include "slavemaster.h"
  45. #include "consolemode.h"
  46. #include <string.h>
  47. #include <stdio.h>
  48. /*
  49. ** Static class data.
  50. */
  51. bool GameSideServerControlClass::Listening = false;
  52. StringClass GameSideServerControlClass::Response;
  53. /***********************************************************************************************
  54. * GameSideServerControlClass::Init -- Start listening for server control messages *
  55. * *
  56. * *
  57. * *
  58. * INPUT: Nothing *
  59. * *
  60. * OUTPUT: Nothing *
  61. * *
  62. * WARNINGS: None *
  63. * *
  64. * HISTORY: *
  65. * 11/16/2001 3:55PM ST : Created *
  66. *=============================================================================================*/
  67. void GameSideServerControlClass::Init(void)
  68. {
  69. char password[64] = DEFAULT_SERVER_CONTROL_PASSWORD;
  70. if (!Listening) {
  71. /*
  72. ** Only for dedicated servers.
  73. */
  74. if (SlaveMaster.Am_I_Slave() || (The_Game() && The_Game()->IsDedicated.Is_True()) || ConsoleBox.Is_Exclusive()) {
  75. /*
  76. ** Get the port number from the registry.
  77. */
  78. RegistryClass reg(APPLICATION_SUB_KEY_NAME_NET_SERVER_CONTROL);
  79. int port = reg.Get_Int(SERVER_CONTROL_PORT_KEY, DEFAULT_SERVER_CONTROL_PORT);
  80. if (port != 0) {
  81. /*
  82. ** See if we should only bind to the loopback address (for slaves).
  83. */
  84. int loopback = reg.Get_Int(SERVER_CONTROL_LOOPBACK_KEY, 0);
  85. if (SlaveMaster.Am_I_Slave()) {
  86. ServerControl.Allow_Remote_Admin((loopback == 0) ? true : false);
  87. }
  88. /*
  89. ** Get the bind IP from the registry.
  90. */
  91. unsigned long ip = reg.Get_Int(SERVER_CONTROL_IP_KEY, 0);
  92. /*
  93. ** Get the password from the registry.
  94. */
  95. reg.Get_String(SERVER_CONTROL_PASSWORD_KEY, password, sizeof(password), password);
  96. /*
  97. ** Start listening.
  98. */
  99. ServerControl.Start_Listening(port, password, &App_Request_Callback, NULL, loopback ? true : false, ip);
  100. Listening = true;
  101. /*
  102. ** Update the welcome message.
  103. */
  104. Set_Welcome_Message();
  105. }
  106. } else {
  107. Listening = false;
  108. }
  109. }
  110. }
  111. /***********************************************************************************************
  112. * GameSideServerControlClass::Set_Welcome_Message -- Update the welcome message *
  113. * *
  114. * *
  115. * *
  116. * INPUT: Nothing *
  117. * *
  118. * OUTPUT: Nothing *
  119. * *
  120. * WARNINGS: None *
  121. * *
  122. * HISTORY: *
  123. * 11/16/2001 3:56PM ST : Created *
  124. *=============================================================================================*/
  125. void GameSideServerControlClass::Set_Welcome_Message(void)
  126. {
  127. char buffer[1024];
  128. buffer[0] = 0;
  129. char welcome[1024];
  130. /*
  131. ** Basic welcome message includes build info.
  132. */
  133. sprintf(welcome, "Welcome to Renegade %s\n", BuildInfoClass::Composite_Build_Info());
  134. /*
  135. ** Get the slave info and add it into the welcome message.
  136. */
  137. SlaveMaster.Get_Slave_Info(buffer, sizeof(buffer));
  138. strcat(welcome, buffer);
  139. welcome[499] = 0;
  140. ServerControl.Set_Welcome_Message(welcome);
  141. }
  142. /***********************************************************************************************
  143. * GameSideServerControlClass::Shutdown -- Stop listening to server control messages *
  144. * *
  145. * *
  146. * *
  147. * INPUT: Nothing *
  148. * *
  149. * OUTPUT: Nothing *
  150. * *
  151. * WARNINGS: None *
  152. * *
  153. * HISTORY: *
  154. * 11/16/2001 3:57PM ST : Created *
  155. *=============================================================================================*/
  156. void GameSideServerControlClass::Shutdown(void)
  157. {
  158. ServerControl.Stop_Listening();
  159. Listening = false;
  160. }
  161. /***********************************************************************************************
  162. * GameSideServerControlClass::App_Request_Callback -- Server control request handler *
  163. * *
  164. * *
  165. * *
  166. * INPUT: Request text *
  167. * *
  168. * OUTPUT: Response text *
  169. * *
  170. * WARNINGS: None *
  171. * *
  172. * HISTORY: *
  173. * 11/16/2001 3:58PM ST : Created *
  174. *=============================================================================================*/
  175. const char *GameSideServerControlClass::App_Request_Callback(char *request)
  176. {
  177. Response.Erase(0, Response.Get_Length());
  178. if (Listening) {
  179. if (GameModeManager::Find("Combat")->Is_Active()) {
  180. ConsoleFunctionManager::Parse_Input(request);
  181. }
  182. }
  183. Response += "\n";
  184. return(Response.Peek_Buffer());
  185. }
  186. /***********************************************************************************************
  187. * GameSideServerControlClass::Print -- Adds to the response packet for the current request *
  188. * *
  189. * *
  190. * *
  191. * INPUT: Response text *
  192. * *
  193. * OUTPUT: Nothing *
  194. * *
  195. * WARNINGS: None *
  196. * *
  197. * HISTORY: *
  198. * 11/16/2001 3:58PM ST : Created *
  199. *=============================================================================================*/
  200. void GameSideServerControlClass::Print(char *text, ...)
  201. {
  202. if (Listening) {
  203. if (Response.Get_Length() > 32768) {
  204. Response.Erase(0, Response.Get_Length());
  205. }
  206. char buffer[8192];
  207. va_list va;
  208. buffer[sizeof(buffer)-1] = 0;
  209. va_start(va, text);
  210. _vsnprintf(&buffer[0], sizeof(buffer)-1, text, va);
  211. va_end(va);
  212. Response += buffer;
  213. }
  214. }
  215. /***********************************************************************************************
  216. * GameSideServerControlClass::Send_Message -- Send a request message to another server *
  217. * *
  218. * *
  219. * *
  220. * INPUT: Message *
  221. * IP *
  222. * Port *
  223. * *
  224. * OUTPUT: Nothing *
  225. * *
  226. * WARNINGS: None *
  227. * *
  228. * HISTORY: *
  229. * 11/16/2001 3:59PM ST : Created *
  230. *=============================================================================================*/
  231. void GameSideServerControlClass::Send_Message(char *text, unsigned long ip, unsigned short port)
  232. {
  233. if (Listening) {
  234. ServerControl.Send_Message(text, ip, port);
  235. }
  236. }