Skip to content

Commit a724c39

Browse files
committed
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.
1 parent e8ff3f0 commit a724c39

File tree

7 files changed

+28
-46
lines changed

7 files changed

+28
-46
lines changed

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void Session::Disconnect()
410410
Log("Destroying session %s", m_session_id);
411411
}
412412
}
413-
m_control_sock->Reset();
413+
m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
414414
m_session_id.clear();
415415
}
416416
} // namespace sam

src/test/fuzz/util.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
2424
FuzzedSock::~FuzzedSock()
2525
{
2626
// Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call
27-
// Sock::Reset() (not FuzzedSock::Reset()!) which will call close(m_socket).
27+
// close(m_socket) if m_socket is not INVALID_SOCKET.
2828
// Avoid closing an arbitrary file descriptor (m_socket is just a random very high number which
2929
// theoretically may concide with a real opened file descriptor).
30-
Reset();
30+
m_socket = INVALID_SOCKET;
3131
}
3232

3333
FuzzedSock& FuzzedSock::operator=(Sock&& other)
@@ -36,11 +36,6 @@ FuzzedSock& FuzzedSock::operator=(Sock&& other)
3636
return *this;
3737
}
3838

39-
void FuzzedSock::Reset()
40-
{
41-
m_socket = INVALID_SOCKET;
42-
}
43-
4439
ssize_t FuzzedSock::Send(const void* data, size_t len, int flags) const
4540
{
4641
constexpr std::array send_errnos{

src/test/fuzz/util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class FuzzedSock : public Sock
5555

5656
FuzzedSock& operator=(Sock&& other) override;
5757

58-
void Reset() override;
59-
6058
ssize_t Send(const void* data, size_t len, int flags) const override;
6159

6260
ssize_t Recv(void* buf, size_t len, int flags) const override;

src/test/sock_tests.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ BOOST_AUTO_TEST_CASE(move_assignment)
6969
BOOST_CHECK(SocketIsClosed(s));
7070
}
7171

72-
BOOST_AUTO_TEST_CASE(reset)
73-
{
74-
const SOCKET s = CreateSocket();
75-
Sock sock(s);
76-
sock.Reset();
77-
BOOST_CHECK(SocketIsClosed(s));
78-
}
79-
8072
#ifndef WIN32 // Windows does not have socketpair(2).
8173

8274
static void CreateSocketPair(int s[2])

src/test/util/net.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,14 @@ class StaticContentsSock : public Sock
100100
m_socket = INVALID_SOCKET - 1;
101101
}
102102

103-
~StaticContentsSock() override { Reset(); }
103+
~StaticContentsSock() override { m_socket = INVALID_SOCKET; }
104104

105105
StaticContentsSock& operator=(Sock&& other) override
106106
{
107107
assert(false && "Move of Sock into MockSock not allowed.");
108108
return *this;
109109
}
110110

111-
void Reset() override
112-
{
113-
m_socket = INVALID_SOCKET;
114-
}
115-
116111
ssize_t Send(const void*, size_t len, int) const override { return len; }
117112

118113
ssize_t Recv(void* buf, size_t len, int flags) const override

src/util/sock.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,18 @@ Sock::Sock(Sock&& other)
3939
other.m_socket = INVALID_SOCKET;
4040
}
4141

42-
Sock::~Sock() { Reset(); }
42+
Sock::~Sock() { Close(); }
4343

4444
Sock& Sock::operator=(Sock&& other)
4545
{
46-
Reset();
46+
Close();
4747
m_socket = other.m_socket;
4848
other.m_socket = INVALID_SOCKET;
4949
return *this;
5050
}
5151

5252
SOCKET Sock::Get() const { return m_socket; }
5353

54-
void Sock::Reset() {
55-
if (m_socket == INVALID_SOCKET) {
56-
return;
57-
}
58-
#ifdef WIN32
59-
int ret = closesocket(m_socket);
60-
#else
61-
int ret = close(m_socket);
62-
#endif
63-
if (ret) {
64-
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
65-
}
66-
m_socket = INVALID_SOCKET;
67-
}
68-
6954
ssize_t Sock::Send(const void* data, size_t len, int flags) const
7055
{
7156
return send(m_socket, static_cast<const char*>(data), len, flags);
@@ -372,6 +357,22 @@ bool Sock::IsConnected(std::string& errmsg) const
372357
}
373358
}
374359

360+
void Sock::Close()
361+
{
362+
if (m_socket == INVALID_SOCKET) {
363+
return;
364+
}
365+
#ifdef WIN32
366+
int ret = closesocket(m_socket);
367+
#else
368+
int ret = close(m_socket);
369+
#endif
370+
if (ret) {
371+
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
372+
}
373+
m_socket = INVALID_SOCKET;
374+
}
375+
375376
#ifdef WIN32
376377
std::string NetworkErrorString(int err)
377378
{

src/util/sock.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ class Sock
6868
*/
6969
[[nodiscard]] virtual SOCKET Get() const;
7070

71-
/**
72-
* Close if non-empty.
73-
*/
74-
virtual void Reset();
75-
7671
/**
7772
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
7873
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
@@ -245,6 +240,12 @@ class Sock
245240
* Contained socket. `INVALID_SOCKET` designates the object is empty.
246241
*/
247242
SOCKET m_socket;
243+
244+
private:
245+
/**
246+
* Close `m_socket` if it is not `INVALID_SOCKET`.
247+
*/
248+
void Close();
248249
};
249250

250251
/** Return readable error string for a network error code */

0 commit comments

Comments
 (0)