OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4PuncherPacket.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include "C4Include.h"
18 
20 
22  pComp->Value(mkNamingAdapt(v4, "IPv4", 0u));
23  pComp->Value(mkNamingAdapt(v6, "IPv6", 0u));
24 }
25 
26 std::unique_ptr<C4NetpuncherPacket> C4NetpuncherPacket::Construct(const C4NetIOPacket& rpack) {
27  if (!rpack.getPData()) return nullptr;
28  try {
29  switch (rpack.getStatus())
30  {
31  case PID_Puncher_AssID: return uptr(new C4NetpuncherPacketAssID(rpack));
32  case PID_Puncher_SReq: return uptr(new C4NetpuncherPacketSReq(rpack));
33  case PID_Puncher_CReq: return uptr(new C4NetpuncherPacketCReq(rpack));
34  default: return nullptr;
35  }
36  }
37  catch (StdCompiler::Exception *e) { delete e; return nullptr; }
38  catch (...) { return nullptr; }
39 }
41  C4NetIOPacket pkt;
42  pkt.SetAddr(addr);
43  StdBuf content(PackInto());
44  char type = GetType();
45  pkt.New(sizeof(type) + content.getSize());
46  pkt.Write(&type, sizeof(type));
47  pkt.Write(content, /*offset*/sizeof(type));
48  return pkt;
49 }
50 
52  C4Network2Address parse_addr;
53  CompileFromBuf<StdCompilerBinRead>(parse_addr, rpack.getPBuf());
54  if (parse_addr.getProtocol() != P_UDP) throw P_UDP;
55  addr = parse_addr.getAddr();
56 }
57 
58 StdBuf C4NetpuncherPacketCReq::PackInto() const {
59  C4Network2Address ser_addr;
60  ser_addr.SetAddr(addr);
61  ser_addr.SetProtocol(P_UDP);
62  return DecompileToBuf<StdCompilerBinWrite>(ser_addr);
63 }
64 
65 template<C4NetpuncherPacketType TYPE>
67  std::istringstream iss(std::string(rpack.getPData(), rpack.getPSize()));
68  iss >> id;
69 }
70 
71 template<C4NetpuncherPacketType TYPE>
73  std::ostringstream oss;
74  oss << GetID();
75  std::string s = oss.str();
76  return StdCopyBuf(s.c_str(), s.length());
77 }
C4NetIOPacket PackTo(const C4NetIO::addr_t &) const
Definition: StdBuf.h:29
StdBuf getPBuf() const
Definition: C4NetIO.h:320
static std::unique_ptr< C4NetpuncherPacket > Construct(const C4NetIOPacket &rpack)
size_t getSize() const
Definition: StdBuf.h:101
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void CompileFunc(StdCompiler *pComp)
void Value(const T &rStruct)
Definition: StdCompiler.h:161
void New(size_t inSize)
Definition: StdBuf.h:146
void SetProtocol(C4Network2IOProtocol enProtocol)
virtual C4NetpuncherPacketType GetType() const =0
void SetAddr(C4NetIO::addr_t naddr)
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:153
C4NetpuncherPacketID(const C4NetIOPacket &rpack)
std::unique_ptr< C4NetpuncherPacket > uptr
C4NetpuncherPacketCReq(const C4NetIOPacket &rpack)
uint8_t getStatus() const
Definition: C4NetIO.h:317
size_t getPSize() const
Definition: C4NetIO.h:319
const char * getPData() const
Definition: C4NetIO.h:318
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
#define s
virtual StdBuf PackInto() const =0