Only close/unmanage the socket if we are connected/connecting
Since the socket is now only created and managed whenever the connection is being established, we only close the socket and if it was created (we use -1 to denote the fact that is not yet created, or has been closed) and we only unmanage the socket if it is effectively managed. fix #2529
This commit is contained in:
@@ -24,6 +24,7 @@ using namespace std::string_literals;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
SocketHandler::SocketHandler(std::shared_ptr<Poller> poller):
|
SocketHandler::SocketHandler(std::shared_ptr<Poller> poller):
|
||||||
|
socket(-1),
|
||||||
poller(poller),
|
poller(poller),
|
||||||
connected(false),
|
connected(false),
|
||||||
connecting(false)
|
connecting(false)
|
||||||
@@ -33,7 +34,7 @@ SocketHandler::SocketHandler(std::shared_ptr<Poller> poller):
|
|||||||
void SocketHandler::init_socket(const struct addrinfo* rp)
|
void SocketHandler::init_socket(const struct addrinfo* rp)
|
||||||
{
|
{
|
||||||
if ((this->socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
|
if ((this->socket = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) == -1)
|
||||||
throw std::runtime_error("Could not create socket");
|
throw std::runtime_error("Could not create socket: "s + strerror(errno));
|
||||||
int optval = 1;
|
int optval = 1;
|
||||||
if (::setsockopt(this->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1)
|
if (::setsockopt(this->socket, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1)
|
||||||
log_warning("Failed to enable TCP keepalive on socket: " << strerror(errno));
|
log_warning("Failed to enable TCP keepalive on socket: " << strerror(errno));
|
||||||
@@ -230,13 +231,18 @@ void SocketHandler::on_send()
|
|||||||
|
|
||||||
void SocketHandler::close()
|
void SocketHandler::close()
|
||||||
{
|
{
|
||||||
|
if (this->connected || this->connecting)
|
||||||
|
this->poller->remove_socket_handler(this->get_socket());
|
||||||
|
if (this->socket != -1)
|
||||||
|
{
|
||||||
|
::close(this->socket);
|
||||||
|
this->socket = -1;
|
||||||
|
}
|
||||||
this->connected = false;
|
this->connected = false;
|
||||||
this->connecting = false;
|
this->connecting = false;
|
||||||
this->in_buf.clear();
|
this->in_buf.clear();
|
||||||
this->out_buf.clear();
|
this->out_buf.clear();
|
||||||
this->port.clear();
|
this->port.clear();
|
||||||
this->poller->remove_socket_handler(this->get_socket());
|
|
||||||
::close(this->socket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_t SocketHandler::get_socket() const
|
socket_t SocketHandler::get_socket() const
|
||||||
|
|||||||
Reference in New Issue
Block a user