diff --git a/src/AsyncHTTPServer.cc b/src/AsyncHTTPServer.cc index ea62645e..db63db7e 100644 --- a/src/AsyncHTTPServer.cc +++ b/src/AsyncHTTPServer.cc @@ -335,7 +335,7 @@ asio::awaitable HTTPClient::recv_websocket_message(size_t max_ } else if (opcode == 0x08) { // Close message co_await this->send_websocket_message(msg.data, msg.opcode); - this->r.get_socket().close(); + this->r.close(); } else if (opcode == 0x09) { // Ping message @@ -343,7 +343,7 @@ asio::awaitable HTTPClient::recv_websocket_message(size_t max_ } else { // Unknown control message type - this->r.get_socket().close(); + this->r.close(); } continue; } @@ -351,7 +351,7 @@ asio::awaitable HTTPClient::recv_websocket_message(size_t max_ // If there's an existing fragment, the current message's opcode should be // zero; if there's no pending message, it must not be zero if (prev_msg_present == (opcode != 0)) { - this->r.get_socket().close(); + this->r.close(); continue; } diff --git a/src/AsyncUtils.hh b/src/AsyncUtils.hh index 94ff2689..60f621b1 100644 --- a/src/AsyncUtils.hh +++ b/src/AsyncUtils.hh @@ -189,8 +189,14 @@ public: return this->sock; } + inline bool is_open() const { + return this->sock.is_open(); + } + inline void close() { - this->sock.close(); + if (this->sock.is_open()) { + this->sock.close(); + } } private: diff --git a/src/GameServer.cc b/src/GameServer.cc index c606c433..6f25c9b4 100644 --- a/src/GameServer.cc +++ b/src/GameServer.cc @@ -118,7 +118,9 @@ vector> GameServer::get_clients_by_identifier(const string& i shared_ptr GameServer::create_client(shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock) { uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address()); if (this->state->banned_ipv4_ranges->check(addr)) { - client_sock.close(); + if (client_sock.is_open()) { + client_sock.close(); + } return nullptr; } diff --git a/src/IPStackSimulator.cc b/src/IPStackSimulator.cc index 75531032..a0e894a9 100644 --- a/src/IPStackSimulator.cc +++ b/src/IPStackSimulator.cc @@ -154,7 +154,9 @@ void IPSSClient::reschedule_idle_timeout() { this->idle_timeout_timer.async_wait([this, sim](std::error_code ec) { if (!ec) { sim->log.info_f("Idle timeout expired on N-{:X}", this->network_id); - this->sock.close(); + if (this->sock.is_open()) { + this->sock.close(); + } } }); } @@ -1233,11 +1235,9 @@ asio::awaitable IPStackSimulator::on_client_tcp_frame(shared_ptrserver_channel) { - this->log.warning_f("Client sent data on TCP connection {}, but server channel is missing", - conn_str); + this->log.warning_f("Client sent data on TCP connection {}, but server channel is missing", conn_str); } else if (!conn->server_channel->connected()) { - this->log.warning_f("Client sent data on TCP connection {}, but server channel is disconnected", - conn_str); + this->log.warning_f("Client sent data on TCP connection {}, but server channel is disconnected", conn_str); } else { conn->server_channel->add_inbound_data(payload, payload_size); } @@ -1425,7 +1425,9 @@ std::shared_ptr IPStackSimulator::create_client( std::shared_ptr listen_sock, asio::ip::tcp::socket&& client_sock) { uint32_t addr = ipv4_addr_for_asio_addr(client_sock.remote_endpoint().address()); if (this->state->banned_ipv4_ranges->check(addr)) { - client_sock.close(); + if (client_sock.is_open()) { + client_sock.close(); + } return nullptr; }