-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_channel.cpp
263 lines (212 loc) · 6.9 KB
/
peer_channel.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "peer_channel.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <algorithm>
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include <boost/bind.hpp>
namespace pt = boost::property_tree;
// Set to the peer id of the originator when messages are being
// exchanged between peers, but set to the id of the receiving peer
// itself when notifications are sent from the server about the state
// of other peers.
const size_t kMaxNameLength = 512;
//
// ChannelMember
//
unsigned int ChannelMember::s_member_id_ = 0;
ChannelMember::ChannelMember(std::shared_ptr<class sender> sender,
std::string& name)
: id_(++s_member_id_),
sender_(sender),
connected_(true),
timer_(sender->get_context()),
strand_(timer_.get_executor()) {
name_ = name;
if (name_.empty())
name_ = "peer_" + std::to_string(id_);
else if (name_.length() > kMaxNameLength)
name_.resize(kMaxNameLength);
printf("%s connected %d\n", __func__, connected());
std::replace(name_.begin(), name_.end(), ',', '_');
}
ChannelMember::~ChannelMember()
{
Close();
printf("%s\n", __func__);
}
void ChannelMember::Close() {
if (connected()) {
set_disconnected();
timer_.cancel();
printf("%s channelmember id %d \n", __func__, id());
if (auto s = sender_.lock())
s->close();
}
}
void ChannelMember::Send(std::shared_ptr<std::string> buffer) {
if (auto s = sender_.lock())
s->send(buffer);
}
void ChannelMember::KeepAlive() {
pt::ptree tree;
std::ostringstream oss;
tree.put("signal", "keep-alive");
write_json(oss, tree);
auto msg = std::make_shared<std::string>(oss.str());
Send(msg);
timer_.expires_after(std::chrono::milliseconds(KEEPALIVE_TIMEOUT));
OnTimeout({});
}
void ChannelMember::OnTimeout(const boost::system::error_code& ec) {
if (ec == boost::asio::error::operation_aborted)
return;
if(ec && ec != boost::asio::error::operation_aborted) {
printf("%s timer error: %s code %d \n", __func__, ec.message().c_str(), ec.value());
Close();
return;
}
// Verify that the timer really expired since the deadline may have moved.
if(timer_.expiry() <= std::chrono::steady_clock::now())
{
// Closing the socket cancels all outstanding operations. They
// will complete with boost::asio::error::operation_aborted
printf("%s timer out \n", __func__);
Close();
return;
}
// Wait on the timer
timer_.async_wait(
boost::asio::bind_executor(
strand_, boost::bind(&ChannelMember::OnTimeout,
shared_from_this(),
boost::asio::placeholders::error)));
}
//
//PeerChannel
//
std::shared_ptr<ChannelMember> PeerChannel::Lookup(unsigned int id) const {
Members::const_iterator iter = members_.begin();
for (; iter != members_.end(); ++iter) {
if (id == (*iter)->id()) {
return *iter;
}
}
return NULL;
}
std::shared_ptr<ChannelMember> PeerChannel::Lookup(std::shared_ptr<sender> sender) const {
for (auto i : members_) {
if (i->get_sender() == sender) {
return i;
}
}
return NULL;
}
void PeerChannel::ForwardRequestToPeer(unsigned int peer_id,
std::shared_ptr<std::string> buffer) {
std::istringstream iss(*buffer.get());
boost::property_tree::ptree tree;
boost::property_tree::read_json(iss, tree);
std::unique_lock<std::recursive_mutex> lock(member_lock_);
auto peer = Lookup(peer_id);
int id = 0;
if (!peer) {
printf("%s Can not found peer. id %u\n", __func__, peer_id);
return;
}
id = peer_dispatch_.GetPeer((int)peer_id);
if (id) {
tree.erase("peer_id");
tree.put("peer_id", id);
std::ostringstream oss;
pt::write_json(oss, tree);
auto response = std::make_shared<std::string>(oss.str());
peer->Send(response);
} else {
printf("%s Can not found id in Map. id %u\n", __func__, peer_id);
}
}
void PeerChannel::AddMember(std::shared_ptr<sender> sender,
std::string name,
bool is_server) {
auto new_guy = std::shared_ptr<ChannelMember> (new ChannelMember(sender, name));
int server_id = 0;
std::unique_lock<std::recursive_mutex> lock(member_lock_);
members_.push_back(new_guy);
if (is_server) {
peer_dispatch_.AddServer(new_guy->id());
} else {
peer_dispatch_.AddClient(new_guy->id());
server_id = peer_dispatch_.Dispatch(new_guy->id());
}
printf("New member added (total=%u): %s %d\n",
members_.size(), new_guy->name().c_str(), new_guy->id());
HandleKeepAlive(sender);
// Let the newly connected peer know about other members of the channel.
auto response = BuildResponseForNewMember(new_guy, server_id);
new_guy->Send(response);
}
void PeerChannel::DeleteMember(std::shared_ptr<sender> sender) {
std::unique_lock<std::recursive_mutex> lock(member_lock_);
auto member = Lookup(sender);
if (member) {
member->Close();
for (Members::iterator i = members_.begin(); i != members_.end(); ++i) {
if (member == *i) {
members_.erase(i);
//In the relay mode, must notify the server that it is also disconnected.
//Because coturn is disconnected from the client, it will not disconnect
//the server at the same time.
if (!peer_dispatch_.IsServer(member->id()))
if (auto server_id = peer_dispatch_.GetPeer(member->id()))
NotifyPeerClose(server_id);
peer_dispatch_.DeleteMember(member->id());
break;
}
}
}
printf("%s use %d\n", __func__, member.use_count());
}
void PeerChannel::HandleKeepAlive(std::shared_ptr<sender> sender) {
std::unique_lock<std::recursive_mutex> lock(member_lock_);
if (auto member = Lookup(sender))
member->KeepAlive();
}
void PeerChannel::NotifyPeerClose(unsigned int peer_id) {
if (auto member = Lookup(peer_id)) {
boost::property_tree::ptree tree;
tree.put("signal", "disconnect");
std::ostringstream oss;
pt::write_json(oss, tree);
auto response = std::make_shared<std::string>(oss.str());
member->Send(response);
}
}
void PeerChannel::CloseAll() {
std::unique_lock<std::recursive_mutex> lock(member_lock_);
Members::const_iterator i = members_.begin();
for (; i != members_.end(); ++i) {
//TO DO
}
DeleteAll();
}
void PeerChannel::DeleteAll() {
std::unique_lock<std::recursive_mutex> lock(member_lock_);
members_.clear();
}
std::shared_ptr<std::string> PeerChannel::BuildResponseForNewMember(
const std::shared_ptr<ChannelMember>& member,
int server_id) {
pt::ptree tree;
pt::ptree children;
std::ostringstream oss;
tree.put("signal", "success");
tree.put("my_id", member->id());
if (server_id)
tree.put("peer_id", server_id);
pt::write_json(oss, tree);
auto response = std::make_shared<std::string> (oss.str());
return response;
}