mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
net: extend Sock with a method to check whether connected
This will be convenient in the I2P SAM implementation.
This commit is contained in:
@@ -250,6 +250,31 @@ std::string Sock::RecvUntilTerminator(uint8_t terminator,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Sock::IsConnected(std::string& errmsg) const
|
||||||
|
{
|
||||||
|
if (m_socket == INVALID_SOCKET) {
|
||||||
|
errmsg = "not connected";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char c;
|
||||||
|
switch (Recv(&c, sizeof(c), MSG_PEEK)) {
|
||||||
|
case -1: {
|
||||||
|
const int err = WSAGetLastError();
|
||||||
|
if (IOErrorIsPermanent(err)) {
|
||||||
|
errmsg = NetworkErrorString(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case 0:
|
||||||
|
errmsg = "closed";
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
std::string NetworkErrorString(int err)
|
std::string NetworkErrorString(int err)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -143,6 +143,13 @@ public:
|
|||||||
std::chrono::milliseconds timeout,
|
std::chrono::milliseconds timeout,
|
||||||
CThreadInterrupt& interrupt) const;
|
CThreadInterrupt& interrupt) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if still connected.
|
||||||
|
* @param[out] err The error string, if the socket has been disconnected.
|
||||||
|
* @return true if connected
|
||||||
|
*/
|
||||||
|
virtual bool IsConnected(std::string& errmsg) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/**
|
/**
|
||||||
* Contained socket. `INVALID_SOCKET` designates the object is empty.
|
* Contained socket. `INVALID_SOCKET` designates the object is empty.
|
||||||
|
|||||||
Reference in New Issue
Block a user