forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_tcp_nat_traversal_client.h
110 lines (87 loc) · 2.99 KB
/
sf_tcp_nat_traversal_client.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_tcp_nat_traversal_client.h
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#pragma once
#include "sf_nocopy.h"
#include "sf_object.hpp"
#include "sf_tcp_client.hpp"
#include "sf_tcp_server.hpp"
#include "sf_serialize_binary.hpp"
#include "sf_define.h"
#include "sf_tcp_nat_traversal_utils.hpp"
#include "sf_random.hpp"
#include <climits>
#include <zconf.h>
namespace skyfire {
/**
* @brief p2p客户端连接上下文
*/
struct sf_p2p_connect_context_t__ {
sf_tcp_nat_traversal_context_t__ tcp_nat_traversal_context;
std::shared_ptr<sf_tcp_client> point_a_client_1;
std::shared_ptr<sf_tcp_client> point_a_client_2;
std::shared_ptr<sf_tcp_server> point_a_server;
std::shared_ptr<sf_tcp_client> point_b_client_1;
std::shared_ptr<sf_tcp_client> point_b_client_2;
std::shared_ptr<sf_tcp_server> point_b_server;
};
/**
* @brief nat穿透客户端类
*/
class sf_tcp_nat_traversal_client : public sf_nocopy<sf_object> {
// 新连接,返回建立连接成功的tcpserver和已经连接上的socket,可通过这两个与被连接方通信
SF_REG_SIGNAL(new_nat_connection, std::shared_ptr<sf_tcp_nat_traversal_connection>, int)
private:
std::shared_ptr<sf_tcp_client> client__{sf_tcp_client::make_client()};
std::set<unsigned long long> client_list__;
unsigned long long self_id__;
std::map<int, sf_p2p_connect_context_t__> connect_context_map__;
sf_addr_info_t server_addr__;
void on_new_connect_required__(sf_tcp_nat_traversal_context_t__ &context);
void on_server_reply_b_addr(sf_tcp_nat_traversal_context_t__ &context);
void on_client_data_coming__(const sf_pkg_header_t &header, const byte_array &data);
void on_client_close__();
sf_tcp_nat_traversal_client();
public:
/**
* 生成客户端
* @return
*/
static std::shared_ptr<sf_tcp_nat_traversal_client> make_client();
/**
* 获取id
* @return id
*/
unsigned long long int get_id();
/**
* 连接到穿透服务器,连接成功后会注册
* @param ip 穿透服务器ip
* @param port 穿透服务器端口
* @return 连接结果
*/
bool connect_to_server(const std::string &ip, unsigned short port);
/**
* 获取终端集合
* @return 终端集合
*/
std::set<unsigned long long> get_clients();
/**
* 来源,连接远程peer端
* @param peer_id 远端id
* @param raw 是否为原始数据格式
* @return -1表示失败,其他表示此次连接的id
*/
int connect_to_peer(unsigned long long peer_id, bool raw);
/**
* 关闭
*/
void close();
};
}