Opened 5 years ago

#13086 new Support Requests

Exception Throw -> { boost::asio::ip::tcp::aceptor * acceptor->open(); }

Reported by: ya.tarakanov.ilya@… Owned by: chris_kohlhoff
Milestone: To Be Determined Component: asio
Version: Boost 1.63.0 Severity: Problem
Keywords: Cc:

Description

asio.h
#include "boost/asio.hpp"

struct SNetGlobal
{
	static boost::asio::io_service * GetStcIOService();
	static bool RunIOServiceInThreads(int nCountThr = 1);
};

asio.cpp
#include "asio.h"
#include <vector>
#include <thread>

static boost::asio::io_service * s_io_service = NULL;

static std::vector<std::thread> s_thread_pool;

io_service * SNetGlobal::GetStcIOService()
{
	if (!s_io_service)
	{
		s_io_service = new io_service;
	}
	return s_io_service;
}

bool SNetGlobal::RunIOServiceInThreads(int nCountThr)
{
	while (nCountThr--)
	{
		s_thread_pool.emplace_back([=] { s_io_service->run(); });
	}
	return true;
}

listener.h
	class CUCListener 
	{
	public:
		void Start();
		void OnAccept(const boost::system::error_code & ec);
	private:
		tcp::acceptor * m_pAcceptor;
		tcp::socket * m_pSocket;
	};

listener.cpp
#include "asio.h"
#include "listener.h"

void CUCListener ::Start()
{
tcp::endpoint endpoint(tcp::v4(), 1974);
m_pAcceptor = new tcp::aceptor(*SNetGlobal::GetStcIOService());
m_pAcceptor->open(endpoint.protocol());
m_pAcceptor->set_option(tcp::acceptor::reuse_address(true));
m_pAcceptor->bind(endpoint);
m_pAcceptor->listen();
m_pAcceptor->async_accept(*m_pSocket, boost::bind(&CUCListener::OnAccept, this, boost::asio::placeholders::error));
SNetGlobal::RunIOServiceInThreads();
}

void CUCListener::OnAccept(const boost::system::error_code & ec)
{
	std::cout << "Accept Success" << std::endl;
}

Hi, sometimes throw exception and function CUCListener::OnAccept not called when connected

socket_ops.ipp

socket_type socket(int af, int type, int protocol,
    boost::system::error_code& ec)
{
  clear_last_error();
#if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  socket_type s = error_wrapper(::WSASocketW(af, type, protocol, 0, 0,
        WSA_FLAG_OVERLAPPED), ec); // Exception Throw
  if (s == invalid_socket)
    return s;
...
}

Change History (0)

Note: See TracTickets for help on using tickets.