コード例 #1
0
void xtcp_server::start_accept()
{
    xdebug_info(_X("Accepting connection..."));
    xtcp_io_object_ptr io_object(new xtcp_io_object(io_service()));
    acceptor_.async_accept(io_object->socket(),
            xbind(&xtcp_server::on_accept, this, io_object, xplaceholders::error));
}
コード例 #2
0
ファイル: player.cpp プロジェクト: Andlon/aria
 void player::connect_speaker(const std::string &host, uint16_t port)
 {
     typedef tcp::resolver::iterator iterator;
     tcp::resolver::query query(host, std::to_string(port));
     _resolver.async_resolve(query, [this] (const error_code & error,
                             iterator endpoint_iterator) {
         if (!error)
         {
             auto socket = std::make_shared<tcp::socket>(io_service());
             boost::asio::async_connect(*socket, endpoint_iterator,
                                        [this, socket] (const error_code & error,
                                        iterator endpoint)
             {
                 iterator end;
                 if (!error && endpoint != end)
                 {
                     run_on_main_thread([=] {
                         _sockets.push_back(socket);
                         _callbacks->connected();
                     });
                 }
                 else
                 {
                     run_on_main_thread([this] { _callbacks->connection_failed(); });
                 }
             });
         }
         else
         {
             // TODO: error handling when resolution fails
         }
     });
 }
コード例 #3
0
ファイル: main.cpp プロジェクト: nannanwuhui/C-
int main( int argc, char * argv[] )
{
	boost::shared_ptr< boost::asio::io_service > io_service(
		new boost::asio::io_service
	);
	boost::shared_ptr< boost::asio::io_service::work > work(
		new boost::asio::io_service::work( *io_service )
	);

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Press [return] to exit." << std::endl;
	global_stream_lock.unlock();

	boost::thread_group worker_threads;
	for( int x = 0; x < 2; ++x )
	{
		worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
	}

	boost::shared_ptr< boost::asio::deadline_timer > timer(
		new boost::asio::deadline_timer( *io_service )
	);
	timer->expires_from_now( boost::posix_time::seconds( 5 ) );
	timer->async_wait( boost::bind( &TimerHandler, _1, timer ) );

	std::cin.get();

	io_service->stop();

	worker_threads.join_all();

	return 0;
}
コード例 #4
0
ファイル: io_service_pool.cpp プロジェクト: Bcorde5/hpx
    io_service_pool::io_service_pool(std::size_t pool_size,
            util::function_nonser<void(std::size_t, char const*)> const& on_start_thread,
            util::function_nonser<void()> const& on_stop_thread,
            char const* pool_name, char const* name_postfix)
      : next_io_service_(0), stopped_(false),
        pool_size_(pool_size),
        on_start_thread_(on_start_thread), on_stop_thread_(on_stop_thread),
        pool_name_(pool_name), pool_name_postfix_(name_postfix)
    {
        if (pool_size == 0)
        {
            HPX_THROW_EXCEPTION(bad_parameter,
                "io_service_pool::io_service_pool",
                "io_service_pool size is 0");
            return;
        }

        // Give all the io_services work to do so that their run() functions
        // will not exit until they are explicitly stopped.
        for (std::size_t i = 0; i < pool_size; ++i)
        {
            io_service_ptr io_service(new boost::asio::io_service);
            work_ptr work(new boost::asio::io_service::work(*io_service));
            io_services_.push_back(io_service);
            work_.push_back(work);
        }
    }
コード例 #5
0
    io_service_pool::io_service_pool(std::size_t pool_size,
            HPX_STD_FUNCTION<void(std::size_t)> on_start_thread,
            HPX_STD_FUNCTION<void()> on_stop_thread,
            char const* pool_name)
      : next_io_service_(0), stopped_(false), pool_size_(pool_size),
        on_start_thread_(on_start_thread), on_stop_thread_(on_stop_thread)
#if defined(DEBUG)
      , pool_name_(pool_name)
#endif
    {
        if (pool_size == 0)
        {
            HPX_THROW_EXCEPTION(bad_parameter,
                "io_service_pool::io_service_pool",
                "io_service_pool size is 0");
        }

        // Give all the io_services work to do so that their run() functions
        // will not exit until they are explicitly stopped.
        for (std::size_t i = 0; i < pool_size; ++i)
        {
            io_service_ptr io_service(new boost::asio::io_service);
            work_ptr work(new boost::asio::io_service::work(*io_service));
            io_services_.push_back(io_service);
            work_.push_back(work);
        }
    }
コード例 #6
0
xnet_tcp_client_ptr xclient::connect_to(const xstring& host, xport_t port)
{
    xnet_tcp_client_ptr tcp_client(new xnet_tcp_client(io_service()));
    tcp_client->set_uuid(_X("{30201EA2-7AD5-46e8-91D6-7D3E878C8DC4}"));
    //tcp_client->set_handler_manager();
    tcp_client->connect_to(host, port);
    return tcp_client;
}
コード例 #7
0
ファイル: communication.cpp プロジェクト: Dim-Sal/short
/*
 * Initialises the io_service
 */
void Communication::InitIoService()
{
    boost::shared_ptr<boost::asio::io_service> io_service(new boost::asio::io_service);
    io_service_ = io_service;

    boost::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(*io_service_));
    work_ = work;

    std::cout << "[" << boost::this_thread::get_id()
              << "] Press <RETURN> to exit." << std::endl;
}
コード例 #8
0
ファイル: main.cpp プロジェクト: hyz/dotfiles
    void run(int, char*const[])
    {
        /// setup(Args(ac, av));

        boost::asio::signal_set signals(io_service(), SIGINT, SIGTERM);
        signals.async_wait([this](boost::system::error_code const&, int) {
                this->teardown();
            });

        for (auto& t : thread_) {
            t = std::thread([this]{ io_service().run(); });
        }
        LOG(INFO) << thread_.size() << "threads";

        for (auto& t : thread_) {
            t.join();
            LOG(ERROR) << "joined" << int(&t - &thread_.front());
        }
        LOG(ERROR) << "bye.";
    }
コード例 #9
0
ファイル: IOServerPool.cpp プロジェクト: UN-IO/UN-IO
        IOServerPool::IOServerPool(size_t pool_size)
            : next_io_service_(0)
        { 
            for (std::size_t i = 0; i < pool_size; ++ i)
            {
                io_service_sptr io_service(new boost::asio::io_service);
                work_sptr work(new boost::asio::io_service::work(*io_service));
                io_services_.push_back(io_service);
                work_.push_back(work);

            }
        }
コード例 #10
0
            io_service_pool::io_service_pool(std::size_t pool_size) : next_io_service_(0)
            {
                if (pool_size == 0) throw std::runtime_error("io_service_pool size is 0");

                for (std::size_t i = 0; i < pool_size; ++i)
                {
                    io_service_ptr io_service(new boost::asio::io_service);
                    work_ptr work(new boost::asio::io_service::work(*io_service));
                    io_services_.push_back(io_service);
                    work_.push_back(work);
                }
            }
コード例 #11
0
ファイル: io_service_pool.cpp プロジェクト: Bcorde5/hpx
    bool io_service_pool::run(bool join_threads)
    {
        boost::lock_guard<boost::mutex> l(mtx_);

        // Create a pool of threads to run all of the io_services.
        if (!threads_.empty())   // should be called only once
        {
            HPX_ASSERT(pool_size_ == io_services_.size());
            HPX_ASSERT(threads_.size() == io_services_.size());
            HPX_ASSERT(work_.size() == io_services_.size());

            if (join_threads)
                join_locked();

            return false;
        }

        // Give all the io_services work to do so that their run() functions
        // will not exit until they are explicitly stopped.
        if (!io_services_.empty())
            clear_locked();

        if (io_services_.empty())
        {
            for (std::size_t i = 0; i < pool_size_; ++i)
            {
                io_service_ptr io_service(new boost::asio::io_service);
                work_ptr work(new boost::asio::io_service::work(*io_service));
                io_services_.push_back(io_service);
                work_.push_back(work);
            }
        }

        for (std::size_t i = 0; i < pool_size_; ++i)
        {
            boost::shared_ptr<boost::thread> thread(new boost::thread(
                boost::bind(&io_service_pool::thread_run, this, i)));
            threads_.push_back(thread);
        }

        next_io_service_ = 0;
        stopped_ = false;

        HPX_ASSERT(pool_size_ == io_services_.size());
        HPX_ASSERT(threads_.size() == io_services_.size());
        HPX_ASSERT(work_.size() == io_services_.size());

        if (join_threads)
            join_locked();

        return true;
    }
コード例 #12
0
	io_service_pool::io_service_pool(std::size_t pool_size)
		: step(io_service_pool::BEGIN)
		, next_io_service_(0)
	{
		// Give all the io_services work to do so that their run() functions will not
		// exit until they are explicitly stopped.
		for (std::size_t i = 0; i < pool_size; ++ i)
		{
			io_service_ptr io_service(new boost::asio::io_service);
			work_ptr work(new boost::asio::io_service::work(*io_service));
			io_services_.push_back(io_service);
			work_.push_back(work);
		}
	}
コード例 #13
0
ファイル: io_service_pool.cpp プロジェクト: sealeks/nsdavinci
        io_service_pool::io_service_pool(std::size_t pool_size)
        : next_io_service_(0) {
            if (pool_size == 0)
                throw std::runtime_error("io_service_pool size is 0");

            // Give all the io_services work to do so that their run() functions will not
            // exit until they are explicitly stopped.
            for (std::size_t i = 0; i < pool_size; ++i) {
                io_service_ptr io_service(new boost::asio::io_service);
                work_ptr work(new boost::asio::io_service::work(*io_service));
                io_services_.push_back(io_service);
                work_.push_back(work);
            }
        }
コード例 #14
0
	void io_thread::do_io()
	{

		std::size_t iterationsLeft = kMaxiumPollIterations;
		bool didSome = false;
		while(iterationsLeft-- > 0)
		{
			if (io_service().poll_one() == 0)
				break;
			didSome = true;
		}
		if (!didSome)
			sleep(1);
	}
コード例 #15
0
  /// Get an io_service to use. if need then create one to use.
  boost::asio::io_service& get_io_service(std::size_t load)
  {
    // Calculate the required number of threads.
    std::size_t threads_number = load / pool_thread_load_;
    if ((threads_number > io_services_.size()) && (io_services_.size() < pool_high_watermark_) && !block_)
    {
      // Create new io_service and start it.
      io_service_ptr io_service(new boost::asio::io_service);
      io_services_.push_back(io_service);
      start_one(io_service);
      next_io_service_ = io_services_.size() - 1;
    }

    return get_io_service();
  }
コード例 #16
0
    io_service_pool::io_service_pool(HPX_STD_FUNCTION<void(std::size_t)> on_start_thread,
            HPX_STD_FUNCTION<void()> on_stop_thread, char const* pool_name)
      : next_io_service_(0), stopped_(false), pool_size_(2),
        on_start_thread_(on_start_thread), on_stop_thread_(on_stop_thread)
#if defined(DEBUG)
      , pool_name_(pool_name)
#endif
    {
        for (std::size_t i = 0; i < pool_size_; ++i)
        {
            io_service_ptr io_service(new boost::asio::io_service);
            work_ptr work(new boost::asio::io_service::work(*io_service));
            io_services_.push_back(io_service);
            work_.push_back(work);
        }
    }
コード例 #17
0
ファイル: io_service_pool.cpp プロジェクト: Bcorde5/hpx
 io_service_pool::io_service_pool(
         util::function_nonser<void(std::size_t, char const*)> const& on_start_thread,
         util::function_nonser<void()> const& on_stop_thread,
         char const* pool_name, char const* name_postfix)
   : next_io_service_(0), stopped_(false),
     pool_size_(2),
     on_start_thread_(on_start_thread), on_stop_thread_(on_stop_thread),
     pool_name_(pool_name), pool_name_postfix_(name_postfix)
 {
     for (std::size_t i = 0; i < pool_size_; ++i)
     {
         io_service_ptr io_service(new boost::asio::io_service);
         work_ptr work(new boost::asio::io_service::work(*io_service));
         io_services_.push_back(io_service);
         work_.push_back(work);
     }
 }
コード例 #18
0
ファイル: core.cpp プロジェクト: a1406/fytx
		core::core(void):next_io_service_(0)//,strand_(io_service_)
		{
			threads_count_		= 1;
			if (threads_count_ <= 0 )
				throw std::runtime_error("thread_pool size is error");
			// multi io service
			for (int i = 0; i < threads_count_; ++i)
			{
				io_service_ptr io_service(new boost::asio::io_service);
				work_ptr work(new boost::asio::io_service::work(*io_service));
				io_services_.push_back(io_service);
				work_pool_.push_back(work);
			}
			work_ptr w(new boost::asio::io_service::work(io_logic_));
			work_pool_.push_back(w);
			work_ptr w1(new boost::asio::io_service::work(io_db_user));
			work_pool_.push_back(w1);
		}
コード例 #19
0
ファイル: server.cpp プロジェクト: grey277/quakeWithSockets
int main() {

    try {
        boost::shared_ptr<boost::asio::io_service> io_service(new boost::asio::io_service);
        boost::shared_ptr<boost::asio::io_service::work> work(new boost::asio::io_service::work(*io_service));
        boost::thread_group threads;
        threads.create_thread(boost::bind(&startServer, io_service));
        boost::shared_ptr<Map> m(new Map(60, 30));
        boost::shared_ptr<server> s(new server(*io_service, tcp::endpoint(tcp::v4(), 4009), m));
        Bot b(58, 28, s, m);

        threads.join_all();

    }
    catch (std::exception &e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}
コード例 #20
0
  /// Construct the io_service pool.
  explicit io_service_pool(std::size_t pool_init_size = BAS_IO_SERVICE_POOL_INIT_SIZE,
      std::size_t pool_high_watermark = BAS_IO_SERVICE_POOL_HIGH_WATERMARK,
      std::size_t pool_thread_load = BAS_IO_SERVICE_POOL_THREAD_LOAD)
    : io_services_(),
      work_(),
      threads_(),
      pool_high_watermark_(pool_high_watermark),
      pool_thread_load_(pool_thread_load),
      next_io_service_(0),
      block_(false)
  {
    BOOST_ASSERT(pool_init_size != 0);
    BOOST_ASSERT(pool_high_watermark >= pool_init_size);
    BOOST_ASSERT(pool_thread_load != 0);

    // Create io_service pool.
    for (std::size_t i = 0; i < pool_init_size; ++i)
    {
      io_service_ptr io_service(new boost::asio::io_service);
      io_services_.push_back(io_service);
    }
  }
コード例 #21
0
ファイル: io_service2f.cpp プロジェクト: wfxr/CppLab
int main2f() {
	std::shared_ptr<boost::asio::io_service> io_service(
		new boost::asio::io_service);
	boost::shared_ptr<boost::asio::io_service::work> work(
		new boost::asio::io_service::work(*io_service));

	global_stream_lock2f.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Press [return] to exit." << std::endl;
	global_stream_lock2f.unlock();

	boost::thread_group worker_threads;
	for (auto i = 0; i < 4; ++i)
		worker_threads.create_thread(std::bind(WorkerThread2f, io_service));

	std::cin.get();

	io_service->stop();

	worker_threads.join_all();

	return 0;
}
コード例 #22
0
  /// Get an io_service to use. if need then create one to use.
  boost::asio::io_service& get_io_service(size_t load)
  {
    // Calculate the required number of threads.
    size_t threads_number = load / pool_thread_load_;

    // Lock for synchronize access to data.
    scoped_lock_t lock(mutex_);

    size_t service_count = io_services_.size();
    if (!blocked_                            && \
        !work_.empty()                       && \
        !threads_.empty()                    && \
        threads_number > service_count       && \
        service_count < pool_high_watermark_)
    {
      // Create new io_service and start it.
      io_service_ptr io_service(new boost::asio::io_service);
      io_services_.push_back(io_service);
      start_one(io_service);
      next_io_service_ = service_count;
    }

    return get_io_service();
  }
コード例 #23
0
ファイル: main.cpp プロジェクト: signmotion/server
void run( int argc, char** argv ) {

    setlocale( LC_ALL, "Russian" );
    setlocale( LC_NUMERIC, "C" );


    ba::io_service::work  work( io_service() );

    boost::thread  thread( boost::bind(
        &ba::io_service::run,
        &io_service()
    ) );


    // параметры подключения
    const auto localAddress =
        ba::ip::address::from_string( argv[ 1 ] );
    const auto localPort =
        boost::lexical_cast< size_t >( argv[ 2 ] );
    const auto remoteAddress =
        ba::ip::address::from_string( argv[ 3 ] );
    const auto remotePort =
        boost::lexical_cast< size_t >( argv[ 4 ] );
    std::cout <<
        "local \t" << localAddress  << ":" << localPort  << std::endl <<
        "remote\t" << remoteAddress << ":" << remotePort << std::endl;


    // слушатель локального адреса
    const ba::ip::tcp::endpoint  localEP( localAddress, localPort);
    ba::ip::tcp::acceptor  listen( io_service(), localEP );
    ba::ip::tcp::socket    localSocket( io_service() );
    listen.accept( localSocket );

    // удалённое соединение
    const ba::ip::tcp::endpoint  remoteEP( remoteAddress, remotePort);
    ba::ip::tcp::socket  remoteSocket( io_service() );
    remoteSocket.open( remoteEP.protocol());
    remoteSocket.connect( remoteEP );

    // слушаем локальное соединение
    localSocket.async_receive(
        ba::buffer( localData, 1024 ),
        boost::bind(
            handleRead,
            boost::ref( localSocket ),
            boost::ref( remoteSocket),
            localData,
            ba::placeholders::bytes_transferred,
            ba::placeholders::error
    ) );

    // слушаем удалённое соединение
    remoteSocket.async_receive(
        boost::asio::buffer( remoteData, 1024 ),
        boost::bind(
            handleRead,
            boost::ref( remoteSocket),
            boost::ref( localSocket ),
            remoteData,
            ba::placeholders::bytes_transferred,
            ba::placeholders::error
    ) );


    // отправляем тестовые данные
    {
        ba::io_service  io;
        ba::ip::tcp::socket  sa( io );
        const std::string request = "test data";
        ba::write( sa, boost::asio::buffer( request.c_str(), request.length() ) );
    }


    thread.join();
}
コード例 #24
0
ファイル: AppBase.cpp プロジェクト: kitschpatrol/Cinder
void AppBase::dispatchAsync( const std::function<void()> &fn )
{
	io_service().post( fn );
}
コード例 #25
0
ファイル: main.cpp プロジェクト: nannanwuhui/C-
int main( int argc, char * argv[] )
{
	boost::shared_ptr< boost::asio::io_service > io_service(
		new boost::asio::io_service
	);
	boost::shared_ptr< boost::asio::io_service::work > work(
		new boost::asio::io_service::work( *io_service )
	);
	boost::shared_ptr< boost::asio::io_service::strand > strand(
		new boost::asio::io_service::strand( *io_service )
	);

	global_stream_lock.lock();
	std::cout << "[" << boost::this_thread::get_id()
		<< "] Press [return] to exit." << std::endl;
	global_stream_lock.unlock();

	boost::thread_group worker_threads;
	for( int x = 0; x < 2; ++x )
	{
		worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
	}

	boost::asio::ip::tcp::socket sock( *io_service );

	try
	{
		boost::asio::ip::tcp::resolver resolver( *io_service );
		boost::asio::ip::tcp::resolver::query query(
			"www.google.com",
			boost::lexical_cast< std::string >( 80 )
		);
		boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve( query );
		boost::asio::ip::tcp::endpoint endpoint = *iterator;

		global_stream_lock.lock();
		std::cout << "Connecting to: " << endpoint << std::endl;
		global_stream_lock.unlock();

		sock.connect( endpoint );
	}
	catch( std::exception & ex )
	{
		global_stream_lock.lock();
		std::cout << "[" << boost::this_thread::get_id()
			<< "] Exception: " << ex.what() << std::endl;
		global_stream_lock.unlock();
	}

	std::cin.get();

	boost::system::error_code ec;
	sock.shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec );
	sock.close( ec );

	io_service->stop();

	worker_threads.join_all();

	return 0;
}
コード例 #26
0
ファイル: main.cpp プロジェクト: hyz/dotfiles
 Main(Args const& a)
     : server_type(io_service(), endpoint_type{ a.ip, a.port }, a.root)
     , thread_(a.threads)
 {}
コード例 #27
0
ファイル: player.cpp プロジェクト: Andlon/aria
    player::player(std::shared_ptr<player_callbacks> callbacks)
        : _callbacks(callbacks), _resolver(io_service())
    {

    }
コード例 #28
0
ファイル: main.cpp プロジェクト: hyz/dotfiles
 void teardown() {
     server_type::teardown(io_service());
     io_service().stop();
 }