[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" |
---|
[5566] | 18 | |
---|
[5647] | 19 | class Synchronizeable; |
---|
| 20 | class NetworkSocket; |
---|
[6139] | 21 | class ServerSocket; |
---|
[6341] | 22 | class NetworkGameManager; |
---|
[5589] | 23 | |
---|
[9246] | 24 | //!< this structure contains informations about the network node |
---|
[7954] | 25 | class PeerInfo |
---|
| 26 | { |
---|
| 27 | public: |
---|
| 28 | PeerInfo() { clear(); } |
---|
| 29 | void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; connectionMonitor = NULL; } |
---|
[9246] | 30 | |
---|
| 31 | |
---|
| 32 | public: |
---|
[7954] | 33 | int userId; |
---|
| 34 | bool isServer; |
---|
| 35 | NetworkSocket * socket; |
---|
| 36 | Handshake * handshake; |
---|
| 37 | ConnectionMonitor * connectionMonitor; |
---|
| 38 | int lastAckedState; |
---|
| 39 | int lastRecvedState; |
---|
| 40 | }; |
---|
| 41 | |
---|
[6139] | 42 | typedef std::list<Synchronizeable*> SynchronizeableList; |
---|
[7954] | 43 | typedef std::map<int,PeerInfo> PeerList; |
---|
[5809] | 44 | |
---|
| 45 | |
---|
[5649] | 46 | class NetworkStream : public DataStream |
---|
[5566] | 47 | { |
---|
| 48 | |
---|
[5996] | 49 | public: |
---|
| 50 | NetworkStream(); |
---|
[7954] | 51 | NetworkStream( std::string host, int port); |
---|
| 52 | NetworkStream( int port ); |
---|
[5804] | 53 | |
---|
[6981] | 54 | virtual ~NetworkStream(); |
---|
[5996] | 55 | void init(); |
---|
[5804] | 56 | |
---|
[6695] | 57 | void createNetworkGameManager(); |
---|
| 58 | void startHandshake(); |
---|
| 59 | |
---|
[5996] | 60 | void connectSynchronizeable(Synchronizeable& sync); |
---|
[6139] | 61 | void disconnectSynchronizeable(Synchronizeable& sync); |
---|
[5566] | 62 | |
---|
[9248] | 63 | inline bool isMasterServer() const { return (this->type == NET_MASTER_SERVER)? true:false; } |
---|
| 64 | inline bool isProxyServer() const { return (this->type == NET_PROXY_SERVER)? true:false; } |
---|
| 65 | inline bool isClient() const { return (this->type == NET_CLIENT)? true:false; } |
---|
[5996] | 66 | inline bool isActive() const { return this->bActive; } |
---|
| 67 | |
---|
[7954] | 68 | inline int getMaxConnections(){ return MAX_CONNECTIONS; } |
---|
[6139] | 69 | |
---|
[5996] | 70 | virtual void processData(); |
---|
| 71 | |
---|
[6341] | 72 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
| 73 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
[6695] | 74 | int getSyncCount(); |
---|
[6341] | 75 | |
---|
[7954] | 76 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
| 77 | inline bool isUserServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isServer; } |
---|
[6634] | 78 | |
---|
[6695] | 79 | void debug(); |
---|
[9246] | 80 | |
---|
[7954] | 81 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
[6695] | 82 | |
---|
| 83 | |
---|
[5996] | 84 | private: |
---|
[6695] | 85 | void updateConnectionList(); |
---|
[7954] | 86 | void handleHandshakes(); |
---|
[8068] | 87 | void handleUpstream( int tick ); |
---|
[9246] | 88 | void handleDownstream(int tick ); |
---|
[7954] | 89 | void handleNewClient( int userId ); |
---|
| 90 | void cleanUpOldSyncList(); |
---|
[9246] | 91 | |
---|
[8623] | 92 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
[6139] | 93 | |
---|
| 94 | |
---|
[6695] | 95 | private: |
---|
| 96 | SynchronizeableList synchronizeables; |
---|
[7954] | 97 | PeerList peers; |
---|
[6695] | 98 | ServerSocket* serverSocket; |
---|
| 99 | int type; |
---|
| 100 | bool bActive; |
---|
| 101 | std::list<int> freeSocketSlots; |
---|
[6341] | 102 | |
---|
[6695] | 103 | int myHostId; |
---|
[9246] | 104 | |
---|
[7954] | 105 | int currentState; //!< current state id |
---|
[6695] | 106 | |
---|
| 107 | NetworkGameManager* networkGameManager; |
---|
[7954] | 108 | |
---|
| 109 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
[9246] | 110 | |
---|
[7954] | 111 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 112 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
| 113 | |
---|
| 114 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
[8623] | 115 | |
---|
| 116 | int dictServer; |
---|
| 117 | int dictClient; |
---|
[5587] | 118 | }; |
---|
[5566] | 119 | #endif /* _NETWORK_STREAM */ |
---|