[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" |
---|
[9275] | 16 | #include "monitor/connection_monitor.h" |
---|
[7954] | 17 | #include "udp_server_socket.h" |
---|
[9252] | 18 | #include "peer_info.h" |
---|
[5566] | 19 | |
---|
[5647] | 20 | class Synchronizeable; |
---|
| 21 | class NetworkSocket; |
---|
[6139] | 22 | class ServerSocket; |
---|
[6341] | 23 | class NetworkGameManager; |
---|
[9268] | 24 | class NetworkMonitor; |
---|
[5589] | 25 | |
---|
[9246] | 26 | |
---|
[6139] | 27 | typedef std::list<Synchronizeable*> SynchronizeableList; |
---|
[7954] | 28 | typedef std::map<int,PeerInfo> PeerList; |
---|
[5809] | 29 | |
---|
| 30 | |
---|
[5649] | 31 | class NetworkStream : public DataStream |
---|
[5566] | 32 | { |
---|
| 33 | |
---|
[5996] | 34 | public: |
---|
| 35 | NetworkStream(); |
---|
[7954] | 36 | NetworkStream( std::string host, int port); |
---|
| 37 | NetworkStream( int port ); |
---|
[5804] | 38 | |
---|
[6981] | 39 | virtual ~NetworkStream(); |
---|
[5996] | 40 | void init(); |
---|
[5804] | 41 | |
---|
[6695] | 42 | void createNetworkGameManager(); |
---|
| 43 | void startHandshake(); |
---|
| 44 | |
---|
[9251] | 45 | /* synchronizeable interface */ |
---|
[5996] | 46 | void connectSynchronizeable(Synchronizeable& sync); |
---|
[6139] | 47 | void disconnectSynchronizeable(Synchronizeable& sync); |
---|
[5566] | 48 | |
---|
[9251] | 49 | /* functions for the localhost settings */ |
---|
[9285] | 50 | inline bool isMasterServer() const { return (this->pInfo->nodeType == NET_MASTER_SERVER)? true:false; } |
---|
| 51 | inline bool isProxyServer() const { return (this->pInfo->nodeType == NET_PROXY_SERVER)? true:false; } |
---|
| 52 | inline bool isClient() const { return (this->pInfo->nodeType == NET_CLIENT)? true:false; } |
---|
[9254] | 53 | // inline bool isActive() const { return this->bActive; } |
---|
[9290] | 54 | inline int getMaxConnections(){ return NET_MAX_CONNECTIONS; } |
---|
[6139] | 55 | |
---|
[9251] | 56 | /* functions for the peerInfo information retreival */ |
---|
[7954] | 57 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
[9282] | 58 | inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } |
---|
| 59 | inline bool isUserProxyServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServer(); } |
---|
| 60 | inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } |
---|
[6634] | 61 | |
---|
[9284] | 62 | /* peering interface */ |
---|
| 63 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
| 64 | inline PeerInfo* getPeerInfo() { return this->pInfo; } |
---|
| 65 | inline PeerList getPeerInfoList() { return this->peers; } |
---|
[9251] | 66 | |
---|
[9284] | 67 | /* data processing*/ |
---|
[9251] | 68 | virtual void processData(); |
---|
| 69 | |
---|
[9284] | 70 | /* debugging */ |
---|
[6695] | 71 | void debug(); |
---|
[9246] | 72 | |
---|
[9251] | 73 | |
---|
| 74 | private: |
---|
[9268] | 75 | |
---|
[9251] | 76 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
| 77 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
[9268] | 78 | void cleanUpOldSyncList(); |
---|
[9251] | 79 | int getSyncCount(); |
---|
[6695] | 80 | |
---|
| 81 | void updateConnectionList(); |
---|
[9268] | 82 | |
---|
[7954] | 83 | void handleHandshakes(); |
---|
[8068] | 84 | void handleUpstream( int tick ); |
---|
[9246] | 85 | void handleDownstream(int tick ); |
---|
[7954] | 86 | void handleNewClient( int userId ); |
---|
[9246] | 87 | |
---|
[9268] | 88 | |
---|
[8623] | 89 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
[6139] | 90 | |
---|
| 91 | |
---|
[6695] | 92 | private: |
---|
[9251] | 93 | PeerList peers; //!< list of the network node informations |
---|
[6341] | 94 | |
---|
[9284] | 95 | PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) |
---|
[9246] | 96 | |
---|
[9251] | 97 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
---|
[7954] | 98 | int currentState; //!< current state id |
---|
[6695] | 99 | |
---|
[9268] | 100 | NetworkMonitor* networkMonitor; //!< the network monitor |
---|
[9251] | 101 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
---|
| 102 | ServerSocket* serverSocket; //!< the listening socket of the server |
---|
[7954] | 103 | |
---|
| 104 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
[9251] | 105 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
---|
[9246] | 106 | |
---|
[7954] | 107 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 108 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 109 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
[8623] | 110 | |
---|
[9251] | 111 | int dictServer; //!< the zip dict for the server |
---|
| 112 | int dictClient; //!< the zip dict for the client |
---|
[5587] | 113 | }; |
---|
[5566] | 114 | #endif /* _NETWORK_STREAM */ |
---|