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