File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -250,6 +250,31 @@ std::string Sock::RecvUntilTerminator(uint8_t terminator,
250
250
}
251
251
}
252
252
253
+ bool Sock::IsConnected (std::string& errmsg) const
254
+ {
255
+ if (m_socket == INVALID_SOCKET) {
256
+ errmsg = " not connected" ;
257
+ return false ;
258
+ }
259
+
260
+ char c;
261
+ switch (Recv (&c, sizeof (c), MSG_PEEK)) {
262
+ case -1 : {
263
+ const int err = WSAGetLastError ();
264
+ if (IOErrorIsPermanent (err)) {
265
+ errmsg = NetworkErrorString (err);
266
+ return false ;
267
+ }
268
+ return true ;
269
+ }
270
+ case 0 :
271
+ errmsg = " closed" ;
272
+ return false ;
273
+ default :
274
+ return true ;
275
+ }
276
+ }
277
+
253
278
#ifdef WIN32
254
279
std::string NetworkErrorString (int err)
255
280
{
Original file line number Diff line number Diff line change @@ -143,6 +143,13 @@ class Sock
143
143
std::chrono::milliseconds timeout,
144
144
CThreadInterrupt& interrupt) const ;
145
145
146
+ /* *
147
+ * Check if still connected.
148
+ * @param[out] err The error string, if the socket has been disconnected.
149
+ * @return true if connected
150
+ */
151
+ virtual bool IsConnected (std::string& errmsg) const ;
152
+
146
153
private:
147
154
/* *
148
155
* Contained socket. `INVALID_SOCKET` designates the object is empty.
You can’t perform that action at this time.
0 commit comments