OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4PacketBase.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) 2009-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_C4PacketBase
17 #define INC_C4PacketBase
18 
19 #include "network/C4NetIO.h"
20 
21 // *** packet base class
22 
24 {
25  friend class C4PacketList;
26 public:
27  C4PacketBase();
28  virtual ~C4PacketBase();
29 
30  // virtual functions to implement by derived classes
31  virtual void CompileFunc(StdCompiler *pComp) = 0;
32 
33  // conversion (using above functions)
34  C4NetIOPacket pack(const C4NetIO::addr_t &addr = C4NetIO::addr_t()) const;
35  C4NetIOPacket pack(uint8_t cStatus, const C4NetIO::addr_t &addr = C4NetIO::addr_t()) const;
36  void unpack(const C4NetIOPacket &Pkt, char *pStatus = nullptr);
37 
38 };
39 
40 inline C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr = C4NetIO::addr_t())
41 {
42  return Pkt.pack(cStatus, addr);
43 }
44 
45 // Filename Adaptor
46 // Converts the network filename separator to the native filename separator
48 {
50  explicit C4NetFilenameAdapt(StdStrBuf &FileName) : FileName(FileName) { }
51  inline void CompileFunc(StdCompiler *pComp) const
52  {
53 #ifdef _WIN32
54  pComp->Value(FileName);
55 #else
56  StdCopyStrBuf FileName2;
57  if (pComp->isSerializer() && FileName)
58  {
59  FileName2.Copy(FileName);
60  SReplaceChar(FileName2.getMData(),DirectorySeparator,'\\');
61  }
62  pComp->Value(FileName2);
63  if (pComp->isDeserializer())
64  {
65  FileName.Take(FileName2);
66  SReplaceChar(FileName.getMData(),'\\',DirectorySeparator);
67  }
68 #endif
69  }
70  template <class T> bool operator == (const T &rVal) { return FileName == rVal; }
71  template <class T> C4NetFilenameAdapt &operator = (const T &rVal) { FileName = rVal; return *this; }
72 };
73 inline C4NetFilenameAdapt mkNetFilenameAdapt(StdStrBuf &FileName) { return C4NetFilenameAdapt(FileName); }
74 
75 // enumaration of all used packet types
77 {
78  PID_None = 0xFF,
79 
80  // *** network
81 
82  // * base packets
83  // ping
84  PID_Ping = 0x00,
85  PID_Pong = 0x01,
86 
87  // connecting
88  PID_Conn = 0x02,
89  PID_ConnRe = 0x03,
90 
91  // msg forwarding
92  PID_FwdReq = 0x04,
93  PID_Fwd = 0x05,
94 
95  // post mortem
97 
98  // (packets before this ID won't be recovered post-mortem)
100 
101  // * game
102  // game status
103  PID_Status = 0x10,
105 
106  // client address propagation
107  PID_Addr = 0x12,
108 
109  // activation request
111 
112  // all data a client needs to get started
113  PID_JoinData = 0x15,
114 
115  // player info
117 
118  // round results league info
120 
121  // * lobby
123  PID_SetScenarioParameter = 0x21, // scenario parameter update
124 
125  // * resources
131 
132  // * control
133  PID_Control = 0x40,
137 
138  // *** control
139  CID_First = 0x80,
140 
144 
145  CID_Vote = CID_First | 0x03,
147 
150  CID_Set = CID_First | 0x07,
154 
158 
161 
165 
170 
173 
174  // Note: There are some more packet types in src/netpuncher/C4PuncherPacket.h
175  // They have been picked to be distinct from these for safety, not for necessary.
176 };
177 
178 // packet classes
180 {
181  PC_Network, // network packet - internal stuff
182  PC_Control // control packet - game data (saved in records)
183 };
184 
185 // enumeration of packet handlers
187 {
188  PH_C4Network2IO = 1 << 0, // network i/o class
189  PH_C4Network2 = 1 << 1, // main network class
190  PH_C4GUIMainDlg = 1 << 2, // network lobby class
191  PH_C4Network2ClientList = 1 << 3, // client list class
192  PH_C4Network2Players = 1 << 4, // player list class
193  PH_C4Network2ResList = 1 << 5, // resource list class
194  PH_C4GameControlNetwork = 1 << 6, // network control class
195 };
196 
197 
198 // packet handling data
200 {
203  const char *Name;
204 
207 
208  int32_t HandlerID; // (C4PacketHandlerID)
209 
210  class C4PacketBase *(*FnUnpack)(StdCompiler *pComp);
211 };
212 extern const C4PktHandlingData PktHandlingData[];
213 
214 const char *PacketNameByID(C4PacketType eID);
215 
216 
217 // *** general packet types
218 
219 // raw packet: contains std buffer
220 class C4PktBuf : public C4PacketBase
221 {
222 protected:
224 public:
225  C4PktBuf();
226  C4PktBuf(const C4PktBuf &rCopy);
227  C4PktBuf(const StdBuf &rCpyData);
228 
229  size_t getSize() const { return Data.getSize(); }
230  const void *getData() const { return Data.getData(); }
231 
232  void CompileFunc(StdCompiler *pComp) override;
233 };
234 
235 // "identified" packet: packet with packet type id
236 class C4IDPacket : public C4PacketBase
237 {
238  friend class C4PacketList;
239 public:
240  C4IDPacket();
241  C4IDPacket(C4PacketType eID, C4PacketBase *pPkt, bool fTakePkt = true);
242  C4IDPacket(const C4IDPacket &Packet2);
243  ~C4IDPacket() override;
244 
245 protected:
247  C4PacketBase *pPkt{nullptr};
248  bool fOwnPkt{true};
249 
250  // used by C4PacketList
251  C4IDPacket *pNext{nullptr};
252 
253 public:
254  C4PacketType getPktType() const { return eID; }
255  C4PacketBase *getPkt() const { return pPkt; }
256  const char *getPktName() const;
257 
258  void Clear();
259  void Default();
260  void Set(C4PacketType eType, C4PacketBase *pPkt);
261 
262  void CompileFunc(StdCompiler *pComp) override;
263 };
264 
265 // list of identified packets
267 {
268 public:
269  C4PacketList();
270  C4PacketList(const C4PacketList &List2);
271  ~C4PacketList() override;
272 
273 protected:
274  C4IDPacket *pFirst{nullptr}, *pLast{nullptr};
275 
276 public:
277  C4IDPacket *firstPkt() const { return pFirst; }
278  C4IDPacket *nextPkt(C4IDPacket *pPkt) const { return pPkt->pNext; }
279 
280  int32_t getPktCnt() const;
281 
282  void Add(C4IDPacket *pPkt);
283  void AddHead(C4IDPacket *pPkt);
284  void Add(C4PacketType eType, C4PacketBase *pPkt);
285  void AddHead(C4PacketType eType, C4PacketBase *pPkt);
286 
287  void Take(C4PacketList &List);
288  void Append(const C4PacketList &List);
289 
290  void Clear();
291  void Remove(C4IDPacket *pPkt);
292  void Delete(C4IDPacket *pPkt);
293 
294  void CompileFunc(StdCompiler *pComp) override;
295 };
296 #endif // INC_C4PacketBase
C4NetFilenameAdapt & operator=(const T &rVal)
Definition: C4PacketBase.h:71
C4PacketBase * pPkt
Definition: C4PacketBase.h:247
const void * getData() const
Definition: StdBuf.h:99
Definition: StdBuf.h:29
C4IDPacket * pNext
Definition: C4PacketBase.h:251
void CompileFunc(StdCompiler *pComp) override
Definition: C4Packet2.cpp:180
void Set(C4PacketType eType, C4PacketBase *pPkt)
void Clear()
Definition: C4Packet2.cpp:221
void Add(C4IDPacket *pPkt)
Definition: C4Packet2.cpp:279
void CompileFunc(StdCompiler *pComp) override
Definition: C4Packet2.cpp:350
C4PacketType eID
Definition: C4PacketBase.h:246
const void * getData() const
Definition: C4PacketBase.h:230
C4IDPacket * pFirst
Definition: C4PacketBase.h:274
const char * Name
Definition: C4PacketBase.h:203
C4PacketType
Definition: C4PacketBase.h:76
C4IDPacket * pLast
Definition: C4PacketBase.h:274
bool operator==(const T &rVal)
Definition: C4PacketBase.h:70
C4PacketClass
Definition: C4PacketBase.h:179
size_t getSize() const
Definition: StdBuf.h:101
void Append(const C4PacketList &List)
Definition: C4Packet2.cpp:311
C4PacketType ID
Definition: C4PacketBase.h:201
char * getMData()
Definition: StdBuf.h:443
C4PacketClass Class
Definition: C4PacketBase.h:202
bool isSerializer()
Definition: StdCompiler.h:54
const char * getPktName() const
Definition: C4Packet2.cpp:207
void Take(C4PacketList &List)
Definition: C4Packet2.cpp:304
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4IDPacket * nextPkt(C4IDPacket *pPkt) const
Definition: C4PacketBase.h:278
void Take(char *pnData)
Definition: StdBuf.h:457
virtual void CompileFunc(StdCompiler *pComp)=0
const C4PktHandlingData PktHandlingData[]
Definition: C4Packet2.cpp:50
void Clear()
Definition: C4Packet2.cpp:317
StdCopyBuf Data
Definition: C4PacketBase.h:223
void Delete(C4IDPacket *pPkt)
Definition: C4Packet2.cpp:344
void Value(const T &rStruct)
Definition: StdCompiler.h:161
C4PacketHandlerID
Definition: C4PacketBase.h:186
C4NetFilenameAdapt(StdStrBuf &FileName)
Definition: C4PacketBase.h:50
void CompileFunc(StdCompiler *pComp) const
Definition: C4PacketBase.h:51
virtual bool isDeserializer()
Definition: StdCompiler.h:53
const char * PacketNameByID(C4PacketType eID)
Definition: C4Packet2.cpp:135
size_t getSize() const
Definition: C4PacketBase.h:229
C4NetFilenameAdapt mkNetFilenameAdapt(StdStrBuf &FileName)
Definition: C4PacketBase.h:73
virtual ~C4PacketBase()
void AddHead(C4IDPacket *pPkt)
Definition: C4Packet2.cpp:286
void Remove(C4IDPacket *pPkt)
Definition: C4Packet2.cpp:323
C4NetIOPacket pack(const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
Definition: C4Packet2.cpp:149
#define DirectorySeparator
void CompileFunc(StdCompiler *pComp) override
Definition: C4Packet2.cpp:227
void Copy()
Definition: StdBuf.h:467
~C4IDPacket() override
Definition: C4Packet2.cpp:202
C4PacketBase * getPkt() const
Definition: C4PacketBase.h:255
int32_t getPktCnt() const
Definition: C4Packet2.cpp:271
C4PacketType getPktType() const
Definition: C4PacketBase.h:254
void Default()
Definition: C4Packet2.cpp:216
void unpack(const C4NetIOPacket &Pkt, char *pStatus=nullptr)
Definition: C4Packet2.cpp:159
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:326
C4IDPacket * firstPkt() const
Definition: C4PacketBase.h:277
~C4PacketList() override
Definition: C4Packet2.cpp:266
StdStrBuf & FileName
Definition: C4PacketBase.h:49