[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> |
---|
[7565] | 11 | #include <map> |
---|
[6139] | 12 | |
---|
[5582] | 13 | #include "data_stream.h" |
---|
[5809] | 14 | #include "network_protocol.h" |
---|
[6139] | 15 | #include "server_socket.h" |
---|
| 16 | #include "handshake.h" |
---|
[5566] | 17 | |
---|
[5647] | 18 | class Synchronizeable; |
---|
| 19 | class NetworkSocket; |
---|
[6139] | 20 | class ServerSocket; |
---|
[5647] | 21 | class ConnectionMonitor; |
---|
[5741] | 22 | class NetworkProtocol; |
---|
[6341] | 23 | class NetworkGameManager; |
---|
[5589] | 24 | |
---|
[7565] | 25 | class PeerInfo |
---|
| 26 | { |
---|
| 27 | public: |
---|
| 28 | PeerInfo() { clear(); } |
---|
| 29 | void clear() { userId = 0; isServer = false; socket = NULL; handshake = NULL; lastAckedState = 0; lastRecvedState = 0; } |
---|
| 30 | int userId; |
---|
| 31 | bool isServer; |
---|
| 32 | NetworkSocket * socket; |
---|
| 33 | Handshake * handshake; |
---|
| 34 | int lastAckedState; |
---|
| 35 | int lastRecvedState; |
---|
| 36 | }; |
---|
| 37 | |
---|
[6139] | 38 | typedef std::list<Synchronizeable*> SynchronizeableList; |
---|
[7565] | 39 | typedef std::map<int,PeerInfo> PeerList; |
---|
[5809] | 40 | |
---|
| 41 | |
---|
[5649] | 42 | class NetworkStream : public DataStream |
---|
[5566] | 43 | { |
---|
| 44 | |
---|
[5996] | 45 | public: |
---|
| 46 | NetworkStream(); |
---|
[7540] | 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 | |
---|
[7575] | 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 | |
---|
[7565] | 70 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
[6634] | 71 | |
---|
[6695] | 72 | void debug(); |
---|
[7565] | 73 | |
---|
| 74 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
[6695] | 75 | |
---|
| 76 | |
---|
[5996] | 77 | private: |
---|
[6695] | 78 | void updateConnectionList(); |
---|
[7565] | 79 | void handleHandshakes(); |
---|
| 80 | void handleUpstream(); |
---|
| 81 | void handleDownstream(); |
---|
[6139] | 82 | |
---|
| 83 | |
---|
[6695] | 84 | private: |
---|
| 85 | NetworkProtocol* networkProtocol; |
---|
| 86 | ConnectionMonitor* connectionMonitor; |
---|
| 87 | SynchronizeableList synchronizeables; |
---|
[7565] | 88 | PeerList peers; |
---|
[6695] | 89 | ServerSocket* serverSocket; |
---|
| 90 | int type; |
---|
| 91 | Header packetHeader; |
---|
| 92 | bool bActive; |
---|
| 93 | std::list<int> freeSocketSlots; |
---|
[6341] | 94 | |
---|
[6695] | 95 | int myHostId; |
---|
[7565] | 96 | |
---|
| 97 | int currentState; |
---|
[6695] | 98 | |
---|
| 99 | NetworkGameManager* networkGameManager; |
---|
[7602] | 100 | |
---|
| 101 | std::map<int,int> oldSynchronizeables; |
---|
[5587] | 102 | }; |
---|
[5566] | 103 | #endif /* _NETWORK_STREAM */ |
---|