1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2011-2017, The OpenClonk Team and contributors
6 * Copyright (c) 2017-2020, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18#pragma once
19
20#include "C4NetIO.h"
21
22const int C4NetMaxDiscover = 64;
23
24const StdStrBuf C4NetDiscoveryAddress{"ff02::1"};
25
26class C4Network2IODiscover : public C4NetIOSimpleUDP, private C4NetIO::CBClass
27{
28public:
29 C4Network2IODiscover(std::uint16_t iRefServerPort) : iRefServerPort(iRefServerPort), fEnabled(false)
30 {
31 C4NetIOSimpleUDP::SetCallback(this);
32 }
33
34protected:
35 // callbacks (will handle everything here)
36 virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override;
37
38public:
39 bool Init(uint16_t iPort = C4NetIO::addr_t::IPPORT_NONE) override;
40 void SetDiscoverable(bool fnEnabled) { fEnabled = fnEnabled; }
41 bool Announce();
42
43private:
44 C4NetIO::addr_t DiscoveryAddr;
45
46 std::uint16_t iRefServerPort;
47 bool fEnabled;
48};
49
50class C4Network2IODiscoverClient : public C4NetIOSimpleUDP, private C4NetIO::CBClass
51{
52public:
53 C4Network2IODiscoverClient() : iDiscoverCount(0)
54 {
55 C4NetIOSimpleUDP::SetCallback(this);
56 }
57
58protected:
59 // callbacks (will handle everything here)
60 virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override;
61
62public:
63 int getDiscoverCount() const { return iDiscoverCount; }
64
65 void Clear() { iDiscoverCount = 0; }
66 bool Init(uint16_t iPort = C4NetIO::addr_t::IPPORT_NONE) override;
67 bool StartDiscovery();
68 bool PopDiscover(C4NetIO::addr_t &Discover);
69
70private:
71 C4NetIO::addr_t DiscoveryAddr;
72
73 int iDiscoverCount;
74 C4NetIO::addr_t Discovers[C4NetMaxDiscover];
75};
76