21
21
static constexpr auto MAX_WAIT_FOR_IO = 1s;
22
22
23
23
/* *
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.
26
25
*/
27
26
class Sock
28
27
{
@@ -63,43 +62,37 @@ class Sock
63
62
virtual Sock& operator =(Sock&& other);
64
63
65
64
/* *
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
73
66
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
74
67
*/
75
68
[[nodiscard]] virtual ssize_t Send (const void * data, size_t len, int flags) const ;
76
69
77
70
/* *
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
79
72
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
80
73
*/
81
74
[[nodiscard]] virtual ssize_t Recv (void * buf, size_t len, int flags) const ;
82
75
83
76
/* *
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
85
78
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
86
79
*/
87
80
[[nodiscard]] virtual int Connect (const sockaddr* addr, socklen_t addr_len) const ;
88
81
89
82
/* *
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
91
84
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
92
85
*/
93
86
[[nodiscard]] virtual int Bind (const sockaddr* addr, socklen_t addr_len) const ;
94
87
95
88
/* *
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
97
90
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
98
91
*/
99
92
[[nodiscard]] virtual int Listen (int backlog) const ;
100
93
101
94
/* *
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))`.
103
96
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock
104
97
* implementation.
105
98
* The returned unique_ptr is empty if `accept()` failed in which case errno will be set.
@@ -108,7 +101,7 @@ class Sock
108
101
109
102
/* *
110
103
* 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
112
105
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
113
106
*/
114
107
[[nodiscard]] virtual int GetSockOpt (int level,
@@ -118,7 +111,7 @@ class Sock
118
111
119
112
/* *
120
113
* 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
122
115
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
123
116
*/
124
117
[[nodiscard]] virtual int SetSockOpt (int level,
@@ -128,7 +121,7 @@ class Sock
128
121
129
122
/* *
130
123
* 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
132
125
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
133
126
*/
134
127
[[nodiscard]] virtual int GetSockName (sockaddr* name, socklen_t * name_len) const ;
@@ -266,6 +259,11 @@ class Sock
266
259
*/
267
260
[[nodiscard]] virtual bool IsConnected (std::string& errmsg) const ;
268
261
262
+ /* *
263
+ * Check if the internal socket is equal to `s`. Use only in tests.
264
+ */
265
+ bool operator ==(SOCKET s) const ;
266
+
269
267
protected:
270
268
/* *
271
269
* Contained socket. `INVALID_SOCKET` designates the object is empty.
0 commit comments