Skip to content

Commit edec7cf

Browse files
committed
net: move-only: improve encapsulation of SockMan
`SockMan` members `AcceptConnection()` `NewSockAccepted()` `GetNewNodeId()` `m_i2p_sam_session` `m_listen private` are now used only by `SockMan`, thus make them private.
1 parent 6230c10 commit edec7cf

File tree

2 files changed

+94
-94
lines changed

2 files changed

+94
-94
lines changed

src/sockman.cpp

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -215,65 +215,6 @@ SockMan::ConnectAndMakeNodeId(const std::variant<CService, StringHostIntPort>& t
215215
return node_id;
216216
}
217217

218-
std::unique_ptr<Sock> SockMan::AcceptConnection(const Sock& listen_sock, CService& addr)
219-
{
220-
sockaddr_storage storage;
221-
socklen_t len{sizeof(storage)};
222-
223-
auto sock{listen_sock.Accept(reinterpret_cast<sockaddr*>(&storage), &len)};
224-
225-
if (!sock) {
226-
const int err{WSAGetLastError()};
227-
if (err != WSAEWOULDBLOCK) {
228-
LogPrintLevel(BCLog::NET,
229-
BCLog::Level::Error,
230-
"Cannot accept new connection: %s\n",
231-
NetworkErrorString(err));
232-
}
233-
return {};
234-
}
235-
236-
if (!addr.SetSockAddr(reinterpret_cast<sockaddr*>(&storage))) {
237-
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "Unknown socket family\n");
238-
}
239-
240-
return sock;
241-
}
242-
243-
void SockMan::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& me, const CService& them)
244-
{
245-
AssertLockNotHeld(m_connected_mutex);
246-
247-
if (!sock->IsSelectable()) {
248-
LogPrintf("connection from %s dropped: non-selectable socket\n", them.ToStringAddrPort());
249-
return;
250-
}
251-
252-
// According to the internet TCP_NODELAY is not carried into accepted sockets
253-
// on all platforms. Set it again here just to be sure.
254-
const int on{1};
255-
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
256-
LogDebug(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
257-
them.ToStringAddrPort());
258-
}
259-
260-
const NodeId node_id{GetNewNodeId()};
261-
262-
{
263-
LOCK(m_connected_mutex);
264-
m_connected.emplace(node_id, std::make_shared<NodeSockets>(std::move(sock)));
265-
}
266-
267-
if (!EventNewConnectionAccepted(node_id, me, them)) {
268-
CloseConnection(node_id);
269-
}
270-
}
271-
272-
NodeId SockMan::GetNewNodeId()
273-
{
274-
return m_next_node_id.fetch_add(1, std::memory_order_relaxed);
275-
}
276-
277218
bool SockMan::CloseConnection(NodeId node_id)
278219
{
279220
LOCK(m_connected_mutex);
@@ -405,6 +346,65 @@ void SockMan::ThreadSocketHandler()
405346
}
406347
}
407348

349+
std::unique_ptr<Sock> SockMan::AcceptConnection(const Sock& listen_sock, CService& addr)
350+
{
351+
sockaddr_storage storage;
352+
socklen_t len{sizeof(storage)};
353+
354+
auto sock{listen_sock.Accept(reinterpret_cast<sockaddr*>(&storage), &len)};
355+
356+
if (!sock) {
357+
const int err{WSAGetLastError()};
358+
if (err != WSAEWOULDBLOCK) {
359+
LogPrintLevel(BCLog::NET,
360+
BCLog::Level::Error,
361+
"Cannot accept new connection: %s\n",
362+
NetworkErrorString(err));
363+
}
364+
return {};
365+
}
366+
367+
if (!addr.SetSockAddr(reinterpret_cast<sockaddr*>(&storage))) {
368+
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "Unknown socket family\n");
369+
}
370+
371+
return sock;
372+
}
373+
374+
void SockMan::NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& me, const CService& them)
375+
{
376+
AssertLockNotHeld(m_connected_mutex);
377+
378+
if (!sock->IsSelectable()) {
379+
LogPrintf("connection from %s dropped: non-selectable socket\n", them.ToStringAddrPort());
380+
return;
381+
}
382+
383+
// According to the internet TCP_NODELAY is not carried into accepted sockets
384+
// on all platforms. Set it again here just to be sure.
385+
const int on{1};
386+
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
387+
LogDebug(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
388+
them.ToStringAddrPort());
389+
}
390+
391+
const NodeId node_id{GetNewNodeId()};
392+
393+
{
394+
LOCK(m_connected_mutex);
395+
m_connected.emplace(node_id, std::make_shared<NodeSockets>(std::move(sock)));
396+
}
397+
398+
if (!EventNewConnectionAccepted(node_id, me, them)) {
399+
CloseConnection(node_id);
400+
}
401+
}
402+
403+
NodeId SockMan::GetNewNodeId()
404+
{
405+
return m_next_node_id.fetch_add(1, std::memory_order_relaxed);
406+
}
407+
408408
SockMan::IOReadiness SockMan::GenerateWaitSockets()
409409
{
410410
AssertLockNotHeld(m_connected_mutex);

src/sockman.h

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,6 @@ class SockMan
106106
CService& me)
107107
EXCLUSIVE_LOCKS_REQUIRED(!m_connected_mutex, !m_unused_i2p_sessions_mutex);
108108

109-
/**
110-
* Accept a connection.
111-
* @param[in] listen_sock Socket on which to accept the connection.
112-
* @param[out] addr Address of the peer that was accepted.
113-
* @return Newly created socket for the accepted connection.
114-
*/
115-
std::unique_ptr<Sock> AcceptConnection(const Sock& listen_sock, CService& addr);
116-
117-
/**
118-
* After a new socket with a peer has been created, configure its flags,
119-
* make a new node id and call `EventNewConnectionAccepted()`.
120-
* @param[in] sock The newly created socket.
121-
* @param[in] me Address at our end of the connection.
122-
* @param[in] them Address of the new peer.
123-
*/
124-
void NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& me, const CService& them)
125-
EXCLUSIVE_LOCKS_REQUIRED(!m_connected_mutex);
126-
127-
/**
128-
* Generate an id for a newly created node.
129-
*/
130-
NodeId GetNewNodeId();
131-
132109
/**
133110
* Disconnect a given peer by closing its socket and release resources occupied by it.
134111
* @return Whether the peer existed and its socket was closed by this call.
@@ -264,18 +241,6 @@ class SockMan
264241
*/
265242
CThreadInterrupt interruptNet;
266243

267-
/**
268-
* I2P SAM session.
269-
* Used to accept incoming and make outgoing I2P connections from a persistent
270-
* address.
271-
*/
272-
std::unique_ptr<i2p::sam::Session> m_i2p_sam_session;
273-
274-
/**
275-
* List of listening sockets.
276-
*/
277-
std::vector<std::shared_ptr<Sock>> m_listen;
278-
279244
protected:
280245

281246
/**
@@ -354,6 +319,29 @@ class SockMan
354319
void ThreadSocketHandler()
355320
EXCLUSIVE_LOCKS_REQUIRED(!m_connected_mutex);
356321

322+
/**
323+
* Accept a connection.
324+
* @param[in] listen_sock Socket on which to accept the connection.
325+
* @param[out] addr Address of the peer that was accepted.
326+
* @return Newly created socket for the accepted connection.
327+
*/
328+
std::unique_ptr<Sock> AcceptConnection(const Sock& listen_sock, CService& addr);
329+
330+
/**
331+
* After a new socket with a peer has been created, configure its flags,
332+
* make a new node id and call `EventNewConnectionAccepted()`.
333+
* @param[in] sock The newly created socket.
334+
* @param[in] me Address at our end of the connection.
335+
* @param[in] them Address of the new peer.
336+
*/
337+
void NewSockAccepted(std::unique_ptr<Sock>&& sock, const CService& me, const CService& them)
338+
EXCLUSIVE_LOCKS_REQUIRED(!m_connected_mutex);
339+
340+
/**
341+
* Generate an id for a newly created node.
342+
*/
343+
NodeId GetNewNodeId();
344+
357345
/**
358346
* Generate a collection of sockets to check for IO readiness.
359347
* @return Sockets to check for readiness plus an aux map to find the
@@ -413,6 +401,18 @@ class SockMan
413401
*/
414402
std::queue<std::unique_ptr<i2p::sam::Session>> m_unused_i2p_sessions GUARDED_BY(m_unused_i2p_sessions_mutex);
415403

404+
/**
405+
* I2P SAM session.
406+
* Used to accept incoming and make outgoing I2P connections from a persistent
407+
* address.
408+
*/
409+
std::unique_ptr<i2p::sam::Session> m_i2p_sam_session;
410+
411+
/**
412+
* List of listening sockets.
413+
*/
414+
std::vector<std::shared_ptr<Sock>> m_listen;
415+
416416
mutable Mutex m_connected_mutex;
417417

418418
/**

0 commit comments

Comments
 (0)