serverConnection.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. Canvas.setContent(PlayGui);
  48. }
  49. }
  50. function GameConnection::onControlObjectChange(%this)
  51. {
  52. echo ("*** Control Object Changed");
  53. // Reset the current FOV to match the new object
  54. // and turn off any current zoom.
  55. resetCurrentFOV();
  56. turnOffZoom();
  57. }
  58. // Called on the new connection object after connect() succeeds.
  59. function GameConnection::onConnectionAccepted(%this)
  60. {
  61. // Startup the physX world on the client before any
  62. // datablocks and objects are ghosted over.
  63. physicsInitWorld( "client" );
  64. }
  65. function GameConnection::onConnectionError(%this, %msg)
  66. {
  67. // General connection error, usually raised by ghosted objects
  68. // initialization problems, such as missing files. We'll display
  69. // the server's connection error message.
  70. disconnectedCleanup();
  71. MessageBoxOK( "DISCONNECT", $ServerConnectionErrorMessage @ " (" @ %msg @ ")" );
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Disconnect
  75. //-----------------------------------------------------------------------------
  76. function disconnect()
  77. {
  78. // We need to stop the client side simulation
  79. // else physics resources will not cleanup properly.
  80. physicsStopSimulation( "client" );
  81. // Delete the connection if it's still there.
  82. if (isObject(ServerConnection))
  83. ServerConnection.delete();
  84. disconnectedCleanup();
  85. // Call destroyServer in case we're hosting
  86. destroyServer();
  87. }
  88. function disconnectedCleanup()
  89. {
  90. // End mission, if it's running.
  91. if( $Client::missionRunning )
  92. clientEndMission();
  93. // Disable mission lighting if it's going, this is here
  94. // in case we're disconnected while the mission is loading.
  95. $lightingMission = false;
  96. $sceneLighting::terminateLighting = true;
  97. // Back to the launch screen
  98. if (isObject( MainMenuGui ))
  99. Canvas.setContent( MainMenuGui );
  100. // Before we destroy the client physics world
  101. // make sure all ServerConnection objects are deleted.
  102. if(isObject(ServerConnection))
  103. {
  104. ServerConnection.deleteAllObjects();
  105. }
  106. // We can now delete the client physics simulation.
  107. physicsDestroyWorld( "client" );
  108. }