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