@@ -343,15 +343,15 @@ enum class IntrRecvError {
343
343
* Sockets can be made non-blocking with SetSocketNonBlocking(const
344
344
* SOCKET&, bool).
345
345
*/
346
- static IntrRecvError InterruptibleRecv (uint8_t * data, size_t len, int timeout, const SOCKET & hSocket)
346
+ static IntrRecvError InterruptibleRecv (uint8_t * data, size_t len, int timeout, const Sock & hSocket)
347
347
{
348
348
int64_t curTime = GetTimeMillis ();
349
349
int64_t endTime = curTime + timeout;
350
350
// Maximum time to wait for I/O readiness. It will take up until this time
351
351
// (in millis) to break off in case of an interruption.
352
352
const int64_t maxWait = 1000 ;
353
353
while (len > 0 && curTime < endTime) {
354
- ssize_t ret = recv ( hSocket, ( char *) data, len, 0 ); // Optimistically try the recv first
354
+ ssize_t ret = hSocket. Recv ( data, len, 0 ); // Optimistically try the recv first
355
355
if (ret > 0 ) {
356
356
len -= ret;
357
357
data += ret;
@@ -360,25 +360,10 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
360
360
} else { // Other error or blocking
361
361
int nErr = WSAGetLastError ();
362
362
if (nErr == WSAEINPROGRESS || nErr == WSAEWOULDBLOCK || nErr == WSAEINVAL) {
363
- if (!IsSelectableSocket (hSocket)) {
364
- return IntrRecvError::NetworkError;
365
- }
366
363
// Only wait at most maxWait milliseconds at a time, unless
367
364
// we're approaching the end of the specified total timeout
368
365
int timeout_ms = std::min (endTime - curTime, maxWait);
369
- #ifdef USE_POLL
370
- struct pollfd pollfd = {};
371
- pollfd.fd = hSocket;
372
- pollfd.events = POLLIN;
373
- int nRet = poll (&pollfd, 1 , timeout_ms);
374
- #else
375
- struct timeval tval = MillisToTimeval (timeout_ms);
376
- fd_set fdset;
377
- FD_ZERO (&fdset);
378
- FD_SET (hSocket, &fdset);
379
- int nRet = select (hSocket + 1 , &fdset, nullptr , nullptr , &tval);
380
- #endif
381
- if (nRet == SOCKET_ERROR) {
366
+ if (!hSocket.Wait (std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
382
367
return IntrRecvError::NetworkError;
383
368
}
384
369
} else {
@@ -442,7 +427,7 @@ static std::string Socks5ErrorString(uint8_t err)
442
427
* @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
443
428
* Version 5</a>
444
429
*/
445
- static bool Socks5 (const std::string& strDest, int port, const ProxyCredentials * auth, const SOCKET & hSocket)
430
+ static bool Socks5 (const std::string& strDest, int port, const ProxyCredentials* auth, const Sock & hSocket)
446
431
{
447
432
IntrRecvError recvr;
448
433
LogPrint (BCLog::NET, " SOCKS5 connecting %s\n " , strDest);
@@ -460,7 +445,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
460
445
vSocks5Init.push_back (0x01 ); // 1 method identifier follows...
461
446
vSocks5Init.push_back (SOCKS5Method::NOAUTH);
462
447
}
463
- ssize_t ret = send ( hSocket, ( const char *) vSocks5Init.data (), vSocks5Init.size (), MSG_NOSIGNAL);
448
+ ssize_t ret = hSocket. Send ( vSocks5Init.data (), vSocks5Init.size (), MSG_NOSIGNAL);
464
449
if (ret != (ssize_t )vSocks5Init.size ()) {
465
450
return error (" Error sending to proxy" );
466
451
}
@@ -482,7 +467,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
482
467
vAuth.insert (vAuth.end (), auth->username .begin (), auth->username .end ());
483
468
vAuth.push_back (auth->password .size ());
484
469
vAuth.insert (vAuth.end (), auth->password .begin (), auth->password .end ());
485
- ret = send ( hSocket, ( const char *) vAuth.data (), vAuth.size (), MSG_NOSIGNAL);
470
+ ret = hSocket. Send ( vAuth.data (), vAuth.size (), MSG_NOSIGNAL);
486
471
if (ret != (ssize_t )vAuth.size ()) {
487
472
return error (" Error sending authentication to proxy" );
488
473
}
@@ -508,7 +493,7 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials
508
493
vSocks5.insert (vSocks5.end (), strDest.begin (), strDest.end ());
509
494
vSocks5.push_back ((port >> 8 ) & 0xFF );
510
495
vSocks5.push_back ((port >> 0 ) & 0xFF );
511
- ret = send ( hSocket, ( const char *) vSocks5.data (), vSocks5.size (), MSG_NOSIGNAL);
496
+ ret = hSocket. Send ( vSocks5.data (), vSocks5.size (), MSG_NOSIGNAL);
512
497
if (ret != (ssize_t )vSocks5.size ()) {
513
498
return error (" Error sending to proxy" );
514
499
}
@@ -787,10 +772,10 @@ bool IsProxy(const CNetAddr &addr) {
787
772
*
788
773
* @returns Whether or not the operation succeeded.
789
774
*/
790
- bool ConnectThroughProxy (const proxyType & proxy, const std::string& strDest, int port, const SOCKET & hSocket, int nTimeout, bool & outProxyConnectionFailed)
775
+ bool ConnectThroughProxy (const proxyType& proxy, const std::string& strDest, int port, const Sock & hSocket, int nTimeout, bool & outProxyConnectionFailed)
791
776
{
792
777
// first connect to proxy server
793
- if (!ConnectSocketDirectly (proxy.proxy , hSocket, nTimeout, true )) {
778
+ if (!ConnectSocketDirectly (proxy.proxy , hSocket. Get () , nTimeout, true )) {
794
779
outProxyConnectionFailed = true ;
795
780
return false ;
796
781
}
0 commit comments