void run(gce::actor<gce::stackful>& self, gce::aid_t base_id) { try { /// wait 1 second for server setup gce::wait(self, gce::seconds_t(1)); /// make a tcp socket to connnect to server gce::yield_t yield = self.get_yield(); gce::io_service_t& ios = ctx_.get_io_service(); boost::asio::ip::tcp::resolver reso(ios); boost::asio::ip::tcp::resolver::query query(host_, port_); boost::asio::ip::tcp::resolver::iterator itr = reso.async_resolve(query, yield); boost::asio::ip::tcp::socket sock(ios); boost::asio::async_connect(sock, itr, yield); char data[32]; for (std::size_t i=0; i<echo_num_; ++i) { boost::asio::async_write(sock, boost::asio::buffer(data, 32), yield); boost::asio::async_read(sock, boost::asio::buffer(data, 32), yield); /// per second a echo gce::wait(self, gce::seconds_t(1)); } } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } gce::send(self, base_id, gce::atom("done")); }
void run(gce::actor<gce::stackful>& self) { try { gce::yield_t yield = self.get_yield(); gce::io_service_t& ios = ctx_.get_io_service(); boost::asio::ip::address addr; addr.from_string(host_); boost::asio::ip::tcp::endpoint ep(addr, port_); acpr_.open(ep.protocol()); acpr_.set_option(boost::asio::socket_base::reuse_address(true)); acpr_.bind(ep); acpr_.set_option(boost::asio::socket_base::receive_buffer_size(640000)); acpr_.set_option(boost::asio::socket_base::send_buffer_size(640000)); acpr_.listen(1024); acpr_.set_option(boost::asio::ip::tcp::no_delay(true)); acpr_.set_option(boost::asio::socket_base::keep_alive(true)); acpr_.set_option(boost::asio::socket_base::enable_connection_aborted(true)); while (true) { gce::errcode_t ec; boost::shared_ptr<socket_t> sock(new socket_t(ios)); acpr_.async_accept(*sock, yield[ec]); if (!ec) { std::cout << "new connection!\n"; gce::spawn(self, boost::bind(&server::session, this, _1, sock)); } else { break; } } } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } }
void session(gce::actor<gce::stackful>& self, boost::shared_ptr<socket_t> sock) { try { gce::yield_t yield = self.get_yield(); char data[32]; for (std::size_t i=0; i<echo_num_; ++i) { boost::asio::async_read(*sock, boost::asio::buffer(data, 32), yield); boost::asio::async_write(*sock, boost::asio::buffer(data, 32), yield); } std::cout << "echo done!\n"; } catch (std::exception& ex) { std::cerr << ex.what() << std::endl; } }