@@ -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-
277218bool 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+
408408SockMan::IOReadiness SockMan::GenerateWaitSockets ()
409409{
410410 AssertLockNotHeld (m_connected_mutex);
0 commit comments