| 1 | /*! |
|---|
| 2 | * @file network_stream.h |
|---|
| 3 | * implementation of a network pipe |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #ifndef _NETWORK_STREAM |
|---|
| 7 | #define _NETWORK_STREAM |
|---|
| 8 | |
|---|
| 9 | #include <vector> |
|---|
| 10 | #include <list> |
|---|
| 11 | #include <map> |
|---|
| 12 | |
|---|
| 13 | #include "data_stream.h" |
|---|
| 14 | #include "server_socket.h" |
|---|
| 15 | #include "handshake.h" |
|---|
| 16 | #include "connection_monitor.h" |
|---|
| 17 | #include "udp_server_socket.h" |
|---|
| 18 | #include "peer_info.h" |
|---|
| 19 | |
|---|
| 20 | class Synchronizeable; |
|---|
| 21 | class NetworkSocket; |
|---|
| 22 | class ServerSocket; |
|---|
| 23 | class NetworkGameManager; |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | typedef std::list<Synchronizeable*> SynchronizeableList; |
|---|
| 27 | typedef std::map<int,PeerInfo> PeerList; |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class NetworkStream : public DataStream |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | public: |
|---|
| 34 | NetworkStream(); |
|---|
| 35 | NetworkStream( std::string host, int port); |
|---|
| 36 | NetworkStream( int port ); |
|---|
| 37 | |
|---|
| 38 | virtual ~NetworkStream(); |
|---|
| 39 | void init(); |
|---|
| 40 | |
|---|
| 41 | void createNetworkGameManager(); |
|---|
| 42 | void startHandshake(); |
|---|
| 43 | |
|---|
| 44 | /* synchronizeable interface */ |
|---|
| 45 | void connectSynchronizeable(Synchronizeable& sync); |
|---|
| 46 | void disconnectSynchronizeable(Synchronizeable& sync); |
|---|
| 47 | |
|---|
| 48 | /* functions for the localhost settings */ |
|---|
| 49 | inline bool isMasterServer() const { return (this->type == NET_MASTER_SERVER)? true:false; } |
|---|
| 50 | inline bool isProxyServer() const { return (this->type == NET_PROXY_SERVER)? true:false; } |
|---|
| 51 | inline bool isClient() const { return (this->type == NET_CLIENT)? true:false; } |
|---|
| 52 | // inline bool isActive() const { return this->bActive; } |
|---|
| 53 | inline int getMaxConnections(){ return MAX_CONNECTIONS; } |
|---|
| 54 | |
|---|
| 55 | /* functions for the peerInfo information retreival */ |
|---|
| 56 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
|---|
| 57 | inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer; } |
|---|
| 58 | inline bool isUserProxyServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServer; } |
|---|
| 59 | inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient; } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | virtual void processData(); |
|---|
| 63 | |
|---|
| 64 | void debug(); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | private: |
|---|
| 68 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
|---|
| 69 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
|---|
| 70 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
|---|
| 71 | |
|---|
| 72 | int getSyncCount(); |
|---|
| 73 | |
|---|
| 74 | void updateConnectionList(); |
|---|
| 75 | void handleHandshakes(); |
|---|
| 76 | void handleUpstream( int tick ); |
|---|
| 77 | void handleDownstream(int tick ); |
|---|
| 78 | void handleNewClient( int userId ); |
|---|
| 79 | void cleanUpOldSyncList(); |
|---|
| 80 | |
|---|
| 81 | void writeToNewDict( byte * data, int length, bool upstream ); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | private: |
|---|
| 85 | PeerList peers; //!< list of the network node informations |
|---|
| 86 | |
|---|
| 87 | int type; //!< the type of this host (NET_CLIENT, NET_MASTER_SERVER,...) |
|---|
| 88 | int myHostId; //!< the host id of the localhost |
|---|
| 89 | /* bool bActive; */ //!< true if this host is active |
|---|
| 90 | |
|---|
| 91 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
|---|
| 92 | int currentState; //!< current state id |
|---|
| 93 | |
|---|
| 94 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
|---|
| 95 | ServerSocket* serverSocket; //!< the listening socket of the server |
|---|
| 96 | |
|---|
| 97 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
|---|
| 98 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
|---|
| 99 | |
|---|
| 100 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
|---|
| 101 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
|---|
| 102 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
|---|
| 103 | |
|---|
| 104 | int dictServer; //!< the zip dict for the server |
|---|
| 105 | int dictClient; //!< the zip dict for the client |
|---|
| 106 | }; |
|---|
| 107 | #endif /* _NETWORK_STREAM */ |
|---|