netInterface.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. };
  56. protected:
  57. Vector<NetConnection *> mPendingConnections; ///< List of connections that are in the startup phase.
  58. U32 mLastTimeoutCheckTime; ///< Last time all the active connections were checked for timeouts.
  59. U32 mRandomHashData[12]; ///< Data that gets hashed with connect challenge requests to prevent connection spoofing.
  60. bool mRandomDataInitialized; ///< Have we initialized our random number generator?
  61. bool mAllowConnections; ///< Is this NetInterface allowing connections at this time?
  62. enum NetInterfaceConstants
  63. {
  64. MaxPendingConnects = 20, ///< Maximum number of pending connections. If new connection requests come in before
  65. ChallengeRetryCount = 4, ///< Number of times to send connect challenge requests before giving up.
  66. ChallengeRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect challenge.
  67. ConnectRetryCount = 4, ///< Number of times to send connect requests before giving up.
  68. ConnectRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect request.
  69. TimeoutCheckInterval = 1500, ///< Interval in milliseconds between checking for connection timeouts.
  70. };
  71. /// Initialize random data.
  72. void initRandomData();
  73. /// @name Connection management
  74. /// Most of these are pretty self-explanatory.
  75. /// @{
  76. void addPendingConnection(NetConnection *conn);
  77. NetConnection *findPendingConnection(const NetAddress *address, U32 packetSequence);
  78. void removePendingConnection(NetConnection *conn);
  79. void sendConnectChallengeRequest(NetConnection *conn);
  80. void handleConnectChallengeRequest(const NetAddress *addr, BitStream *stream);
  81. void handleConnectChallengeResponse(const NetAddress *address, BitStream *stream);
  82. void sendConnectRequest(NetConnection *conn);
  83. void handleConnectRequest(const NetAddress *address, BitStream *stream);
  84. void sendConnectAccept(NetConnection *conn);
  85. void handleConnectAccept(const NetAddress *address, BitStream *stream);
  86. void sendConnectReject(NetConnection *conn, const char *reason);
  87. void handleConnectReject(const NetAddress *address, BitStream *stream);
  88. void handleDisconnect(const NetAddress *address, BitStream *stream);
  89. /// @}
  90. /// Calculate an MD5 sum representing a connection, and store it into addressDigest.
  91. void computeNetMD5(const NetAddress *address, U32 connectSequence, U32 addressDigest[4]);
  92. public:
  93. NetInterface();
  94. /// Returns whether or not this NetInterface allows connections from remote hosts.
  95. bool doesAllowConnections() { return mAllowConnections; }
  96. /// Sets whether or not this NetInterface allows connections from remote hosts.
  97. void setAllowsConnections(bool conn) { mAllowConnections = conn; }
  98. /// Dispatch function for processing all network packets through this NetInterface.
  99. virtual void processPacketReceiveEvent(NetAddress srcAddress, RawData packetData);
  100. /// Handles all packets that don't fall into the category of connection handshake or game data.
  101. virtual void handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream);
  102. /// Checks all connections marked as client to server for packet sends.
  103. void processClient();
  104. /// Checks all connections marked as server to client for packet sends.
  105. void processServer();
  106. /// Begins the connection handshaking process for a connection.
  107. void startConnection(NetConnection *conn);
  108. /// Checks for timeouts on all valid and pending connections.
  109. void checkTimeouts();
  110. /// Send a disconnect packet on a connection, along with a reason.
  111. void sendDisconnectPacket(NetConnection *conn, const char *reason);
  112. };
  113. /// The global net interface instance.
  114. extern NetInterface *GNet;
  115. #endif