forked from lpcvoid/cpp-net-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.hpp
More file actions
228 lines (204 loc) · 10.2 KB
/
Copy pathclient.hpp
File metadata and controls
228 lines (204 loc) · 10.2 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include "endpoint_accessor.hpp"
#include "service_resolver.hpp"
#include "socket.hpp"
#include "thread_pool.hpp"
#include <future>
#include <iostream>
#include <optional>
#include <variant>
#include <vector>
namespace netlib {
using namespace std::chrono_literals;
class client {
protected:
std::optional<netlib::socket> _socket;
addrinfo* _endpoint_addr = nullptr;
netlib::thread_pool _thread_pool = netlib::thread_pool::create<1,1>();
static inline std::pair<std::error_condition, std::chrono::milliseconds> wait_for_operation(socket_t sock, OperationClass op_class, std::chrono::milliseconds timeout) {
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(sock, &fdset);
timeval tv{.tv_sec = timeout.count() / 1000, .tv_usec=static_cast<int32_t>((timeout.count() % 1000) * 1000)};
std::cout << "tv.tv_sec = " << tv.tv_sec << ", usec=" << tv.tv_usec << std::endl;
fd_set* fdset_ptr_read = ((op_class == OperationClass::read) || (op_class == OperationClass::both)) ? &fdset : nullptr;
fd_set* fdset_ptr_write = ((op_class == OperationClass::write) || (op_class == OperationClass::both)) ? &fdset : nullptr;
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
int32_t select_res = select(sock + 1, fdset_ptr_read, fdset_ptr_write, nullptr, &tv);
std::chrono::time_point<std::chrono::steady_clock> end = std::chrono::steady_clock::now();
std::chrono::milliseconds ms_taken = std::chrono::duration_cast<std::chrono::milliseconds>((end - start));
if (select_res == 1)
{
return {{}, ms_taken};
// int32_t so_error = 0;
// socklen_t len = sizeof(so_error);
// getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
// if (so_error == 0) {
// //success
// std::cout << "client select no error" << std::endl;
// return {{}, ms_taken};
// }
// std::error_condition error_condition = static_cast<std::errc>(so_error);
// std::cout << "client select ret error" << error_condition.message() << std::endl;
// return {static_cast<std::errc>(so_error), ms_taken};
} else if (select_res == 0) {
//timeout
std::cout << "select ret 0 timeout" << std::endl;
return {std::errc::timed_out, ms_taken};
} else {
//error
std::cout << "select ret -1" << socket_get_last_error().message() << std::endl;
return {socket_get_last_error(), ms_taken};
}
}
public:
client() = default;
client(netlib::socket sock, addrinfo* endpoint){
_socket = sock;
_endpoint_addr = endpoint;
}
virtual ~client() {
disconnect();
}
inline std::error_condition connect(const std::string& host,
const std::variant<std::string, uint16_t>& service,
AddressFamily address_family,
AddressProtocol address_protocol,
std::chrono::milliseconds timeout) {
if (is_connected()) {
auto ec = disconnect();
if (ec && ec != std::errc::not_connected) {
return ec;
}
}
const std::string service_string = std::holds_alternative<uint16_t>(service) ? std::to_string(std::get<uint16_t>(service)) : std::get<std::string>(service);
std::pair<addrinfo*, std::error_condition> addrinfo_result = service_resolver::get_addrinfo(host, service_string, address_family, address_protocol, AI_ADDRCONFIG);
if (addrinfo_result.first == nullptr) {
return addrinfo_result.second;
}
std::error_condition global_connect_error {};
for (addrinfo* res_addrinfo = addrinfo_result.first; res_addrinfo != nullptr; res_addrinfo = res_addrinfo->ai_next) {
auto sock = netlib::socket();
std::error_condition s_create_error = sock.create(res_addrinfo->ai_family, res_addrinfo->ai_socktype, res_addrinfo->ai_protocol);
if (s_create_error) {
freeaddrinfo(addrinfo_result.first);
return s_create_error; //we already failed creating the socket, oof
}
sock.set_nonblocking(true);
int32_t connect_result = ::connect(sock.get_raw().value(), res_addrinfo->ai_addr, res_addrinfo->ai_addrlen);
if (connect_result == -1) {
std::error_condition error_condition = socket_get_last_error();
if (error_condition != std::errc::operation_in_progress) {
freeaddrinfo(addrinfo_result.first);
return error_condition;
}
}
auto connect_error = wait_for_operation(sock.get_raw().value(), OperationClass::write, timeout);
if (connect_error.first) {
sock.close();
global_connect_error = connect_error.first;
continue;
}
_endpoint_addr = res_addrinfo;
_socket = sock;
break;
}
if (_socket) {
return {};
}
return global_connect_error;
}
inline std::future<std::error_condition> connect_async(const std::string& host,
const std::variant<std::string, uint16_t>& service,
AddressFamily address_family,
AddressProtocol address_protocol,
std::chrono::milliseconds timeout = 1000ms) {
return _thread_pool.add_task([&](const std::string &host,
const std::variant<std::string, uint16_t>& service,
netlib::AddressFamily address_family,
netlib::AddressProtocol address_protocol,
std::chrono::milliseconds timeout){
return this->connect(host, service, address_family, address_protocol, timeout);
}, host, service, address_family, address_protocol, timeout);
}
inline std::future<std::pair<std::size_t, std::error_condition>> send_async(const std::vector<uint8_t> &data, std::optional<std::chrono::milliseconds> timeout) {
return _thread_pool.add_task([&](const std::vector<uint8_t> &data, std::optional<std::chrono::milliseconds> timeout){
return this->send(data, timeout);
}, data, timeout);
}
inline std::pair<std::size_t, std::error_condition> send(const std::vector<uint8_t> &data, std::optional<std::chrono::milliseconds> timeout) {
std::cout << "client send: " << data.size() << std::endl;
if (!is_connected()) {
return {0, std::errc::not_connected};
}
if (timeout.has_value() && timeout->count() < 0) {
return {0, std::errc::timed_out};
}
auto wait_res = wait_for_operation(_socket.value().get_raw().value(), OperationClass::write, timeout.value());
if (wait_res.first) {
return {0, wait_res.first};
}
ssize_t send_res = ::send(_socket.value().get_raw().value(), data.data(), data.size(), 0);
if (send_res >= 0) {
if (send_res == data.size()) {
return {send_res, {}};
}
return {send_res, std::errc::message_size};
}
return {0, socket_get_last_error()};
}
inline std::future<std::pair<std::vector<uint8_t>, std::error_condition>> recv_async(std::size_t byte_count, std::optional<std::chrono::milliseconds> timeout) {
return _thread_pool.add_task([&](std::size_t byte_count, std::optional<std::chrono::milliseconds> timeout){
return this->recv(byte_count, timeout);
}, byte_count, timeout);
}
inline std::pair<std::vector<uint8_t>, std::error_condition> recv(std::size_t byte_count, std::optional<std::chrono::milliseconds> timeout) {
if (!is_connected()) {
return {{}, std::errc::not_connected};
}
if (timeout.has_value() && timeout->count() < 0) {
std::cout << "client recv timeout returned" << std::endl;
return {{}, std::errc::timed_out};
}
auto wait_res = wait_for_operation(_socket.value().get_raw().value(), OperationClass::read, timeout.value());
if (wait_res.first) {
std::cout << "client recv select timeout returned" << std::endl;
return {{}, wait_res.first};
}
std::cout << "client recv after wait" << std::endl;
std::vector<uint8_t> data(byte_count, 0);
ssize_t recv_res = ::recv(_socket.value().get_raw().value(), data.data(), byte_count, 0);
if (recv_res > 0) {
std::cout << "client recv " << recv_res << std::endl;
data.resize(recv_res);
return {data, {}};
} else if (recv_res == 0){
std::cout << "client recv 0" << std::endl;
return {{}, std::errc::connection_aborted};
}
std::cout << "client recv error: " << recv_res << " : " << socket_get_last_error().message() << std::endl;
return {{}, socket_get_last_error()};
}
inline std::error_condition disconnect() {
if (!_socket.has_value()){
return std::errc::not_connected;
}
_socket->close();
_socket.reset();
if (_endpoint_addr) {
freeaddrinfo(_endpoint_addr);
}
std::cout << "client disconnected" << std::endl;
return {};
}
inline bool is_connected() {
return _socket.has_value() && _socket->is_valid();
}
[[nodiscard]] inline std::optional<netlib::socket> get_socket() const {
return _socket;
}
inline std::optional<std::string> get_ip_addr() {
return endpoint_accessor::ip_to_string(_endpoint_addr);
}
};
}// namespace netlib