DDraceNetwork Docs
server.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_SERVER_H
4#define ENGINE_SERVER_H
5
6#include <optional>
7#include <type_traits>
8
9#include <base/hash.h>
10#include <base/math.h>
11#include <base/system.h>
12
13#include "kernel.h"
14#include "message.h"
20
22
23// When recording a demo on the server, the ClientId -1 is used
24enum
25{
27};
28
29class IServer : public IInterface
30{
32protected:
34
35public:
36 /*
37 Structure: CClientInfo
38 */
40 {
41 const char *m_pName;
45 const char *m_pDDNetVersionStr;
47 };
48
49 int Tick() const { return m_CurrentGameTick; }
50 int TickSpeed() const { return SERVER_TICK_SPEED; }
51
52 virtual int Port() const = 0;
53 virtual int MaxClients() const = 0;
54 virtual int ClientCount() const = 0;
55 virtual int DistinctClientCount() const = 0;
56 virtual const char *ClientName(int ClientId) const = 0;
57 virtual const char *ClientClan(int ClientId) const = 0;
58 virtual int ClientCountry(int ClientId) const = 0;
59 virtual bool ClientSlotEmpty(int ClientId) const = 0;
60 virtual bool ClientIngame(int ClientId) const = 0;
61 virtual bool GetClientInfo(int ClientId, CClientInfo *pInfo) const = 0;
62 virtual void SetClientDDNetVersion(int ClientId, int DDNetVersion) = 0;
63 virtual void GetClientAddr(int ClientId, char *pAddrStr, int Size) const = 0;
64
75 virtual int GetClientVersion(int ClientId) const = 0;
76 virtual int SendMsg(CMsgPacker *pMsg, int Flags, int ClientId) = 0;
77
78 template<class T, typename std::enable_if<!protocol7::is_sixup<T>::value, int>::type = 0>
79 inline int SendPackMsg(const T *pMsg, int Flags, int ClientId)
80 {
81 int Result = 0;
82 if(ClientId == -1)
83 {
84 for(int i = 0; i < MaxClients(); i++)
85 if(ClientIngame(i))
86 Result = SendPackMsgTranslate(pMsg, Flags, i);
87 }
88 else
89 {
90 Result = SendPackMsgTranslate(pMsg, Flags, ClientId);
91 }
92 return Result;
93 }
94
95 template<class T, typename std::enable_if<protocol7::is_sixup<T>::value, int>::type = 1>
96 inline int SendPackMsg(const T *pMsg, int Flags, int ClientId)
97 {
98 int Result = 0;
99 if(ClientId == -1)
100 {
101 for(int i = 0; i < MaxClients(); i++)
102 if(ClientIngame(i) && IsSixup(i))
103 Result = SendPackMsgOne(pMsg, Flags, i);
104 }
105 else if(IsSixup(ClientId))
106 Result = SendPackMsgOne(pMsg, Flags, ClientId);
107
108 return Result;
109 }
110
111 template<class T>
112 int SendPackMsgTranslate(const T *pMsg, int Flags, int ClientId)
113 {
114 return SendPackMsgOne(pMsg, Flags, ClientId);
115 }
116
117 int SendPackMsgTranslate(const CNetMsg_Sv_Emoticon *pMsg, int Flags, int ClientId)
118 {
119 CNetMsg_Sv_Emoticon MsgCopy;
120 mem_copy(&MsgCopy, pMsg, sizeof(MsgCopy));
121 return Translate(MsgCopy.m_ClientId, ClientId) && SendPackMsgOne(&MsgCopy, Flags, ClientId);
122 }
123
124 int SendPackMsgTranslate(const CNetMsg_Sv_Chat *pMsg, int Flags, int ClientId)
125 {
126 CNetMsg_Sv_Chat MsgCopy;
127 mem_copy(&MsgCopy, pMsg, sizeof(MsgCopy));
128
129 char aBuf[1000];
130 if(MsgCopy.m_ClientId >= 0 && !Translate(MsgCopy.m_ClientId, ClientId))
131 {
132 str_format(aBuf, sizeof(aBuf), "%s: %s", ClientName(MsgCopy.m_ClientId), MsgCopy.m_pMessage);
133 MsgCopy.m_pMessage = aBuf;
134 MsgCopy.m_ClientId = VANILLA_MAX_CLIENTS - 1;
135 }
136
137 if(IsSixup(ClientId))
138 {
140 Msg7.m_ClientId = MsgCopy.m_ClientId;
141 Msg7.m_pMessage = MsgCopy.m_pMessage;
143 Msg7.m_TargetId = -1;
144 return SendPackMsgOne(&Msg7, Flags, ClientId);
145 }
146
147 return SendPackMsgOne(&MsgCopy, Flags, ClientId);
148 }
149
150 int SendPackMsgTranslate(const CNetMsg_Sv_KillMsg *pMsg, int Flags, int ClientId)
151 {
152 CNetMsg_Sv_KillMsg MsgCopy;
153 mem_copy(&MsgCopy, pMsg, sizeof(MsgCopy));
154 if(!Translate(MsgCopy.m_Victim, ClientId))
155 return 0;
156 if(!Translate(MsgCopy.m_Killer, ClientId))
157 MsgCopy.m_Killer = MsgCopy.m_Victim;
158 return SendPackMsgOne(&MsgCopy, Flags, ClientId);
159 }
160
161 int SendPackMsgTranslate(const CNetMsg_Sv_RaceFinish *pMsg, int Flags, int ClientId)
162 {
163 if(IsSixup(ClientId))
164 {
166 Msg7.m_ClientId = pMsg->m_ClientId;
167 Msg7.m_Diff = pMsg->m_Diff;
168 Msg7.m_Time = pMsg->m_Time;
169 Msg7.m_RecordPersonal = pMsg->m_RecordPersonal;
170 Msg7.m_RecordServer = pMsg->m_RecordServer;
171 return SendPackMsgOne(&Msg7, Flags, ClientId);
172 }
173 return SendPackMsgOne(pMsg, Flags, ClientId);
174 }
175
176 template<class T>
177 int SendPackMsgOne(const T *pMsg, int Flags, int ClientId)
178 {
179 dbg_assert(ClientId != -1, "SendPackMsgOne called with -1");
180 CMsgPacker Packer(T::ms_MsgId, false, protocol7::is_sixup<T>::value);
181
182 if(pMsg->Pack(&Packer))
183 return -1;
184 return SendMsg(&Packer, Flags, ClientId);
185 }
186
187 bool Translate(int &Target, int Client)
188 {
189 if(IsSixup(Client))
190 return true;
192 return true;
193 int *pMap = GetIdMap(Client);
194 bool Found = false;
195 for(int i = 0; i < VANILLA_MAX_CLIENTS; i++)
196 {
197 if(Target == pMap[i])
198 {
199 Target = i;
200 Found = true;
201 break;
202 }
203 }
204 return Found;
205 }
206
207 bool ReverseTranslate(int &Target, int Client)
208 {
209 if(IsSixup(Client))
210 return true;
212 return true;
213 Target = clamp(Target, 0, VANILLA_MAX_CLIENTS - 1);
214 int *pMap = GetIdMap(Client);
215 if(pMap[Target] == -1)
216 return false;
217 Target = pMap[Target];
218 return true;
219 }
220
221 virtual void GetMapInfo(char *pMapName, int MapNameSize, int *pMapSize, SHA256_DIGEST *pSha256, int *pMapCrc) = 0;
222
223 virtual bool WouldClientNameChange(int ClientId, const char *pNameRequest) = 0;
224 virtual bool WouldClientClanChange(int ClientId, const char *pClanRequest) = 0;
225 virtual void SetClientName(int ClientId, const char *pName) = 0;
226 virtual void SetClientClan(int ClientId, const char *pClan) = 0;
227 virtual void SetClientCountry(int ClientId, int Country) = 0;
228 virtual void SetClientScore(int ClientId, std::optional<int> Score) = 0;
229 virtual void SetClientFlags(int ClientId, int Flags) = 0;
230
231 virtual int SnapNewId() = 0;
232 virtual void SnapFreeId(int Id) = 0;
233 virtual void *SnapNewItem(int Type, int Id, int Size) = 0;
234
235 template<typename T>
236 T *SnapNewItem(int Id)
237 {
238 const int Type = protocol7::is_sixup<T>::value ? -T::ms_MsgId : T::ms_MsgId;
239 return static_cast<T *>(SnapNewItem(Type, Id, sizeof(T)));
240 }
241
242 virtual void SnapSetStaticsize(int ItemType, int Size) = 0;
243
244 enum
245 {
248 };
249 virtual void SetRconCid(int ClientId) = 0;
250 virtual int GetAuthedState(int ClientId) const = 0;
251 virtual const char *GetAuthName(int ClientId) const = 0;
252 virtual void Kick(int ClientId, const char *pReason) = 0;
253 virtual void Ban(int ClientId, int Seconds, const char *pReason, bool VerbatimReason) = 0;
254 virtual void RedirectClient(int ClientId, int Port, bool Verbose = false) = 0;
255 virtual void ChangeMap(const char *pMap) = 0;
256 virtual void ReloadMap() = 0;
257
259
260 // DDRace
261
262 virtual void SaveDemo(int ClientId, float Time) = 0;
263 virtual void StartRecord(int ClientId) = 0;
264 virtual void StopRecord(int ClientId) = 0;
265 virtual bool IsRecording(int ClientId) = 0;
266 virtual void StopDemos() = 0;
267
268 virtual void GetClientAddr(int ClientId, NETADDR *pAddr) const = 0;
269
270 virtual int *GetIdMap(int ClientId) = 0;
271
272 virtual bool DnsblWhite(int ClientId) = 0;
273 virtual bool DnsblPending(int ClientId) = 0;
274 virtual bool DnsblBlack(int ClientId) = 0;
275 virtual const char *GetAnnouncementLine() = 0;
276 virtual bool ClientPrevIngame(int ClientId) = 0;
277 virtual const char *GetNetErrorString(int ClientId) = 0;
278 virtual void ResetNetErrorString(int ClientId) = 0;
279 virtual bool SetTimedOut(int ClientId, int OrigId) = 0;
280 virtual void SetTimeoutProtected(int ClientId) = 0;
281
282 virtual void SetErrorShutdown(const char *pReason) = 0;
283 virtual void ExpireServerInfo() = 0;
284
285 virtual void FillAntibot(CAntibotRoundData *pData) = 0;
286
287 virtual void SendMsgRaw(int ClientId, const void *pData, int Size, int Flags) = 0;
288
289 virtual const char *GetMapName() const = 0;
290
291 virtual bool IsSixup(int ClientId) const = 0;
292};
293
295{
296 MACRO_INTERFACE("gameserver")
297protected:
298public:
299 // `pPersistentData` may be null if this is the first time `IGameServer`
300 // is instantiated.
301 virtual void OnInit(const void *pPersistentData) = 0;
302 virtual void OnConsoleInit() = 0;
303 virtual void OnMapChange(char *pNewMapName, int MapNameSize) = 0;
304 // `pPersistentData` may be null if this is the last time `IGameServer`
305 // is destroyed.
306 virtual void OnShutdown(void *pPersistentData) = 0;
307
308 virtual void OnTick() = 0;
309 virtual void OnPreSnap() = 0;
310 virtual void OnSnap(int ClientId) = 0;
311 virtual void OnPostSnap() = 0;
312
313 virtual void OnMessage(int MsgId, CUnpacker *pUnpacker, int ClientId) = 0;
314
315 // Called before map reload, for any data that the game wants to
316 // persist to the next map.
317 //
318 // Has the size of the return value of `PersistentClientDataSize()`.
319 //
320 // Returns whether the game should be supplied with the data when the
321 // client connects for the next map.
322 virtual bool OnClientDataPersist(int ClientId, void *pData) = 0;
323
324 // Called when a client connects.
325 //
326 // If it is reconnecting to the game after a map change, the
327 // `pPersistentData` point is nonnull and contains the data the game
328 // previously stored.
329 virtual void OnClientConnected(int ClientId, void *pPersistentData) = 0;
330
331 virtual void OnClientEnter(int ClientId) = 0;
332 virtual void OnClientDrop(int ClientId, const char *pReason) = 0;
333 virtual void OnClientPrepareInput(int ClientId, void *pInput) = 0;
334 virtual void OnClientDirectInput(int ClientId, void *pInput) = 0;
335 virtual void OnClientPredictedInput(int ClientId, void *pInput) = 0;
336 virtual void OnClientPredictedEarlyInput(int ClientId, void *pInput) = 0;
337
338 virtual bool IsClientReady(int ClientId) const = 0;
339 virtual bool IsClientPlayer(int ClientId) const = 0;
340
341 virtual int PersistentDataSize() const = 0;
342 virtual int PersistentClientDataSize() const = 0;
343
344 virtual CUuid GameUuid() const = 0;
345 virtual const char *GameType() const = 0;
346 virtual const char *Version() const = 0;
347 virtual const char *NetVersion() const = 0;
348
349 // DDRace
350
351 virtual void OnPreTickTeehistorian() = 0;
352
353 virtual void OnSetAuthed(int ClientId, int Level) = 0;
354 virtual bool PlayerExists(int ClientId) const = 0;
355
356 virtual void TeehistorianRecordAntibot(const void *pData, int DataSize) = 0;
357 virtual void TeehistorianRecordPlayerJoin(int ClientId, bool Sixup) = 0;
358 virtual void TeehistorianRecordPlayerDrop(int ClientId, const char *pReason) = 0;
359 virtual void TeehistorianRecordPlayerRejoin(int ClientId) = 0;
360 virtual void TeehistorianRecordPlayerName(int ClientId, const char *pName) = 0;
361 virtual void TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks) = 0;
362 virtual void TeehistorianRecordTeamFinish(int TeamId, int TimeTicks) = 0;
363
364 virtual void FillAntibot(CAntibotRoundData *pData) = 0;
365
372 virtual void OnUpdatePlayerServerInfo(CJsonStringWriter *pJSonWriter, int Id) = 0;
373};
374
376#endif
Definition: jsonwriter.h:105
Definition: message.h:10
Definition: packer.h:54
Definition: server.h:295
virtual void OnClientPredictedInput(int ClientId, void *pInput)=0
virtual void OnClientDirectInput(int ClientId, void *pInput)=0
virtual const char * GameType() const =0
virtual void TeehistorianRecordPlayerFinish(int ClientId, int TimeTicks)=0
virtual void TeehistorianRecordPlayerName(int ClientId, const char *pName)=0
virtual void OnMessage(int MsgId, CUnpacker *pUnpacker, int ClientId)=0
virtual int PersistentDataSize() const =0
virtual bool OnClientDataPersist(int ClientId, void *pData)=0
virtual void FillAntibot(CAntibotRoundData *pData)=0
virtual void OnSetAuthed(int ClientId, int Level)=0
virtual void OnSnap(int ClientId)=0
virtual CUuid GameUuid() const =0
virtual void OnPreSnap()=0
virtual void OnClientPredictedEarlyInput(int ClientId, void *pInput)=0
virtual void OnPreTickTeehistorian()=0
virtual void TeehistorianRecordTeamFinish(int TeamId, int TimeTicks)=0
virtual const char * NetVersion() const =0
virtual void OnClientEnter(int ClientId)=0
virtual void TeehistorianRecordAntibot(const void *pData, int DataSize)=0
virtual void OnUpdatePlayerServerInfo(CJsonStringWriter *pJSonWriter, int Id)=0
virtual void OnTick()=0
virtual void OnClientConnected(int ClientId, void *pPersistentData)=0
virtual void OnConsoleInit()=0
virtual const char * Version() const =0
virtual bool IsClientPlayer(int ClientId) const =0
virtual bool PlayerExists(int ClientId) const =0
virtual void OnPostSnap()=0
virtual void OnClientDrop(int ClientId, const char *pReason)=0
virtual void TeehistorianRecordPlayerDrop(int ClientId, const char *pReason)=0
virtual bool IsClientReady(int ClientId) const =0
virtual void OnShutdown(void *pPersistentData)=0
virtual void OnClientPrepareInput(int ClientId, void *pInput)=0
virtual void TeehistorianRecordPlayerRejoin(int ClientId)=0
virtual void TeehistorianRecordPlayerJoin(int ClientId, bool Sixup)=0
virtual void OnMapChange(char *pNewMapName, int MapNameSize)=0
virtual int PersistentClientDataSize() const =0
virtual void OnInit(const void *pPersistentData)=0
Definition: kernel.h:10
Definition: server.h:30
virtual int ClientCount() const =0
virtual void SetTimeoutProtected(int ClientId)=0
bool ReverseTranslate(int &Target, int Client)
Definition: server.h:207
int SendPackMsgTranslate(const CNetMsg_Sv_RaceFinish *pMsg, int Flags, int ClientId)
Definition: server.h:161
virtual int SnapNewId()=0
virtual void GetMapInfo(char *pMapName, int MapNameSize, int *pMapSize, SHA256_DIGEST *pSha256, int *pMapCrc)=0
virtual void SetRconCid(int ClientId)=0
int SendPackMsg(const T *pMsg, int Flags, int ClientId)
Definition: server.h:79
virtual bool WouldClientClanChange(int ClientId, const char *pClanRequest)=0
virtual void SetClientName(int ClientId, const char *pName)=0
virtual const char * GetMapName() const =0
virtual bool WouldClientNameChange(int ClientId, const char *pNameRequest)=0
int SendPackMsgTranslate(const T *pMsg, int Flags, int ClientId)
Definition: server.h:112
virtual void SnapSetStaticsize(int ItemType, int Size)=0
virtual int Port() const =0
virtual bool DnsblBlack(int ClientId)=0
virtual void SnapFreeId(int Id)=0
int Tick() const
Definition: server.h:49
virtual bool DnsblWhite(int ClientId)=0
virtual int SendMsg(CMsgPacker *pMsg, int Flags, int ClientId)=0
virtual const char * GetAuthName(int ClientId) const =0
virtual bool IsSixup(int ClientId) const =0
int SendPackMsgTranslate(const CNetMsg_Sv_Chat *pMsg, int Flags, int ClientId)
Definition: server.h:124
virtual void StopDemos()=0
virtual void ExpireServerInfo()=0
virtual int ClientCountry(int ClientId) const =0
@ RCON_CID_VOTE
Definition: server.h:247
@ RCON_CID_SERV
Definition: server.h:246
virtual void Ban(int ClientId, int Seconds, const char *pReason, bool VerbatimReason)=0
virtual void GetClientAddr(int ClientId, char *pAddrStr, int Size) const =0
virtual int MaxClients() const =0
virtual void RedirectClient(int ClientId, int Port, bool Verbose=false)=0
virtual void SetClientScore(int ClientId, std::optional< int > Score)=0
virtual int * GetIdMap(int ClientId)=0
virtual void ReloadMap()=0
virtual void ChangeMap(const char *pMap)=0
virtual bool ClientPrevIngame(int ClientId)=0
virtual bool GetClientInfo(int ClientId, CClientInfo *pInfo) const =0
virtual void FillAntibot(CAntibotRoundData *pData)=0
virtual void SetErrorShutdown(const char *pReason)=0
virtual void SetClientFlags(int ClientId, int Flags)=0
virtual bool IsRecording(int ClientId)=0
int SendPackMsgOne(const T *pMsg, int Flags, int ClientId)
Definition: server.h:177
virtual void * SnapNewItem(int Type, int Id, int Size)=0
virtual int DistinctClientCount() const =0
int TickSpeed() const
Definition: server.h:50
virtual const char * ClientName(int ClientId) const =0
int m_CurrentGameTick
Definition: server.h:33
virtual int GetAuthedState(int ClientId) const =0
T * SnapNewItem(int Id)
Definition: server.h:236
virtual void SetClientDDNetVersion(int ClientId, int DDNetVersion)=0
virtual void DemoRecorder_HandleAutoStart()=0
virtual void GetClientAddr(int ClientId, NETADDR *pAddr) const =0
virtual void SetClientClan(int ClientId, const char *pClan)=0
virtual bool ClientIngame(int ClientId) const =0
virtual void StopRecord(int ClientId)=0
bool Translate(int &Target, int Client)
Definition: server.h:187
virtual void SetClientCountry(int ClientId, int Country)=0
int SendPackMsgTranslate(const CNetMsg_Sv_KillMsg *pMsg, int Flags, int ClientId)
Definition: server.h:150
virtual const char * GetAnnouncementLine()=0
virtual void Kick(int ClientId, const char *pReason)=0
virtual bool DnsblPending(int ClientId)=0
virtual void SaveDemo(int ClientId, float Time)=0
virtual void StartRecord(int ClientId)=0
int SendPackMsgTranslate(const CNetMsg_Sv_Emoticon *pMsg, int Flags, int ClientId)
Definition: server.h:117
virtual int GetClientVersion(int ClientId) const =0
virtual void ResetNetErrorString(int ClientId)=0
virtual void SendMsgRaw(int ClientId, const void *pData, int Size, int Flags)=0
virtual const char * ClientClan(int ClientId) const =0
virtual const char * GetNetErrorString(int ClientId)=0
virtual bool SetTimedOut(int ClientId, int OrigId)=0
virtual bool ClientSlotEmpty(int ClientId) const =0
@ VERSION_DDNET_OLD
Definition: protocol.h:114
@ SERVER_TICK_SPEED
Definition: protocol.h:80
@ VANILLA_MAX_CLIENTS
Definition: protocol.h:89
#define dbg_assert(test, msg)
Definition: system.h:76
void mem_copy(void *dest, const void *source, size_t size)
Definition: system.cpp:174
#define MACRO_INTERFACE(Name)
Definition: kernel.h:25
@ CHAT_ALL
Definition: protocol7.h:99
@ CHAT_TEAM
Definition: protocol7.h:100
@ SERVER_DEMO_CLIENT
Definition: server.h:26
IGameServer * CreateGameServer()
Definition: gamecontext.cpp:4478
Definition: antibot_data.h:98
Definition: protocol.h:693
const char * m_pMessage
Definition: protocol.h:697
int m_Team
Definition: protocol.h:695
Definition: protocol.h:781
Definition: protocol.h:709
int m_Victim
Definition: protocol.h:712
int m_Killer
Definition: protocol.h:711
Definition: uuid_manager.h:17
Definition: server.h:40
const char * m_pName
Definition: server.h:41
const CUuid * m_pConnectionId
Definition: server.h:46
int m_Latency
Definition: server.h:42
int m_DDNetVersion
Definition: server.h:44
bool m_GotDDNetVersion
Definition: server.h:43
const char * m_pDDNetVersionStr
Definition: server.h:45
Definition: types.h:67
Definition: hash.h:15
Definition: score.cpp:40
Definition: protocol7.h:519
const char * m_pMessage
Definition: protocol7.h:525
int m_Mode
Definition: protocol7.h:522
Definition: protocol7.h:1093
int m_RecordServer
Definition: protocol7.h:1100
int m_Time
Definition: protocol7.h:1097
int m_RecordPersonal
Definition: protocol7.h:1099
int m_Diff
Definition: protocol7.h:1098
Definition: protocol7.h:247
#define str_format
Definition: system.cpp:2830