serverConnection.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Functions dealing with connecting to a server
  23. //-----------------------------------------------------------------------------
  24. // Server connection error
  25. //-----------------------------------------------------------------------------
  26. addMessageCallback( 'MsgConnectionError', handleConnectionErrorMessage );
  27. function handleConnectionErrorMessage(%msgType, %msgString, %msgError)
  28. {
  29. // On connect the server transmits a message to display if there
  30. // are any problems with the connection. Most connection errors
  31. // are game version differences, so hopefully the server message
  32. // will tell us where to get the latest version of the game.
  33. $ServerConnectionErrorMessage = %msgError;
  34. }
  35. //----------------------------------------------------------------------------
  36. // GameConnection client callbacks
  37. //----------------------------------------------------------------------------
  38. function GameConnection::initialControlSet(%this)
  39. {
  40. echo ("*** Initial Control Object");
  41. // The first control object has been set by the server
  42. // and we are now ready to go.
  43. // first check if the editor is active
  44. if (!isToolBuild() || !Editor::checkActiveLoadDone())
  45. {
  46. if (Canvas.getContent() != PlayGui.getId())
  47. {
  48. Canvas.setContent(PlayGui);
  49. }
  50. }
  51. }
  52. function GameConnection::onControlObjectChange(%this)
  53. {
  54. echo ("*** Control Object Changed");
  55. // Reset the current FOV to match the new object
  56. // and turn off any current zoom.
  57. resetCurrentFOV();
  58. turnOffZoom();
  59. }
  60. function GameConnection::setLagIcon(%this, %state)
  61. {
  62. if (%this.getAddress() $= "local")
  63. return;
  64. LagIcon.setVisible(%state $= "true");
  65. }
  66. function GameConnection::onFlash(%this, %state)
  67. {
  68. if (isObject(FlashFx))
  69. {
  70. if (%state)
  71. {
  72. FlashFx.enable();
  73. }
  74. else
  75. {
  76. FlashFx.disable();
  77. }
  78. }
  79. }
  80. // Called on the new connection object after connect() succeeds.
  81. function GameConnection::onConnectionAccepted(%this)
  82. {
  83. // Called on the new connection object after connect() succeeds.
  84. LagIcon.setVisible(false);
  85. // Startup the physics world on the client before any
  86. // datablocks and objects are ghosted over.
  87. physicsInitWorld( "client" );
  88. }
  89. function GameConnection::onConnectionTimedOut(%this)
  90. {
  91. // Called when an established connection times out
  92. disconnectedCleanup();
  93. MessageBoxOK( "TIMED OUT", "The server connection has timed out.");
  94. }
  95. function GameConnection::onConnectionDropped(%this, %msg)
  96. {
  97. // Established connection was dropped by the server
  98. disconnectedCleanup();
  99. MessageBoxOK( "DISCONNECT", "The server has dropped the connection: " @ %msg);
  100. }
  101. function GameConnection::onConnectionError(%this, %msg)
  102. {
  103. // General connection error, usually raised by ghosted objects
  104. // initialization problems, such as missing files. We'll display
  105. // the server's connection error message.
  106. disconnectedCleanup();
  107. MessageBoxOK( "DISCONNECT", $ServerConnectionErrorMessage @ " (" @ %msg @ ")" );
  108. }
  109. //----------------------------------------------------------------------------
  110. // Connection Failed Events
  111. //----------------------------------------------------------------------------
  112. function GameConnection::onConnectRequestRejected( %this, %msg )
  113. {
  114. switch$(%msg)
  115. {
  116. case "CR_INVALID_PROTOCOL_VERSION":
  117. %error = "Incompatible protocol version: Your game version is not compatible with this server.";
  118. case "CR_INVALID_CONNECT_PACKET":
  119. %error = "Internal Error: badly formed network packet";
  120. case "CR_YOUAREBANNED":
  121. %error = "You are not allowed to play on this server.";
  122. case "CR_SERVERFULL":
  123. %error = "This server is full.";
  124. case "CHR_PASSWORD":
  125. // XXX Should put up a password-entry dialog.
  126. if ($Client::Password $= "")
  127. MessageBoxOK( "REJECTED", "That server requires a password.");
  128. else {
  129. $Client::Password = "";
  130. MessageBoxOK( "REJECTED", "That password is incorrect.");
  131. }
  132. return;
  133. case "CHR_PROTOCOL":
  134. %error = "Incompatible protocol version: Your game version is not compatible with this server.";
  135. case "CHR_CLASSCRC":
  136. %error = "Incompatible game classes: Your game version is not compatible with this server.";
  137. case "CHR_INVALID_CHALLENGE_PACKET":
  138. %error = "Internal Error: Invalid server response packet";
  139. default:
  140. %error = "Connection error. Please try another server. Error code: (" @ %msg @ ")";
  141. }
  142. disconnectedCleanup();
  143. MessageBoxOK( "REJECTED", %error);
  144. }
  145. function GameConnection::onConnectRequestTimedOut(%this)
  146. {
  147. disconnectedCleanup();
  148. MessageBoxOK( "TIMED OUT", "Your connection to the server timed out." );
  149. }
  150. //-----------------------------------------------------------------------------
  151. // Disconnect
  152. //-----------------------------------------------------------------------------
  153. function disconnect()
  154. {
  155. // We need to stop the client side simulation
  156. // else physics resources will not cleanup properly.
  157. physicsStopSimulation( "client" );
  158. // Delete the connection if it's still there.
  159. if (isObject(ServerConnection))
  160. ServerConnection.delete();
  161. disconnectedCleanup();
  162. // Call destroyServer in case we're hosting
  163. destroyServer();
  164. }
  165. function disconnectedCleanup()
  166. {
  167. // End mission, if it's running.
  168. if( $Client::missionRunning )
  169. clientEndMission();
  170. // Disable mission lighting if it's going, this is here
  171. // in case we're disconnected while the mission is loading.
  172. $lightingMission = false;
  173. $sceneLighting::terminateLighting = true;
  174. // Clear misc script stuff
  175. HudMessageVector.clear();
  176. //
  177. LagIcon.setVisible(false);
  178. PlayerListGui.clear();
  179. // Clear all print messages
  180. clientCmdclearBottomPrint();
  181. clientCmdClearCenterPrint();
  182. // Back to the launch screen
  183. if (isObject( MainMenuGui ))
  184. Canvas.setContent( MainMenuGui );
  185. // Before we destroy the client physics world
  186. // make sure all ServerConnection objects are deleted.
  187. if(isObject(ServerConnection))
  188. {
  189. ServerConnection.deleteAllObjects();
  190. }
  191. // We can now delete the client physics simulation.
  192. physicsDestroyWorld( "client" );
  193. }