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);
162  C4Network2Client *GetLocal() const { return pLocal; }
165  unsigned int Count();
166 
167  void Init(C4ClientList *pClientList, bool fHost);
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;
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;
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
const int32_t C4ClientIDHost
Definition: C4Client.h:25
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
C4Network2ClientStatus
@ NCS_Ready
@ NCS_Remove
@ NCS_Chasing
@ NCS_Joining
@ NCS_NotReady
const int32_t C4NetClientConnectAttempts
const int32_t C4NetClientConnectInterval
const int32_t C4ClientMaxAddr
bool isObserver() const
Definition: C4Client.h:60
const char * getName() const
Definition: C4Client.h:69
int32_t getID() const
Definition: C4Client.h:57
bool isActivated() const
Definition: C4Client.h:59
bool isLocal() const
Definition: C4Client.h:116
const C4ClientCore & getCore() const
Definition: C4Client.h:104
C4Network2Address Addr[C4ClientMaxAddr]
void CloseConns(const char *szMsg)
bool DoConnectAttempt(class C4Network2IO *pIO)
void AddAddrFromPuncher(const C4NetIO::addr_t &addr)
C4Network2IOConnection * pMsgConn
class C4TableGraph * getStatPing() const
bool hasAddr(const C4Network2Address &addr) const
bool DoTCPSimultaneousOpen(class C4Network2IO *pIO, const C4Network2Address &addr)
const char * getName() const
void SetStatus(C4Network2ClientStatus enStatus)
int32_t getID() const
C4Network2Client * pNext
const std::set< int > & getInterfaceIDs() const
bool isActivated() const
C4Network2Client(C4Client *pClient)
bool isReady() const
bool isObserver() const
int32_t AddrAttempts[C4ClientMaxAddr]
bool isConnected() const
void RemoveConn(C4Network2IOConnection *pConn)
class C4TableGraph * pstatPing
C4Network2IOConnection * getDataConn() const
bool isLocal() const
bool SendMsg(C4NetIOPacket rPkt) const
bool isWaitedFor() const
time_t getNextConnAttempt() const
C4Network2IOConnection * getMsgConn() const
void SetLastActivity(int32_t iTick)
C4Network2IOConnection * pDataConn
C4Network2ClientStatus eStatus
std::set< int > InterfaceIDs
std::unique_ptr< C4NetIOTCP::Socket > TcpSimOpenSocket
bool hasJoinData() const
class C4Network2ClientList * pParent
void SetDataConn(C4Network2IOConnection *pConn)
bool isHost() const
bool SendData(C4NetIOPacket rPkt) const
C4Network2ClientStatus getStatus() const
void SendAddresses(C4Network2IOConnection *pConn)
bool AddAddr(const C4Network2Address &addr, bool fAnnounce)
const C4Network2Address & getAddr(int32_t i) const
bool isChasing() const
C4Network2Client * getNext() const
C4NetIO::addr_t IPv6AddrFromPuncher
C4Client * getClient() const
bool hasConn(C4Network2IOConnection *pConn)
int32_t getLastActivity() const
int32_t getAddrCnt() const
bool isRemoved() const
void SetMsgConn(C4Network2IOConnection *pConn)
void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP)
const C4ClientCore & getCore() const
C4ClientList * pClientList
C4Network2Client * GetClient(const char *szName) const
C4Network2Client * GetHost()
bool SendMsgToClient(int32_t iClient, C4NetIOPacket &&rPkt)
C4Network2Client * GetNextClientAfterID(int32_t iSmallerClientID) const
C4Network2Client * GetNextClient(C4Network2Client *pClient)
C4Network2Client * GetClientByID(int32_t iID) const
C4Network2Client * GetLocal() const
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
C4Network2Client * pLocal
void Init(C4ClientList *pClientList, bool fHost)
bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt)
C4Network2Client * pFirst
bool SendMsgToHost(C4NetIOPacket rPkt)
C4Network2ClientList(C4Network2IO *pIO)
C4Network2Client * RegClient(C4Client *pClient)
void DeleteClient(C4Network2Client *pClient)
void SendAddresses(C4Network2IOConnection *pConn)
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
C4PacketAddr()=default
C4Network2Address addr
C4PacketAddr(int32_t iClientID, const C4Network2Address &addr)
int32_t getClientID() const
void CompileFunc(StdCompiler *pComp) override
const C4Network2Address & getAddr() const
C4PacketTCPSimOpen()=default
void CompileFunc(StdCompiler *pComp) override
C4Network2Address addr
int32_t getClientID() const
C4PacketTCPSimOpen(int32_t ClientID, const C4Network2Address &addr)
const C4Network2Address & getAddr() const