OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Client.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) 2010-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 C4CLIENT_H
17 #define C4CLIENT_H
18 
19 #include "lib/C4InputValidation.h"
20 #include "network/C4NetIO.h"
21 #include "network/C4PacketBase.h"
22 
23 // special ids
24 const int32_t C4ClientIDUnknown = -1,
27 
28 // client core difference levels
29 const int32_t C4ClientCoreDL_None = 0, // identical
30  C4ClientCoreDL_IDMatch = 1, // status change
31  C4ClientCoreDL_IDChange = 2, // identification changed (host only!)
32  C4ClientCoreDL_Different = 3; // really different
33 
34 class C4ClientCore : public C4PacketBase
35 {
36 public:
37  C4ClientCore();
38  ~C4ClientCore() override;
39 
40 protected:
41 
42  // identification
43  int32_t iID{-1};
47 
48  // version info
49  int iVersion[4];
50 
51  // status
52  bool fActivated{false}, fObserver{false}, fLobbyReady{false};
53 
54 public:
55 
56  // status data
57  int32_t getID() const { return iID; }
58  bool isHost() const { return iID == C4ClientIDHost; }
59  bool isActivated() const { return fActivated; }
60  bool isObserver() const { return fObserver; }
61  bool isLobbyReady() const { return fLobbyReady; }
62  void SetID(int32_t inID) { iID = inID; }
63  void SetName(const char *sznName) { Name.CopyValidated(sznName); }
64  void SetActivated(bool fnActivated) { fActivated = fnActivated; fObserver = false; }
65  void SetObserver(bool fnObserver) { fActivated &= !(fObserver = fnObserver); }
66  void SetLobbyReady(bool fnLobbyReady){ fLobbyReady = fnLobbyReady; }
67 
68  // misc
69  const char *getName() const { return Name.getData(); }
70  const char *getCUID() const { return CUID.getData(); }
71  const char *getNick() const { return Nick.getData(); }
72  const char *getRevision() const { return Revision.getData(); }
73  bool isRegistered()const { return CUID.getLength()>0; }
74 
75  // initialization
76  void SetLocal(int32_t iID, bool fnActivated, bool fnObserver);
77  void SetUnknown(int32_t iID);
78 
79  int32_t getDiffLevel(const C4ClientCore &CCore2) const;
80 
81  void CompileFunc(StdCompiler *pComp) override;
82 };
83 
84 class C4Client
85 {
86  friend class C4ClientList;
87 public:
88  C4Client();
89  C4Client(const C4ClientCore &Core);
90  ~C4Client();
91 
92 private:
93  C4ClientCore Core;
94 
95  bool fLocal{false}; // Local, NoSync
96  class C4Network2Client *pNetClient{nullptr}; // Local, NoSync
97  time_t last_lobby_ready_change{0}; // Local, NoSync: Time when the lobby ready state was changed last through the SetLobbyReady call. 0 for never changed.
98 
99  bool fIsIgnored{false}; // Local, NoSync: chat messages from this client are suppressed
100 
101  C4Client *pNext;
102 
103 public:
104  const C4ClientCore &getCore() const { return Core; }
105  int32_t getID() const { return Core.getID(); }
106  bool isHost() const { return Core.isHost(); }
107  const char *getName() const { return Core.getName(); }
108  const char *getCUID() const { return Core.getCUID(); }
109  const char *getNick() const { return Core.getNick(); }
110  bool isActivated() const { return Core.isActivated(); }
111  bool isObserver() const { return Core.isObserver(); }
112  bool isLobbyReady()const { return Core.isLobbyReady(); }
113  bool isRegistered()const { return Core.isRegistered(); }
114  bool IsIgnored() const { return fIsIgnored; }
115 
116  bool isLocal() const { return fLocal; }
117  C4Network2Client *getNetClient() const { return pNetClient; }
118 
119  void SetCore(const C4ClientCore &NewCore) { Core = NewCore; }
120  void SetID(int32_t iID) { Core.SetID(iID); }
121 
122  void SetActivated(bool fnActivated);
123  void SetObserver() { Core.SetObserver(true); }
124  void SetLobbyReady(bool fnLobbyReady, time_t *time_since_last_change = nullptr);
125  void SetLocal();
126 
127  void ToggleIgnore() { fIsIgnored = !fIsIgnored; }
128 
129  void UnlinkNetClient() { pNetClient = nullptr; }
130 
131  void Remove();
132 
133  void CompileFunc(StdCompiler *pComp);
134 
135 };
136 
138 {
139 public:
140  C4ClientList();
141  ~C4ClientList();
142 
143  C4ClientList &operator = (const C4ClientList &List);
144 
145  void Clear();
146 
147 protected:
148  // client list
149  C4Client *pFirst{nullptr};
150 
151  // pointer to local client
152  C4Client *pLocal{nullptr};
153 
154  // network client list (may be nullptr if network is not active)
156 
157  void Add(C4Client *pClient);
158 
159 public:
160  C4Client *getClient(const C4Client *pAfter = nullptr) const { return pAfter ? pAfter->pNext : pFirst; }
161  C4Client *getLocal() const { return pLocal; }
162  C4Client *getClientByID(int32_t iID) const;
164  C4Client *getNextClientByID(int32_t iAfterID) const;
165  C4Client *getClientByName(const char *szName) const;
166 
167  int32_t getClientCnt() const;
168 
169  const C4ClientCore &getLocalCore() const { return getLocal()->getCore(); }
170  const char *getLocalName() const { return pLocal ? getLocalCore().getName() : "???"; }
171  int32_t getLocalID() const { return pLocal ? getLocalCore().getID() : C4ClientIDUnknown; }
173 
174  bool Init(int32_t iLocalClientID = C4ClientIDHost);
176  void ClearNetwork();
177 
178  bool Remove(C4Client *pClient, bool fTemporary = false);
179  C4Client *Add(const C4ClientCore &Core);
180  C4Client *AddLocal(int32_t iID, bool fActivated, bool fObserver);
181  void SetLocalID(int32_t iID);
182 
183  void CtrlRemove(const C4Client *pClient, const char *szReason);
184 
185  void RemoveRemote();
186 
187  void CompileFunc(StdCompiler *pComp);
188 };
189 
190 #endif // C4CLIENT_H
const char * getData() const
Definition: StdBuf.h:442
C4Client * getClientByName(const char *szName) const
Definition: C4Client.cpp:216
C4Client * getLocal() const
Definition: C4Client.h:161
void SetObserver(bool fnObserver)
Definition: C4Client.h:65
bool fObserver
Definition: C4Client.h:52
int32_t iID
Definition: C4Client.h:43
const int32_t C4ClientCoreDL_None
Definition: C4Client.h:29
void SetLocalID(int32_t iID)
Definition: C4Client.cpp:324
StdStrBuf GetAllClientNames() const
Definition: C4Client.cpp:232
bool isLobbyReady() const
Definition: C4Client.h:112
bool isRegistered() const
Definition: C4Client.h:113
C4Client * pLocal
Definition: C4Client.h:152
bool isActivated() const
Definition: C4Client.h:110
void SetLobbyReady(bool fnLobbyReady, time_t *time_since_last_change=nullptr)
Definition: C4Client.cpp:128
void SetID(int32_t inID)
Definition: C4Client.h:62
void SetUnknown(int32_t iID)
Definition: C4Client.cpp:65
void CompileFunc(StdCompiler *pComp)
Definition: C4Client.cpp:393
bool isHost() const
Definition: C4Client.h:106
void RemoveRemote()
Definition: C4Client.cpp:346
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
C4Client * getHost() const
Definition: C4Client.h:163
C4Client * AddLocal(int32_t iID, bool fActivated, bool fObserver)
Definition: C4Client.cpp:308
C4Network2Client * getNetClient() const
Definition: C4Client.h:117
ValidatedStdCopyStrBuf< C4InVal::VAL_NameAllowEmpty > Revision
Definition: C4Client.h:46
void CtrlRemove(const C4Client *pClient, const char *szReason)
Definition: C4Client.cpp:333
const int32_t C4ClientCoreDL_IDChange
Definition: C4Client.h:31
const char * getCUID() const
Definition: C4Client.h:70
bool isHost() const
Definition: C4Client.h:58
int32_t getID() const
Definition: C4Client.h:105
const C4ClientCore & getLocalCore() const
Definition: C4Client.h:169
void CompileFunc(StdCompiler *pComp) override
Definition: C4Client.cpp:92
bool isObserver() const
Definition: C4Client.h:60
const int32_t C4ClientIDStart
Definition: C4Client.h:26
void ClearNetwork()
Definition: C4Client.cpp:262
~C4ClientCore() override
bool isObserver() const
Definition: C4Client.h:111
bool isActivated() const
Definition: C4Client.h:59
void SetID(int32_t iID)
Definition: C4Client.h:120
void SetName(const char *sznName)
Definition: C4Client.h:63
bool Init(int32_t iLocalClientID=C4ClientIDHost)
Definition: C4Client.cpp:243
void UnlinkNetClient()
Definition: C4Client.h:129
int32_t getLocalID() const
Definition: C4Client.h:171
void SetLobbyReady(bool fnLobbyReady)
Definition: C4Client.h:66
const char * getLocalName() const
Definition: C4Client.h:170
const C4ClientCore & getCore() const
Definition: C4Client.h:104
bool Remove(C4Client *pClient, bool fTemporary=false)
Definition: C4Client.cpp:270
ValidatedStdCopyStrBuf< C4InVal::VAL_NameNoEmpty > Nick
Definition: C4Client.h:44
bool isRegistered() const
Definition: C4Client.h:73
void SetObserver()
Definition: C4Client.h:123
void Clear()
Definition: C4Client.cpp:170
int32_t getID() const
Definition: C4Client.h:57
void Remove()
Definition: C4Client.cpp:147
const char * getRevision() const
Definition: C4Client.h:72
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
int32_t getDiffLevel(const C4ClientCore &CCore2) const
Definition: C4Client.cpp:75
bool fActivated
Definition: C4Client.h:52
void SetLocal()
Definition: C4Client.cpp:141
ValidatedStdCopyStrBuf< C4InVal::VAL_NameAllowEmpty > CUID
Definition: C4Client.h:45
void SetCore(const C4ClientCore &NewCore)
Definition: C4Client.h:119
const char * getNick() const
Definition: C4Client.h:71
C4ClientList & operator=(const C4ClientList &List)
Definition: C4Client.cpp:361
class C4Network2ClientList * pNetClients
Definition: C4Client.h:155
bool fLobbyReady
Definition: C4Client.h:52
int iVersion[4]
Definition: C4Client.h:49
int32_t getClientCnt() const
Definition: C4Client.cpp:224
C4Client * getClientByID(int32_t iID) const
Definition: C4Client.cpp:200
const char * getCUID() const
Definition: C4Client.h:108
ValidatedStdCopyStrBuf< C4InVal::VAL_NameNoEmpty > Name
Definition: C4Client.h:44
C4Client * getNextClientByID(int32_t iAfterID) const
Definition: C4Client.cpp:208
const char * getName() const
Definition: C4Client.h:107
void ToggleIgnore()
Definition: C4Client.h:127
bool IsIgnored() const
Definition: C4Client.h:114
bool isLocal() const
Definition: C4Client.h:116
C4Network2Client * pNext
bool isLobbyReady() const
Definition: C4Client.h:61
void SetActivated(bool fnActivated)
Definition: C4Client.h:64
const char * getNick() const
Definition: C4Client.h:109
C4Client * pFirst
Definition: C4Client.h:149
size_t getLength() const
Definition: StdBuf.h:445
void CompileFunc(StdCompiler *pComp)
Definition: C4Client.cpp:153
const char * getName() const
Definition: C4Client.h:69
void SetLocal(int32_t iID, bool fnActivated, bool fnObserver)
Definition: C4Client.cpp:38
C4Client * getClient(const C4Client *pAfter=nullptr) const
Definition: C4Client.h:160
void InitNetwork(class C4Network2ClientList *pNetClients)
Definition: C4Client.cpp:251
void SetActivated(bool fnActivated)
Definition: C4Client.cpp:120
void CopyValidated(const char *szFromVal)
void Add(C4Client *pClient)
Definition: C4Client.cpp:181
const int32_t C4ClientCoreDL_Different
Definition: C4Client.h:32
const int32_t C4ClientIDHost
Definition: C4Client.h:25