netInterface.h
Engine/source/sim/netInterface.h
Classes:
class
NetInterface class.
Public Variables
The global net interface instance.
Detailed Description
Public Variables
NetInterface * GNet
The global net interface instance.
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _H_NETINTERFACE 25#define _H_NETINTERFACE 26 27/// NetInterface class. Manages all valid and pending notify protocol connections. 28/// 29/// @see NetConnection, GameConnection, NetObject, NetEvent 30class NetInterface 31{ 32public: 33 /// PacketType is encoded as the first byte of each packet. If the LSB of 34 /// the first byte is set (i.e. if the type number is odd), then the packet 35 /// is a data protocol packet, otherwise it's an OOB packet, suitable for 36 /// use in strange protocols, like game querying or connection initialization. 37 enum PacketTypes 38 { 39 MasterServerGameTypesRequest = 2, 40 MasterServerGameTypesResponse = 4, 41 MasterServerListRequest = 6, 42 MasterServerListResponse = 8, 43 GameMasterInfoRequest = 10, 44 GameMasterInfoResponse = 12, 45 GamePingRequest = 14, 46 GamePingResponse = 16, 47 GameInfoRequest = 18, 48 GameInfoResponse = 20, 49 GameHeartbeat = 22, 50 GGCPacket = 24, 51 ConnectChallengeRequest = 26, 52 ConnectChallengeReject = 28, 53 ConnectChallengeResponse = 30, 54 ConnectRequest = 32, 55 ConnectReject = 34, 56 ConnectAccept = 36, 57 Disconnect = 38, 58 MasterServerExtendedListResponse = 40, 59 MasterServerChallenge = 42, 60 MasterServerExtendedListRequest = 44, 61 }; 62protected: 63 64 Vector<NetConnection*> mPendingConnections; ///< List of connections that are in the startup phase. 65 U32 mLastTimeoutCheckTime; ///< Last time all the active connections were checked for timeouts. 66 U32 mRandomHashData[12]; ///< Data that gets hashed with connect challenge requests to prevent connection spoofing. 67 bool mRandomDataInitialized; ///< Have we initialized our random number generator? 68 bool mAllowConnections; ///< Is this NetInterface allowing connections at this time? 69 70 enum NetInterfaceConstants 71 { 72 MaxPendingConnects = 20, ///< Maximum number of pending connections. If new connection requests come in before 73 ChallengeRetryCount = 4, ///< Number of times to send connect challenge requests before giving up. 74 ChallengeRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect challenge. 75 76 ConnectRetryCount = 4, ///< Number of times to send connect requests before giving up. 77 ConnectRetryTime = 2500, ///< Timeout interval in milliseconds before retrying connect request. 78 TimeoutCheckInterval = 1500, ///< Interval in milliseconds between checking for connection timeouts. 79 }; 80 81 /// Initialize random data. 82 void initRandomData(); 83 84 /// @name Connection management 85 /// Most of these are pretty self-explanatory. 86 /// @{ 87 88 void addPendingConnection(NetConnection *conn); 89 NetConnection *findPendingConnection(const NetAddress *address, U32 packetSequence); 90 void removePendingConnection(NetConnection *conn); 91 92 void sendConnectChallengeRequest(NetConnection *conn); 93 void handleConnectChallengeRequest(const NetAddress *addr, BitStream *stream); 94 95 void handleConnectChallengeResponse(const NetAddress *address, BitStream *stream); 96 97 void sendConnectRequest(NetConnection *conn); 98 void handleConnectRequest(const NetAddress *address, BitStream *stream); 99 100 void sendConnectAccept(NetConnection *conn); 101 void handleConnectAccept(const NetAddress *address, BitStream *stream); 102 103 void sendConnectReject(NetConnection *conn, const char *reason); 104 void handleConnectReject(const NetAddress *address, BitStream *stream); 105 106 void handleDisconnect(const NetAddress *address, BitStream *stream); 107 108 /// @} 109 110 /// Calculate an MD5 sum representing a connection, and store it into addressDigest. 111 void computeNetMD5(const NetAddress *address, U32 connectSequence, U32 addressDigest[4]); 112 113public: 114 NetInterface(); 115 116 /// Returns whether or not this NetInterface allows connections from remote hosts. 117 bool doesAllowConnections() { return mAllowConnections; } 118 119 /// Sets whether or not this NetInterface allows connections from remote hosts. 120 void setAllowsConnections(bool conn) { mAllowConnections = conn; } 121 122 /// Dispatch function for processing all network packets through this NetInterface. 123 virtual void processPacketReceiveEvent(NetAddress srcAddress, RawData packetData); 124 125 /// Handles all packets that don't fall into the category of connection handshake or game data. 126 virtual void handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream); 127 128 /// Checks all connections marked as client to server for packet sends. 129 void processClient(); 130 131 /// Checks all connections marked as server to client for packet sends. 132 void processServer(); 133 134 /// Begins the connection handshaking process for a connection. 135 void startConnection(NetConnection *conn); 136 137 /// Checks for timeouts on all valid and pending connections. 138 void checkTimeouts(); 139 140 /// Send a disconnect packet on a connection, along with a reason. 141 void sendDisconnectPacket(NetConnection *conn, const char *reason); 142}; 143 144/// The global net interface instance. 145extern NetInterface *GNet; 146#endif 147