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