Skip to content

Commit 7829272

Browse files
committed
net: remove now unnecessary Sock::Get()
`Sock::Get()` was used only in `sock.{cpp,h}`. Remove it and access `Sock::m_socket` directly. Unit tests that used `Get()` to test for equality still verify that the behavior is correct by using the added `operator==()`.
1 parent 944b21b commit 7829272

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

src/test/sock_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ BOOST_AUTO_TEST_CASE(constructor_and_destructor)
3838
{
3939
const SOCKET s = CreateSocket();
4040
Sock* sock = new Sock(s);
41-
BOOST_CHECK_EQUAL(sock->Get(), s);
41+
BOOST_CHECK(*sock == s);
4242
BOOST_CHECK(!SocketIsClosed(s));
4343
delete sock;
4444
BOOST_CHECK(SocketIsClosed(s));
@@ -51,7 +51,7 @@ BOOST_AUTO_TEST_CASE(move_constructor)
5151
Sock* sock2 = new Sock(std::move(*sock1));
5252
delete sock1;
5353
BOOST_CHECK(!SocketIsClosed(s));
54-
BOOST_CHECK_EQUAL(sock2->Get(), s);
54+
BOOST_CHECK(*sock2 == s);
5555
delete sock2;
5656
BOOST_CHECK(SocketIsClosed(s));
5757
}
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(move_assignment)
6464
*sock2 = std::move(*sock1);
6565
delete sock1;
6666
BOOST_CHECK(!SocketIsClosed(s));
67-
BOOST_CHECK_EQUAL(sock2->Get(), s);
67+
BOOST_CHECK(*sock2 == s);
6868
delete sock2;
6969
BOOST_CHECK(SocketIsClosed(s));
7070
}

src/util/sock.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ Sock& Sock::operator=(Sock&& other)
4444
return *this;
4545
}
4646

47-
SOCKET Sock::Get() const { return m_socket; }
48-
4947
ssize_t Sock::Send(const void* data, size_t len, int flags) const
5048
{
5149
return send(m_socket, static_cast<const char*>(data), len, flags);
@@ -411,6 +409,11 @@ void Sock::Close()
411409
m_socket = INVALID_SOCKET;
412410
}
413411

412+
bool Sock::operator==(SOCKET s) const
413+
{
414+
return m_socket == s;
415+
};
416+
414417
std::string NetworkErrorString(int err)
415418
{
416419
#if defined(WIN32)

src/util/sock.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
static constexpr auto MAX_WAIT_FOR_IO = 1s;
2222

2323
/**
24-
* RAII helper class that manages a socket. Mimics `std::unique_ptr`, but instead of a pointer it
25-
* contains a socket and closes it automatically when it goes out of scope.
24+
* RAII helper class that manages a socket and closes it automatically when it goes out of scope.
2625
*/
2726
class Sock
2827
{
@@ -63,43 +62,37 @@ class Sock
6362
virtual Sock& operator=(Sock&& other);
6463

6564
/**
66-
* Get the value of the contained socket.
67-
* @return socket or INVALID_SOCKET if empty
68-
*/
69-
[[nodiscard]] virtual SOCKET Get() const;
70-
71-
/**
72-
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
65+
* send(2) wrapper. Equivalent to `send(m_socket, data, len, flags);`. Code that uses this
7366
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
7467
*/
7568
[[nodiscard]] virtual ssize_t Send(const void* data, size_t len, int flags) const;
7669

7770
/**
78-
* recv(2) wrapper. Equivalent to `recv(this->Get(), buf, len, flags);`. Code that uses this
71+
* recv(2) wrapper. Equivalent to `recv(m_socket, buf, len, flags);`. Code that uses this
7972
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
8073
*/
8174
[[nodiscard]] virtual ssize_t Recv(void* buf, size_t len, int flags) const;
8275

8376
/**
84-
* connect(2) wrapper. Equivalent to `connect(this->Get(), addr, addrlen)`. Code that uses this
77+
* connect(2) wrapper. Equivalent to `connect(m_socket, addr, addrlen)`. Code that uses this
8578
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
8679
*/
8780
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
8881

8982
/**
90-
* bind(2) wrapper. Equivalent to `bind(this->Get(), addr, addr_len)`. Code that uses this
83+
* bind(2) wrapper. Equivalent to `bind(m_socket, addr, addr_len)`. Code that uses this
9184
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
9285
*/
9386
[[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
9487

9588
/**
96-
* listen(2) wrapper. Equivalent to `listen(this->Get(), backlog)`. Code that uses this
89+
* listen(2) wrapper. Equivalent to `listen(m_socket, backlog)`. Code that uses this
9790
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
9891
*/
9992
[[nodiscard]] virtual int Listen(int backlog) const;
10093

10194
/**
102-
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
95+
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(m_socket, addr, addr_len))`.
10396
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock
10497
* implementation.
10598
* The returned unique_ptr is empty if `accept()` failed in which case errno will be set.
@@ -108,7 +101,7 @@ class Sock
108101

109102
/**
110103
* getsockopt(2) wrapper. Equivalent to
111-
* `getsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
104+
* `getsockopt(m_socket, level, opt_name, opt_val, opt_len)`. Code that uses this
112105
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
113106
*/
114107
[[nodiscard]] virtual int GetSockOpt(int level,
@@ -118,7 +111,7 @@ class Sock
118111

119112
/**
120113
* setsockopt(2) wrapper. Equivalent to
121-
* `setsockopt(this->Get(), level, opt_name, opt_val, opt_len)`. Code that uses this
114+
* `setsockopt(m_socket, level, opt_name, opt_val, opt_len)`. Code that uses this
122115
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
123116
*/
124117
[[nodiscard]] virtual int SetSockOpt(int level,
@@ -128,7 +121,7 @@ class Sock
128121

129122
/**
130123
* getsockname(2) wrapper. Equivalent to
131-
* `getsockname(this->Get(), name, name_len)`. Code that uses this
124+
* `getsockname(m_socket, name, name_len)`. Code that uses this
132125
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
133126
*/
134127
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
@@ -266,6 +259,11 @@ class Sock
266259
*/
267260
[[nodiscard]] virtual bool IsConnected(std::string& errmsg) const;
268261

262+
/**
263+
* Check if the internal socket is equal to `s`. Use only in tests.
264+
*/
265+
bool operator==(SOCKET s) const;
266+
269267
protected:
270268
/**
271269
* Contained socket. `INVALID_SOCKET` designates the object is empty.

0 commit comments

Comments
 (0)