Changeset 20017


Ignore:
Timestamp:
12/28/20 17:25:10 (4 years ago)
Author:
tbretz
Message:
Made ready for boost 1.70 by replacing get_io_context; the SSL connection has to be properly shutdown, but this required a new socket.. therefore switched to boost::optional to keep the socket.
Location:
trunk/FACT++/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/ConnectionSSL.cc

    r20005 r20017  
    1818namespace dummy = ba::placeholders;
    1919
    20 typedef ssl::stream<boost::asio::ip::tcp::socket> stream;
    21 
    2220using ba::ip::tcp;
    2321
     
    3432void ConnectionSSL::AsyncRead(const ba::mutable_buffers_1 buffers, int type)
    3533{
    36     ba::async_read(*this, buffers,
     34    ba::async_read(*stream, buffers,
    3735                   boost::bind(&ConnectionSSL::HandleReceivedData, this,
    3836                               dummy::error, dummy::bytes_transferred, type));
     
    4139void ConnectionSSL::AsyncWrite(const ba::const_buffers_1 &buffers)
    4240{
    43     ba::async_write(*this, buffers,
     41    ba::async_write(*stream, buffers,
    4442                    boost::bind(&ConnectionSSL::HandleSentData, this,
    4543                                dummy::error, dummy::bytes_transferred));
     
    6967
    7068    // AsyncConnect + Deadline
    71      lowest_layer().async_connect(endpoint,
     69    stream->lowest_layer().async_connect(endpoint,
    7270                  boost::bind(&ConnectionSSL::ConnectIter,
    7371                              this, iterator, ba::placeholders::error));
     
    8078{
    8179    // AsyncConnect + Deadline
    82      lowest_layer().async_connect(fEndpoint,
     80     stream->lowest_layer().async_connect(fEndpoint,
    8381                  boost::bind(&ConnectionSSL::ConnectAddr,
    8482                              this, fEndpoint, ba::placeholders::error));
     
    8785    //AsyncWait(fConnectTimeout, 5, &ConnectionSSL::HandleConnectTimeout);
    8886}
     87
     88void ConnectionSSL::HandleAsyncShutdown(const bs::error_code &error)
     89{
     90    stream->lowest_layer().close();
     91
     92    if (!error || error==error.default_error_condition())
     93        return;
     94
     95    ostringstream str;
     96    str << "Shutdown of SSL connection to " << URL() << "[" << error.value() << "]: " << error.message() << " (" << error << ")";// << endl;
     97    Error(str);
     98}
     99
    89100
    90101// ------------------------ close --------------------------
     
    103114
    104115    // Close possible open connections
    105     lowest_layer().close();
     116    if (!IsClosed())
     117    {
     118        // Adapted from
     119        // https://stackoverflow.com/questions/49122521/cant-implement-boostasiosslstreamboostasioiptcpsocket-reconnect
     120
     121#if BOOST_VERSION < 107000
     122        auto& io_context = stream->get_io_service();
     123#else
     124        const auto& io_context = stream->get_executor();
     125#endif
     126
     127        stream->lowest_layer().cancel(); // Remove all pending stuff in the queue
     128        stream->async_shutdown(boost::bind(&ConnectionSSL::HandleAsyncShutdown, this, dummy::error));
     129
     130        stream.emplace(io_context, *this);
     131    }
    106132
    107133    // Reset the connection status
     
    128154void ConnectionSSL::PostClose(bool restart)
    129155{
    130     get_io_service().post(boost::bind(&ConnectionSSL::CloseImp, this, restart));
     156#if BOOST_VERSION < 107000
     157    stream->get_io_service().post(boost::bind(&ConnectionSSL::CloseImp, this, restart));
     158#else
     159    ba::post(boost::bind(&ConnectionSSL::CloseImp, this, restart));
     160#endif
    131161}
    132162
     
    265295    if (!error)
    266296    {
    267         lowest_layer().set_option(boost::asio::socket_base::keep_alive(true));
     297        stream->lowest_layer().set_option(boost::asio::socket_base::keep_alive(true));
    268298
    269299        const int optval = 30;
    270300
    271 #if BOOST_VERSION <107000
     301#if BOOST_VERSION < 107000
    272302        // First keep alive after 30s
    273         setsockopt(lowest_layer().native(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
     303        setsockopt(stream->lowest_layer().native(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
    274304        // New keep alive after 30s
    275         setsockopt(lowest_layer().native(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
     305        setsockopt(stream->lowest_layer().native(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
    276306#else
    277307        // First keep alive after 30s
    278         setsockopt(lowest_layer().native_handle(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
     308        setsockopt(stream->lowest_layer().native_handle(), SOL_TCP, TCP_KEEPIDLE, &optval, sizeof(optval));
    279309        // New keep alive after 30s
    280         setsockopt(lowest_layer().native_handle(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
     310        setsockopt(stream->lowest_layer().native_handle(), SOL_TCP, TCP_KEEPINTVL, &optval, sizeof(optval));
    281311#endif
    282312
     
    331361        //set_default_verify_paths();
    332362
    333         async_handshake(stream::client,
     363        stream->async_handshake(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>::client,
    334364                        boost::bind(&ConnectionSSL::HandleHandshake, this,
    335365                                    endpoint, dummy::error));
     
    454484    else
    455485    {
    456         tcp::resolver resolver(get_io_service());
     486#if BOOST_VERSION < 107000
     487        tcp::resolver resolver(stream->get_io_service());
     488#else
     489        tcp::resolver resolver(stream->get_executor());
     490#endif
    457491
    458492        tcp::resolver::query query(fAddress, fPort);
     
    529563
    530564ConnectionSSL::ConnectionSSL(ba::io_service& ioservice, ostream &out) : MessageImp(out),
     565#if BOOST_VERSION < 107000
    531566ssl::context(ioservice, boost::asio::ssl::context::method::sslv23_client),
    532 stream(ioservice, *this), fLog(0), fVerbose(true), fDebugTx(false),
     567#else
     568ssl::context(boost::asio::ssl::context::method::sslv23_client),
     569#endif
     570stream(boost::in_place_init, ioservice, *this),
     571fLog(0), fVerbose(true), fDebugTx(false),
    533572fInTimeout(ioservice), fOutTimeout(ioservice), fConnectionTimer(ioservice),
    534573fQueueSize(0), fConnectionStatus(kDisconnected)
  • trunk/FACT++/src/ConnectionSSL.h

    r19049 r20017  
    88#include <boost/bind.hpp>
    99#include <boost/asio.hpp>
     10#include <boost/optional.hpp>
    1011#include <boost/asio/ssl.hpp>
    1112#include <boost/function.hpp>
     
    1415#include "MessageImp.h"
    1516
    16 class ConnectionSSL : public MessageImp, public boost::asio::ssl::context,
    17     public boost::asio::ssl::stream<boost::asio::ip::tcp::socket>
     17typedef boost::optional<boost::asio::ssl::stream<boost::asio::ip::tcp::socket>> Stream;
     18
     19class ConnectionSSL : public MessageImp, public boost::asio::ssl::context/*,
     20    public Stream*/
    1821{
     22public:
     23    Stream stream;
     24
    1925private:
    2026    MessageImp *fLog;
     
    99105    void HandleSentData(const boost::system::error_code& error, size_t);
    100106    void HandleHandshake(const boost::asio::ip::tcp::endpoint &endpoint, const boost::system::error_code& error);
     107    void HandleAsyncShutdown(const boost::system::error_code &error);
    101108
    102109    int Write(const Time &t, const std::string &txt, int qos=kInfo);
     
    143150    bool IsTxQueueEmpty() const { return fQueueSize==0; /*fOutQueue.empty();*/ }
    144151
    145     int IsClosed() const { return !lowest_layer().is_open(); }
     152    int IsClosed() const { return !stream->lowest_layer().is_open(); }
    146153
    147154    bool IsDisconnected() const { return fConnectionStatus==kDisconnected; }
  • trunk/FACT++/src/tngweather.cc

    r19711 r20017  
    261261    void StartReadReport()
    262262    {
    263         async_read_some(ba::buffer(fArray),
     263        stream->async_read_some(ba::buffer(fArray),
    264264                        boost::bind(&ConnectionWeather::HandleRead, this,
    265265                                    dummy::error, dummy::bytes_transferred));
     
    466466        return T::GetCurrentState();
    467467    }
    468 
    469     int Reconnect(const EventImp &evt)
    470     {
    471         // Close all connections to supress the warning in SetEndpoint
    472         fWeather.PostClose(false);
    473 
    474         // Now wait until all connection have been closed and
    475         // all pending handlers have been processed
    476         poll();
    477 
    478         if (evt.GetBool())
    479             fWeather.SetEndpoint(evt.GetString());
    480 
    481         // Now we can reopen the connection
     468*/
     469    int Connect()
     470    {
    482471        fWeather.PostClose(true);
    483 
    484472        return T::GetCurrentState();
    485473    }
    486 */
     474
    487475    int Execute()
    488476    {
     
    515503            (bind(&StateMachineWeather::Disconnect, this))
    516504            ("disconnect from ethernet");
    517 
    518         AddEvent("RECONNECT", "O")
    519             (bind(&StateMachineWeather::Reconnect, this, placeholders::_1))
    520             ("(Re)connect ethernet connection to FTM, a new address can be given"
    521              "|[host][string]:new ethernet address in the form <host:port>");
    522505*/
     506        T::AddEvent("CONNECT", "", State::kDisconnected)
     507            (bind(&StateMachineWeather::Connect, this))
     508            ("Connect");
     509
    523510    }
    524511
Note: See TracChangeset for help on using the changeset viewer.