44#include " socket.hpp"
55#include " thread_pool.hpp"
66#include < future>
7+ #include < iostream>
78#include < optional>
89#include < variant>
910#include < vector>
@@ -18,11 +19,12 @@ namespace netlib {
1819 addrinfo* _endpoint_addr = nullptr ;
1920 netlib::thread_pool _thread_pool = netlib::thread_pool::create<1 ,1 >();
2021
21- inline std::pair<std::error_condition, std::chrono::milliseconds> wait_for_operation (socket_t sock, OperationClass op_class, std::chrono::milliseconds timeout) {
22+ static inline std::pair<std::error_condition, std::chrono::milliseconds> wait_for_operation (socket_t sock, OperationClass op_class, std::chrono::milliseconds timeout) {
2223 fd_set fdset;
2324 FD_ZERO (&fdset);
2425 FD_SET (sock, &fdset);
25- timeval tv{.tv_sec = timeout.count () / 1000 , .tv_usec =(timeout.count () % 1000 ) * 1000 };
26+ timeval tv{.tv_sec = timeout.count () / 1000 , .tv_usec =static_cast <int32_t >((timeout.count () % 1000 ) * 1000 )};
27+ std::cout << " tv.tv_sec = " << tv.tv_sec << " , usec=" << tv.tv_usec << std::endl;
2628 fd_set* fdset_ptr_read = ((op_class == OperationClass::read) || (op_class == OperationClass::both)) ? &fdset : nullptr ;
2729 fd_set* fdset_ptr_write = ((op_class == OperationClass::write) || (op_class == OperationClass::both)) ? &fdset : nullptr ;
2830 std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now ();
@@ -31,19 +33,29 @@ namespace netlib {
3133 std::chrono::milliseconds ms_taken = std::chrono::duration_cast<std::chrono::milliseconds>((end - start));
3234 if (select_res == 1 )
3335 {
34- int32_t so_error = 0 ;
35- socklen_t len = sizeof (so_error);
36- getsockopt (sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
37- if (so_error == 0 ) {
38- // success
39- return {{}, ms_taken};
40- }
41- return {static_cast <std::errc>(so_error), ms_taken};
36+ return {{}, ms_taken};
37+
38+
39+
40+
41+ // int32_t so_error = 0;
42+ // socklen_t len = sizeof(so_error);
43+ // getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
44+ // if (so_error == 0) {
45+ // //success
46+ // std::cout << "client select no error" << std::endl;
47+ // return {{}, ms_taken};
48+ // }
49+ // std::error_condition error_condition = static_cast<std::errc>(so_error);
50+ // std::cout << "client select ret error" << error_condition.message() << std::endl;
51+ // return {static_cast<std::errc>(so_error), ms_taken};
4252 } else if (select_res == 0 ) {
4353 // timeout
54+ std::cout << " select ret 0 timeout" << std::endl;
4455 return {std::errc::timed_out, ms_taken};
4556 } else {
4657 // error
58+ std::cout << " select ret -1" << socket_get_last_error ().message () << std::endl;
4759 return {socket_get_last_error (), ms_taken};
4860 }
4961 }
@@ -127,6 +139,8 @@ namespace netlib {
127139 }, data, timeout);
128140 }
129141 inline std::pair<std::size_t , std::error_condition> send (const std::vector<uint8_t > &data, std::optional<std::chrono::milliseconds> timeout) {
142+
143+ std::cout << " client send: " << data.size () << std::endl;
130144 if (!is_connected ()) {
131145 return {0 , std::errc::not_connected};
132146 }
@@ -161,22 +175,29 @@ namespace netlib {
161175 }
162176
163177 if (timeout.has_value () && timeout->count () < 0 ) {
178+ std::cout << " client recv timeout returned" << std::endl;
164179 return {{}, std::errc::timed_out};
165180 }
166181
167182 auto wait_res = wait_for_operation (_socket.value ().get_raw ().value (), OperationClass::read, timeout.value ());
168183 if (wait_res.first ) {
184+ std::cout << " client recv select timeout returned" << std::endl;
169185 return {{}, wait_res.first };
170186 }
171187
188+ std::cout << " client recv after wait" << std::endl;
189+
172190 std::vector<uint8_t > data (byte_count, 0 );
173191 ssize_t recv_res = ::recv (_socket.value ().get_raw ().value (), data.data (), byte_count, 0 );
174192 if (recv_res > 0 ) {
193+ std::cout << " client recv " << recv_res << std::endl;
175194 data.resize (recv_res);
176195 return {data, {}};
177196 } else if (recv_res == 0 ){
197+ std::cout << " client recv 0" << std::endl;
178198 return {{}, std::errc::connection_aborted};
179199 }
200+ std::cout << " client recv error: " << recv_res << " : " << socket_get_last_error ().message () << std::endl;
180201 return {{}, socket_get_last_error ()};
181202 }
182203
@@ -190,10 +211,12 @@ namespace netlib {
190211 if (_endpoint_addr) {
191212 freeaddrinfo (_endpoint_addr);
192213 }
214+
215+ std::cout << " client disconnected" << std::endl;
193216 return {};
194217 }
195218 inline bool is_connected () {
196- return _socket.has_value ();
219+ return _socket.has_value () && _socket-> is_valid () ;
197220 }
198221 [[nodiscard]] inline std::optional<netlib::socket> get_socket () const {
199222 return _socket;
0 commit comments