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