OpenClonk
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  // request to perform TCP simultaneous open
114 
115  // all data a client needs to get started
116  PID_JoinData = 0x15,
117 
118  // player info
120 
121  // round results league info
123 
124  // * lobby
126  PID_SetScenarioParameter = 0x21, // scenario parameter update
127 
128  // * resources
134 
135  // * control
136  PID_Control = 0x40,
140 
141  // *** control
142  CID_First = 0x80,
143 
147 
148  CID_Vote = CID_First | 0x03,
150 
153  CID_Set = CID_First | 0x07,
157 
161 
164 
168 
173 
176 
177  // Note: There are some more packet types in src/netpuncher/C4PuncherPacket.h
178  // They have been picked to be distinct from these for safety, not for necessary.
179 };
180 
181 // packet classes
183 {
184  PC_Network, // network packet - internal stuff
185  PC_Control // control packet - game data (saved in records)
186 };
187 
188 // enumeration of packet handlers
190 {
191  PH_C4Network2IO = 1 << 0, // network i/o class
192  PH_C4Network2 = 1 << 1, // main network class
193  PH_C4GUIMainDlg = 1 << 2, // network lobby class
194  PH_C4Network2ClientList = 1 << 3, // client list class
195  PH_C4Network2Players = 1 << 4, // player list class
196  PH_C4Network2ResList = 1 << 5, // resource list class
197  PH_C4GameControlNetwork = 1 << 6, // network control class
198 };
199 
200 
201 // packet handling data
203 {
206  const char *Name;
207 
210 
211  int32_t HandlerID; // (C4PacketHandlerID)
212 
213  class C4PacketBase *(*FnUnpack)(StdCompiler *pComp);
214 };
215 extern const C4PktHandlingData PktHandlingData[];
216 
217 const char *PacketNameByID(C4PacketType eID);
218 
219 
220 // *** general packet types
221 
222 // raw packet: contains std buffer
223 class C4PktBuf : public C4PacketBase
224 {
225 protected:
227 public:
228  C4PktBuf();
229  C4PktBuf(const C4PktBuf &rCopy);
230  C4PktBuf(const StdBuf &rCpyData);
231 
232  size_t getSize() const { return Data.getSize(); }
233  const void *getData() const { return Data.getData(); }
234 
235  void CompileFunc(StdCompiler *pComp) override;
236 };
237 
238 // "identified" packet: packet with packet type id
239 class C4IDPacket : public C4PacketBase
240 {
241  friend class C4PacketList;
242 public:
243  C4IDPacket();
244  C4IDPacket(C4PacketType eID, C4PacketBase *pPkt, bool fTakePkt = true);
245  C4IDPacket(const C4IDPacket &Packet2);
246  ~C4IDPacket() override;
247 
248 protected:
250  C4PacketBase *pPkt{nullptr};
251  bool fOwnPkt{true};
252 
253  // used by C4PacketList
254  C4IDPacket *pNext{nullptr};
255 
256 public:
257  C4PacketType getPktType() const { return eID; }
258  C4PacketBase *getPkt() const { return pPkt; }
259  const char *getPktName() const;
260 
261  void Clear();
262  void Default();
263  void Set(C4PacketType eType, C4PacketBase *pPkt);
264 
265  void CompileFunc(StdCompiler *pComp) override;
266 };
267 
268 // list of identified packets
270 {
271 public:
272  C4PacketList();
273  C4PacketList(const C4PacketList &List2);
274  ~C4PacketList() override;
275 
276 protected:
277  C4IDPacket *pFirst{nullptr}, *pLast{nullptr};
278 
279 public:
280  C4IDPacket *firstPkt() const { return pFirst; }
281  C4IDPacket *nextPkt(C4IDPacket *pPkt) const { return pPkt->pNext; }
282 
283  int32_t getPktCnt() const;
284 
285  void Add(C4IDPacket *pPkt);
286  void AddHead(C4IDPacket *pPkt);
287  void Add(C4PacketType eType, C4PacketBase *pPkt);
288  void AddHead(C4PacketType eType, C4PacketBase *pPkt);
289 
290  void Take(C4PacketList &List);
291  void Append(const C4PacketList &List);
292 
293  void Clear();
294  void Remove(C4IDPacket *pPkt);
295  void Delete(C4IDPacket *pPkt);
296 
297  void CompileFunc(StdCompiler *pComp) override;
298 };
299 #endif // INC_C4PacketBase
Definition: StdBuf.h:29
void CompileFunc(StdCompiler *pComp) const
Definition: C4PacketBase.h:51
C4IDPacket * pNext
Definition: C4PacketBase.h:254
C4PacketType getPktType() const
Definition: C4PacketBase.h:257
const char * Name
Definition: C4PacketBase.h:206
C4PacketType
Definition: C4PacketBase.h:76
C4PacketClass
Definition: C4PacketBase.h:182
C4PacketType ID
Definition: C4PacketBase.h:204
char * getMData()
Definition: StdBuf.h:443
C4PacketClass Class
Definition: C4PacketBase.h:205
bool isSerializer()
Definition: StdCompiler.h:54
friend class C4PacketList
Definition: C4PacketBase.h:25
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
size_t getSize() const
Definition: StdBuf.h:101
void Take(char *pnData)
Definition: StdBuf.h:457
virtual void CompileFunc(StdCompiler *pComp)=0
const C4PktHandlingData PktHandlingData[]
Definition: C4Packet2.cpp:50
size_t getSize() const
Definition: C4PacketBase.h:232
void Clear()
Definition: C4Packet2.cpp:318
StdCopyBuf Data
Definition: C4PacketBase.h:226
void Value(const T &rStruct)
Definition: StdCompiler.h:161
C4PacketHandlerID
Definition: C4PacketBase.h:189
C4NetFilenameAdapt(StdStrBuf &FileName)
Definition: C4PacketBase.h:50
virtual bool isDeserializer()
Definition: StdCompiler.h:53
const void * getData() const
Definition: StdBuf.h:99
const char * PacketNameByID(C4PacketType eID)
Definition: C4Packet2.cpp:136
C4NetFilenameAdapt mkNetFilenameAdapt(StdStrBuf &FileName)
Definition: C4PacketBase.h:73
virtual ~C4PacketBase()
C4IDPacket * firstPkt() const
Definition: C4PacketBase.h:280
const void * getData() const
Definition: C4PacketBase.h:233
C4NetIOPacket pack(const C4NetIO::addr_t &addr=C4NetIO::addr_t()) const
Definition: C4Packet2.cpp:150
C4PacketBase * getPkt() const
Definition: C4PacketBase.h:258
#define DirectorySeparator
bool operator==(const C4TimeMilliseconds &lhs, const C4TimeMilliseconds &rhs)
void Copy()
Definition: StdBuf.h:467
void unpack(const C4NetIOPacket &Pkt, char *pStatus=nullptr)
Definition: C4Packet2.cpp:160
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:348
C4IDPacket * nextPkt(C4IDPacket *pPkt) const
Definition: C4PacketBase.h:281
StdStrBuf & FileName
Definition: C4PacketBase.h:49