Повышенная версия: 1.68
C ++ Стандарт: C ++ 17
Платформа разработки: MSVC 2017
Операционная система: Windows 10 Professional
ПКАрхитектура: x64
Я использую Boost :: Asio для создания асинхронного TCP-соединения.Во время первого успешного подключения все работает правильно.Из-за определенной проблемы, он разрывает сокет, он пытается повторно соединиться, и тогда я получаю ошибку во время выполнения.Даже при том, что я получаю ошибку во время выполнения, программа все еще в состоянии получить данные.
Сначала я пытался повторно соединить сокет в цикле главной функции while (бесконечный) (основной поток), но я получалОшибка.Я получаю сообщение об ошибке в
D: \ vcpkg \ Установлено \ x64-windows \ include \ boost \ asio \ detail \ impl \ win_iocp_io_context.ipp
size_t win_iocp_io_context::run(boost::system::error_code& ec)
{
if (::InterlockedExchangeAdd(&outstanding_work_, 0) == 0)
{
stop();
ec = boost::system::error_code();
return 0;
}
win_iocp_thread_info this_thread;
thread_call_stack::context ctx(this, this_thread);
size_t n = 0;
while (do_one(INFINITE, ec))
if (n != (std::numeric_limits<size_t>::max)())
++n;
return n;
}
на линии, когда n = 13
while (do_one (INFINITE, ec))
Я решил ошибку, добавив мой вызов подключения в мой обработчик получения и отправки, как только ониобнаруженное соединение разорвано или сокет не работает.
Может кто-нибудь объяснить, почему я столкнулся с проблемой при попытке переподключения из основного потока, и проблема была решена, когда я попытался переподключиться сразу после того, как сокет был разорван в потоке io_context.
Я перезапустил контекст до того, как вызвал команду run после потери соединения и выхода из потока io_context.
Пока цикл в моей основной функции:
while (true)
{
fmt::print("Socket Alive : {}\n", as.isSocketAlive());
while(not as.isSocketAlive() and not as.isConnectionInProcess())
as.connectSocket();
if (not as.isSocketAlive())
continue;
if (as.isReadComplete())
as.sendDataSocket(as.getRecievedData());
}
Мои функции асинхронного сокета:
void AsyncSocket::runContext(void)
{
std::atomic_store(std::addressof(this->contextExitted), false);
this->context.run();
std::atomic_store(std::addressof(this->contextExitted), true);
}
void AsyncSocket::restartContext(void)
{
if (std::atomic_load(std::addressof(this->contextExitted)))
this->context.restart();
}
void AsyncSocket::connectSocket(void)
{
std::atomic_store(std::addressof(this->socketAlive), false);
std::atomic_store(std::addressof(this->connectionInProcess), true);
//this->socket.async_connect(this->endpoint, std::bind(&AsyncSocket::connectHandler, this, boost::asio::placeholders::error));
this->socket.async_connect(this->endpoint, boost::bind(&AsyncSocket::connectHandler, this, boost::asio::placeholders::error));
//this->socket.connect(this->endpoint, );
if (this->context.stopped())
{
if (this->isContextExitted())
this->restartContext();
this->t = std::thread(&AsyncSocket::runContext, this);
}
std::call_once(this->firstRun, [this]() {
t = std::thread(&AsyncSocket::runContext, this);
});
}
void AsyncSocket::recieveHandler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
fmt::print("In recieve handler, Error Code : {}\nBytes recieved : {}\n", ec.message(), bytes_transferred);
try
{
std::atomic_store(std::addressof(this->receivingData), false);
if (ec not_eq boost::system::errc::success)
{
#ifdef _DEBUG
fmt::print("Error in WebSocket::recieveHandler. Error : {0}\n", ec.message());
#endif // _DEBUG
LOG_ERROR << ec.message();
switch (ec.value())
{
case boost::asio::error::eof :
case boost::asio::error::connection_reset :
case boost::asio::error::connection_aborted :
case boost::asio::error::network_reset :
case boost::asio::error::network_down :
case boost::asio::error::network_unreachable :
if (this->isSocketAlive() and this->socket.is_open())
this->socket.close();
std::atomic_store(std::addressof(this->socketAlive), false);
this->connectSocket();// If I comment this line and try to reconnect in my main function (infinite while loop), I get mentioned run-time error
return;
default:
break;
}
}
else
{
this->readDataQueue.push(std::string(reinterpret_cast <const char *> (this->readDataBuffer.data()), bytes_transferred));
std::atomic_store(std::addressof(this->readComplete), true);
}
}
catch (const std::exception& ex)
{
#ifdef _DEBUG
fmt::print("Error in WebSocket::sendHandler. Error : {0}\n", ex.what());
#endif // _DEBUG
LOG_ERROR << "Exception : " << ex.what() << "Data : " << this->writeDataQueue.front();
}
this->recieveDataSocket();
}
void AsyncSocket::sendHandler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
fmt::print("In send handler, Error Code : {}\nBytes recieved : {}\n", ec.message(), bytes_transferred);
try
{
if (ec not_eq boost::system::errc::success)
{
#ifdef _DEBUG
fmt::print("Error in WebSocket::recieveHandler. Error : {0}\n", ec.message());
#endif // _DEBUG
LOG_ERROR << ec.message();
switch (ec.value())
{
case boost::asio::error::eof:
case boost::asio::error::connection_reset:
case boost::asio::error::connection_aborted:
case boost::asio::error::network_reset:
case boost::asio::error::network_down:
case boost::asio::error::network_unreachable:
if (this->isSocketAlive() and this->socket.is_open())
this->socket.close();
std::atomic_store(std::addressof(this->socketAlive), false);
this->connectSocket();// If I comment this line and try to reconnect in my main function (infinite while loop), I get mentioned run-time error
return;
default:
break;
}
}
this->writeDataQueue.pop();
std::atomic_init(std::addressof(this->sendingData), false);
this->writeSocket();
}
catch (const std::exception& ex)
{
#ifdef _DEBUG
fmt::print("Error in WebSocket::sendHandler. Error : {0}\n", ex.what());
#endif // _DEBUG
LOG_ERROR << "Exception : " << ex.what() << "Data : " << this->writeDataQueue.front();
}
}