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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#pragma once
#include <erebos/network.h>
#include "channel.h"
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <netinet/in.h>
using std::condition_variable;
using std::monostate;
using std::mutex;
using std::optional;
using std::shared_ptr;
using std::string;
using std::thread;
using std::unique_ptr;
using std::variant;
using std::vector;
using std::tuple;
using std::weak_ptr;
using std::enable_shared_from_this;
namespace chrono = std::chrono;
using chrono::steady_clock;
namespace erebos {
class ReplyBuilder;
struct WaitingRef;
struct Server::Peer
{
Peer(const Peer &) = delete;
Peer & operator=(const Peer &) = delete;
Priv & server;
const sockaddr_in addr;
variant<monostate,
shared_ptr<struct WaitingRef>,
Identity> identity;
variant<monostate,
Stored<ChannelRequest>,
shared_ptr<struct WaitingRef>,
Stored<ChannelAccept>,
unique_ptr<Channel>> channel;
Storage tempStorage;
PartialStorage partStorage;
vector<tuple<UUID, shared_ptr<WaitingRef>>> serviceQueue {};
vector<vector<uint8_t>> secureOutQueue {};
shared_ptr<erebos::Peer::Priv> lpeer = nullptr;
void send(const struct TransportHeader &, const vector<Object> &, bool secure);
void updateIdentity(ReplyBuilder &);
void updateChannel(ReplyBuilder &);
void updateService(ReplyBuilder &);
void trySendOutQueue();
};
struct Peer::Priv : enable_shared_from_this<Peer::Priv>
{
weak_ptr<Server::Peer> speer;
weak_ptr<PeerList::Priv> list;
size_t listIndex;
void notifyWatchers();
};
struct PeerList::Priv : enable_shared_from_this<PeerList::Priv>
{
mutex dataMutex;
vector<shared_ptr<Peer::Priv>> peers;
vector<function<void(size_t, const Peer *)>> watchers;
void push(const shared_ptr<Server::Peer> &);
};
struct TransportHeader
{
enum class Type {
Acknowledged,
DataRequest,
DataResponse,
AnnounceSelf,
AnnounceUpdate,
ChannelRequest,
ChannelAccept,
ServiceType,
ServiceRef,
};
struct Item {
const Type type;
const variant<PartialRef, UUID> value;
};
TransportHeader(const vector<Item> & items): items(items) {}
static optional<TransportHeader> load(const PartialRef &);
static optional<TransportHeader> load(const PartialObject &);
PartialObject toObject() const;
const vector<Item> items;
};
class ReplyBuilder
{
public:
void header(TransportHeader::Item &&);
void body(const Ref &);
const vector<TransportHeader::Item> & header() const { return mheader; }
vector<Object> body() const;
private:
vector<TransportHeader::Item> mheader;
vector<Ref> mbody;
};
struct WaitingRef
{
const Storage storage;
const PartialRef ref;
const Server::Peer & peer;
vector<Digest> missing;
optional<Ref> check(ReplyBuilder &);
};
struct Server::Priv
{
Priv(const Head<LocalState> & local, const Identity & self,
vector<unique_ptr<Service>> && svcs);
~Priv();
shared_ptr<Priv> getptr();
void doListen();
void doAnnounce();
bool isSelfAddress(const sockaddr_in & paddr);
Peer & getPeer(const sockaddr_in & paddr);
void handlePacket(Peer &, const TransportHeader &, ReplyBuilder &);
void handleLocalHeadChange(const Head<LocalState> &);
constexpr static uint16_t discoveryPort { 29665 };
constexpr static chrono::seconds announceInterval { 60 };
mutex dataMutex;
condition_variable announceCondvar;
bool finish = false;
Identity self;
Bhv<LocalState> localState;
WatchedHead<LocalState> localHead;
thread threadListen;
thread threadAnnounce;
vector<shared_ptr<Peer>> peers;
PeerList plist;
vector<struct TransportHeader> outgoing;
vector<weak_ptr<WaitingRef>> waiting;
int sock;
vector<in_addr> localAddresses;
vector<in_addr> bcastAddresses;
// Start destruction with finalizing services
vector<unique_ptr<Service>> services;
};
}
|