@@ -506,7 +506,16 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
506
506
if (!addr_bind.IsValid ()) {
507
507
addr_bind = GetBindAddress (sock->Get ());
508
508
}
509
- CNode* pnode = new CNode (id, nLocalServices, sock->Release (), addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type, /* inbound_onion */ false );
509
+ CNode* pnode = new CNode (id,
510
+ nLocalServices,
511
+ std::move (sock),
512
+ addrConnect,
513
+ CalculateKeyedNetGroup (addrConnect),
514
+ nonce,
515
+ addr_bind,
516
+ pszDest ? pszDest : " " ,
517
+ conn_type,
518
+ /* inbound_onion=*/ false );
510
519
pnode->AddRef ();
511
520
512
521
// We're making a new connection, harvest entropy from the time (and our peer count)
@@ -518,11 +527,10 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
518
527
void CNode::CloseSocketDisconnect ()
519
528
{
520
529
fDisconnect = true ;
521
- LOCK (cs_hSocket);
522
- if (hSocket != INVALID_SOCKET)
523
- {
530
+ LOCK (m_sock_mutex);
531
+ if (m_sock) {
524
532
LogPrint (BCLog::NET, " disconnecting peer=%d\n " , id);
525
- CloseSocket (hSocket );
533
+ m_sock. reset ( );
526
534
}
527
535
}
528
536
@@ -802,10 +810,11 @@ size_t CConnman::SocketSendData(CNode& node) const
802
810
assert (data.size () > node.nSendOffset );
803
811
int nBytes = 0 ;
804
812
{
805
- LOCK (node.cs_hSocket );
806
- if (node.hSocket == INVALID_SOCKET)
813
+ LOCK (node.m_sock_mutex );
814
+ if (! node.m_sock ) {
807
815
break ;
808
- nBytes = send (node.hSocket , reinterpret_cast <const char *>(data.data ()) + node.nSendOffset , data.size () - node.nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
816
+ }
817
+ nBytes = node.m_sock ->Send (reinterpret_cast <const char *>(data.data ()) + node.nSendOffset , data.size () - node.nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
809
818
}
810
819
if (nBytes > 0 ) {
811
820
node.m_last_send = GetTime<std::chrono::seconds>();
@@ -1200,7 +1209,16 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1200
1209
}
1201
1210
1202
1211
const bool inbound_onion = std::find (m_onion_binds.begin (), m_onion_binds.end (), addr_bind) != m_onion_binds.end ();
1203
- CNode* pnode = new CNode (id, nodeServices, sock->Release (), addr, CalculateKeyedNetGroup (addr), nonce, addr_bind, " " , ConnectionType::INBOUND, inbound_onion);
1212
+ CNode* pnode = new CNode (id,
1213
+ nodeServices,
1214
+ std::move (sock),
1215
+ addr,
1216
+ CalculateKeyedNetGroup (addr),
1217
+ nonce,
1218
+ addr_bind,
1219
+ /* addrNameIn=*/ " " ,
1220
+ ConnectionType::INBOUND,
1221
+ inbound_onion);
1204
1222
pnode->AddRef ();
1205
1223
pnode->m_permissionFlags = permissionFlags;
1206
1224
pnode->m_prefer_evict = discouraged;
@@ -1384,17 +1402,18 @@ bool CConnman::GenerateSelectSet(const std::vector<CNode*>& nodes,
1384
1402
select_send = !pnode->vSendMsg .empty ();
1385
1403
}
1386
1404
1387
- LOCK (pnode->cs_hSocket );
1388
- if (pnode->hSocket == INVALID_SOCKET)
1405
+ LOCK (pnode->m_sock_mutex );
1406
+ if (! pnode->m_sock ) {
1389
1407
continue ;
1408
+ }
1390
1409
1391
- error_set.insert (pnode->hSocket );
1410
+ error_set.insert (pnode->m_sock -> Get () );
1392
1411
if (select_send) {
1393
- send_set.insert (pnode->hSocket );
1412
+ send_set.insert (pnode->m_sock -> Get () );
1394
1413
continue ;
1395
1414
}
1396
1415
if (select_recv) {
1397
- recv_set.insert (pnode->hSocket );
1416
+ recv_set.insert (pnode->m_sock -> Get () );
1398
1417
}
1399
1418
}
1400
1419
@@ -1564,23 +1583,25 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
1564
1583
bool sendSet = false ;
1565
1584
bool errorSet = false ;
1566
1585
{
1567
- LOCK (pnode->cs_hSocket );
1568
- if (pnode->hSocket == INVALID_SOCKET)
1586
+ LOCK (pnode->m_sock_mutex );
1587
+ if (! pnode->m_sock ) {
1569
1588
continue ;
1570
- recvSet = recv_set.count (pnode->hSocket ) > 0 ;
1571
- sendSet = send_set.count (pnode->hSocket ) > 0 ;
1572
- errorSet = error_set.count (pnode->hSocket ) > 0 ;
1589
+ }
1590
+ recvSet = recv_set.count (pnode->m_sock ->Get ()) > 0 ;
1591
+ sendSet = send_set.count (pnode->m_sock ->Get ()) > 0 ;
1592
+ errorSet = error_set.count (pnode->m_sock ->Get ()) > 0 ;
1573
1593
}
1574
1594
if (recvSet || errorSet)
1575
1595
{
1576
1596
// typical socket buffer is 8K-64K
1577
1597
uint8_t pchBuf[0x10000 ];
1578
1598
int nBytes = 0 ;
1579
1599
{
1580
- LOCK (pnode->cs_hSocket );
1581
- if (pnode->hSocket == INVALID_SOCKET)
1600
+ LOCK (pnode->m_sock_mutex );
1601
+ if (! pnode->m_sock ) {
1582
1602
continue ;
1583
- nBytes = recv (pnode->hSocket , (char *)pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1603
+ }
1604
+ nBytes = pnode->m_sock ->Recv (pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
1584
1605
}
1585
1606
if (nBytes > 0 )
1586
1607
{
@@ -2965,8 +2986,9 @@ ServiceFlags CConnman::GetLocalServices() const
2965
2986
2966
2987
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
2967
2988
2968
- CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
2969
- : m_connected{GetTime<std::chrono::seconds>()},
2989
+ CNode::CNode (NodeId idIn, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> sock, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
2990
+ : m_sock{sock},
2991
+ m_connected{GetTime<std::chrono::seconds>()},
2970
2992
addr (addrIn),
2971
2993
addrBind(addrBindIn),
2972
2994
m_addr_name{addrNameIn.empty () ? addr.ToStringIPPort () : addrNameIn},
@@ -2978,7 +3000,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
2978
3000
nLocalServices(nLocalServicesIn)
2979
3001
{
2980
3002
if (inbound_onion) assert (conn_type_in == ConnectionType::INBOUND);
2981
- hSocket = hSocketIn;
2982
3003
if (conn_type_in != ConnectionType::BLOCK_RELAY) {
2983
3004
m_tx_relay = std::make_unique<TxRelay>();
2984
3005
}
@@ -2997,11 +3018,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
2997
3018
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer ());
2998
3019
}
2999
3020
3000
- CNode::~CNode ()
3001
- {
3002
- CloseSocket (hSocket);
3003
- }
3004
-
3005
3021
bool CConnman::NodeFullyConnected (const CNode* pnode)
3006
3022
{
3007
3023
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect ;
0 commit comments