mirror of
https://github.com/bitcoin/bitcoin.git
synced 2024-05-17 23:56:39 +00:00
i2p: avoid using Sock::Get() for checking for a valid socket
Peeking at the underlying socket file descriptor of `Sock` and checkig if it is `INVALID_SOCKET` is bad encapsulation and stands in the way of testing/mocking/fuzzing. Instead use an empty unique_ptr to denote that there is no valid socket.
This commit is contained in:
parent
083316c4fe
commit
5ac1a51ee5
10
src/i2p.cpp
10
src/i2p.cpp
@ -119,7 +119,6 @@ Session::Session(const fs::path& private_key_file,
|
|||||||
: m_private_key_file{private_key_file},
|
: m_private_key_file{private_key_file},
|
||||||
m_control_host{control_host},
|
m_control_host{control_host},
|
||||||
m_interrupt{interrupt},
|
m_interrupt{interrupt},
|
||||||
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
|
|
||||||
m_transient{false}
|
m_transient{false}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -127,7 +126,6 @@ Session::Session(const fs::path& private_key_file,
|
|||||||
Session::Session(const CService& control_host, CThreadInterrupt* interrupt)
|
Session::Session(const CService& control_host, CThreadInterrupt* interrupt)
|
||||||
: m_control_host{control_host},
|
: m_control_host{control_host},
|
||||||
m_interrupt{interrupt},
|
m_interrupt{interrupt},
|
||||||
m_control_sock{std::make_unique<Sock>(INVALID_SOCKET)},
|
|
||||||
m_transient{true}
|
m_transient{true}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -315,7 +313,7 @@ void Session::CheckControlSock()
|
|||||||
LOCK(m_mutex);
|
LOCK(m_mutex);
|
||||||
|
|
||||||
std::string errmsg;
|
std::string errmsg;
|
||||||
if (!m_control_sock->IsConnected(errmsg)) {
|
if (m_control_sock && !m_control_sock->IsConnected(errmsg)) {
|
||||||
Log("Control socket error: %s", errmsg);
|
Log("Control socket error: %s", errmsg);
|
||||||
Disconnect();
|
Disconnect();
|
||||||
}
|
}
|
||||||
@ -364,7 +362,7 @@ Binary Session::MyDestination() const
|
|||||||
void Session::CreateIfNotCreatedAlready()
|
void Session::CreateIfNotCreatedAlready()
|
||||||
{
|
{
|
||||||
std::string errmsg;
|
std::string errmsg;
|
||||||
if (m_control_sock->IsConnected(errmsg)) {
|
if (m_control_sock && m_control_sock->IsConnected(errmsg)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,14 +435,14 @@ std::unique_ptr<Sock> Session::StreamAccept()
|
|||||||
|
|
||||||
void Session::Disconnect()
|
void Session::Disconnect()
|
||||||
{
|
{
|
||||||
if (m_control_sock->Get() != INVALID_SOCKET) {
|
if (m_control_sock) {
|
||||||
if (m_session_id.empty()) {
|
if (m_session_id.empty()) {
|
||||||
Log("Destroying incomplete SAM session");
|
Log("Destroying incomplete SAM session");
|
||||||
} else {
|
} else {
|
||||||
Log("Destroying SAM session %s", m_session_id);
|
Log("Destroying SAM session %s", m_session_id);
|
||||||
}
|
}
|
||||||
|
m_control_sock.reset();
|
||||||
}
|
}
|
||||||
m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
|
|
||||||
m_session_id.clear();
|
m_session_id.clear();
|
||||||
}
|
}
|
||||||
} // namespace sam
|
} // namespace sam
|
||||||
|
@ -261,6 +261,7 @@ private:
|
|||||||
* ("SESSION CREATE"). With the established session id we later open
|
* ("SESSION CREATE"). With the established session id we later open
|
||||||
* other connections to the SAM service to accept incoming I2P
|
* other connections to the SAM service to accept incoming I2P
|
||||||
* connections and make outgoing ones.
|
* connections and make outgoing ones.
|
||||||
|
* If not connected then this unique_ptr will be empty.
|
||||||
* See https://geti2p.net/en/docs/api/samv3
|
* See https://geti2p.net/en/docs/api/samv3
|
||||||
*/
|
*/
|
||||||
std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);
|
std::unique_ptr<Sock> m_control_sock GUARDED_BY(m_mutex);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user