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 "monitor/connection_monitor.h" |
---|
17 | #include "udp_server_socket.h" |
---|
18 | #include "peer_info.h" |
---|
19 | |
---|
20 | #include "shared_network_data.h" |
---|
21 | |
---|
22 | class Synchronizeable; |
---|
23 | class NetworkSocket; |
---|
24 | class ServerSocket; |
---|
25 | class NetworkGameManager; |
---|
26 | class NetworkMonitor; |
---|
27 | |
---|
28 | |
---|
29 | typedef std::list<Synchronizeable*> SynchronizeableList; |
---|
30 | typedef std::map<int,PeerInfo> PeerList; |
---|
31 | |
---|
32 | |
---|
33 | class NetworkStream : public DataStream |
---|
34 | { |
---|
35 | |
---|
36 | public: |
---|
37 | NetworkStream(); |
---|
38 | NetworkStream( std::string host, int port); |
---|
39 | NetworkStream( int port ); |
---|
40 | |
---|
41 | virtual ~NetworkStream(); |
---|
42 | void init(); |
---|
43 | |
---|
44 | void createNetworkGameManager(); |
---|
45 | void startHandshake(); |
---|
46 | |
---|
47 | /* synchronizeable interface */ |
---|
48 | void connectSynchronizeable(Synchronizeable& sync); |
---|
49 | void disconnectSynchronizeable(Synchronizeable& sync); |
---|
50 | |
---|
51 | /* functions for the localhost settings */ |
---|
52 | inline bool isMasterServer() const { return (this->pInfo->nodeType == NET_MASTER_SERVER)? true:false; } |
---|
53 | inline bool isProxyServer() const { return (this->pInfo->nodeType == NET_PROXY_SERVER_ACTIVE)? true:false; } |
---|
54 | inline bool isClient() const { return (this->pInfo->nodeType == NET_CLIENT)? true:false; } |
---|
55 | // inline bool isActive() const { return this->bActive; } |
---|
56 | inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); } |
---|
57 | |
---|
58 | /* functions for the peerInfo information retreival */ |
---|
59 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
60 | inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } |
---|
61 | inline bool isUserProxyServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServer(); } |
---|
62 | inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } |
---|
63 | |
---|
64 | /* peering interface */ |
---|
65 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
66 | inline PeerInfo* getPeerInfo() { return this->pInfo; } |
---|
67 | inline PeerList getPeerInfoList() { return this->peers; } |
---|
68 | |
---|
69 | /* data processing*/ |
---|
70 | virtual void processData(); |
---|
71 | |
---|
72 | /* debugging */ |
---|
73 | void debug(); |
---|
74 | |
---|
75 | |
---|
76 | private: |
---|
77 | |
---|
78 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
79 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
80 | void cleanUpOldSyncList(); |
---|
81 | int getSyncCount(); |
---|
82 | |
---|
83 | void updateConnectionList(); |
---|
84 | /* handle processes */ |
---|
85 | void handleHandshakes(); |
---|
86 | void handleUpstream( int tick ); |
---|
87 | void handleDownstream(int tick ); |
---|
88 | |
---|
89 | /* handle events*/ |
---|
90 | void handleNewClient( int userId ); |
---|
91 | void handleReconnect( int userId ); |
---|
92 | |
---|
93 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
94 | |
---|
95 | |
---|
96 | private: |
---|
97 | PeerList peers; //!< list of the network node informations |
---|
98 | |
---|
99 | PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) |
---|
100 | |
---|
101 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
---|
102 | int currentState; //!< current state id |
---|
103 | |
---|
104 | NetworkMonitor* networkMonitor; //!< the network monitor |
---|
105 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
---|
106 | ServerSocket* serverSocket; //!< the listening socket of the server |
---|
107 | |
---|
108 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
109 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
---|
110 | |
---|
111 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
112 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
113 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
114 | |
---|
115 | int dictServer; //!< the zip dict for the server |
---|
116 | int dictClient; //!< the zip dict for the client |
---|
117 | }; |
---|
118 | #endif /* _NETWORK_STREAM */ |
---|