@@ -574,7 +574,17 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
574
574
if (!addr_bind.IsValid ()) {
575
575
addr_bind = GetBindAddress (sock->Get ());
576
576
}
577
- CNode* pnode = new CNode (id, nLocalServices, sock->Release (), addrConnect, CalculateKeyedNetGroup (addrConnect), nonce, addr_bind, pszDest ? pszDest : " " , conn_type, /* inbound_onion */ false , std::move (i2p_transient_session));
577
+ CNode* pnode = new CNode (id,
578
+ nLocalServices,
579
+ std::move (sock),
580
+ addrConnect,
581
+ CalculateKeyedNetGroup (addrConnect),
582
+ nonce,
583
+ addr_bind,
584
+ pszDest ? pszDest : " " ,
585
+ conn_type,
586
+ /* inbound_onion=*/ false ,
587
+ std::move (i2p_transient_session));
578
588
pnode->AddRef ();
579
589
statsClient.inc (" peers.connect" , 1 .0f );
580
590
@@ -589,15 +599,15 @@ void CNode::CloseSocketDisconnect(CConnman* connman)
589
599
AssertLockHeld (connman->m_nodes_mutex );
590
600
591
601
fDisconnect = true ;
592
- LOCK2 (connman->cs_mapSocketToNode , cs_hSocket );
593
- if (hSocket == INVALID_SOCKET ) {
602
+ LOCK2 (connman->cs_mapSocketToNode , m_sock_mutex );
603
+ if (!m_sock ) {
594
604
return ;
595
605
}
596
606
597
607
fHasRecvData = false ;
598
608
fCanSendData = false ;
599
609
600
- connman->mapSocketToNode .erase (hSocket );
610
+ connman->mapSocketToNode .erase (m_sock-> Get () );
601
611
{
602
612
LOCK (connman->cs_sendable_receivable_nodes );
603
613
connman->mapReceivableNodes .erase (GetId ());
@@ -611,12 +621,12 @@ void CNode::CloseSocketDisconnect(CConnman* connman)
611
621
}
612
622
}
613
623
614
- if (connman->m_edge_trig_events && !connman->m_edge_trig_events ->UnregisterEvents (hSocket )) {
624
+ if (connman->m_edge_trig_events && !connman->m_edge_trig_events ->UnregisterEvents (m_sock-> Get () )) {
615
625
LogPrint (BCLog::NET, " EdgeTriggeredEvents::UnregisterEvents() failed\n " );
616
626
}
617
627
618
628
LogPrint (BCLog::NET, " disconnecting peer=%d\n " , id);
619
- CloseSocket (hSocket );
629
+ m_sock. reset ( );
620
630
m_i2p_sam_session.reset ();
621
631
622
632
statsClient.inc (" peers.disconnect" , 1 .0f );
@@ -909,10 +919,11 @@ size_t CConnman::SocketSendData(CNode& node)
909
919
assert (data.size () > node.nSendOffset );
910
920
int nBytes = 0 ;
911
921
{
912
- LOCK (node.cs_hSocket );
913
- if (node.hSocket == INVALID_SOCKET)
922
+ LOCK (node.m_sock_mutex );
923
+ if (! node.m_sock ) {
914
924
break ;
915
- nBytes = send (node.hSocket , reinterpret_cast <const char *>(data.data ()) + node.nSendOffset , data.size () - node.nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
925
+ }
926
+ nBytes = node.m_sock ->Send (reinterpret_cast <const char *>(data.data ()) + node.nSendOffset , data.size () - node.nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT);
916
927
}
917
928
if (nBytes > 0 ) {
918
929
node.m_last_send = GetTime<std::chrono::seconds>();
@@ -1347,29 +1358,41 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
1347
1358
}
1348
1359
1349
1360
const bool inbound_onion = std::find (m_onion_binds.begin (), m_onion_binds.end (), addr_bind) != m_onion_binds.end ();
1350
- CNode* pnode = new CNode (id, nodeServices, sock->Release (), addr, CalculateKeyedNetGroup (addr), nonce, addr_bind, " " , ConnectionType::INBOUND, inbound_onion);
1361
+ CNode* pnode = new CNode (id,
1362
+ nodeServices,
1363
+ std::move (sock),
1364
+ addr,
1365
+ CalculateKeyedNetGroup (addr),
1366
+ nonce,
1367
+ addr_bind,
1368
+ /* addrNameIn=*/ " " ,
1369
+ ConnectionType::INBOUND,
1370
+ inbound_onion);
1351
1371
pnode->AddRef ();
1352
1372
pnode->m_permissionFlags = permissionFlags;
1353
1373
// If this flag is present, the user probably expect that RPC and QT report it as whitelisted (backward compatibility)
1354
1374
pnode->m_legacyWhitelisted = legacyWhitelisted;
1355
1375
pnode->m_prefer_evict = discouraged;
1356
1376
m_msgproc->InitializeNode (pnode);
1357
1377
1358
- if (fLogIPs ) {
1359
- LogPrint (BCLog::NET_NETCONN, " connection from %s accepted, sock=%d, peer=%d\n " , addr.ToString (), sock->Get (), pnode->GetId ());
1360
- } else {
1361
- LogPrint (BCLog::NET_NETCONN, " connection accepted, sock=%d, peer=%d\n " , sock->Get (), pnode->GetId ());
1378
+ {
1379
+ LOCK (pnode->m_sock_mutex );
1380
+ if (fLogIPs ) {
1381
+ LogPrint (BCLog::NET_NETCONN, " connection from %s accepted, sock=%d, peer=%d\n " , addr.ToString (), pnode->m_sock ->Get (), pnode->GetId ());
1382
+ } else {
1383
+ LogPrint (BCLog::NET_NETCONN, " connection accepted, sock=%d, peer=%d\n " , pnode->m_sock ->Get (), pnode->GetId ());
1384
+ }
1362
1385
}
1363
1386
1364
1387
{
1365
1388
LOCK (m_nodes_mutex);
1366
1389
m_nodes.push_back (pnode);
1367
1390
}
1368
1391
{
1369
- LOCK ( pnode->cs_hSocket );
1370
- WITH_LOCK (cs_mapSocketToNode, mapSocketToNode.emplace (pnode->hSocket , pnode) );
1392
+ LOCK2 (cs_mapSocketToNode, pnode->m_sock_mutex );
1393
+ mapSocketToNode.emplace (pnode->m_sock -> Get () , pnode);
1371
1394
if (m_edge_trig_events) {
1372
- if (!m_edge_trig_events->RegisterEvents (pnode->hSocket )) {
1395
+ if (!m_edge_trig_events->RegisterEvents (pnode->m_sock -> Get () )) {
1373
1396
LogPrint (BCLog::NET, " EdgeTriggeredEvents::RegisterEvents() failed\n " );
1374
1397
}
1375
1398
}
@@ -1456,10 +1479,10 @@ void CConnman::DisconnectNodes()
1456
1479
if (GetTimeMillis () < pnode->nDisconnectLingerTime ) {
1457
1480
// everything flushed to the kernel?
1458
1481
if (!pnode->fSocketShutdown && pnode->nSendMsgSize == 0 ) {
1459
- LOCK (pnode->cs_hSocket );
1460
- if (pnode->hSocket != INVALID_SOCKET ) {
1482
+ LOCK (pnode->m_sock_mutex );
1483
+ if (pnode->m_sock ) {
1461
1484
// Give the other side a chance to detect the disconnect as early as possible (recv() will return 0)
1462
- ::shutdown (pnode->hSocket , SD_SEND);
1485
+ ::shutdown (pnode->m_sock-> Get () , SD_SEND);
1463
1486
}
1464
1487
pnode->fSocketShutdown = true ;
1465
1488
}
@@ -1661,16 +1684,17 @@ bool CConnman::GenerateSelectSet(const std::vector<CNode*>& nodes,
1661
1684
bool select_recv = !pnode->fHasRecvData ;
1662
1685
bool select_send = !pnode->fCanSendData ;
1663
1686
1664
- LOCK (pnode->cs_hSocket );
1665
- if (pnode->hSocket == INVALID_SOCKET)
1687
+ LOCK (pnode->m_sock_mutex );
1688
+ if (! pnode->m_sock ) {
1666
1689
continue ;
1690
+ }
1667
1691
1668
- error_set.insert (pnode->hSocket );
1692
+ error_set.insert (pnode->m_sock -> Get () );
1669
1693
if (select_send) {
1670
- send_set.insert (pnode->hSocket );
1694
+ send_set.insert (pnode->m_sock -> Get () );
1671
1695
}
1672
1696
if (select_recv) {
1673
- recv_set.insert (pnode->hSocket );
1697
+ recv_set.insert (pnode->m_sock -> Get () );
1674
1698
}
1675
1699
}
1676
1700
@@ -2137,10 +2161,10 @@ size_t CConnman::SocketRecvData(CNode *pnode)
2137
2161
uint8_t pchBuf[0x10000 ];
2138
2162
int nBytes = 0 ;
2139
2163
{
2140
- LOCK (pnode->cs_hSocket );
2141
- if (pnode->hSocket == INVALID_SOCKET )
2164
+ LOCK (pnode->m_sock_mutex );
2165
+ if (! pnode->m_sock )
2142
2166
return 0 ;
2143
- nBytes = recv (pnode->hSocket , (char *)pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
2167
+ nBytes = recv (pnode->m_sock -> Get () , (char *)pchBuf, sizeof (pchBuf), MSG_DONTWAIT);
2144
2168
if (nBytes < (int )sizeof (pchBuf)) {
2145
2169
pnode->fHasRecvData = false ;
2146
2170
}
@@ -3047,8 +3071,8 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
3047
3071
}
3048
3072
3049
3073
{
3050
- LOCK (pnode->cs_hSocket );
3051
- LogPrint (BCLog::NET_NETCONN, " CConnman::%s -- successfully connected to %s, sock=%d, peer=%d\n " , __func__, getIpStr (), pnode->hSocket , pnode->GetId ());
3074
+ LOCK (pnode->m_sock_mutex );
3075
+ LogPrint (BCLog::NET_NETCONN, " CConnman::%s -- successfully connected to %s, sock=%d, peer=%d\n " , __func__, getIpStr (), pnode->m_sock -> Get () , pnode->GetId ());
3052
3076
}
3053
3077
3054
3078
if (grantOutbound)
@@ -3060,17 +3084,19 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
3060
3084
pnode->m_masternode_probe_connection = true ;
3061
3085
3062
3086
{
3063
- LOCK2 (cs_mapSocketToNode, pnode->cs_hSocket );
3064
- mapSocketToNode.emplace (pnode->hSocket , pnode);
3087
+ LOCK2 (cs_mapSocketToNode, pnode->m_sock_mutex );
3088
+ mapSocketToNode.emplace (pnode->m_sock -> Get () , pnode);
3065
3089
}
3066
3090
3067
3091
m_msgproc->InitializeNode (pnode);
3068
3092
{
3069
3093
LOCK (m_nodes_mutex);
3070
3094
m_nodes.push_back (pnode);
3095
+ }
3096
+ {
3071
3097
if (m_edge_trig_events) {
3072
- LOCK (pnode->cs_hSocket );
3073
- if (!m_edge_trig_events->RegisterEvents (pnode->hSocket )) {
3098
+ LOCK (pnode->m_sock_mutex );
3099
+ if (!m_edge_trig_events->RegisterEvents (pnode->m_sock -> Get () )) {
3074
3100
LogPrint (BCLog::NET, " EdgeTriggeredEvents::RegisterEvents() failed\n " );
3075
3101
}
3076
3102
}
@@ -3579,7 +3605,7 @@ void CConnman::StopNodes()
3579
3605
pnode->CloseSocketDisconnect (this );
3580
3606
}
3581
3607
for (ListenSocket& hListenSocket : vhListenSocket) {
3582
- if (hListenSocket.sock -> Get () != INVALID_SOCKET ) {
3608
+ if (hListenSocket.sock ) {
3583
3609
if (m_edge_trig_events && !m_edge_trig_events->RemoveSocket (hListenSocket.sock ->Get ())) {
3584
3610
LogPrintf (" EdgeTriggeredEvents::RemoveSocket() failed\n " );
3585
3611
}
@@ -4046,8 +4072,9 @@ ServiceFlags CConnman::GetLocalServices() const
4046
4072
4047
4073
unsigned int CConnman::GetReceiveFloodSize () const { return nReceiveFloodSize; }
4048
4074
4049
- 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, std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
4050
- : nTimeConnected{GetTimeSeconds ()},
4075
+ 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, std::unique_ptr<i2p::sam::Session>&& i2p_sam_session)
4076
+ : m_sock{sock},
4077
+ nTimeConnected{GetTimeSeconds ()},
4051
4078
addr{addrIn},
4052
4079
addrBind{addrBindIn},
4053
4080
m_addr_name{addrNameIn.empty () ? addr.ToStringIPPort () : addrNameIn},
@@ -4060,7 +4087,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
4060
4087
m_i2p_sam_session{std::move (i2p_sam_session)}
4061
4088
{
4062
4089
if (inbound_onion) assert (conn_type_in == ConnectionType::INBOUND);
4063
- hSocket = hSocketIn;
4064
4090
4065
4091
for (const std::string &msg : getAllNetMessageTypes ())
4066
4092
mapRecvBytesPerMsgCmd[msg] = 0 ;
@@ -4076,11 +4102,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const
4076
4102
m_serializer = std::make_unique<V1TransportSerializer>(V1TransportSerializer ());
4077
4103
}
4078
4104
4079
- CNode::~CNode ()
4080
- {
4081
- CloseSocket (hSocket);
4082
- }
4083
-
4084
4105
bool CConnman::NodeFullyConnected (const CNode* pnode)
4085
4106
{
4086
4107
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect ;
0 commit comments