forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_tcp_nat_traversal_utils.hpp
81 lines (71 loc) · 2.94 KB
/
sf_tcp_nat_traversal_utils.hpp
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
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_tcp_nat_traversal_utils.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#pragma once
#include "sf_tcp_nat_traversal_utils.h"
namespace skyfire {
inline bool sf_tcp_nat_traversal_connection::send(const byte_array &data) {
if (type__ == sf_tcp_nat_traversal_connection_type::type_client_valid) {
return client__->send(data);
}
return server__->send(sock__, data);
}
inline bool sf_tcp_nat_traversal_connection::send(int type, const byte_array &data) {
if (type__ == sf_tcp_nat_traversal_connection_type::type_client_valid) {
return client__->send(type, data);
}
return server__->send(sock__, type, data);
}
inline sf_tcp_nat_traversal_connection::sf_tcp_nat_traversal_connection(std::shared_ptr <sf_tcp_client> client,
std::shared_ptr <sf_tcp_server> server, int sock,
int connect_id,
sf_tcp_nat_traversal_connection_type type) :
client__(std::move(client)),
server__(std::move(server)),
sock__(sock),
connect_id__(connect_id),
type__(type) {
if (type__ == sf_tcp_nat_traversal_connection_type::type_client_valid) {
sf_bind_signal(client__,
data_coming,
[=](const sf_pkg_header_t &header, const byte_array &data) {
data_coming(header, data);
}, true);
sf_bind_signal(client__,
raw_data_coming,
[=](const byte_array &data) {
raw_data_coming(data);
}, true);
sf_bind_signal(client__,
closed,
[=]() {
closed();
},
true);
} else {
sf_bind_signal(server__,
data_coming,
[=](SOCKET, const sf_pkg_header_t &header, const byte_array &data) {
data_coming(header, data);
}, true);
sf_bind_signal(server__,
raw_data_coming,
[=](SOCKET, const byte_array &data) {
raw_data_coming(data);
}, true);
sf_bind_signal(server__,
closed,
[=](SOCKET) {
closed();
},
true);
}
}
}