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