blob: ad29496d1e5c2399c2bea6f9467c78cf684988f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#pragma once
#include <erebos/network.h>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <netinet/in.h>
using std::condition_variable;
using std::mutex;
using std::optional;
using std::string;
using std::thread;
using std::unique_ptr;
using std::variant;
using std::vector;
namespace chrono = std::chrono;
using chrono::steady_clock;
namespace erebos {
struct Peer
{
const int sock;
const sockaddr_in addr;
void send(const struct TransportHeader &, const vector<Object> &);
};
struct TransportHeader
{
enum class Type {
Acknowledged,
DataRequest,
DataResponse,
AnnounceSelf,
AnnounceUpdate,
ChannelRequest,
ChannelAccept,
ServiceType,
ServiceRef,
};
struct Item {
const Type type;
const variant<Ref, string> value;
};
TransportHeader(const vector<Item> & items): items(items) {}
static optional<TransportHeader> load(const Ref &);
static optional<TransportHeader> load(const Object &);
Object toObject() const;
Ref store(const Storage & st) const;
const vector<Item> items;
};
struct Server::Priv
{
Priv(const Identity & self);
~Priv();
void doListen();
void doAnnounce();
Peer & getPeer(const sockaddr_in & paddr);
void handlePacket(Peer &, const TransportHeader &);
constexpr static uint16_t discoveryPort { 29665 };
constexpr static chrono::seconds announceInterval { 60 };
mutex dataMutex;
condition_variable announceCondvar;
bool finish = false;
Identity self;
thread threadListen;
thread threadAnnounce;
vector<unique_ptr<Peer>> peers;
vector<struct TransportHeader> outgoing;
int sock;
vector<in_addr> bcastAddresses;
};
}
|