Example #1
0
void ServicePort::handle(Acceptor_ptr acceptor, boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error)
{
	if(!error)
	{
		if(m_services.empty())
		{
#ifdef __DEBUG_NET__
			std::clog << "[Error - ServerPort::handle] No services running!" << std::endl;
#endif
			return;
		}

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

		uint32_t remoteIp = 0;
		if(!error)
			remoteIp = htonl(ip.address().to_v4().to_ulong());

		Connection_ptr connection;
		if(remoteIp && ConnectionManager::getInstance()->acceptConnection(remoteIp) &&
			(connection = ConnectionManager::getInstance()->createConnection(
			socket, m_io_service, shared_from_this())))
		{
			if(m_services.front()->isSingleSocket())
				connection->handle(m_services.front()->makeProtocol(connection));
			else
				connection->accept();
		}
		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;
		}

#ifdef __DEBUG_NET_DETAIL__
		std::clog << "handle - OK" << std::endl;
#endif
		accept(acceptor);
	}
	else if(error != boost::asio::error::operation_aborted)
	{
		PRINT_ASIO_ERROR("Handling");
		close();
		if(!m_pendingStart)
		{
			m_pendingStart = true;
			Scheduler::getInstance().addEvent(createSchedulerTask(5000, boost::bind(
				&ServicePort::service, boost::weak_ptr<ServicePort>(shared_from_this()),
				m_acceptors[acceptor], m_serverPort)));
		}
	}
#ifdef __DEBUG_NET__
	else
		std::clog << "[Error - ServerPort::handle] Operation aborted." << std::endl;
#endif
}
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->accept(service->make_protocol(connection));
			} else {
				connection->accept();
			}
		} 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,
			                     std::bind(&ServicePort::openAcceptor, std::weak_ptr<ServicePort>(shared_from_this()), m_serverPort)));
		}
	}
}
Example #3
0
void ServicePort::handle(boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error)
{
	if(!error)
	{
		if(m_services.empty())
		{
			return;
		}

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

		uint32_t remoteIp = 0;
		if(!error)
			remoteIp = htonl(ip.address().to_v4().to_ulong());

		Connection_ptr connection;
		if(remoteIp && ConnectionManager::getInstance()->acceptConnection(remoteIp) &&
			(connection = ConnectionManager::getInstance()->createConnection(
			socket, m_io_service, shared_from_this())))
		{
			if(m_services.front()->isSingleSocket())
				connection->handle(m_services.front()->makeProtocol(connection));
			else
				connection->accept();
		}
		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)
	{
		close();
		if(!m_pendingStart)
		{
			m_pendingStart = true;
			server.scheduler().addTask(SchedulerTask::create(Milliseconds(5000), std::bind(
				&ServicePort::onOpen, std::weak_ptr<ServicePort>(shared_from_this()), m_serverPort)));
		}
	}
}