void ServicePort::accept(Acceptor_ptr acceptor) { try{ boost::asio::ip::tcp::socket* socket = new boost::asio::ip::tcp::socket(m_io_service); acceptor->async_accept(*socket, boost::bind(&ServicePort::onAccept, this, acceptor, socket, boost::asio::placeholders::error)); } catch(boost::system::system_error& e){ if(m_logError){ LOG_MESSAGE("NETWORK", LOGTYPE_ERROR, 1, e.what()); m_logError = false; } } }
void ServicePort::accept(Acceptor_ptr acceptor) { try { boost::asio::ip::tcp::socket* socket = new boost::asio::ip::tcp::socket(m_io_service); acceptor->async_accept(*socket, boost::bind( &ServicePort::handle, this, acceptor, socket, boost::asio::placeholders::error)); } catch(std::exception& e) { if(m_logError) { LOG_MESSAGE(LOGTYPE_ERROR, e.what(), "NETWORK") m_logError = false; } } }
void ServicePort::onAccept(Acceptor_ptr acceptor, boost::asio::ip::tcp::socket* socket, const boost::system::error_code& error) { if(!error){ if(m_services.empty()){ #ifdef __DEBUG_NET_DETAIL__ 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()){ // Only one handler, and it will send first connection->acceptConnection(m_services.front()->make_protocol(connection)); } else{ connection->acceptConnection(); } } else{ //close the socket 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::cout << "accept - OK" << std::endl; #endif accept(acceptor); } else{ if(error != boost::asio::error::operation_aborted){ close(); if(!m_pendingStart){ m_pendingStart = true; g_scheduler.addEvent(createSchedulerTask(5000, boost::bind(&ServicePort::openAcceptor, boost::weak_ptr<ServicePort>(shared_from_this()), acceptor->local_endpoint().address().to_v4(), m_serverPort))); } } else{ #ifdef __DEBUG_NET_DETAIL__ std::cout << "Error: [ServerPort::onAccept] Operation aborted." << std::endl; #endif } } }