2727#include < boost/iostreams/stream.hpp>
2828#include < boost/shared_ptr.hpp>
2929#include < boost/thread.hpp>
30+ #include < boost/version.hpp>
31+
32+ #if BOOST_VERSION >= 107300
33+ using namespace boost ::placeholders;
34+ #endif
3035
3136using namespace boost ;
3237using namespace boost ::asio;
@@ -41,11 +46,19 @@ static std::string rpcWarmupStatus("RPC server started");
4146static CCriticalSection cs_rpcWarmup;
4247
4348// ! These are created by StartRPCThreads, destroyed in StopRPCThreads
49+ #if BOOST_VERSION >= 108700
50+ static boost::asio::io_context* rpc_io_service = NULL ;
51+ static map<string, boost::shared_ptr<boost::asio::steady_timer> > deadlineTimers;
52+ static ssl::context* rpc_ssl_context = NULL ;
53+ static boost::thread_group* rpc_worker_group = NULL ;
54+ static boost::asio::executor_work_guard<boost::asio::io_context::executor_type>* rpc_dummy_work = NULL ;
55+ #else
4456static asio::io_service* rpc_io_service = NULL ;
4557static map<string, boost::shared_ptr<deadline_timer> > deadlineTimers;
4658static ssl::context* rpc_ssl_context = NULL ;
4759static boost::thread_group* rpc_worker_group = NULL ;
4860static boost::asio::io_service::work* rpc_dummy_work = NULL ;
61+ #endif
4962static std::vector<CSubNet> rpc_allow_subnets; // !< List of subnets to allow RPC connections from
5063static std::vector<boost::shared_ptr<ip::tcp::acceptor> > rpc_acceptors;
5164
@@ -409,8 +422,13 @@ CNetAddr BoostAsioToCNetAddr(boost::asio::ip::address address)
409422{
410423 CNetAddr netaddr;
411424 // Make sure that IPv4-compatible and IPv4-mapped IPv6 addresses are treated as IPv4 addresses
425+ #if BOOST_VERSION >= 108700
426+ if (address.is_v6 () && address.to_v6 ().is_v4_mapped ())
427+ address = boost::asio::ip::make_address_v4 (boost::asio::ip::v4_mapped, address.to_v6 ());
428+ #else
412429 if (address.is_v6 () && (address.to_v6 ().is_v4_compatible () || address.to_v6 ().is_v4_mapped ()))
413430 address = address.to_v6 ().to_v4 ();
431+ #endif
414432
415433 if (address.is_v4 ()) {
416434 boost::asio::ip::address_v4::bytes_type bytes = address.to_v4 ().to_bytes ();
@@ -435,6 +453,16 @@ template <typename Protocol>
435453class AcceptedConnectionImpl : public AcceptedConnection
436454{
437455public:
456+ #if BOOST_VERSION >= 108700
457+ AcceptedConnectionImpl (
458+ boost::asio::io_context& io_context,
459+ ssl::context& context,
460+ bool fUseSSL ) : sslStream(io_context, context),
461+ _d(sslStream, fUseSSL ),
462+ _stream(_d)
463+ {
464+ }
465+ #else
438466 AcceptedConnectionImpl (
439467 asio::io_service& io_service,
440468 ssl::context& context,
@@ -443,6 +471,7 @@ class AcceptedConnectionImpl : public AcceptedConnection
443471 _stream(_d)
444472 {
445473 }
474+ #endif
446475
447476 virtual std::iostream& stream ()
448477 {
@@ -486,7 +515,13 @@ static void RPCListen(boost::shared_ptr<basic_socket_acceptor<Protocol, SocketAc
486515 const bool fUseSSL )
487516{
488517 // Accept connection
518+ #if BOOST_VERSION >= 108700
519+ boost::shared_ptr<AcceptedConnectionImpl<Protocol> > conn (new AcceptedConnectionImpl<Protocol>(*rpc_io_service, context, fUseSSL ));
520+ #elif BOOST_VERSION >= 106600
489521 boost::shared_ptr<AcceptedConnectionImpl<Protocol> > conn (new AcceptedConnectionImpl<Protocol>(static_cast <boost::asio::io_service&>(acceptor->get_executor ().context ()), context, fUseSSL ));
522+ #else
523+ boost::shared_ptr<AcceptedConnectionImpl<Protocol> > conn (new AcceptedConnectionImpl<Protocol>(acceptor->get_io_service (), context, fUseSSL ));
524+ #endif
490525
491526 acceptor->async_accept (
492527 conn->sslStream .lowest_layer (),
@@ -539,7 +574,11 @@ static ip::tcp::endpoint ParseEndpoint(const std::string& strEndpoint, int defau
539574 std::string addr;
540575 int port = defaultPort;
541576 SplitHostPort (strEndpoint, port, addr);
577+ #if BOOST_VERSION >= 108700
578+ return ip::tcp::endpoint (boost::asio::ip::make_address (addr), port);
579+ #else
542580 return ip::tcp::endpoint (asio::ip::address::from_string (addr), port);
581+ #endif
543582}
544583
545584void StartRPCThreads ()
@@ -591,7 +630,11 @@ void StartRPCThreads()
591630 }
592631
593632 assert (rpc_io_service == NULL );
633+ #if BOOST_VERSION >= 108700
634+ rpc_io_service = new boost::asio::io_context ();
635+ #else
594636 rpc_io_service = new asio::io_service ();
637+ #endif
595638 rpc_ssl_context = new ssl::context (ssl::context::sslv23);
596639
597640 const bool fUseSSL = GetBoolArg (" -rpcssl" , false );
@@ -668,7 +711,11 @@ void StartRPCThreads()
668711 v6_only_error);
669712
670713 acceptor->bind (endpoint);
714+ #if BOOST_VERSION >= 108700
715+ acceptor->listen (boost::asio::socket_base::max_listen_connections);
716+ #else
671717 acceptor->listen (socket_base::max_connections);
718+ #endif
672719
673720 RPCListen (acceptor, *rpc_ssl_context, fUseSSL );
674721
@@ -692,19 +739,30 @@ void StartRPCThreads()
692739
693740 rpc_worker_group = new boost::thread_group ();
694741 for (int i = 0 ; i < GetArg (" -rpcthreads" , 4 ); i++)
742+ #if BOOST_VERSION >= 108700
743+ rpc_worker_group->create_thread (boost::bind (&boost::asio::io_context::run, rpc_io_service));
744+ #else
695745 rpc_worker_group->create_thread (boost::bind (&asio::io_service::run, rpc_io_service));
746+ #endif
696747 fRPCRunning = true ;
697748}
698749
699750void StartDummyRPCThread ()
700751{
701752 if (rpc_io_service == NULL ) {
753+ #if BOOST_VERSION >= 108700
754+ rpc_io_service = new boost::asio::io_context ();
755+ rpc_dummy_work = new boost::asio::executor_work_guard<boost::asio::io_context::executor_type>(boost::asio::make_work_guard (*rpc_io_service));
756+ rpc_worker_group = new boost::thread_group ();
757+ rpc_worker_group->create_thread (boost::bind (&boost::asio::io_context::run, rpc_io_service));
758+ #else
702759 rpc_io_service = new asio::io_service ();
703760 /* Create dummy "work" to keep the thread from exiting when no timeouts active,
704761 * see http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.stopping_the_io_service_from_running_out_of_work */
705762 rpc_dummy_work = new asio::io_service::work (*rpc_io_service);
706763 rpc_worker_group = new boost::thread_group ();
707764 rpc_worker_group->create_thread (boost::bind (&asio::io_service::run, rpc_io_service));
765+ #endif
708766 fRPCRunning = true ;
709767 }
710768}
@@ -725,7 +783,11 @@ void StopRPCThreads()
725783 LogPrintf (" %s: Warning: %s when cancelling acceptor" , __func__, ec.message ());
726784 }
727785 rpc_acceptors.clear ();
786+ #if BOOST_VERSION >= 108700
787+ BOOST_FOREACH (const PAIRTYPE (std::string, boost::shared_ptr<boost::asio::steady_timer>) & timer, deadlineTimers) {
788+ #else
728789 BOOST_FOREACH (const PAIRTYPE (std::string, boost::shared_ptr<deadline_timer>) & timer, deadlineTimers) {
790+ #endif
729791 timer.second ->cancel (ec);
730792 if (ec)
731793 LogPrintf (" %s: Warning: %s when cancelling timer" , __func__, ec.message ());
@@ -783,10 +845,19 @@ void RPCRunLater(const std::string& name, boost::function<void(void)> func, int6
783845 assert (rpc_io_service != NULL );
784846
785847 if (deadlineTimers.count (name) == 0 ) {
848+ #if BOOST_VERSION >= 108700
849+ deadlineTimers.insert (make_pair (name,
850+ boost::shared_ptr<boost::asio::steady_timer>(new boost::asio::steady_timer (*rpc_io_service))));
851+ #else
786852 deadlineTimers.insert (make_pair (name,
787853 boost::shared_ptr<deadline_timer>(new deadline_timer (*rpc_io_service))));
854+ #endif
788855 }
856+ #if BOOST_VERSION >= 108700
857+ deadlineTimers[name]->expires_after (std::chrono::seconds (nSeconds));
858+ #else
789859 deadlineTimers[name]->expires_from_now (posix_time::seconds (nSeconds));
860+ #endif
790861 deadlineTimers[name]->async_wait (boost::bind (RPCRunHandler, _1, func));
791862}
792863
0 commit comments