websocketpp icon indicating copy to clipboard operation
websocketpp copied to clipboard

Cannot reinit asio after .dll shutdown

Open chunkbanned opened this issue 1 year ago • 0 comments

Hello, me and my friend are a plugin developer for a game called Euro Truck Simulator 2 We have an issue where asio will not be unloaded even if we unload the plugin, using the in-game command sdk unload, and the file is still in use after doing so. The error is always Invalid State We have looked everywhere for a solution and haven't found anything. Here is the code

#include <iostream>
#include <thread>
#include <vector>
#include <iostream>

#include <boost/asio/signal_set.hpp>
#include "websocketpp/config/asio_no_tls_client.hpp"
#include "websocketpp/client.hpp"

typedef websocketpp::client<websocketpp::config::asio_client> client;

using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;

client c;
client::connection_ptr con;

void webSocket::init() {
    try {
        c.init_asio();
    }
    catch (const std::exception& e)
    {
        std::cout << "Failed to init asio. Error: " << e.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "Failed to init asio. Unknown error occurred." << std::endl;
    }
}

void webSocket::connect() {

    std::string uri = "url redacted";

    c.set_access_channels(websocketpp::log::alevel::all);
    c.clear_access_channels(websocketpp::log::alevel::frame_payload);

    websocketpp::lib::error_code ec;
    con = c.get_connection(uri, ec);
    if (ec) {
        std::cout << "could not create connection because: " << ec.message() << std::endl;
    }

    c.connect(con);

    c.run();
}

void webSocket::disconnect() {
    boost::asio::io_service io_service;
    boost::asio::io_context io_context;
    boost::asio::ip::tcp::socket socket(io_context);

    con->close(websocketpp::close::status::normal, "");
    socket.close();

    io_service.stop();
    io_service.~io_service();
    new(&io_service) boost::asio::io_service;

    io_context.stop();
    c.reset();
    con = nullptr;
}

If we could have some help it would be great, thank you!

chunkbanned avatar Mar 28 '24 20:03 chunkbanned