1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 2016-2017, The OpenClonk Team and contributors
5 * Copyright (c) 2019-2020, The LegacyClonk 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
17#pragma once
18
19/* I'm cooking up a seperate solution for the netpuncher because using C4Packet2.cpp would require introducing half a ton of stubs, and it seems like overkill */
20
21#include "C4NetIO.h"
22#include <memory>
23
24enum C4NetpuncherPacketType : char
25{
26 PID_Puncher_AssID = 0x51, // Puncher announcing ID to host
27 PID_Puncher_SReq = 0x52, // Client requesting to be served with punching (for an ID)
28 PID_Puncher_CReq = 0x53, // Puncher requesting clients to punch (towards an address)
29 PID_Puncher_IDReq = 0x54, // Host requesting an ID
30 // Extend this with exchanging ICE parameters, some day?
31};
32
33struct C4NetpuncherID
34{
35 using value = std::uint32_t;
36
37 value v4 = 0, v6 = 0;
38
39 void CompileFunc(StdCompiler *comp);
40 bool operator==(const C4NetpuncherID &other) const { return v4 == other.v4 && v6 == other.v6; }
41};
42
43class C4NetpuncherPacket
44{
45public:
46 using uptr = std::unique_ptr<C4NetpuncherPacket>;
47 static uptr Construct(const C4NetIOPacket &pkt);
48 virtual ~C4NetpuncherPacket() = default;
49 virtual C4NetpuncherPacketType GetType() const = 0;
50 C4NetIOPacket PackTo(const C4NetIO::addr_t &) const;
51
52protected:
53 virtual StdBuf PackInto() const = 0;
54 using CID = C4NetpuncherID::value;
55};
56
57class C4NetpuncherPacketIDReq : public C4NetpuncherPacket
58{
59public:
60 C4NetpuncherPacketIDReq() = default;
61 C4NetpuncherPacketIDReq(const C4NetIOPacket &pkt) {}
62 C4NetpuncherPacketType GetType() const override final { return PID_Puncher_IDReq; }
63
64private:
65 StdBuf PackInto() const override { return StdBuf{}; }
66};
67
68template<C4NetpuncherPacketType Type>
69class C4NetpuncherPacketID : public C4NetpuncherPacket
70{
71public:
72 C4NetpuncherPacketType GetType() const override final { return Type; }
73 CID GetID() const { return id; }
74 virtual ~C4NetpuncherPacketID() = default;
75
76protected:
77 C4NetpuncherPacketID(const C4NetIOPacket &pkt);
78 C4NetpuncherPacketID(const CID id) : id{id} {}
79
80private:
81 CID id;
82 StdBuf PackInto() const override;
83};
84
85#pragma push_macro("PIDC")
86#undef PIDC
87#define PIDC(n) \
88 class C4NetpuncherPacket##n : public C4NetpuncherPacketID<PID_Puncher_##n> \
89 { \
90 public: \
91 explicit C4NetpuncherPacket##n(const C4NetIOPacket &p) : C4NetpuncherPacketID<PID_Puncher_##n>{p} {} \
92 explicit C4NetpuncherPacket##n(const CID id) : C4NetpuncherPacketID<PID_Puncher_##n>{id} {} \
93 }
94PIDC(AssID); PIDC(SReq);
95#pragma pop_macro("PIDC")
96
97class C4NetpuncherPacketCReq : public C4NetpuncherPacket
98{
99public:
100 explicit C4NetpuncherPacketCReq(const C4NetIOPacket &pkt);
101 explicit C4NetpuncherPacketCReq(const C4NetIO::addr_t &addr) : addr{addr} {}
102 C4NetpuncherPacketType GetType() const override final { return PID_Puncher_CReq; }
103 const C4NetIO::addr_t &GetAddr() { return addr; }
104
105private:
106 C4NetIO::addr_t addr;
107 StdBuf PackInto() const override;
108};
109