From 1acc33feaffbf706ccd89408e1e38c23eb733692 Mon Sep 17 00:00:00 2001 From: James Yonan Date: Thu, 25 Jun 2015 13:59:12 -0600 Subject: [PATCH] Obsoleted asiodispatch in favor of C++11 lambdas. --- openvpn/client/cliconnect.hpp | 40 +++++++++++++++---- openvpn/client/cliproto.hpp | 15 +++++-- openvpn/client/remotelist.hpp | 11 ++--- openvpn/common/actionthread.hpp | 6 ++- openvpn/common/runcontext.hpp | 16 ++++++-- openvpn/server/servproto.hpp | 6 ++- openvpn/transport/client/httpcli.hpp | 14 ++++--- openvpn/transport/client/tcpcli.hpp | 15 +++---- openvpn/transport/client/udpcli.hpp | 15 +++---- openvpn/transport/udplink.hpp | 6 ++- openvpn/tun/tunio.hpp | 6 ++- .../common/asiodispatch.hpp => test/unused | 0 12 files changed, 100 insertions(+), 50 deletions(-) rename openvpn/common/asiodispatch.hpp => test/unused (100%) diff --git a/openvpn/client/cliconnect.hpp b/openvpn/client/cliconnect.hpp index eae30a20..ef365073 100644 --- a/openvpn/client/cliconnect.hpp +++ b/openvpn/client/cliconnect.hpp @@ -161,7 +161,10 @@ namespace openvpn { void thread_safe_stop() { if (!halt) - io_service.post(asio_dispatch_post(&ClientConnect::graceful_stop, this)); + io_service.post([self=Ptr(this)]() + { + self->graceful_stop(); + }); } void pause(const std::string& reason) @@ -203,26 +206,38 @@ namespace openvpn { OPENVPN_LOG("Client terminated, reconnecting in " << seconds << "..."); server_poll_timer.cancel(); restart_wait_timer.expires_at(Time::now() + Time::Duration::seconds(seconds)); - restart_wait_timer.async_wait(asio_dispatch_timer_arg(&ClientConnect::restart_wait_callback, this, generation)); + restart_wait_timer.async_wait([self=Ptr(this), gen=generation](const asio::error_code& error) + { + self->restart_wait_callback(gen, error); + }); } } void thread_safe_pause(const std::string& reason) { if (!halt) - io_service.post(asio_dispatch_post_arg(&ClientConnect::pause, this, reason)); + io_service.post([self=Ptr(this), reason]() + { + self->pause(reason); + }); } void thread_safe_resume() { if (!halt) - io_service.post(asio_dispatch_post(&ClientConnect::resume, this)); + io_service.post([self=Ptr(this)]() + { + self->resume(); + }); } void thread_safe_reconnect(int seconds) { if (!halt) - io_service.post(asio_dispatch_post_arg(&ClientConnect::reconnect, this, seconds)); + io_service.post([self=Ptr(this), seconds]() + { + self->reconnect(seconds); + }); } void dont_restart() @@ -307,7 +322,10 @@ namespace openvpn { if (!conn_timer_pending && conn_timeout > 0) { conn_timer.expires_at(Time::now() + Time::Duration::seconds(conn_timeout)); - conn_timer.async_wait(asio_dispatch_timer_arg(&ClientConnect::conn_timer_callback, this, generation)); + conn_timer.async_wait([self=Ptr(this), gen=generation](const asio::error_code& error) + { + self->conn_timer_callback(gen, error); + }); conn_timer_pending = true; } } @@ -351,7 +369,10 @@ namespace openvpn { server_poll_timer.cancel(); interim_finalize(); restart_wait_timer.expires_at(Time::now() + Time::Duration::seconds(delay)); - restart_wait_timer.async_wait(asio_dispatch_timer_arg(&ClientConnect::restart_wait_callback, this, generation)); + restart_wait_timer.async_wait([self=Ptr(this), gen=generation](const asio::error_code& error) + { + self->restart_wait_callback(gen, error); + }); } virtual void client_proto_terminate() @@ -510,7 +531,10 @@ namespace openvpn { if (client_options->server_poll_timeout_enabled()) { server_poll_timer.expires_at(Time::now() + client_options->server_poll_timeout()); - server_poll_timer.async_wait(asio_dispatch_timer_arg(&ClientConnect::server_poll_callback, this, generation)); + server_poll_timer.async_wait([self=Ptr(this), gen=generation](const asio::error_code& error) + { + self->server_poll_callback(gen, error); + }); } conn_timer_start(); client->start(); diff --git a/openvpn/client/cliproto.hpp b/openvpn/client/cliproto.hpp index e3d0ce57..46f239ae 100644 --- a/openvpn/client/cliproto.hpp +++ b/openvpn/client/cliproto.hpp @@ -646,7 +646,10 @@ namespace openvpn { if (!received_options.partial()) { push_request_timer.expires_at(now() + dur); - push_request_timer.async_wait(asio_dispatch_timer_arg(&Session::send_push_request_callback, this, dur)); + push_request_timer.async_wait([self=Ptr(this), dur](const asio::error_code& error) + { + self->send_push_request_callback(dur, error); + }); } } @@ -699,7 +702,10 @@ namespace openvpn { next.max(now()); housekeeping_schedule.reset(next); housekeeping_timer.expires_at(next); - housekeeping_timer.async_wait(asio_dispatch_timer(&Session::housekeeping_callback, this)); + housekeeping_timer.async_wait([self=Ptr(this)](const asio::error_code& error) + { + self->housekeeping_callback(error); + }); } else { @@ -728,7 +734,10 @@ namespace openvpn { void schedule_inactive_timer() { inactive_timer.expires_at(now() + inactive_duration); - inactive_timer.async_wait(asio_dispatch_timer(&Session::inactive_callback, this)); + inactive_timer.async_wait([self=Ptr(this)](const asio::error_code& error) + { + self->inactive_callback(error); + }); } void inactive_callback(const asio::error_code& e) // fixme for DCO diff --git a/openvpn/client/remotelist.hpp b/openvpn/client/remotelist.hpp index b52d99be..b068d171 100644 --- a/openvpn/client/remotelist.hpp +++ b/openvpn/client/remotelist.hpp @@ -37,7 +37,6 @@ #include #include #include -#include #include #include #include @@ -247,11 +246,6 @@ namespace openvpn { // to initial tunnel establishment. class PreResolve : public RC { - typedef AsioDispatchResolve AsioDispatchResolveTCP; - public: typedef RCPtr Ptr; @@ -327,7 +321,10 @@ namespace openvpn { // call into Asio to do the resolve operation OPENVPN_LOG_REMOTELIST("*** PreResolve RESOLVE on " << item.server_host); asio::ip::tcp::resolver::query query(item.server_host, "0"); - resolver.async_resolve(query, AsioDispatchResolveTCP(&PreResolve::resolve_callback, this)); + resolver.async_resolve(query, [self=Ptr(this)](const asio::error_code& error, asio::ip::tcp::resolver::iterator endpoint_iterator) + { + self->resolve_callback(error, endpoint_iterator); + }); return; } } diff --git a/openvpn/common/actionthread.hpp b/openvpn/common/actionthread.hpp index 8afaf834..1b901c3c 100644 --- a/openvpn/common/actionthread.hpp +++ b/openvpn/common/actionthread.hpp @@ -27,7 +27,6 @@ #include #include -#include #include namespace openvpn { @@ -96,7 +95,10 @@ namespace openvpn { { OPENVPN_LOG("ActionThread Exception: " << e.what()); } - io_service.post(asio_dispatch_post_arg(&ActionThread::completion_post, this, status)); + io_service.post([self=Ptr(this), status]() + { + self->completion_post(status); + }); } asio::io_service& io_service; diff --git a/openvpn/common/runcontext.hpp b/openvpn/common/runcontext.hpp index eaf7f935..5497576b 100644 --- a/openvpn/common/runcontext.hpp +++ b/openvpn/common/runcontext.hpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -90,7 +89,10 @@ namespace openvpn { #ifdef OPENVPN_EXIT_IN exit_timer.expires_at(Time::now() + Time::Duration::seconds(OPENVPN_EXIT_IN)); - exit_timer.async_wait(asio_dispatch_timer(&RunContext::exit_timer_callback, this)); + exit_timer.async_wait([self=Ptr(this)](const asio::error_code& error) + { + self->exit_timer_callback(error); + }); #endif } @@ -193,7 +195,10 @@ namespace openvpn { exit_timer.cancel(); if (signals) - io_service.post(asio_dispatch_post(&ASIOSignals::cancel, signals.get())); + io_service.post([sig=signals]() + { + sig->cancel(); + }); unsigned int stopped = 0; for (size_t i = 0; i < servlist.size(); ++i) @@ -239,7 +244,10 @@ namespace openvpn { void signal_rearm() { - signals->register_signals_all(asio_dispatch_signal(&RunContext::signal, this)); + signals->register_signals_all([self=Ptr(this)](const asio::error_code& error, int signal_number) + { + self->signal(error, signal_number); + }); } // these vars only used by main thread diff --git a/openvpn/server/servproto.hpp b/openvpn/server/servproto.hpp index e28bdd01..42fc1a8c 100644 --- a/openvpn/server/servproto.hpp +++ b/openvpn/server/servproto.hpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -507,7 +506,10 @@ namespace openvpn { next.max(now()); housekeeping_schedule.reset(next); housekeeping_timer.expires_at(next); - housekeeping_timer.async_wait(asio_dispatch_timer(&Session::housekeeping_callback, this)); + housekeeping_timer.async_wait([self=Ptr(this)](const asio::error_code& error) + { + self->housekeeping_callback(error); + }); } else { diff --git a/openvpn/transport/client/httpcli.hpp b/openvpn/transport/client/httpcli.hpp index 58de6f0c..950f58d0 100644 --- a/openvpn/transport/client/httpcli.hpp +++ b/openvpn/transport/client/httpcli.hpp @@ -220,10 +220,6 @@ namespace openvpn { friend class ClientConfig; // calls constructor friend LinkImpl; // calls tcp_read_handler - typedef AsioDispatchResolve AsioDispatchResolveTCP; - public: virtual void transport_start() { @@ -253,7 +249,10 @@ namespace openvpn { asio::ip::tcp::resolver::query query(proxy_host, proxy_port); parent.transport_pre_resolve(); - resolver.async_resolve(query, AsioDispatchResolveTCP(&Client::do_resolve_, this)); + resolver.async_resolve(query, [self=Ptr(this)](const asio::error_code& error, asio::ip::tcp::resolver::iterator endpoint_iterator) + { + self->do_resolve_(error, endpoint_iterator); + }); } } } @@ -898,7 +897,10 @@ namespace openvpn { } #endif socket.set_option(asio::ip::tcp::no_delay(true)); - socket.async_connect(server_endpoint, asio_dispatch_connect(&Client::start_impl_, this)); + socket.async_connect(server_endpoint, [self=Ptr(this)](const asio::error_code& error) + { + self->start_impl_(error); + }); } // start I/O on TCP socket diff --git a/openvpn/transport/client/tcpcli.hpp b/openvpn/transport/client/tcpcli.hpp index eb5707c0..94557ba9 100644 --- a/openvpn/transport/client/tcpcli.hpp +++ b/openvpn/transport/client/tcpcli.hpp @@ -74,11 +74,6 @@ namespace openvpn { friend class ClientConfig; // calls constructor friend LinkImpl; // calls tcp_read_handler - typedef AsioDispatchResolve AsioDispatchResolveTCP; - public: virtual void transport_start() { @@ -94,7 +89,10 @@ namespace openvpn { asio::ip::tcp::resolver::query query(server_host, server_port); parent.transport_pre_resolve(); - resolver.async_resolve(query, AsioDispatchResolveTCP(&Client::do_resolve_, this)); + resolver.async_resolve(query, [self=Ptr(this)](const asio::error_code& error, asio::ip::tcp::resolver::iterator endpoint_iterator) + { + self->do_resolve_(error, endpoint_iterator); + }); } } } @@ -255,7 +253,10 @@ namespace openvpn { } #endif socket.set_option(asio::ip::tcp::no_delay(true)); - socket.async_connect(server_endpoint, asio_dispatch_connect(&Client::start_impl_, this)); + socket.async_connect(server_endpoint, [self=Ptr(this)](const asio::error_code& error) + { + self->start_impl_(error); + }); } // start I/O on TCP socket diff --git a/openvpn/transport/client/udpcli.hpp b/openvpn/transport/client/udpcli.hpp index 5cc22a60..b03190c2 100644 --- a/openvpn/transport/client/udpcli.hpp +++ b/openvpn/transport/client/udpcli.hpp @@ -76,11 +76,6 @@ namespace openvpn { typedef Link LinkImpl; - typedef AsioDispatchResolve AsioDispatchResolveUDP; - public: virtual void transport_start() { @@ -96,7 +91,10 @@ namespace openvpn { asio::ip::udp::resolver::query query(server_host, server_port); parent.transport_pre_resolve(); - resolver.async_resolve(query, AsioDispatchResolveUDP(&Client::do_resolve_, this)); + resolver.async_resolve(query, [self=Ptr(this)](const asio::error_code& error, asio::ip::udp::resolver::iterator endpoint_iterator) + { + self->do_resolve_(error, endpoint_iterator); + }); } } } @@ -241,7 +239,10 @@ namespace openvpn { } } #endif - socket.async_connect(server_endpoint, asio_dispatch_connect(&Client::start_impl_, this)); + socket.async_connect(server_endpoint, [self=Ptr(this)](const asio::error_code& error) + { + self->start_impl_(error); + }); } // start I/O on UDP socket diff --git a/openvpn/transport/udplink.hpp b/openvpn/transport/udplink.hpp index 534370f4..31594375 100644 --- a/openvpn/transport/udplink.hpp +++ b/openvpn/transport/udplink.hpp @@ -29,7 +29,6 @@ #include #include -#include #include #include #include @@ -142,7 +141,10 @@ namespace openvpn { frame_context.prepare(udpfrom->buf); socket.async_receive_from(frame_context.mutable_buffers_1(udpfrom->buf), udpfrom->sender_endpoint, - asio_dispatch_read(&Link::handle_read, this, udpfrom)); + [self=Ptr(this), udpfrom](const asio::error_code& error, const size_t bytes_recvd) + { + self->handle_read(udpfrom, error, bytes_recvd); + }); } void handle_read(PacketFrom *udpfrom, const asio::error_code& error, const size_t bytes_recvd) diff --git a/openvpn/tun/tunio.hpp b/openvpn/tun/tunio.hpp index 8ad99e56..3b93bf74 100644 --- a/openvpn/tun/tunio.hpp +++ b/openvpn/tun/tunio.hpp @@ -28,7 +28,6 @@ #include #include -#include #include #include #include @@ -207,7 +206,10 @@ namespace openvpn { // queue read on tun device stream->async_read_some(frame_context.mutable_buffers_1(tunfrom->buf), - asio_dispatch_read(&TunIO::handle_read, this, tunfrom)); + [self=Ptr(this), tunfrom](const asio::error_code& error, const size_t bytes_recvd) + { + self->handle_read(tunfrom, error, bytes_recvd); + }); } void handle_read(PacketFrom *tunfrom, const asio::error_code& error, const size_t bytes_recvd) diff --git a/openvpn/common/asiodispatch.hpp b/test/unused similarity index 100% rename from openvpn/common/asiodispatch.hpp rename to test/unused