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(int nodeType); |
---|
39 | virtual ~NetworkStream(); |
---|
40 | |
---|
41 | void init(); |
---|
42 | |
---|
43 | void connectToMasterServer(std::string host, int port); |
---|
44 | void connectToProxyServer(int proxyId, std::string host, int port); |
---|
45 | void createServer(int clientPort, int proxyPort); |
---|
46 | |
---|
47 | void createNetworkGameManager(); |
---|
48 | void startHandshake(int userId = NET_ID_MASTER_SERVER); |
---|
49 | |
---|
50 | /* synchronizeable interface */ |
---|
51 | void connectSynchronizeable(Synchronizeable& sync); |
---|
52 | void disconnectSynchronizeable(Synchronizeable& sync); |
---|
53 | |
---|
54 | inline int getMaxConnections(){ return SharedNetworkData::getInstance()->getMaxPlayer(); } |
---|
55 | |
---|
56 | /* functions for the peerInfo information retreival */ |
---|
57 | /** @returns true if this userId is activated at this client false if not*/ |
---|
58 | inline bool isUserIdActive( int userID ) { return (peers.find(userID) != peers.end()); } |
---|
59 | /** @returns true if this userId is a local client */ |
---|
60 | inline bool isUserLocal( int userID) { return this->isUserIdActive(userID); } |
---|
61 | /** @returns true if this user is a master server */ |
---|
62 | inline bool isUserMasterServer( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isMasterServer(); } |
---|
63 | /** @returns true if this user is a proxy server */ |
---|
64 | inline bool isUserProxyServerActive( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isProxyServerActive(); } |
---|
65 | /** @returns true if this user is a client */ |
---|
66 | inline bool isUserClient( int userID ){ if ( !isUserIdActive(userID) ) return false; return peers[userID].isClient(); } |
---|
67 | |
---|
68 | /* peering interface */ |
---|
69 | inline PeerInfo & getPeerInfo( int userId ) { return peers[userId]; } |
---|
70 | inline PeerInfo* getPeerInfo() { return this->pInfo; } |
---|
71 | inline PeerList getPeerInfoList() { return this->peers; } |
---|
72 | |
---|
73 | /* data processing*/ |
---|
74 | virtual void processData(); |
---|
75 | |
---|
76 | /* debugging */ |
---|
77 | void debug(); |
---|
78 | |
---|
79 | |
---|
80 | private: |
---|
81 | |
---|
82 | inline SynchronizeableList::const_iterator getSyncBegin(){ return synchronizeables.begin(); } |
---|
83 | inline SynchronizeableList::const_iterator getSyncEnd(){ return synchronizeables.end(); } |
---|
84 | void cleanUpOldSyncList(); |
---|
85 | int getSyncCount(); |
---|
86 | |
---|
87 | void updateConnectionList(); |
---|
88 | |
---|
89 | /* handle processes */ |
---|
90 | void handleHandshakes(); |
---|
91 | void handleUpstream( int tick ); |
---|
92 | void handleDownstream(int tick ); |
---|
93 | |
---|
94 | /* handle events*/ |
---|
95 | void handleNewClient( int userId ); |
---|
96 | |
---|
97 | void handleConnect( int userId); |
---|
98 | void handleReconnect( int userId ); |
---|
99 | void handleDisconnect( int userId ); |
---|
100 | |
---|
101 | void writeToNewDict( byte * data, int length, bool upstream ); |
---|
102 | |
---|
103 | |
---|
104 | private: |
---|
105 | PeerList peers; //!< list of the network node informations |
---|
106 | |
---|
107 | PeerInfo* pInfo; //!< the info about the local peer node (not in the PeerList) |
---|
108 | |
---|
109 | std::list<int> freeSocketSlots; //!< list of free sockets (to ensure not to resycle sockets) |
---|
110 | int currentState; //!< current state id |
---|
111 | |
---|
112 | NetworkMonitor* networkMonitor; //!< the network monitor |
---|
113 | NetworkGameManager* networkGameManager; //!< reference to the network game manager |
---|
114 | ServerSocket* clientSocket; //!< the listening socket of the server |
---|
115 | ServerSocket* proxySocket; //!< socket for proxy connections |
---|
116 | |
---|
117 | std::map<int,int> oldSynchronizeables; //!< used to save recently deleted sync ids to not recreate them |
---|
118 | SynchronizeableList synchronizeables; //!< list of the synchronizeables |
---|
119 | |
---|
120 | byte buf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
121 | byte compBuf[UDP_PACKET_SIZE]; //!< used by handleUp/Downstream |
---|
122 | int remainingBytesToWriteToDict; //!< if > 0 NetworkStream will write packets to DATA/dicts/newdict |
---|
123 | |
---|
124 | int dictServer; //!< the zip dict for the server |
---|
125 | int dictClient; //!< the zip dict for the client |
---|
126 | |
---|
127 | bool bRedirect; //!< true if the master server sent a redirect command |
---|
128 | }; |
---|
129 | #endif /* _NETWORK_STREAM */ |
---|