OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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;
59 
60  // interface ids
61  std::set<int> InterfaceIDs;
62 
63  // status
65 
66  // frame of last activity
67  int32_t iLastActivity;
68 
69  // connections
72 
73  // part of client list
76 
77  // statistics
79 
80 public:
81 
82  C4Client *getClient() const { return pClient; }
83  const C4ClientCore &getCore() const { return getClient()->getCore(); }
84  int32_t getID() const { return getCore().getID(); }
85  bool isLocal() const { return pClient->isLocal(); }
86  bool isHost() const { return getID() == C4ClientIDHost; }
87  const char *getName() const { return getCore().getName(); }
88  bool isActivated() const { return getCore().isActivated(); }
89  bool isObserver() const { return getCore().isObserver(); }
90 
91  int32_t getAddrCnt() const { return iAddrCnt; }
92  const C4Network2Address &getAddr(int32_t i) const { return Addr[i]; }
93 
94  const std::set<int> &getInterfaceIDs() const { return InterfaceIDs; }
95 
97  bool hasJoinData() const { return getStatus() != NCS_Joining; }
98  bool isChasing() const { return getStatus() == NCS_Chasing; }
99  bool isReady() const { return getStatus() == NCS_Ready; }
100  bool isWaitedFor() const { return getStatus() == NCS_NotReady || getStatus() == NCS_Ready; }
101  bool isRemoved() const { return getStatus() == NCS_Remove; }
102 
103  bool isConnected() const { return !! pMsgConn; }
104  time_t getNextConnAttempt() const { return iNextConnAttempt; }
105  int32_t getLastActivity() const { return iLastActivity; }
106  class C4TableGraph *getStatPing() const { return pstatPing; }
107 
108  C4Network2Client *getNext() const { return pNext; }
109 
110  void SetStatus(C4Network2ClientStatus enStatus) { eStatus = enStatus; }
111  void SetLastActivity(int32_t iTick) { iLastActivity = iTick; }
112 
115  bool hasConn(C4Network2IOConnection *pConn);
116  void SetMsgConn(C4Network2IOConnection *pConn);
117  void SetDataConn(C4Network2IOConnection *pConn);
118  void RemoveConn(C4Network2IOConnection *pConn);
119  void CloseConns(const char *szMsg);
120 
121  bool SendMsg(C4NetIOPacket rPkt) const;
122  bool SendData(C4NetIOPacket rPkt) const;
123 
124  bool DoConnectAttempt(class C4Network2IO *pIO);
125 
126  // addresses
127  bool hasAddr(const C4Network2Address &addr) const;
128  void ClearAddr();
129  bool AddAddr(const C4Network2Address &addr, bool fAnnounce);
130  void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP);
131 
133 
134  // runtime statistics
135  void CreateGraphs();
136  void ClearGraphs();
137 };
138 
140 {
141 public:
144 
145 protected:
150  bool fHost;
151 
152 public:
153  C4Network2Client *GetClientByID(int32_t iID) const;
154  C4Network2Client *GetNextClientAfterID(int32_t iSmallerClientID) const;
155  C4Network2Client *GetClient(const char *szName) const;
156  C4Network2Client *GetClient(const C4ClientCore &CCore, int32_t iMaxDiffLevel = C4ClientCoreDL_IDMatch);
158  C4Network2Client *GetLocal() const { return pLocal; }
161  unsigned int Count();
162 
163  void Init(C4ClientList *pClientList, bool fHost);
165  void DeleteClient(C4Network2Client *pClient);
166  void Clear();
167 
168  // messages
169  bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt);
170  bool BroadcastMsgToClients(const C4NetIOPacket &rPkt);
171  bool SendMsgToHost(C4NetIOPacket rPkt);
172  bool SendMsgToClient(int32_t iClient, C4NetIOPacket &&rPkt);
173 
174  // packet handling
175  void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn);
176 
177  // addresses
179 
180  // connecting
181  void DoConnectAttempts();
182 
183  // ready-ness
184  void ResetReady();
185  bool AllClientsReady() const;
186 
187  // activity
188  void UpdateClientActivity();
189 };
190 
191 // * Packets *
192 
194 {
195 public:
196  C4PacketAddr() = default;
198  : iClientID(iClientID), addr(addr)
199  { }
200 
201 protected:
202  int32_t iClientID;
204 
205 public:
206  int32_t getClientID() const { return iClientID; }
207  const C4Network2Address &getAddr() const { return addr; }
208 
209  void CompileFunc(StdCompiler *pComp) override;
210 };
211 
212 #endif
const C4Network2Address & getAddr(int32_t i) const
C4Network2Address addr
bool SendMsgToHost(C4NetIOPacket rPkt)
bool isObserver() const
class C4Network2ClientList * pParent
void RemoveConn(C4Network2IOConnection *pConn)
C4Network2Client * GetClientByID(int32_t iID) const
void Init(C4ClientList *pClientList, bool fHost)
C4Network2Client * GetNextClient(C4Network2Client *pClient)
void DeleteClient(C4Network2Client *pClient)
bool SendData(C4NetIOPacket rPkt) const
C4Network2IOConnection * getMsgConn() const
C4Network2Client(C4Client *pClient)
void SetLastActivity(int32_t iTick)
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
void CompileFunc(StdCompiler *pComp) override
C4Network2Client * GetClient(const char *szName) const
C4Network2Client * pFirst
bool hasAddr(const C4Network2Address &addr) const
int32_t getID() const
bool isChasing() const
C4Network2IOConnection * getDataConn() const
bool isConnected() const
bool isObserver() const
Definition: C4Client.h:60
class C4TableGraph * pstatPing
bool isActivated() const
Definition: C4Client.h:59
std::set< int > InterfaceIDs
bool AllClientsReady() const
C4Network2Client * pLocal
const C4ClientCore & getCore() const
void SetMsgConn(C4Network2IOConnection *pConn)
int32_t getLastActivity() const
C4Network2Address Addr[C4ClientMaxAddr]
const C4ClientCore & getCore() const
Definition: C4Client.h:104
void SetDataConn(C4Network2IOConnection *pConn)
const int32_t C4NetClientConnectInterval
bool isWaitedFor() const
bool isRemoved() const
const int32_t C4ClientMaxAddr
const std::set< int > & getInterfaceIDs() const
bool isActivated() const
const char * getName() const
C4Network2IOConnection * pDataConn
int32_t getID() const
Definition: C4Client.h:57
const int32_t C4NetClientConnectAttempts
bool hasConn(C4Network2IOConnection *pConn)
C4PacketAddr(int32_t iClientID, const C4Network2Address &addr)
C4PacketAddr()=default
bool isReady() const
bool SendMsgToClient(int32_t iClient, C4NetIOPacket &&rPkt)
C4Network2Client * GetNextClientAfterID(int32_t iSmallerClientID) const
void SendAddresses(C4Network2IOConnection *pConn)
void SetStatus(C4Network2ClientStatus enStatus)
C4Network2Client * RegClient(C4Client *pClient)
class C4TableGraph * getStatPing() const
C4Network2ClientList(C4Network2IO *pIO)
const C4Network2Address & getAddr() const
C4Network2Client * getNext() const
bool isLocal() const
Definition: C4Client.h:116
C4Network2Client * pNext
C4Network2ClientStatus getStatus() const
C4Network2ClientStatus eStatus
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
bool isLocal() const
bool DoConnectAttempt(class C4Network2IO *pIO)
C4Network2ClientStatus
C4ClientList * pClientList
int32_t AddrAttempts[C4ClientMaxAddr]
C4Network2Client * GetLocal() const
bool hasJoinData() const
bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt)
int32_t getClientID() const
C4Network2IOConnection * pMsgConn
const char * getName() const
Definition: C4Client.h:69
C4Network2Client * GetHost()
bool SendMsg(C4NetIOPacket rPkt) const
int32_t getAddrCnt() const
void CloseConns(const char *szMsg)
C4Client * getClient() const
void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP)
bool AddAddr(const C4Network2Address &addr, bool fAnnounce)
const int32_t C4ClientIDHost
Definition: C4Client.h:25
bool isHost() const
time_t getNextConnAttempt() const
void SendAddresses(C4Network2IOConnection *pConn)