-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnkn_client_session.cpp
76 lines (64 loc) · 2.66 KB
/
nkn_client_session.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
//
// Created by yc chow on 2021/2/7.
//
#include "nkn_client_session.h"
nkn_client_session::nkn_client_session(std::shared_ptr<tcp::socket> sock, std::shared_ptr<smux_sess> sess)
: client_session(sock, sess) {
}
void nkn_client_session::run(uint service_id) {
TRACE
async_write_stream_metadata(0, service_id, false);
}
void nkn_client_session::async_write_stream_metadata(int port_id, int service_id, bool is_payment) {
TRACE
auto self = shared_from_this();
auto md = std::make_shared<pb::StreamMetadata>();
md->set_port_id(port_id);
md->set_service_id(service_id);
md->set_is_payment(is_payment);
size_t md_buf_len = md->ByteSizeLong();
char buff[md_buf_len];
md->SerializeToArray(buff, md_buf_len);
char len_buf[4];
encode32u(reinterpret_cast<byte *>(len_buf), md_buf_len);
//char *buf_with_len = static_cast<char *>(malloc(md_buf_len + 4));
memcpy(stream_metadata_, len_buf, 4);
memcpy(stream_metadata_ + 4, buff, md_buf_len);
sess_->async_write(stream_metadata_, md_buf_len + 4,
[this, self](std::error_code ec, std::size_t) {
if (ec) {
TRACE
destroy();
return;
}
client_session::run();
});
}
void nkn_client_session::async_write_service_metadata(int port_id, int service_id, bool is_payment) {
auto self = shared_from_this();
auto md = std::make_shared<pb::StreamMetadata>();
md->set_port_id(port_id);
md->set_service_id(service_id);
md->set_is_payment(is_payment);
size_t md_buf_len = md->ByteSizeLong();
char buff[md_buf_len];
md->SerializeToArray(buff, md_buf_len);
char len_buf[4];
encode32u(reinterpret_cast<byte *>(len_buf), md_buf_len);
char *buf_with_len = static_cast<char *>(malloc(md_buf_len + 4));
memcpy(buf_with_len, len_buf, 4);
memcpy(buf_with_len + 4, buff, md_buf_len);
sess_->async_write(buf_with_len, md_buf_len + 4,
[this, self, buf_with_len](std::error_code ec, std::size_t) {
free(buf_with_len);
sess_->async_read_some(buf2_, sizeof(buf2_),
[this, self](std::error_code ec, std::size_t len) {
//client_session::run();
});
});
}
void nkn_client_session::run_exit_reverse(uint service_id) {
//async_write_stream_metadata(0, service_id, false);
do_pipe1();
do_pipe2();
}