Socket* RemoteSessionHandle::ConnectSocket(std::string const& host, std::string const& port) { if (!reportStatus.empty()) { reportStatus("Connecting to host " + host + " on port " + port); } try { tcp::resolver rsv(*ios); tcp::resolver::query query(host, port); tcp::resolver::iterator iter = rsv.resolve(query); tcp::socket* s = new tcp::socket(*ios); // TODO: Change this to connect async? boost::asio::connect(*s, iter); socket_base::keep_alive ka_option(true); s->set_option(ka_option); return s; } catch (boost::system::system_error& ex) { BOOST_THROW_EXCEPTION(SSHConnectionError() << ssh_error_string("SessionConnection: Error connecting to host " + host + " on port " + port + ". " + ex.what()) << ssh_error_id(ex.code().value())); } }
static void test_base() { log::asio_logger lgr; log::logger_t lg = boost::bind(&gce::log::asio_logger::output, &lgr, _arg1, ""); try { size_t cln_count = 100; errcode_t ec; attributes attrs; attrs.lg_ = lg; context ctx_svr(attrs); context ctx_cln(attrs); threaded_actor base_svr = spawn(ctx_svr); threaded_actor base_cln = spawn(ctx_cln); aid_t svr = spawn(base_svr, boost::bind(&ssl_ut::echo_server, _arg1), monitored); base_svr->send(svr, "init"); base_svr->recv("ready"); tcp::resolver rsv(base_cln); boost::asio::ip::tcp::resolver::query qry("127.0.0.1", "23333"); rsv.async_resolve(qry); boost::shared_ptr<tcp_resolver_t::iterator> eitr; base_cln->match(tcp::as_resolve).recv(ec, eitr); GCE_VERIFY(!ec).except(ec); boost::shared_ptr<boost::asio::ssl::context> ssl_ctx = boost::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23); ssl_ctx->load_verify_file("test_ssl_asio/ca.pem"); for (size_t i=0; i<cln_count; ++i) { aid_t cln = spawn(base_cln, boost::bind(&ssl_ut::echo_client, _arg1), monitored); base_cln->send(cln, "init", eitr, ssl_ctx); } for (size_t i=0; i<cln_count; ++i) { base_cln->recv(exit); } base_svr->send(svr, "end"); base_svr->recv(exit); } catch (std::exception& ex) { GCE_ERROR(lg) << ex.what(); } }
static void echo_server(stackful_actor self) { context& ctx = self.get_context(); log::logger_t& lg = ctx.get_logger(); try { aid_t sender = self->recv("init"); size_t scount = 0; errcode_t ec; tcp::resolver rsv(self); tcp_resolver_t::query qry("0.0.0.0", "23333"); rsv.async_resolve(qry); boost::shared_ptr<tcp_resolver_t::iterator> eitr; self->match(tcp::as_resolve).recv(ec, eitr); GCE_VERIFY(!ec).except(ec); tcp::acceptor acpr(self); boost::asio::ip::tcp::endpoint ep = **eitr; 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)); self->send(sender, "ready"); /// ssl context boost::asio::ssl::context ssl_ctx(boost::asio::ssl::context::sslv23); ssl_ctx.set_options( boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::single_dh_use); ssl_ctx.set_password_callback(boost::bind(&ssl_ut::get_password)); ssl_ctx.use_certificate_chain_file("test_ssl_asio/server.pem"); ssl_ctx.use_private_key_file("test_ssl_asio/server.pem", boost::asio::ssl::context::pem); ssl_ctx.use_tmp_dh_file("test_ssl_asio/dh512.pem"); while (true) { boost::shared_ptr<ssl_socket_t> skt = boost::make_shared<ssl_socket_t>(boost::ref(ctx.get_io_service()), boost::ref(ssl_ctx)); acpr.async_accept(skt->lowest_layer()); match_t type; errcode_t ec; message msg; self->match(tcp::as_accept, "end", type).raw(msg).recv(); if (type == atom("end")) { break; } msg >> ec; if (!ec) { aid_t cln = spawn(self, boost::bind(&ssl_ut::echo_session, _arg1), monitored); self->send(cln, "init", skt); ++scount; } } for (size_t i=0; i<scount; ++i) { self->recv(exit); } } catch (std::exception& ex) { GCE_ERROR(lg) << ex.what(); } }