home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / client / scripts / serverConnection.cs < prev    next >
Encoding:
Text File  |  2005-11-23  |  5.4 KB  |  169 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Functions dealing with connecting to a server
  7.  
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Server connection error
  11. //-----------------------------------------------------------------------------
  12.  
  13. addMessageCallback( 'MsgConnectionError', handleConnectionErrorMessage );
  14.  
  15. function handleConnectionErrorMessage(%msgType, %msgString, %msgError)
  16. {
  17.    // On connect the server transmits a message to display if there
  18.    // are any problems with the connection.  Most connection errors
  19.    // are game version differences, so hopefully the server message
  20.    // will tell us where to get the latest version of the game.
  21.    $ServerConnectionErrorMessage = %msgError;
  22. }
  23.  
  24.  
  25. //----------------------------------------------------------------------------
  26. // GameConnection client callbacks
  27. //----------------------------------------------------------------------------
  28.  
  29. function GameConnection::initialControlSet(%this)
  30. {
  31.    echo ("*** Initial Control Object");
  32.  
  33.    // The first control object has been set by the server
  34.    // and we are now ready to go.
  35.    
  36.    // first check if the editor is active
  37.    if (!Editor::checkActiveLoadDone())
  38.    {
  39.       // Activate if not already set...
  40.       if (Canvas.getContent() != $Client::GameGUI.getId())
  41.          Canvas.setContent($Client::GameGUI);
  42.    }
  43. }
  44.  
  45. function GameConnection::setLagIcon(%this, %state)
  46. {
  47.    if (%this.getAddress() $= "local")
  48.       return;
  49.    LagIcon.setVisible(%state $= "true");
  50. }
  51.  
  52. function GameConnection::onConnectionAccepted(%this)
  53. {
  54.    // Called on the new connection object after connect() succeeds.
  55.    LagIcon.setVisible(false);
  56. }
  57.  
  58. function GameConnection::onConnectionTimedOut(%this)
  59. {
  60.    // Called when an established connection times out
  61.    disconnectedCleanup();
  62.    MessageBoxOK( "TIMED OUT", "The server connection has timed out.");
  63. }
  64.  
  65. function GameConnection::onConnectionDropped(%this, %msg)
  66. {
  67.    // Established connection was dropped by the server
  68.    disconnectedCleanup();
  69.    MessageBoxOK( "DISCONNECT", "The server has dropped the connection: " @ %msg);
  70. }
  71.  
  72. function GameConnection::onConnectionError(%this, %msg)
  73. {
  74.    // General connection error, usually raised by ghosted objects
  75.    // initialization problems, such as missing files.  We'll display
  76.    // the server's connection error message.
  77.    disconnectedCleanup();
  78.    MessageBoxOK( "DISCONNECT", $ServerConnectionErrorMessage @ " (" @ %msg @ ")" );
  79. }
  80.  
  81.  
  82. //----------------------------------------------------------------------------
  83. // Connection Failed Events
  84. //----------------------------------------------------------------------------
  85.  
  86. function GameConnection::onConnectRequestRejected( %this, %msg )
  87. {
  88.    switch$(%msg)
  89.    {
  90.       case "CR_INVALID_PROTOCOL_VERSION":
  91.          %error = "Incompatible protocol version: Your game version is not compatible with this server.";
  92.       case "CR_INVALID_CONNECT_PACKET":
  93.          %error = "Internal Error: badly formed network packet";
  94.       case "CR_YOUAREBANNED":
  95.          %error = "You are not allowed to play on this server.";
  96.       case "CR_SERVERFULL":
  97.          %error = "This server is full.";
  98.       case "CHR_PASSWORD":
  99.          // XXX Should put up a password-entry dialog.
  100.          if ($Client::Password $= "")
  101.             MessageBoxOK( "REJECTED", "That server requires a password.");
  102.          else {
  103.             $Client::Password = "";
  104.             MessageBoxOK( "REJECTED", "That password is incorrect.");
  105.          }
  106.          return;
  107.       case "CHR_PROTOCOL":
  108.          %error = "Incompatible protocol version: Your game version is not compatible with this server.";
  109.       case "CHR_CLASSCRC":
  110.          %error = "Incompatible game classes: Your game version is not compatible with this server.";
  111.       case "CHR_INVALID_CHALLENGE_PACKET":
  112.          %error = "Internal Error: Invalid server response packet";
  113.       default:
  114.          %error = "Connection error.  Please try another server.  Error code: (" @ %msg @ ")";
  115.    }
  116.    disconnectedCleanup();
  117.    MessageBoxOK( "REJECTED", %error);
  118. }
  119.  
  120. function GameConnection::onConnectRequestTimedOut(%this)
  121. {
  122.    disconnectedCleanup();
  123.    MessageBoxOK( "TIMED OUT", "Your connection to the server timed out." );
  124. }
  125.  
  126.  
  127. //-----------------------------------------------------------------------------
  128. // Disconnect
  129. //-----------------------------------------------------------------------------
  130.  
  131. function disconnect()
  132. {
  133.    // Delete the connection if it's still there.
  134.    if (isObject(ServerConnection))
  135.       ServerConnection.delete();
  136.    disconnectedCleanup();
  137.  
  138.    // Call destroyServer in case we're hosting
  139.    destroyServer();
  140. }
  141.  
  142. function disconnectedCleanup()
  143. {
  144.    // Clear misc script stuff
  145.    HudMessageVector.clear();
  146.  
  147.    // Terminate all playing sounds
  148.    alxStopAll();
  149.    if (isObject(MusicPlayer))
  150.       MusicPlayer.stop();
  151.  
  152.    //
  153.    LagIcon.setVisible(false);
  154.    PlayerListGui.clear();
  155.    
  156.    // Clear all print messages
  157.    clientCmdclearBottomPrint();
  158.    clientCmdClearCenterPrint();
  159.  
  160.    // Back to the launch screen
  161.    if (Canvas.getContent() != MainMenuGui.getId())
  162.       Canvas.setContent(MainMenuGui);
  163.  
  164.    // Dump anything we're not using
  165.    clearTextureHolds();
  166.    purgeResources();
  167. }
  168.  
  169.