Esempio n. 1
0
void ServicePort::onAccept(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error)
{
	if(!error)
	{
		if(m_services.empty())
		{
#ifdef __DEBUG_NET__
			std::cout << "Error: [ServerPort::accept] No services running!" << std::endl;
#endif
			return;
		}

		boost::system::error_code error;
		const boost::asio::ip::tcp::endpoint endpoint = socket->remote_endpoint(error);
		uint32_t remote_ip = 0;
		if(!error)
			remote_ip = htonl(endpoint.address().to_v4().to_ulong());

		if(remote_ip != 0/* && g_bans.acceptConnection(remote_ip)*/)
		{
			Connection_ptr connection = ConnectionManager::getInstance()->createConnection(socket, m_io_service, shared_from_this());
			if(m_services.front()->is_single_socket())
				connection->acceptConnection(m_services.front()->make_protocol(connection));
			else
				connection->acceptConnection();
		}
		else if(socket->is_open())
		{
			boost::system::error_code error;
			socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
			socket->close(error);
			delete socket;
		}
		accept();
	}
	else if(error != boost::asio::error::operation_aborted)
	{
		if(!m_pendingStart)
		{
			close();
			m_pendingStart = true;
			g_scheduler.addEvent(createSchedulerTask(15000,
				boost::bind(&ServicePort::openAcceptor, boost::weak_ptr<ServicePort>(shared_from_this()), m_serverPort)));
		}
	}
	else
	{
		#ifdef __DEBUG_NET__
		std::cout << "Error: [ServicePort::onAccept] Operation aborted." << std::endl;
		#endif
	}
}
Esempio n. 2
0
void ServicePort::onAccept(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error)
{
	if (!error) {
		if (m_services.empty()) {
			return;
		}

		boost::system::error_code socketError;
		const boost::asio::ip::tcp::endpoint endpoint = socket->remote_endpoint(socketError);

		uint32_t remote_ip = 0;
		if (!socketError) {
			remote_ip = htonl(endpoint.address().to_v4().to_ulong());
		}

		if (remote_ip != 0 && g_bans.acceptConnection(remote_ip)) {
			Connection_ptr connection = ConnectionManager::getInstance()->createConnection(socket, m_io_service, shared_from_this());
			Service_ptr service = m_services.front();

			if (service->is_single_socket()) {
				connection->acceptConnection(service->make_protocol(connection));
			} else {
				connection->acceptConnection();
			}
		} else if (socket->is_open()) {
			socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, socketError);
			socket->close(socketError);
			delete socket;
		}

		accept();
	} else if (error != boost::asio::error::operation_aborted) {
		if (!m_pendingStart) {
			close();
			m_pendingStart = true;
			g_scheduler.addEvent(createSchedulerTask(15000,
			                     boost::bind(&ServicePort::openAcceptor, boost::weak_ptr<ServicePort>(shared_from_this()), m_serverPort)));
		}
	}
}