OpenClonk
C4Network2Client.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #ifndef INC_C4Network2Client
17 #define INC_C4Network2Client
18 
19 #include "network/C4Client.h"
20 #include "network/C4NetIO.h"
22 #include "network/C4Network2IO.h"
23 #include "network/C4PacketBase.h"
24 
26 
27 // maximum address count that is saved for one client
28 const int32_t C4ClientMaxAddr = 40;
29 
30 // retry count and interval for connecting a client
31 const int32_t C4NetClientConnectAttempts = 3,
33 
34 // network client status (host only)
36 {
37  NCS_Joining, // waiting for join data
38  NCS_Chasing, // client is behind (status not acknowledged, isn't waited for)
39  NCS_NotReady, // client is behind (status not acknowledged)
40  NCS_Ready, // client acknowledged network status
41  NCS_Remove // client is to be removed
42 };
43 
45 {
46  friend class C4Network2ClientList;
47 public:
50 
51 protected:
52  // client data
54 
55  // addresses
58  int32_t iAddrCnt;
60 
61  // interface ids
62  std::set<int> InterfaceIDs;
63 
64  // status
66 
67  // frame of last activity
68  int32_t iLastActivity;
69 
70  // connections
73  std::unique_ptr<C4NetIOTCP::Socket> TcpSimOpenSocket;
74 
75  // part of client list
78 
79  // statistics
81 
82 public:
83 
84  C4Client *getClient() const { return pClient; }
85  const C4ClientCore &getCore() const { return getClient()->getCore(); }
86  int32_t getID() const { return getCore().getID(); }
87  bool isLocal() const { return pClient->isLocal(); }
88  bool isHost() const { return getID() == C4ClientIDHost; }
89  const char *getName() const { return getCore().getName(); }
90  bool isActivated() const { return getCore().isActivated(); }
91  bool isObserver() const { return getCore().isObserver(); }
92 
93  int32_t getAddrCnt() const { return iAddrCnt; }
94  const C4Network2Address &getAddr(int32_t i) const { return Addr[i]; }
95 
96  const std::set<int> &getInterfaceIDs() const { return InterfaceIDs; }
97 
99  bool hasJoinData() const { return getStatus() != NCS_Joining; }
100  bool isChasing() const { return getStatus() == NCS_Chasing; }
101  bool isReady() const { return getStatus() == NCS_Ready; }
102  bool isWaitedFor() const { return getStatus() == NCS_NotReady || getStatus() == NCS_Ready; }
103  bool isRemoved() const { return getStatus() == NCS_Remove; }
104 
105  bool isConnected() const { return !! pMsgConn; }
106  time_t getNextConnAttempt() const { return iNextConnAttempt; }
107  int32_t getLastActivity() const { return iLastActivity; }
108  class C4TableGraph *getStatPing() const { return pstatPing; }
109 
110  C4Network2Client *getNext() const { return pNext; }
111 
112  void SetStatus(C4Network2ClientStatus enStatus) { eStatus = enStatus; }
113  void SetLastActivity(int32_t iTick) { iLastActivity = iTick; }
114 
117  bool hasConn(C4Network2IOConnection *pConn);
118  void SetMsgConn(C4Network2IOConnection *pConn);
119  void SetDataConn(C4Network2IOConnection *pConn);
120  void RemoveConn(C4Network2IOConnection *pConn);
121  void CloseConns(const char *szMsg);
122 
123  bool SendMsg(C4NetIOPacket rPkt) const;
124  bool SendData(C4NetIOPacket rPkt) const;
125 
126  bool DoConnectAttempt(class C4Network2IO *pIO);
127  bool DoTCPSimultaneousOpen(class C4Network2IO *pIO, const C4Network2Address &addr);
128 
129  // addresses
130  bool hasAddr(const C4Network2Address &addr) const;
131  void ClearAddr();
132  void AddAddrFromPuncher(const C4NetIO::addr_t &addr);
133  bool AddAddr(const C4Network2Address &addr, bool fAnnounce);
134  void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP);
135 
137 
138  // runtime statistics
139  void CreateGraphs();
140  void ClearGraphs();
141 };
142 
144 {
145 public:
148 
149 protected:
154  bool fHost;
155 
156 public:
157  C4Network2Client *GetClientByID(int32_t iID) const;
158  C4Network2Client *GetNextClientAfterID(int32_t iSmallerClientID) const;
159  C4Network2Client *GetClient(const char *szName) const;
160  C4Network2Client *GetClient(const C4ClientCore &CCore, int32_t iMaxDiffLevel = C4ClientCoreDL_IDMatch);
161  C4Network2Client *GetClient(C4Network2IOConnection *pConn) const;
162  C4Network2Client *GetLocal() const { return pLocal; }
163  C4Network2Client *GetHost();
164  C4Network2Client *GetNextClient(C4Network2Client *pClient);
165  unsigned int Count();
166 
167  void Init(C4ClientList *pClientList, bool fHost);
168  C4Network2Client *RegClient(C4Client *pClient);
169  void DeleteClient(C4Network2Client *pClient);
170  void Clear();
171 
172  // messages
173  bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt);
174  bool BroadcastMsgToClients(const C4NetIOPacket &rPkt);
175  bool SendMsgToHost(C4NetIOPacket rPkt);
176  bool SendMsgToClient(int32_t iClient, C4NetIOPacket &&rPkt);
177 
178  // packet handling
179  void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn);
180 
181  // addresses
183 
184  // connecting
185  void DoConnectAttempts();
186 
187  // ready-ness
188  void ResetReady();
189  bool AllClientsReady() const;
190 
191  // activity
192  void UpdateClientActivity();
193 };
194 
195 // * Packets *
196 
198 {
199 public:
200  C4PacketAddr() = default;
201  C4PacketAddr(int32_t iClientID, const C4Network2Address &addr)
202  : iClientID(iClientID), addr(addr)
203  { }
204 
205 protected:
206  int32_t iClientID;
208 
209 public:
210  int32_t getClientID() const { return iClientID; }
211  const C4Network2Address &getAddr() const { return addr; }
212 
213  void CompileFunc(StdCompiler *pComp) override;
214 };
215 
217 {
218 public:
219  C4PacketTCPSimOpen() = default;
220  C4PacketTCPSimOpen(int32_t ClientID, const C4Network2Address &addr)
221  : ClientID(ClientID), addr(addr)
222  { }
223 
224 protected:
225  int32_t ClientID;
227 
228 public:
229  int32_t getClientID() const { return ClientID; }
230  const C4Network2Address &getAddr() const { return addr; }
231 
232  void CompileFunc(StdCompiler *pComp) override;
233 };
234 
235 #endif
C4Network2Address addr
C4Network2Client * GetLocal() const
class C4Network2ClientList * pParent
void RemoveConn(C4Network2IOConnection *pConn)
C4Network2Address addr
class C4TableGraph * getStatPing() const
C4Network2Client * getNext() const
C4Client * getClient() const
bool SendMsg(C4NetIOPacket rPkt) const
const char * getName() const
Definition: C4Client.h:69
C4Network2Client(C4Client *pClient)
bool isReady() const
void SetLastActivity(int32_t iTick)
bool hasAddr(const C4Network2Address &addr) const
void AddAddrFromPuncher(const C4NetIO::addr_t &addr)
const C4ClientCore & getCore() const
Definition: C4Client.h:104
C4NetIO::addr_t IPv6AddrFromPuncher
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
bool isConnected() const
bool isLocal() const
C4Network2Client * pFirst
const C4Network2Address & getAddr() const
bool isObserver() const
Definition: C4Client.h:60
C4Network2IOConnection * getDataConn() const
bool isActivated() const
Definition: C4Client.h:59
C4Network2IOConnection * getMsgConn() const
bool isObserver() const
bool isRemoved() const
class C4TableGraph * pstatPing
int32_t getClientID() const
time_t getNextConnAttempt() const
C4PacketTCPSimOpen(int32_t ClientID, const C4Network2Address &addr)
std::set< int > InterfaceIDs
bool hasJoinData() const
C4Network2Client * pLocal
void SetMsgConn(C4Network2IOConnection *pConn)
C4Network2Address Addr[C4ClientMaxAddr]
void SetDataConn(C4Network2IOConnection *pConn)
const int32_t C4NetClientConnectInterval
const C4ClientCore & getCore() const
bool isLocal() const
Definition: C4Client.h:116
const int32_t C4ClientMaxAddr
int32_t getLastActivity() const
C4Network2IOConnection * pDataConn
const C4Network2Address & getAddr() const
const int32_t C4NetClientConnectAttempts
const C4Network2Address & getAddr(int32_t i) const
bool hasConn(C4Network2IOConnection *pConn)
C4PacketAddr(int32_t iClientID, const C4Network2Address &addr)
void SendAddresses(C4Network2IOConnection *pConn)
void SetStatus(C4Network2ClientStatus enStatus)
bool DoTCPSimultaneousOpen(class C4Network2IO *pIO, const C4Network2Address &addr)
bool isHost() const
C4Network2Client * pNext
C4Network2ClientStatus getStatus() const
C4Network2ClientStatus eStatus
friend class C4Network2ClientList
bool DoConnectAttempt(class C4Network2IO *pIO)
C4Network2ClientStatus
C4ClientList * pClientList
const char * getName() const
int32_t AddrAttempts[C4ClientMaxAddr]
int32_t getID() const
Definition: C4Client.h:57
int32_t getID() const
C4Network2IOConnection * pMsgConn
bool isWaitedFor() const
void CloseConns(const char *szMsg)
const std::set< int > & getInterfaceIDs() const
void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP)
bool SendData(C4NetIOPacket rPkt) const
bool AddAddr(const C4Network2Address &addr, bool fAnnounce)
void CompileFunc(C4Real &rValue, StdCompiler *pComp)
Definition: C4Real.cpp:9033
int32_t getClientID() const
bool isActivated() const
bool isChasing() const
std::unique_ptr< C4NetIOTCP::Socket > TcpSimOpenSocket
const int32_t C4ClientIDHost
Definition: C4Client.h:25
int32_t getAddrCnt() const