netInterface.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef _H_NETINTERFACE
  23. #define _H_NETINTERFACE
  24. /// NetInterface class. Manages all valid and pending notify protocol connections.
  25. ///
  26. /// @see NetConnection, GameConnection, NetObject, NetEvent
  27. class NetInterface
  28. {
  29. public:
  30. /// PacketType is encoded as the first byte of each packet. If the LSB of
  31. /// the first byte is set (i.e. if the type number is odd), then the packet
  32. /// is a data protocol packet, otherwise it's an OOB packet, suitable for
  33. /// use in strange protocols, like game querying or connection initialization.
  34. enum PacketTypes
  35. {
  36. MasterServerGameTypesRequest = 2,
  37. MasterServerGameTypesResponse = 4,
  38. MasterServerListRequest = 6,
  39. MasterServerListResponse = 8,
  40. GameMasterInfoRequest = 10,
  41. GameMasterInfoResponse = 12,
  42. GamePingRequest = 14,
  43. GamePingResponse = 16,
  44. GameInfoRequest = 18,
  45. GameInfoResponse = 20,
  46. GameHeartbeat = 22,
  47. GGCPacket = 24,
  48. ConnectChallengeRequest = 26,
  49. ConnectChallengeReject = 28,
  50. ConnectChallengeResponse = 30,
  51. ConnectRequest = 32,
  52. ConnectReject = 34,
  53. ConnectAccept = 36,
  54. Disconnect = 38,
  55. MasterServerExtendedListResponse = 40,
  56. MasterServerChallenge = 42,
  57. MasterServerExtendedListRequest = 44,
  58. };
  59. protected:
  60. Vector<NetConnection *> mPendingConnections; ///< List of connections that are in the startup phase.
  61. U32 mLastTimeoutCheckTime; ///< Last time all the active connections were checked for timeouts.
  62. U32 mRandomHashData[12]; ///< Data that gets hashed with connect challenge requests to prevent connection spoofing.
  63. bool mRandomDataInitialized; ///< Have we initialized our random number generator?
  64. bool mAllowConnections; ///< Is this NetInterface allowing connections at this time?
  65. enum NetInterfaceConstants
  66. {
  67. MaxPendingConnects = 20, ///< Maximum number of pending connections. If new connection requests come in before
  68. ChallengeRetryCount = 4, ///< Number of times to send connect challenge requests before giving up.
  69. ChallengeRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect challenge.
  70. ConnectRetryCount = 4, ///< Number of times to send connect requests before giving up.
  71. ConnectRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect request.
  72. TimeoutCheckInterval = 1500, ///< Interval in milliseconds between checking for connection timeouts.
  73. };
  74. /// Initialize random data.
  75. void initRandomData();
  76. /// @name Connection management
  77. /// Most of these are pretty self-explanatory.
  78. /// @{
  79. void addPendingConnection(NetConnection *conn);
  80. NetConnection *findPendingConnection(const NetAddress *address, U32 packetSequence);
  81. void removePendingConnection(NetConnection *conn);
  82. void sendConnectChallengeRequest(NetConnection *conn);
  83. void handleConnectChallengeRequest(const NetAddress *addr, BitStream *stream);
  84. void handleConnectChallengeResponse(const NetAddress *address, BitStream *stream);
  85. void sendConnectRequest(NetConnection *conn);
  86. void handleConnectRequest(const NetAddress *address, BitStream *stream);
  87. void sendConnectAccept(NetConnection *conn);
  88. void handleConnectAccept(const NetAddress *address, BitStream *stream);
  89. void sendConnectReject(NetConnection *conn, const char *reason);
  90. void handleConnectReject(const NetAddress *address, BitStream *stream);
  91. void handleDisconnect(const NetAddress *address, BitStream *stream);
  92. /// @}
  93. /// Calculate an MD5 sum representing a connection, and store it into addressDigest.
  94. void computeNetMD5(const NetAddress *address, U32 connectSequence, U32 addressDigest[4]);
  95. public:
  96. NetInterface();
  97. /// Returns whether or not this NetInterface allows connections from remote hosts.
  98. bool doesAllowConnections() { return mAllowConnections; }
  99. /// Sets whether or not this NetInterface allows connections from remote hosts.
  100. void setAllowsConnections(bool conn) { mAllowConnections = conn; }
  101. /// Dispatch function for processing all network packets through this NetInterface.
  102. virtual void processPacketReceiveEvent(NetAddress srcAddress, RawData packetData);
  103. /// Handles all packets that don't fall into the category of connection handshake or game data.
  104. virtual void handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream);
  105. /// Checks all connections marked as client to server for packet sends.
  106. void processClient();
  107. /// Checks all connections marked as server to client for packet sends.
  108. void processServer();
  109. /// Begins the connection handshaking process for a connection.
  110. void startConnection(NetConnection *conn);
  111. /// Checks for timeouts on all valid and pending connections.
  112. void checkTimeouts();
  113. /// Send a disconnect packet on a connection, along with a reason.
  114. void sendDisconnectPacket(NetConnection *conn, const char *reason);
  115. };
  116. /// The global net interface instance.
  117. extern NetInterface *GNet;
  118. #endif