1 | /*! |
---|
2 | * @file network_stream.h |
---|
3 | * implementation of a network pipe. This node handles the handshake of two network nodes and exchanges informationas |
---|
4 | * vial to the connection setup. |
---|
5 | * |
---|
6 | * the local node only writes in the localState variable. the remoteState variable will be written from the remote network node |
---|
7 | * so don't write into it! |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef _HANDSHAKE |
---|
11 | #define _HANDSHAKE |
---|
12 | |
---|
13 | #include "base_object.h" |
---|
14 | #include "ip.h" |
---|
15 | #include "synchronizeable.h" |
---|
16 | #include "shared_network_data.h" |
---|
17 | |
---|
18 | //!< a struct to save the handshakes to |
---|
19 | struct HandshakeState |
---|
20 | { |
---|
21 | int orxId; //!< orxonox id |
---|
22 | int version; //!< network protocol version |
---|
23 | |
---|
24 | int networkManagerId; //!< unique id of the network manager |
---|
25 | int messageManagerId; //!< unique id of the message manager |
---|
26 | int hostId; //!< host id |
---|
27 | int nodeType; //!< type of the network node |
---|
28 | |
---|
29 | int completed; //!< true if completed |
---|
30 | int canDel; //!< true if marked for deletion |
---|
31 | |
---|
32 | int error; //!< error number |
---|
33 | |
---|
34 | std::string errorString; //!< error string |
---|
35 | |
---|
36 | //additional data |
---|
37 | std::string preferedNickName; //!< prefered nick name |
---|
38 | }; |
---|
39 | |
---|
40 | |
---|
41 | //!< the handshake itself with some interface functions |
---|
42 | class Handshake : public Synchronizeable |
---|
43 | { |
---|
44 | ObjectListDeclaration(Handshake); |
---|
45 | public: |
---|
46 | Handshake( int nodeType, int clientId = 0, int networkGameManagerId = 0, int messageManagerId = 0 ); |
---|
47 | |
---|
48 | |
---|
49 | /* functions indicating states of the handshake */ |
---|
50 | /** @returns true if the handshake is completed */ |
---|
51 | inline bool completed() const { return localState.completed != 0 && remoteState.completed != 0; } |
---|
52 | /** @returns true if no error has occured until now */ |
---|
53 | inline bool ok() const { return localState.error == 0 && remoteState.error == 0; } |
---|
54 | /** stops the handshake and reject the other side with @param reason: string describing the reason */ |
---|
55 | inline void doReject( std::string reason ){ localState.error = 1; localState.errorString = "the server rejected your connection ["+ reason +"]"; } |
---|
56 | /** @returns true if the handshake is finished and the instances can be deleted */ |
---|
57 | inline bool canDel() const { return localState.canDel == 1 && remoteState.canDel == 1; } |
---|
58 | /** @returns true if the local state can be removed*/ |
---|
59 | inline bool allowDel() const { return localState.canDel == 1; } |
---|
60 | /** marks the handshake to be deleted */ |
---|
61 | inline void del() { localState.canDel = 1; } |
---|
62 | |
---|
63 | |
---|
64 | /* the actual informations exchanged in the handshake */ |
---|
65 | /** @returns the host id of the remote host */ |
---|
66 | inline int getHostId() const { return remoteState.hostId; } |
---|
67 | /** @returns the unique id of the network game manager*/ |
---|
68 | inline int getNetworkGameManagerId() const { return remoteState.networkManagerId; } |
---|
69 | /** @returns the unique id of the message manager */ |
---|
70 | inline int getMessageManagerId() const { return remoteState.messageManagerId; } |
---|
71 | /** @returns the node type of the remote host */ |
---|
72 | inline int getRemoteNodeType() const { return this->remoteState.nodeType; } |
---|
73 | |
---|
74 | /** sets @param nick the prefereded nick name */ |
---|
75 | inline void setPreferedNickName( const std::string & nick ){ localState.preferedNickName = nick; } |
---|
76 | /** @returns the prefered nick name */ |
---|
77 | inline const std::string& getPreferedNickName() const { return remoteState.preferedNickName; } |
---|
78 | |
---|
79 | /** @returns if true the local client should reconnect to a proxy server from the proxy server list */ |
---|
80 | inline bool redirect() const { return this->redirectProxy; } |
---|
81 | /** @param flag: indicating if the client should be redirected */ |
---|
82 | inline void setRedirect(bool flag) { if( SharedNetworkData::getInstance()->isClient()) return; this->redirectProxy = (int)flag; } |
---|
83 | |
---|
84 | /** @param address: the address of the proxy server 1 if any */ |
---|
85 | inline void setProxy1Address(IP address) { if( SharedNetworkData::getInstance()->isClient()) return; this->proxy1 = address; } |
---|
86 | /** @returns the address of the proxy server 1 if any */ |
---|
87 | inline IP getProxy1Address() const { return this->proxy1; } |
---|
88 | /** @param address: the address of the proxy server 2 if any */ |
---|
89 | inline void setProxy2Address(IP address) { if( SharedNetworkData::getInstance()->isClient()) return; this->proxy2 = address; } |
---|
90 | /** @returns the address of the proxy server 2 if any */ |
---|
91 | inline IP getProxy2Address() const { return this->proxy2; } |
---|
92 | |
---|
93 | |
---|
94 | /* variable handler function */ |
---|
95 | virtual void varChangeHandler( std::list<int> & id ); |
---|
96 | |
---|
97 | |
---|
98 | private: |
---|
99 | HandshakeState localState; //!< the local handshake state |
---|
100 | HandshakeState remoteState; //!< the remote handshake state |
---|
101 | |
---|
102 | int orxId_handler; //!< orxonox id handler |
---|
103 | int version_handler; //!< orxonox version id handler |
---|
104 | int netManId_handler; //!< network manager handler |
---|
105 | int msgManId_handler; //!< message manager handler |
---|
106 | int hostId_handler; //!< host id handler |
---|
107 | int nodeTypeHandler; //!< node type handler |
---|
108 | int proxy1Handler; //!< handler for the proxy |
---|
109 | int proxy2Handler; //!< handler for the proxy |
---|
110 | |
---|
111 | int completed_handler; //!< handshake completion handler |
---|
112 | int error_handler; //!< handshake error handler |
---|
113 | int errorString_handler; //!< handshake error string handler |
---|
114 | int candel_id; //!< handshake deletion handler |
---|
115 | int nodeType; //!, the type of the network node |
---|
116 | |
---|
117 | int redirectProxy; //!< true if the client should reconnect to a proxy server |
---|
118 | IP proxy1; //!< ip address of the first proxy (0.0.0.0 of not available) |
---|
119 | IP proxy2; //!< ip address of the second proxy (0.0.0.0 of not available) |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | #endif |
---|