-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelay_thread.cc
59 lines (50 loc) · 1.22 KB
/
relay_thread.cc
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
//
// Created by Gao Mingfei on 2021/5/23.
//
#include "relay_thread.h"
namespace Relay {
RelayThread::RelayThread(NetworkChannelPtr src_channel,
NetworkChannelPtr dst_channel)
: src_channel_(std::move(src_channel)),
dst_channel_(std::move(dst_channel)) {
buf_ = ::malloc(kBufSize);
assert(buf_);
thread_ = std::thread([this]() { ThreadFun(); });
}
void RelayThread::ThreadFun() {
size_t len;
err_t ret;
ret = src_channel_->Start();
if (ret != RelayOK) {
SPDLOG_ERROR("Channel Start error! Exit Relay thread!");
return;
}
ret = dst_channel_->WaitReady();
if (ret != RelayOK) {
SPDLOG_ERROR("Another Channel Start error! Exit Relay thread!");
return;
}
while (!exit_) {
ret = src_channel_->Recv(buf_, kBufSize, &len);
if (ret != RelayOK) {
SPDLOG_ERROR("Recv error! Exit Relay thread!");
return;
}
ret = dst_channel_->Send(buf_, len);
if (ret != RelayOK) {
SPDLOG_ERROR("Send error! Exit Relay thread!");
return;
}
}
}
err_t RelayThread::Stop() {
exit_ = true;
return RelayOK;
}
RelayThread::~RelayThread() {
auto ret = Stop();
assert(ret == RelayOK);
thread_.join();
::free(buf_);
}
} // namespace Relay