bool ServicePort::add_service(Service_ptr new_svc)
{
	for(std::vector<Service_ptr>::const_iterator svc_iter = m_services.begin(); svc_iter != m_services.end(); ++svc_iter){
		Service_ptr svc = *svc_iter;
		if(svc->is_single_socket())
			return false;
	}

	m_services.push_back(new_svc);
	return true;
}
Beispiel #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)));
		}
	}
}