-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeer_channel.h
93 lines (70 loc) · 2.48 KB
/
peer_channel.h
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
#pragma once
#include "boost/asio.hpp"
#include <string>
#include <list>
#include <queue>
#include "session/sender.h"
#include "peer_dispatch.h"
using namespace boost::asio::ip;
#define KEEPALIVE_TIMEOUT 30000
//Represents a single peer connected to the server.
class ChannelMember : public std::enable_shared_from_this<ChannelMember> {
public:
explicit ChannelMember(std::shared_ptr<sender> sender,
std::string& name);
~ChannelMember();
bool connected() const { return connected_; }
int id() const { return id_; }
void set_disconnected() {
connected_ = false;
}
const std::string& name() const { return name_; }
std::shared_ptr<class sender> get_sender() {
return sender_.lock();
}
void Close();
void KeepAlive();
void OnTimeout(const boost::system::error_code& e);
void Send(std::shared_ptr<std::string> buffer);
protected:
int id_;
bool connected_;
boost::asio::steady_timer timer_;
std::string name_;
static unsigned int s_member_id_;
std::weak_ptr<class sender> sender_;
boost::asio::strand<
boost::asio::io_context::executor_type> strand_;
};
class PeerChannel {
public:
typedef std::list<std::shared_ptr<ChannelMember> > Members;
PeerChannel() {}
~PeerChannel() { DeleteAll(); }
std::shared_ptr<ChannelMember> Lookup(unsigned int id) const;
std::shared_ptr<ChannelMember> Lookup(std::shared_ptr<sender> sesion) const;
// Adds a new ChannelMember instance to the list of connected peers and
// associates it with the socket.
void AddMember(std::shared_ptr<sender> sender,
std::string name,
bool is_server);
void DeleteMember(std::shared_ptr<sender> sender);
// Cloes all connection and sends a "shutting down" message to all
// connected peers.
void CloseAll();
// Called when a socket was determined to be closing by the peer (or if the
// connection went dead).
void OnClosing(tcp::socket& ds);
void HandleKeepAlive(std::shared_ptr<sender> sesion);
void ForwardRequestToPeer(unsigned int peer_id,
std::shared_ptr<std::string> buffer);
void NotifyPeerClose(unsigned int peer_id);
protected:
void DeleteAll();
// Builds a simple list of "name,id\n" entries for each member.
std::shared_ptr<std::string> BuildResponseForNewMember(const std::shared_ptr<ChannelMember>& member, int server_id);
protected:
Members members_;
PeerDispatch peer_dispatch_;
std::recursive_mutex member_lock_;
};