[5566] | 1 | /*! |
---|
| 2 | * @file network_stream.h |
---|
| 3 | * implementation of a network pipe |
---|
| 4 | */ |
---|
[5587] | 5 | |
---|
[5566] | 6 | #ifndef _NETWORK_STREAM |
---|
| 7 | #define _NETWORK_STREAM |
---|
| 8 | |
---|
[6139] | 9 | #include <vector> |
---|
| 10 | #include <list> |
---|
[7954] | 11 | #include <map> |
---|
[6139] | 12 | |
---|
[5582] | 13 | #include "data_stream.h" |
---|
[6139] | 14 | #include "server_socket.h" |
---|
| 15 | #include "handshake.h" |
---|
[9406] | 16 | #include "monitor/connection_monitor.h" |
---|
[7954] | 17 | #include "udp_server_socket.h" |
---|
[9406] | 18 | #include "peer_info.h" |
---|
[5566] | 19 | |
---|
[9406] | 20 | #include "shared_network_data.h" |
---|
| 21 | |
---|
[5647] | 22 | class Synchronizeable; |
---|
| 23 | class NetworkSocket; |
---|
[6139] | 24 | class ServerSocket; |
---|
[6341] | 25 | class NetworkGameManager; |
---|
[9406] | 26 | class NetworkMonitor; |
---|
[5589] | 27 | |
---|
[9246] | 28 | |
---|
[6139] | 29 | typedef std::list<Synchronizeable*> SynchronizeableList; |
---|
[7954] | 30 | typedef std::map<int,PeerInfo> PeerList; |
---|
[5809] | 31 | |
---|
| 32 | |
---|
[5649] | 33 | class NetworkStream : public DataStream |
---|
[5566] | 34 | { |
---|
[9869] | 35 | ObjectListDeclaration(NetworkStream); |
---|
[5996] | 36 | public: |
---|
| 37 | NetworkStream(); |
---|
[9406] | 38 | NetworkStream(int nodeType); |
---|
| 39 | virtual ~NetworkStream(); |
---|
[5804] | 40 | |
---|
[5996] | 41 | void init(); |
---|
[5804] | 42 | |
---|
[9656] | 43 | /* network interface controls */ |
---|
[9406] | 44 | void connectToMasterServer(std::string host, int port); |
---|
[9494] | 45 | void connectToProxyServer(int proxyId, std::string host, int port); |
---|
[9656] | 46 | void createServer(int clientPort, int proxyPort, int clientSoftPort); |
---|
[9406] | 47 | |
---|
[6695] | 48 | void createNetworkGameManager(); |
---|
[9494] | 49 | void startHandshake(int userId = NET_ID_MASTER_SERVER); |
---|
[6695] | 50 | |
---|
[9656] | 51 | void reconnectToServer(IP address); |
---|
| 52 | void softReconnectToServer(int serverUserId, IP address); |
---|
| 53 | void prepareSoftConnection(int userId); |
---|
| 54 | |
---|
| 55 | |
---|
[9406] | 56 | /* synchronizeable interface */ |
---|
[5996] | 57 | void connectSynchronizeable(Synchronizeable& sync); |
---|
[6139] | 58 | void disconnectSynchronizeable(Synchronizeable& sync); |
---|
[5566] | 59 | |
---|
[9406] | 60 | inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); } |
---|
[5996] | 61 | |
---|
[9406] | 62 | /* functions for the peerInfo information retreival */ |
---|
[9494] | 63 | /** @returns true if this userId is activated at this client false if not*/ |
---|
[9406] | 64 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
[9494] | 65 | /** @returns true if this userId is a local client */ |
---|
| 66 | inline bool isUserLocal( int userID) { return this->isUserIdActive(userID); } |
---|
| 67 | /** @returns true if this user is a master server */ |
---|
[9406] | 68 | inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } |
---|
[9494] | 69 | /** @returns true if this user is a proxy server */ |
---|
| 70 | inline bool isUserProxyServerActive( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServerActive(); } |
---|
| 71 | /** @returns true if this user is a client */ |
---|
[9406] | 72 | inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } |
---|
[6139] | 73 | |
---|
[9406] | 74 | /* peering interface */ |
---|
| 75 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
| 76 | inline PeerInfo* getPeerInfo() { return this->pInfo; } |
---|
| 77 | inline PeerList getPeerInfoList() { return this->peers; } |
---|
| 78 | |
---|
[9656] | 79 | inline void setRedirectionTest() { this->bSoftRedirect = true; } |
---|
| 80 | |
---|
[9406] | 81 | /* data processing*/ |
---|
[5996] | 82 | virtual void processData(); |
---|
| 83 | |
---|
[9406] | 84 | /* debugging */ |
---|
| 85 | void debug(); |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | private: |
---|
| 89 | |
---|
[6341] | 90 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
| 91 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
[9406] | 92 | void cleanUpOldSyncList(); |
---|
[6695] | 93 | int getSyncCount(); |
---|
[6341] | 94 | |
---|
[6695] | 95 | void updateConnectionList(); |
---|
[9494] | 96 | |
---|
[9406] | 97 | /* handle processes */ |
---|
[7954] | 98 | void handleHandshakes(); |
---|
[8068] | 99 | void handleUpstream( int tick ); |
---|
[9246] | 100 | void handleDownstream(int tick ); |
---|
[9406] | 101 | |
---|
| 102 | /* handle events*/ |
---|
[7954] | 103 | void handleNewClient( int userId ); |
---|
[9494] | 104 | |
---|
| 105 | void handleConnect( int userId); |
---|
[9656] | 106 | void handleSoftConnect( int userId); |
---|
[9406] | 107 | void handleReconnect( int userId ); |
---|
[9494] | 108 | void handleDisconnect( int userId ); |
---|
[9656] | 109 | void handleSoftDisconnect( int userId); |
---|
[9246] | 110 | |
---|
[8623] | 111 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
[6139] | 112 | |
---|
| 113 | |
---|
[6695] | 114 | private: |
---|
[9406] | 115 | PeerList peers; //!< list of the network node informations |
---|
[6341] | 116 | |
---|
[9406] | 117 | PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) |
---|
[9246] | 118 | |
---|
[9406] | 119 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
---|
[7954] | 120 | int currentState; //!< current state id |
---|
[6695] | 121 | |
---|
[9406] | 122 | NetworkMonitor* networkMonitor; //!< the network monitor |
---|
| 123 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
---|
[9656] | 124 | ServerSocket* clientSocket; //!< the listening socket for clients of the server |
---|
| 125 | ServerSocket* clientSoftSocket; //!< the listening socket for soft connections to the server |
---|
[9494] | 126 | ServerSocket* proxySocket; //!< socket for proxy connections |
---|
[7954] | 127 | |
---|
| 128 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
[9406] | 129 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
---|
[9246] | 130 | |
---|
[7954] | 131 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 132 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 133 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
[8623] | 134 | |
---|
[9406] | 135 | int dictServer; //!< the zip dict for the server |
---|
| 136 | int dictClient; //!< the zip dict for the client |
---|
[9494] | 137 | |
---|
| 138 | bool bRedirect; //!< true if the master server sent a redirect command |
---|
[9656] | 139 | int redirectionUID; //!< uid of the redir host |
---|
| 140 | bool bSoftRedirect; //!< tsting |
---|
[5587] | 141 | }; |
---|
[5566] | 142 | #endif /* _NETWORK_STREAM */ |
---|