gameConnection.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _GAMECONNECTION_H_
  23. #define _GAMECONNECTION_H_
  24. #ifndef _SIMBASE_H_
  25. #include "sim/simBase.h"
  26. #endif
  27. #ifndef _NETCONNECTION_H_
  28. #include "network/netConnection.h"
  29. #endif
  30. #ifndef _BITVECTOR_H_
  31. #include "collection/bitVector.h"
  32. #endif
  33. enum GameConnectionConstants {
  34. MaxClients = 126,
  35. DataBlockQueueCount = 16
  36. };
  37. class AudioAsset;
  38. class MatrixF;
  39. class MatrixF;
  40. class Point3F;
  41. struct AuthInfo;
  42. #define GameString "Torque"
  43. /// Formerly contained a certificate, showing that something was valid.
  44. class Auth2Certificate
  45. {
  46. U32 xxx;
  47. };
  48. /// Formerly contained data indicating whether a user is valid.
  49. struct AuthInfo
  50. {
  51. enum {
  52. MaxNameLen = 31,
  53. };
  54. bool valid;
  55. char name[MaxNameLen + 1];
  56. };
  57. /// Formerly validated the server's authentication info.
  58. inline bool validateAuthenticatedServer()
  59. {
  60. return true;
  61. }
  62. /// Formerly validated the client's authentication info.
  63. inline bool validateAuthenticatedClient()
  64. {
  65. return true;
  66. }
  67. class GameConnection : public NetConnection
  68. {
  69. private:
  70. typedef NetConnection Parent;
  71. char mDisconnectReason[256];
  72. U32 mMissionCRC; // crc of the current mission file from the server
  73. public:
  74. /// @name Protocol Versions
  75. ///
  76. /// Protocol versions are used to indicated changes in network traffic.
  77. /// These could be changes in how any object transmits or processes
  78. /// network information. You can specify backwards compatability by
  79. /// specifying a MinRequireProtocolVersion. If the client
  80. /// protocol is >= this min value, the connection is accepted.
  81. ///
  82. /// Torque (V12) SDK 1.0 uses protocol = 1
  83. ///
  84. /// Torque SDK 1.1 uses protocol = 2
  85. /// @{
  86. static const U32 CurrentProtocolVersion;
  87. static const U32 MinRequiredProtocolVersion;
  88. /// @}
  89. /// Configuration
  90. enum Constants {
  91. GameConnectionBlockTypeCount,
  92. MaxConnectArgs = 16,
  93. };
  94. /// Set connection arguments; these are passed to the server when we connect.
  95. void setConnectArgs(U32 argc, const char **argv);
  96. /// Set the server password to use when we join.
  97. void setJoinPassword(const char *password);
  98. /// @name Event Handling
  99. /// @{
  100. virtual void onTimedOut();
  101. virtual void onConnectTimedOut();
  102. virtual void onDisconnect(const char *reason);
  103. virtual void onConnectionRejected(const char *reason);
  104. virtual void onConnectionEstablished(bool isInitiator);
  105. virtual void handleStartupError(const char *errorString);
  106. /// @}
  107. /// @name Packet I/O
  108. /// @{
  109. virtual void writeConnectRequest(BitStream *stream);
  110. virtual bool readConnectRequest(BitStream *stream, const char **errorString);
  111. virtual void writeConnectAccept(BitStream *stream);
  112. virtual bool readConnectAccept(BitStream *stream, const char **errorString);
  113. /// @}
  114. bool canRemoteCreate();
  115. private:
  116. /// @name Connection State
  117. /// This data is set with setConnectArgs() and setJoinPassword(), and
  118. /// sent across the wire when we connect.
  119. /// @{
  120. U32 mConnectArgc;
  121. char *mConnectArgv[MaxConnectArgs];
  122. char *mJoinPassword;
  123. /// @}
  124. protected:
  125. struct GamePacketNotify : public NetConnection::PacketNotify
  126. {
  127. GamePacketNotify();
  128. };
  129. PacketNotify *allocNotify();
  130. AuthInfo * mAuthInfo;
  131. static S32 mLagThresholdMS;
  132. S32 mLastPacketTime;
  133. bool mLagging;
  134. /// @}
  135. /// @name Packet I/O
  136. /// @{
  137. void readPacket (BitStream *bstream);
  138. void writePacket (BitStream *bstream, PacketNotify *note);
  139. void packetReceived (PacketNotify *note);
  140. void packetDropped (PacketNotify *note);
  141. void connectionError (const char *errorString);
  142. /// @}
  143. public:
  144. DECLARE_CONOBJECT(GameConnection);
  145. void handleConnectionMessage(U32 message, U32 sequence, U32 ghostCount);
  146. static void consoleInit();
  147. void setDisconnectReason(const char *reason);
  148. GameConnection();
  149. ~GameConnection();
  150. bool onAdd();
  151. void onRemove();
  152. static GameConnection *getServerConnection() { return dynamic_cast<GameConnection*>((NetConnection *) mServerConnection); }
  153. static GameConnection *getLocalClientConnection() { return dynamic_cast<GameConnection*>((NetConnection *) mLocalClientConnection); }
  154. /// @}
  155. void detectLag();
  156. /// @name Authentication
  157. ///
  158. /// This is remnant code from Tribes 2.
  159. /// @{
  160. void setAuthInfo(const AuthInfo *info);
  161. const AuthInfo *getAuthInfo();
  162. /// @}
  163. };
  164. #endif