Exemplo n.º 1
0
  void
  onResolveSuccess(const boost::system::error_code& error,
                   BoostResolver::iterator remoteEndpoint,
                   const shared_ptr<Resolver>& self)
  {
    m_scheduler.cancelEvent(m_resolveTimeout);

    if (error)
      {
        if (error == boost::system::errc::operation_canceled)
          {
            return;
          }

        return m_onError("Remote endpoint hostname or port cannot be resolved: " +
                         error.category().message(error.value()));
      }

    BoostResolver::iterator end;
    for (; remoteEndpoint != end; ++remoteEndpoint)
      {
        IpAddress address(EndPoint(*remoteEndpoint).address());

        if (m_addressSelector(address))
          {
            return m_onSuccess(address);
          }
      }

    m_onError("No endpoint matching the specified address selector found");
  }
Exemplo n.º 2
0
 bool
 equivalent (boost::system::error_code const& error,
     int ev) const noexcept override
 {
     return error.value() == ev &&
         &error.category() == this;
 }
Exemplo n.º 3
0
  void
  onResolveSuccess(const boost::system::error_code& error,
                   typename resolver::iterator remoteEndpoint,
                   const shared_ptr<Resolver>& self)
  {
    scheduler::cancel(m_resolveTimeout);

    if (error)
      {
        if (error == boost::system::errc::operation_canceled)
          return;

        return m_onError("Remote endpoint hostname or port cannot be resolved: " +
                         error.category().message(error.value()));
      }

    typename resolver::iterator end;
    for (; remoteEndpoint != end; ++remoteEndpoint)
      {
        if (m_addressSelector(typename Protocol::endpoint(*remoteEndpoint).address()))
          return m_onSuccess(*remoteEndpoint);
      }

    m_onError("No endpoint matching the specified address selector found");
  }
Exemplo n.º 4
0
 // Record a reactor-based operation that is associated with a handler.
 static void reactor_operation(const tracked_handler& h,
     const char* op_name, const boost::system::error_code& ec)
 {
   std::printf(
       "Performed operation %s.%s for native_handle = %" PRIuMAX
       ", ec = %s:%d\n", h.object_type_, op_name, h.native_handle_,
       ec.category().name(), ec.value());
 }
Exemplo n.º 5
0
void OptiTrackNatNetClient::handle_data_receive(const boost::system::error_code& ec,
        std::size_t bytes_transferred)
{
    if (ec)
    {
        serr << ec.category().name() << " ERROR while receiving data from " << recv_data_endpoint << sendl;
    }
    else
    {
        sout << "Received " << bytes_transferred << "b data from " << recv_data_endpoint << sendl;
        sPacket& PacketIn = *recv_data_packet;
        switch (PacketIn.iMessage)
        {
        case NAT_MODELDEF:
        {
            sout << "Received MODELDEF" << sendl;
            if (serverInfoReceived)
            {
                decodeModelDef(PacketIn);
            }
            else if (!this->serverName.isSet())
            {
                server_endpoint = recv_data_endpoint;
                server_endpoint.port(PORT_COMMAND);
                serr << "Requesting server info to " << server_endpoint << sendl;
                boost::array<unsigned short, 2> helloMsg;
                helloMsg[0] = NAT_PING; helloMsg[1] = 0;
                command_socket->send_to(boost::asio::buffer(helloMsg), server_endpoint);
            }
            break;
        }
        case NAT_FRAMEOFDATA:
        {
            sout << "Received FRAMEOFDATA" << sendl;
            if (serverInfoReceived)
            {
                decodeFrame(PacketIn);
            }
            else if (!this->serverName.isSet())
            {
                server_endpoint = recv_data_endpoint;
                server_endpoint.port(PORT_COMMAND);
                serr << "Requesting server info to " << server_endpoint << sendl;
                boost::array<unsigned short, 2> helloMsg;
                helloMsg[0] = NAT_PING; helloMsg[1] = 0;
                command_socket->send_to(boost::asio::buffer(helloMsg), server_endpoint);
            }
            break;
        }
        default:
        {
            serr << "Received unrecognized data packet type: " << PacketIn.iMessage << sendl;
            break;
        }
        }
    }
    start_data_receive();
}
Exemplo n.º 6
0
	std::string FormatError(boost::system::error_code err)
	{
#if BOOST_OS_WINDOWS
		if (err.category() == boost::system::system_category())
			err.assign(err.value(), ext::boost_system_utf8_category());
#endif

		return FormatErrorImpl(err);
	}
Exemplo n.º 7
0
void ClientConnection::PostConnect(const boost::system::error_code& err,
                                   tcp::resolver::iterator endpoint_iterator) {
  if (err == asio::error::operation_aborted || err == asio::error::eof
      || connection_state_ == CLOSED) {
    return;
  }
  timer_.cancel();
  if (err) {
    delete endpoint_;
    endpoint_ = NULL;

    if (++endpoint_iterator != tcp::resolver::iterator()) {
      // Try next endpoint.
      CreateChannel();

      if (Logging::log->loggingActive(LEVEL_DEBUG)) {
        Logging::log->getLog(LEVEL_DEBUG) << "failed: next endpoint"
            << err.message() << "\n";
      }

      PostResolve(err, endpoint_iterator);
    } else {
      Reset();
      string ssl_error_info;
#ifdef HAS_OPENSSL
      if (err.category() == asio::error::ssl_category) {
        ssl_error_info = ERR_error_string(ERR_get_error(), NULL);
      }
#endif  // HAS_OPENSSL
      SendError(POSIX_ERROR_EIO,
                "could not connect to host '" + server_name_ + ":"
                    + server_port_ + "': " + err.message()+" "+ssl_error_info);
    }
  } else {
    // Do something useful.
    reconnect_interval_s_ = 1;
    next_reconnect_at_ = posix_time::not_a_date_time;

    if (Logging::log->loggingActive(LEVEL_DEBUG)) {
      Logging::log->getLog(LEVEL_DEBUG) << "connected to "
          << (*endpoint_iterator).host_name() << ":"
          << (*endpoint_iterator).service_name() << endl;
#ifdef HAS_OPENSSL
      if (ssl_context_ != NULL) {
        Logging::log->getLog(LEVEL_DEBUG) << "Using SSL/TLS version '"
            << ((SSLSocketChannel*) socket_)->ssl_tls_version() << "'." << endl;
      }
#endif  // HAS_OPENSSL
    }

    connection_state_ = IDLE;
    if (!requests_.empty()) {
      SendRequest();
      ReceiveRequest();
    }
  }
}
Exemplo n.º 8
0
 // Record a reactor-based operation that is associated with a handler.
 static void reactor_operation(const tracked_handler& h,
     const char* op_name, const boost::system::error_code& ec,
     std::size_t bytes_transferred)
 {
   std::printf(
       "Performed operation %s.%s for native_handle = %" PRIuMAX
       ", ec = %s:%d, n = %" PRIuMAX "\n", h.object_type_, op_name,
       h.native_handle_, ec.category().name(), ec.value(),
       static_cast<uintmax_t>(bytes_transferred));
 }
Exemplo n.º 9
0
 virtual bool isShutdownError(const boost::system::error_code& ec)
 {
    // boost returns "short_read" when the peer calls SSL_shutdown()
    if (ec.category() == boost::asio::error::get_ssl_category() &&
        ec.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ))
    {
       return true;
    }
    else
    {
       return false;
    }
 }
Exemplo n.º 10
0
void TCPServer::HandleAccept(SocketP socket, 
                             const boost::system::error_code &ec) 
    throw() {
  if (!ec) {    
    socket->lowest_layer().set_option(tcp::no_delay(true));
    listeners_.push_back(ServerP(
        new SocketServer(socket, protocol_, 
                         request_processor_->Duplicate())));
    listeners_.back()->Listen();
  } else {
    LOG4CPLUS_WARN(logger_, 
		   WStringFromUTF8String(
				  std::string("Error in TCPServer::HandleAccept: ")
                                  + ec.category().name() + " " 
                                  + boost::lexical_cast<std::string>(ec.value())));
  }
  StartAccept();
}
Exemplo n.º 11
0
 void handle_read(const boost::system::error_code& error,
                  size_t bytes_transferred)
 {
     if (!error)
     {
         total_transferred_ += bytes_transferred;
         if (total_transferred_ >= data_required)
         {
             // std::cout << total_transferred_ << std::endl;
             return;
         }
         send_request();
     }
     else
     {
         std::cerr << error.value() << std::endl;
         std::cerr << error.category().name() << std::endl;
     }
 }
Exemplo n.º 12
0
void HttpRequest::on_body(boost::system::error_code const &err, size_t xfer)
{
    LogSpam << "HttpRequest::on_body()";
    if (!!err)
    {
        LogWarning << "HttpRequest::on_body(): " << err;
        if ((xfer > 0) &&
            (err.category() != boost::asio::error::get_misc_category() ||
                err.value() != boost::asio::error::eof))
        {
            error();
            return;
        }
    }
    if (debugHttp.enabled())
    {
        LogNotice << "http on_body" << xfer << "bytes";
    }
    //  got the body!
    onBody_();
    onBody_.disconnect_all_slots();
    onError_.disconnect_all_slots();
}
Exemplo n.º 13
0
void MOB_Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
	if (error)
	{
		jWARN("WARN: %s\n",error.message().c_str());
		jWARN("> category=%s , value=%d\n", error.category().name() ,error.value());
		
		call_MOB_OnDisconnect("socket out error",0);
		return;
	}
	const int iSIZE_m_pDataBuff_Readed = nMOB::BUFFER_SIZE*8;
	const int iHEADER_SIZE= sizeof(int);

	if((bytes_transferred + m_iReadedSize) >= iSIZE_m_pDataBuff_Readed )
	{
		DoDisconnect(("socket out  if((bytes_transferred+ m_iReadedSize)>= iSIZE_m_pDataBuff_Readed)"),0);
		return;
	}

	memcpy(m_pDataBuff_Readed+m_iReadedSize,m_pDataBuff_Read,bytes_transferred);
	m_iReadedSize+=bytes_transferred;

	if(m_iReadedSize<= iHEADER_SIZE)
	{
		_async_read_some();
		return;
	}

	char* pBody = m_pDataBuff_Readed;
	int iLengthBody=0;
	memcpy(&iLengthBody , pBody, iHEADER_SIZE);
	pBody+=iHEADER_SIZE;

	if(iLengthBody<=0 || (iLengthBody>=(BUFFER_SIZE-iHEADER_SIZE) ) )
	{
		DoDisconnect(("socket out  error if(iLengthBody<=0 || (iLengthBody>=(BUFFER_SIZE-iHEADER_SIZE) ) )"),0);
		return;
	}

	if(iLengthBody > (m_iReadedSize-iHEADER_SIZE) )
	{
		// 바디가 iLengthBody만큼 못읽었으모.
		_async_read_some();
		return;
	}

	size_t iStayReadedSize = m_iReadedSize;
	try
	{
		for(;;)
		{
			if(!m_pPlugin->MOB_OnReadPacket(pBody, iLengthBody))
			{
				DoDisconnect(__FUNCTION__,0);
				return;
			}
			iStayReadedSize -= (iLengthBody+iHEADER_SIZE);
			if(iStayReadedSize<=0)
			{
				m_iReadedSize=0;
				break;
			}

			jWARN(_T("data뭉쳐서왔다."));
			if(iStayReadedSize<= iHEADER_SIZE)
			{
				memmove(m_pDataBuff_Readed, pBody, iStayReadedSize);
				m_iReadedSize=iStayReadedSize;
				break;
			}
			//파싱할 패킷이 남았다.
			pBody = pBody+iLengthBody;
			memcpy(&iLengthBody,pBody,iHEADER_SIZE);
			if(iLengthBody > (int)(iStayReadedSize-iHEADER_SIZE ) ) //아직 바디 데이타가 완성안되었으모.
			{
				memmove(m_pDataBuff_Readed, pBody, iLengthBody);
				m_iReadedSize=iLengthBody;
				break;
			}
			pBody+=iHEADER_SIZE;

		}//for(;;)
		_async_read_some();

	}
	catch (...) 
	{
		DoDisconnect("MOB_Connection::handle_read try",0);
	}
}
 bool equivalent (boost::system::error_code const& code, int ev) const noexcept
 {
     return *this == code.category() && code.value() == ev;
 }
void downloader_instance_simple::handle_read(const boost::system::error_code& ec, std::size_t length)
{
    if (!ec)
    {
        ofstream_.write(buffer_, length);
        readed_bytes_ += length;
        
        if (owner()->progress_notify_)
            owner()->progress_notify_(readed_bytes_);


        {
            boost::mutex::scoped_lock lock(owner()->stateGuard_);

            while (owner()->command_ != http_downloader_simple::commandNull)
            {
                switch (owner()->command_)
                {
                case http_downloader_simple::commandPause:
                    {
                        owner()->state_ = http_downloader_simple::statePaused;
                        owner()->command_ = http_downloader_simple::commandNull;

                        if (owner()->state_notify_)
                            owner()->state_notify_(http_downloader::statePause);

                        while (
                            owner()->command_ != http_downloader_simple::commandCancel &&
                            owner()->command_ != http_downloader_simple::commandResume
                            )   
                        {
                            lock.unlock();
                            boost::this_thread::sleep( boost::posix_time::milliseconds(100) ); 
                            lock.lock();                                                                                                
                        }

                        if (owner()->command_ == http_downloader_simple::commandResume)
                        {
                            owner()->state_ = http_downloader_simple::stateDownload;
                            owner()->command_ = http_downloader_simple::commandNull;
                            if (owner()->state_notify_)
                                owner()->state_notify_(http_downloader::stateResume);
                        }

                    }
                    break;

                case http_downloader_simple::commandCancel:
                    {
                        owner()->command_ = http_downloader_simple::commandNull;
                        ofstream_.close();
                        if (owner()->state_notify_)
                            owner()->state_notify_(http_downloader::stateCancel);
                        return;
                    }
                    break;

                case http_downloader_simple::commandResume:
                    break;

                }
            }
            
        }

        read_stream_.async_read_some(
            boost::asio::buffer(buffer_),
            boost::bind(&downloader_instance_simple::handle_read,
            shared_from_this(), _1, _2));
    } 
    else
    {
        ofstream_.close();
        if ( boost::asio::error::misc_category == ec.category() )
        {
            if (boost::asio::error::eof == ec.value())
            {
                if (owner()->state_notify_)
                    owner()->state_notify_(http_downloader::stateFinish);    
            }
        }
        else
        {
                if (owner()->state_notify_)
                    owner()->state_notify_(http_downloader::stateError);    
        }
    }
}
Exemplo n.º 16
0
        error::errors last_error_enum(
            boost::system::error_code const & ec)
        {
            if (!ec) {
                return success;
            } else if (ec == boost::asio::error::would_block) {
                return would_block;
            } else if (ec == ppbox::demux::error::no_more_sample) {
                return stream_end;
            } else if (ec == boost::asio::error::operation_aborted) {
                return operation_canceled;
            } else if (ec == boost::asio::error::connection_aborted) {
                return operation_canceled;
            } else if (ec.category() == ppbox::error::get_category()) {
                return (error::errors)ec.value();
            } else if (ec.category() == ppbox::certify::error::get_category()) {
                return certify_error;
#ifdef PPBOX_CERTIFY_ERROR_AUTH_ERRORS
            } else if (ec.category() == ppbox::certify::error::get_auth_category()) {
                return certify_error;
#endif
#ifdef PPBOX_CERTIFY_ERROR_MUTEX_ERRORS
            } else if (ec.category() == ppbox::certify::error::get_mutex_category()) {
                return certify_error;
#endif
            } else if (ec.category() == ppbox::demux::error::get_category()) {
                return demux_error;
            } else if (ec.category() == framework::system::logic_error::get_category()) {
                return ppbox::error::logic_error;
            } else if (ec.category() == util::protocol::http_error::get_category()) {
                return network_error;
            } else if (ec.category() == boost::asio::error::get_system_category()) {
                return network_error;
            } else if (ec.category() == boost::asio::error::get_netdb_category()) {
                return network_error;
            } else if (ec.category() == boost::asio::error::get_addrinfo_category()) {
                return network_error;
            } else if (ec.category() == boost::asio::error::get_misc_category()) {
                return network_error;
            } else {
                return other_error;
            }
        }
Exemplo n.º 17
0
	/// Translate network error to processor error
	void signalError( const boost::system::error_code& e )
	{
		ConnectionHandler::NetworkSignal	ns;
		std::string				name;

		int errorcode = e.value();

#if defined(_WIN32)
#ifdef WITH_SSL
		// Rewrite error code got for a missing SSL_shutdown to a connection reset
		// because on Windows SSL_shutdown may not be called:
		if (e.category() == boost::asio::error::get_ssl_category()
		&&  e.value() == ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ))
		{
			LOG_DEBUG << "Connection terminated abruptly by client, got no SSL_shutdown(); "
					<< "error: " << e.value() << ", category: " << e.category().name()
					<< ", message: " << e.message();
			errorcode = boost::asio::error::connection_reset;
		}
#endif
#endif
		switch( errorcode )	{
			case boost::asio::error::eof :
				ns = ConnectionHandler::END_OF_FILE;
				name = "EOF";
				break;
	
			case boost::asio::error::operation_aborted :
				ns = ConnectionHandler::OPERATION_CANCELLED;
				name = "OPERATION CANCELLED";
				break;
	
			case boost::asio::error::broken_pipe :
				ns = ConnectionHandler::BROKEN_PIPE;
				name = "BROKEN PIPE";
				break;
	
			case boost::asio::error::connection_reset :
				ns = ConnectionHandler::CONNECTION_RESET;
				name = "CONNECTION RESET";
				break;
			default:	{
				std::string err = e.message();
#ifdef WITH_SSL
				if ( e.category() == boost::asio::error::get_ssl_category() )	{
					err = std::string( "(" )
							+ boost::lexical_cast< std::string >( ERR_GET_LIB( e.value() ) ) + ", "
							+ boost::lexical_cast< std::string >( ERR_GET_FUNC( e.value() ) )+ ", "
							+ boost::lexical_cast< std::string >( ERR_GET_REASON( e.value() ) ) + ")";
					//ERR_PACK /* crypto/err/err.h */
					char buf[ 128 ];
					::ERR_error_string_n( e.value(), buf, sizeof( buf ) );
					err += buf;
				}
#endif // WITH_SSL
				LOG_DEBUG << "Unknown error: " << e.value() << ", category: " << e.category().name()
					  << ", message: " << err;
				ns = ConnectionHandler::UNKNOWN_ERROR;
				name = "UNKNOWN ERROR";
				break;
			}
		}
		m_connHandler->signalOccured( ns );
		LOG_DATA << "Signalled " << name << " to processor for connection to " << identifier();
	}
Exemplo n.º 18
0
void OptiTrackNatNetClient::handle_command_receive(const boost::system::error_code& ec,
        std::size_t bytes_transferred)
{
    if (ec)
    {
        serr << ec.category().name() << " ERROR while receiving command from " << recv_command_endpoint << sendl;
    }
    else
    {
        sout << "Received " << bytes_transferred << "b command from " << recv_command_endpoint << sendl;
        sPacket& PacketIn = *recv_command_packet;
        switch (PacketIn.iMessage)
        {
        case NAT_MODELDEF:
        {
            sout << "Received MODELDEF" << sendl;
            if (serverInfoReceived)
            {
                decodeModelDef(PacketIn);
            }
            else if (!this->serverName.isSet())
            {
                server_endpoint = recv_command_endpoint;
                server_endpoint.port(PORT_COMMAND);
                serr << "Requesting server info to " << server_endpoint << sendl;
                boost::array<unsigned short, 2> helloMsg;
                helloMsg[0] = NAT_PING; helloMsg[1] = 0;
                command_socket->send_to(boost::asio::buffer(helloMsg), server_endpoint);
            }
            break;
        }
        case NAT_FRAMEOFDATA:
        {
            sout << "Received FRAMEOFDATA" << sendl;
            if (serverInfoReceived)
            {
                decodeFrame(PacketIn);
            }
            else if (!this->serverName.isSet())
            {
                server_endpoint = recv_command_endpoint;
                server_endpoint.port(PORT_COMMAND);
                serr << "Requesting server info to " << server_endpoint << sendl;
                boost::array<unsigned short, 2> helloMsg;
                helloMsg[0] = NAT_PING; helloMsg[1] = 0;
                command_socket->send_to(boost::asio::buffer(helloMsg), server_endpoint);
            }
            break;
        }
        case NAT_PINGRESPONSE:
        {
            serverInfoReceived = true;
            serverString = PacketIn.Data.Sender.szName;
            for(int i=0; i<4; i++)
            {
                natNetVersion[i] = PacketIn.Data.Sender.NatNetVersion[i];
                serverVersion[i] = PacketIn.Data.Sender.Version[i];
            }
            serr << "Connected to server \"" << serverString << "\" v" << (int)serverVersion[0];
            if (serverVersion[1] || serverVersion[2] || serverVersion[3])
                serr << "." << (int)serverVersion[1];
            if (serverVersion[2] || serverVersion[3])
                serr << "." << (int)serverVersion[2];
            if (serverVersion[3])
                serr << "." << (int)serverVersion[3];
            serr << " protocol v" << (int)natNetVersion[0];
            if (natNetVersion[1] || natNetVersion[2] || natNetVersion[3])
                serr << "." << (int)natNetVersion[1];
            if (natNetVersion[2] || natNetVersion[3])
                serr << "." << (int)natNetVersion[2];
            if (natNetVersion[3])
                serr << "." << (int)natNetVersion[3];
            serr << sendl;
            // request scene info
            boost::array<unsigned short, 2> reqMsg;
            reqMsg[0] = NAT_REQUEST_MODELDEF; reqMsg[1] = 0;
            command_socket->send_to(boost::asio::buffer(reqMsg), server_endpoint);
            break;
        }
        case NAT_RESPONSE:
        {
            sout << "Received response : " << PacketIn.Data.szData << sendl;
            break;
        }
        case NAT_UNRECOGNIZED_REQUEST:
        {
            serr << "Received 'unrecognized request'" << sendl;
            break;
        }
        case NAT_MESSAGESTRING:
        {
            sout << "Received message: " << PacketIn.Data.szData << sendl;
            break;
        }
        default:
        {
            serr << "Received unrecognized command packet type: " << PacketIn.iMessage << sendl;
            break;
        }
        }
    }
    start_command_receive();
}
Exemplo n.º 19
0
 error(const boost::system::error_code& ec)  noexcept :  category(err::detail::cat_from_boost_system( ec.category() ) ), value(ec.value()) {}