0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -05:00

net: rename Sock::Reset() to Sock::Close() and make it private

Outside of `Sock`, `Sock::Reset()` was used in just one place (in
`i2p.cpp`) which can use the assignment operator instead.

This simplifies the public `Sock` API by having one method less.
This commit is contained in:
Vasil Dimov 2022-06-21 15:23:37 +02:00
parent e8ff3f0c52
commit a724c39606
No known key found for this signature in database
GPG key ID: 54DF06F64B55CBBF
7 changed files with 28 additions and 46 deletions

View file

@ -410,7 +410,7 @@ void Session::Disconnect()
Log("Destroying session %s", m_session_id); Log("Destroying session %s", m_session_id);
} }
} }
m_control_sock->Reset(); m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
m_session_id.clear(); m_session_id.clear();
} }
} // namespace sam } // namespace sam

View file

@ -24,10 +24,10 @@ FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
FuzzedSock::~FuzzedSock() FuzzedSock::~FuzzedSock()
{ {
// Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call // Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call
// Sock::Reset() (not FuzzedSock::Reset()!) which will call close(m_socket). // close(m_socket) if m_socket is not INVALID_SOCKET.
// Avoid closing an arbitrary file descriptor (m_socket is just a random very high number which // Avoid closing an arbitrary file descriptor (m_socket is just a random very high number which
// theoretically may concide with a real opened file descriptor). // theoretically may concide with a real opened file descriptor).
Reset(); m_socket = INVALID_SOCKET;
} }
FuzzedSock& FuzzedSock::operator=(Sock&& other) FuzzedSock& FuzzedSock::operator=(Sock&& other)
@ -36,11 +36,6 @@ FuzzedSock& FuzzedSock::operator=(Sock&& other)
return *this; return *this;
} }
void FuzzedSock::Reset()
{
m_socket = INVALID_SOCKET;
}
ssize_t FuzzedSock::Send(const void* data, size_t len, int flags) const ssize_t FuzzedSock::Send(const void* data, size_t len, int flags) const
{ {
constexpr std::array send_errnos{ constexpr std::array send_errnos{

View file

@ -55,8 +55,6 @@ public:
FuzzedSock& operator=(Sock&& other) override; FuzzedSock& operator=(Sock&& other) override;
void Reset() override;
ssize_t Send(const void* data, size_t len, int flags) const override; ssize_t Send(const void* data, size_t len, int flags) const override;
ssize_t Recv(void* buf, size_t len, int flags) const override; ssize_t Recv(void* buf, size_t len, int flags) const override;

View file

@ -69,14 +69,6 @@ BOOST_AUTO_TEST_CASE(move_assignment)
BOOST_CHECK(SocketIsClosed(s)); BOOST_CHECK(SocketIsClosed(s));
} }
BOOST_AUTO_TEST_CASE(reset)
{
const SOCKET s = CreateSocket();
Sock sock(s);
sock.Reset();
BOOST_CHECK(SocketIsClosed(s));
}
#ifndef WIN32 // Windows does not have socketpair(2). #ifndef WIN32 // Windows does not have socketpair(2).
static void CreateSocketPair(int s[2]) static void CreateSocketPair(int s[2])

View file

@ -100,7 +100,7 @@ public:
m_socket = INVALID_SOCKET - 1; m_socket = INVALID_SOCKET - 1;
} }
~StaticContentsSock() override { Reset(); } ~StaticContentsSock() override { m_socket = INVALID_SOCKET; }
StaticContentsSock& operator=(Sock&& other) override StaticContentsSock& operator=(Sock&& other) override
{ {
@ -108,11 +108,6 @@ public:
return *this; return *this;
} }
void Reset() override
{
m_socket = INVALID_SOCKET;
}
ssize_t Send(const void*, size_t len, int) const override { return len; } ssize_t Send(const void*, size_t len, int) const override { return len; }
ssize_t Recv(void* buf, size_t len, int flags) const override ssize_t Recv(void* buf, size_t len, int flags) const override

View file

@ -39,11 +39,11 @@ Sock::Sock(Sock&& other)
other.m_socket = INVALID_SOCKET; other.m_socket = INVALID_SOCKET;
} }
Sock::~Sock() { Reset(); } Sock::~Sock() { Close(); }
Sock& Sock::operator=(Sock&& other) Sock& Sock::operator=(Sock&& other)
{ {
Reset(); Close();
m_socket = other.m_socket; m_socket = other.m_socket;
other.m_socket = INVALID_SOCKET; other.m_socket = INVALID_SOCKET;
return *this; return *this;
@ -51,21 +51,6 @@ Sock& Sock::operator=(Sock&& other)
SOCKET Sock::Get() const { return m_socket; } SOCKET Sock::Get() const { return m_socket; }
void Sock::Reset() {
if (m_socket == INVALID_SOCKET) {
return;
}
#ifdef WIN32
int ret = closesocket(m_socket);
#else
int ret = close(m_socket);
#endif
if (ret) {
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
}
m_socket = INVALID_SOCKET;
}
ssize_t Sock::Send(const void* data, size_t len, int flags) const ssize_t Sock::Send(const void* data, size_t len, int flags) const
{ {
return send(m_socket, static_cast<const char*>(data), len, flags); return send(m_socket, static_cast<const char*>(data), len, flags);
@ -372,6 +357,22 @@ bool Sock::IsConnected(std::string& errmsg) const
} }
} }
void Sock::Close()
{
if (m_socket == INVALID_SOCKET) {
return;
}
#ifdef WIN32
int ret = closesocket(m_socket);
#else
int ret = close(m_socket);
#endif
if (ret) {
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
}
m_socket = INVALID_SOCKET;
}
#ifdef WIN32 #ifdef WIN32
std::string NetworkErrorString(int err) std::string NetworkErrorString(int err)
{ {

View file

@ -68,11 +68,6 @@ public:
*/ */
[[nodiscard]] virtual SOCKET Get() const; [[nodiscard]] virtual SOCKET Get() const;
/**
* Close if non-empty.
*/
virtual void Reset();
/** /**
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this * send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
* wrapper can be unit tested if this method is overridden by a mock Sock implementation. * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
@ -245,6 +240,12 @@ protected:
* Contained socket. `INVALID_SOCKET` designates the object is empty. * Contained socket. `INVALID_SOCKET` designates the object is empty.
*/ */
SOCKET m_socket; SOCKET m_socket;
private:
/**
* Close `m_socket` if it is not `INVALID_SOCKET`.
*/
void Close();
}; };
/** Return readable error string for a network error code */ /** Return readable error string for a network error code */