void Connection::handleWriteError(const boost::system::error_code& error) { #ifdef __DEBUG_NET_DETAIL__ PRINT_ASIO_ERROR("Writing - detail"); #endif if(error == boost::asio::error::operation_aborted) { //Operation aborted because connection will be closed //Do NOT call close() from here } else if(error == boost::asio::error::eof) { //No more to read close(); } else if(error == boost::asio::error::connection_reset || error == boost::asio::error::connection_aborted){ //Connection closed remotely close(); } else { PRINT_ASIO_ERROR("Writting"); close(); } m_writeError = true; }
void Connection::handleReadError(const boost::system::error_code& error) { #ifdef __DEBUG_NET_DETAIL__ PRINT_ASIO_ERROR("Reading - detail"); #endif boost::recursive_mutex::scoped_lock lockClass(m_connectionLock); if(error == boost::asio::error::operation_aborted){ //Operation aborted because connection will be closed //Do NOT call closeConnection() from here } else if(error == boost::asio::error::eof){ //No more to read closeConnection(); } else if(error == boost::asio::error::connection_reset || error == boost::asio::error::connection_aborted){ //Connection closed remotely closeConnection(); } else{ PRINT_ASIO_ERROR("Reading"); closeConnection(); } m_readError = true; }
bool Connection::closingConnection() { //any thread #ifdef __DEBUG_NET_DETAIL__ std::clog << "Connection::closingConnection" << std::endl; #endif if(m_pendingWrite == 0 || m_writeError == true) { if(!m_socketClosed) { #ifdef __DEBUG_NET_DETAIL__ std::clog << "Closing socket" << std::endl; #endif boost::system::error_code error; m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); if(error) { if(error == boost::asio::error::not_connected) { //Transport endpoint is not connected. } else PRINT_ASIO_ERROR("Shutdown"); } m_socket.close(error); m_socketClosed = true; if(error) PRINT_ASIO_ERROR("Close"); } if(m_pendingRead == 0) { #ifdef __DEBUG_NET_DETAIL__ std::clog << "Deleting Connection" << std::endl; #endif m_connectionLock.unlock(); Dispatcher::getInstance().addTask( createTask(boost::bind(&Connection::releaseConnection, this))); return true; } } return false; }
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 Connection::closeSocket() { #ifdef __DEBUG_NET_DETAIL__ std::cout << "Connection::closeSocket" << std::endl; #endif m_connectionLock.lock(); if(m_socket->is_open()){ #ifdef __DEBUG_NET_DETAIL__ std::cout << "Closing socket" << std::endl; #endif m_pendingRead = 0; m_pendingWrite = 0; try{ boost::system::error_code error; m_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); if(error){ if(error == boost::asio::error::not_connected){ //Transport endpoint is not connected. } else{ PRINT_ASIO_ERROR("Shutdown"); } } m_socket->close(error); if(error){ PRINT_ASIO_ERROR("Close"); } } catch(boost::system::system_error& e){ if(m_logError){ LOG_MESSAGE("NETWORK", LOGTYPE_ERROR, 1, e.what()); m_logError = false; } } } m_connectionLock.unlock(); }
uint32_t Connection::getIP() const { //ip is expressed in network byte order boost::system::error_code error; const boost::asio::ip::tcp::endpoint ip = m_socket->remote_endpoint(error); if(!error) return htonl(ip.address().to_v4().to_ulong()); PRINT_ASIO_ERROR("Getting remote ip"); return 0; }
void ServicePort::close() { for (std::vector<Acceptor_ptr>::iterator aptr = m_tcp_acceptors.begin(); aptr != m_tcp_acceptors.end(); ++aptr){ if((*aptr)->is_open()){ boost::system::error_code error; (*aptr)->close(error); if(error){ PRINT_ASIO_ERROR("Closing listen socket"); } } } m_tcp_acceptors.clear(); }
void Connection::handleWriteError(const boost::system::error_code& error) { #ifdef __DEBUG_NET_DETAIL__ PRINT_ASIO_ERROR("Writing - detail"); #endif boost::recursive_mutex::scoped_lock lockClass(m_connectionLock); if(error == boost::asio::error::operation_aborted) //Operation aborted because connection will be closed {} else if(error == boost::asio::error::eof || error == boost::asio::error::connection_reset || error == boost::asio::error::connection_aborted) { //Connection closed remotely or nothing more to read close(); } else { PRINT_ASIO_ERROR("Writing"); close(); } m_writeError = true; }
void ServicePort::close() { if(m_acceptor){ if(m_acceptor->is_open()){ boost::system::error_code error; m_acceptor->close(error); if(error){ PRINT_ASIO_ERROR("Closing listen socket"); } } delete m_acceptor; m_acceptor = NULL; } }
void ServicePort::close() { if(!m_acceptors.size()) return; for(AcceptorVec::iterator it = m_acceptors.begin(); it != m_acceptors.end(); ++it) { if(it->first->is_open()) continue; boost::system::error_code error; it->first->close(error); if(error) { PRINT_ASIO_ERROR("Closing listen socket"); } } m_acceptors.clear(); }