HomePhabricator

net: add RAII socket and use it instead of bare SOCKET, add Sock unit tests

Description

net: add RAII socket and use it instead of bare SOCKET, add Sock unit tests

Summary:
Introduce a class to manage the lifetime of a socket - when the object that contains the socket goes out of scope, the underlying socket will be closed.

In addition, the new Sock class has a Send(), Recv() and Wait() methods that can be overridden by unit tests to mock the socket operations.

The Wait() method also hides the #ifdef USE_POLL poll() #else select() #endif technique from higher level code.

This is a backport of core#20788 [3/5] and core#21159 (bugfix)
https://github.com/bitcoin/bitcoin/pull/20788/commits/ba9d73268f9585d4b9254adcf54708f88222798b
https://github.com/bitcoin/bitcoin/pull/20788/commits/615ba0eb96cf131364c1ceca9d3dedf006fa1e1c
https://github.com/bitcoin/bitcoin/pull/21159/commits/9cc8e30125df14fe47e21e55ab3bf26f4d416565

Depends on D11004

Test Plan:
ninja all check-all

Run the seeder:
./src/seeder/bitcoin-seeder

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11005

Details

Provenance
Vasil Dimov <vd@FreeBSD.org>Authored on Dec 23 2020, 15:40
PiRKCommitted on Feb 8 2022, 13:41
PiRKPushed on Feb 8 2022, 13:41
Reviewer
Restricted Project
Differential Revision
D11005: net: add RAII socket and use it instead of bare SOCKET, add Sock unit tests
Parents
rABCdbfdac4aa4c3: net: move CloseSocket() from netbase to util/sock
Branches
Unknown
Tags
Unknown