[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" |
---|
[7954] | 16 | #include "connection_monitor.h" |
---|
| 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 */ |
---|
[9270] | 50 | inline bool isMasterServer() const { return (this->nodeType == NET_MASTER_SERVER)? true:false; } |
---|
| 51 | inline bool isProxyServer() const { return (this->nodeType == NET_PROXY_SERVER)? true:false; } |
---|
| 52 | inline bool isClient() const { return (this->nodeType == NET_CLIENT)? true:false; } |
---|
[9254] | 53 | // inline bool isActive() const { return this->bActive; } |
---|
[7954] | 54 | inline int getMaxConnections(){ return 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()); } |
---|
[9249] | 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 | |
---|
[9251] | 62 | |
---|
| 63 | virtual void processData(); |
---|
| 64 | |
---|
[6695] | 65 | void debug(); |
---|
[9246] | 66 | |
---|
[9251] | 67 | |
---|
| 68 | private: |
---|
[9268] | 69 | |
---|
[9251] | 70 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
| 71 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
[7954] | 72 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
[9268] | 73 | void cleanUpOldSyncList(); |
---|
[9251] | 74 | int getSyncCount(); |
---|
[6695] | 75 | |
---|
| 76 | void updateConnectionList(); |
---|
[9268] | 77 | |
---|
[7954] | 78 | void handleHandshakes(); |
---|
[8068] | 79 | void handleUpstream( int tick ); |
---|
[9246] | 80 | void handleDownstream(int tick ); |
---|
[7954] | 81 | void handleNewClient( int userId ); |
---|
[9246] | 82 | |
---|
[9268] | 83 | |
---|
[8623] | 84 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
[6139] | 85 | |
---|
| 86 | |
---|
[6695] | 87 | private: |
---|
[9251] | 88 | PeerList peers; //!< list of the network node informations |
---|
[6341] | 89 | |
---|
[9270] | 90 | int nodeType; //!< the type of this host (NET_CLIENT, NET_MASTER_SERVER,...) |
---|
[9251] | 91 | int myHostId; //!< the host id of the localhost |
---|
[9246] | 92 | |
---|
[9251] | 93 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
---|
[7954] | 94 | int currentState; //!< current state id |
---|
[6695] | 95 | |
---|
[9268] | 96 | NetworkMonitor* networkMonitor; //!< the network monitor |
---|
[9251] | 97 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
---|
| 98 | ServerSocket* serverSocket; //!< the listening socket of the server |
---|
[7954] | 99 | |
---|
| 100 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
[9251] | 101 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
---|
[9246] | 102 | |
---|
[7954] | 103 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 104 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 105 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
[8623] | 106 | |
---|
[9251] | 107 | int dictServer; //!< the zip dict for the server |
---|
| 108 | int dictClient; //!< the zip dict for the client |
---|
[5587] | 109 | }; |
---|
[5566] | 110 | #endif /* _NETWORK_STREAM */ |
---|