-
Notifications
You must be signed in to change notification settings - Fork 0
/
posix_network_channel.cc
135 lines (116 loc) · 3.54 KB
/
posix_network_channel.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Created by Gao Mingfei on 2021/5/23.
//
#include "posix_network_channel.h"
#include <sys/socket.h>
namespace Relay {
err_t PosixNetworkChannel::Start() {
if (init_) {
SPDLOG_ERROR("{} Already Started!", ep_.str_);
return RelayMultiCall;
}
// Only allow start once, so always set init_
std::lock_guard lck(init_mtx_);
init_ = true;
if (ep_.protocol_ == IPPROTO_TCP) {
sock_ = ::socket(AF_INET, SOCK_STREAM, ep_.protocol_);
} else if (ep_.protocol_ == IPPROTO_UDP) {
sock_ = ::socket(AF_INET, SOCK_DGRAM, ep_.protocol_);
} else {
assert(false);
}
if (ep_.dir_ == NetworkEndPoint::Dir::kDirListen) {
int opt_val = 1;
auto ret = ::setsockopt(sock_, SOL_SOCKET, SO_REUSEPORT, &opt_val,
sizeof(opt_val));
if (ret != 0) {
SPDLOG_ERROR("{} Reuse port error! errno: {}", ep_.str_, errno);
init_ret_ = RelayNetworkError;
return init_ret_;
}
ret = ::bind(sock_, (struct sockaddr *)&ep_.sock_addr_,
sizeof(ep_.sock_addr_));
if (ret != 0) {
SPDLOG_ERROR("{} Bind error! errno: {}", ep_.str_, errno);
init_ret_ = RelayNetworkError;
return init_ret_;
}
SPDLOG_INFO("{} Listen.....", ep_.str_);
::listen(sock_, 1);
struct sockaddr_in peer_addr;
socklen_t peer_addr_len = sizeof(peer_addr);
fd_ = ::accept(sock_, (struct sockaddr *)&peer_addr, &peer_addr_len);
if (fd_ < 0) {
SPDLOG_ERROR("{} Accept error! errno: {}", ep_.str_, errno);
init_ret_ = RelayNetworkError;
return init_ret_;
}
// Close listen socket
ret = ::close(sock_);
if (ret != 0) {
SPDLOG_ERROR("{} Close listen socket error! errno: {}", ep_.str_, errno);
init_ret_ = RelayNetworkError;
return init_ret_;
}
} else if (ep_.dir_ == NetworkEndPoint::Dir::kDirConnect) {
SPDLOG_INFO("{} Connect.....", ep_.str_);
auto ret = ::connect(sock_, (struct sockaddr *)&ep_.sock_addr_,
sizeof(ep_.sock_addr_));
if (ret != 0) {
SPDLOG_ERROR("{} Connect error! errno: {}", ep_.str_, errno);
init_ret_ = RelayNetworkError;
return init_ret_;
}
fd_ = sock_;
} else {
assert(false);
}
SPDLOG_INFO("{} Start!", ep_.str_);
init_ret_ = RelayOK;
return init_ret_;
}
err_t PosixNetworkChannel::Send(const void *buf, size_t len) {
if (!init_) {
SPDLOG_ERROR("{} Not Start yet!", ep_.str_);
return RelayNotInit;
}
while (len > 0) {
auto n = ::write(fd_, buf, len);
if (n < 0) {
SPDLOG_ERROR("{} Send error! errno: {}", ep_.str_, errno);
return RelayNetworkError;
}
SPDLOG_DEBUG("{} Send {} Bytes.", ep_.str_, n);
len -= n;
buf = (uint8_t *)buf + n;
}
return RelayOK;
}
err_t PosixNetworkChannel::Recv(void *buf, size_t buf_size, size_t *len) {
if (!init_) {
SPDLOG_ERROR("{} Not Start yet!", ep_.str_);
return RelayNotInit;
}
auto n = ::read(fd_, buf, buf_size);
if (n <= 0) {
// read return 0 means socket closed
// Ref:
// https://stackoverflow.com/questions/2416944/can-read-function-on-a-connected-socket-return-zero-bytes
if (n == 0) {
SPDLOG_WARN("{} Connection Closed by peer!", ep_.str_);
} else {
SPDLOG_ERROR("{} Recv error! errno: {}", ep_.str_, errno);
}
*len = 0;
return RelayNetworkError;
}
SPDLOG_DEBUG("{} Recv {} Bytes.", ep_.str_, n);
*len = n;
return RelayOK;
}
err_t PosixNetworkChannel::WaitReady() {
std::unique_lock lck(init_mtx_);
init_cv_.wait(lck, [this]() { return init_; });
return init_ret_;
}
} // namespace Relay