-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_session.cpp
More file actions
83 lines (71 loc) · 2.55 KB
/
server_session.cpp
File metadata and controls
83 lines (71 loc) · 2.55 KB
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
//
// Created by yc chow on 2021/3/11.
//
#include "server_session.h"
#include <utility>
static kvar session_kvar("server_session");
server_session::server_session(boost::asio::io_context &io_context, std::shared_ptr<smux_sess> sess,
tcp::endpoint local_endpoint) : context_(io_context), sess_(std::move(sess)),
sock_(io_context),
local_endpoint_(std::move(local_endpoint)),
kvar_(session_kvar) {
}
void server_session::run() {
auto self = shared_from_this();
sock_.async_connect(local_endpoint_, [this, self](std::error_code ec) {
if (ec) {
std::cout << ec.message() << std::endl;
return;
}
sess_->async_read_some(buf1_, 4, [this, self](std::error_code ec, std::size_t len) {
do_pipe1();
do_pipe2();
});
});
}
void server_session::do_pipe1() {
auto self = shared_from_this();
sock_.async_read_some(
boost::asio::buffer(buf1_, sizeof(buf1_)),
[this, self](std::error_code ec, std::size_t len) {
if (ec) {
destroy();
return;
}
sess_->async_write(
buf1_, len, [this, self](std::error_code ec, std::size_t len) {
if (ec) {
destroy();
return;
}
do_pipe1();
});
});
}
void server_session::do_pipe2() {
auto self = shared_from_this();
sess_->async_read_some(buf2_, sizeof(buf2_), [this,
self](std::error_code ec,
std::size_t len) {
if (ec) {
destroy();
return;
}
boost::asio::async_write(sock_, boost::asio::buffer(buf2_, len),
[this, self](std::error_code ec, std::size_t len) {
if (ec) {
destroy();
return;
}
do_pipe2();
});
});
}
void server_session::call_this_on_destroy() {
auto self = shared_from_this();
Destroy::call_this_on_destroy();
sock_.close();
if (sess_) {
sess_->destroy();
}
}