"Fossies" - the Fresh Open Source Software Archive

Member "zuluCrypt-5.7.0/external_libraries/NetworkAccessManager/network_access_manager.hpp" (12 Sep 2019, 7033 Bytes) of package /linux/misc/zuluCrypt-5.7.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "network_access_manager.hpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * copyright: 2016
    3  * name : Francis Banyikwa
    4  * email: mhogomchungu@gmail.com
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  *
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in
   14  *    the documentation and/or other materials provided with the
   15  *    distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   21  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
   23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  */
   30 
   31 #ifndef NetworkAccessManager_H
   32 #define NetworkAccessManager_H
   33 
   34 #include <QtNetwork/QNetworkAccessManager>
   35 #include <QtNetwork/QNetworkRequest>
   36 #include <QtNetwork/QNetworkReply>
   37 
   38 #include <QEventLoop>
   39 #include <QTimer>
   40 
   41 #include <vector>
   42 #include <functional>
   43 #include <utility>
   44 #include <memory>
   45 
   46 class NetworkAccessManagerTimeOutManager : public QObject
   47 {
   48     Q_OBJECT
   49 public:
   50     NetworkAccessManagerTimeOutManager( std::function< bool( QNetworkReply * ) > e,
   51                         std::function< void() > s,
   52                         QNetworkReply * m,
   53                         int w,
   54                         QObject * n ) :
   55         m_object( n ),
   56         m_reply( m ),
   57         m_cancel( std::move( e ) ),
   58         m_timeout( std::move( s ) )
   59     {
   60         connect( &m_timer,SIGNAL( timeout() ),
   61              this,SLOT( timeout() ),Qt::QueuedConnection ) ;
   62 
   63         m_timer.start( 1000 * w ) ;
   64     }
   65 private slots:
   66     void timeout()
   67     {
   68         m_timer.stop() ;
   69 
   70         disconnect( m_object,SIGNAL( finished( QNetworkReply * ) ),
   71                 this,SLOT( networkReply( QNetworkReply * ) ) ) ;
   72 
   73         m_cancel( m_reply ) ;
   74 
   75         m_timeout() ;
   76 
   77         this->deleteLater() ;
   78     }
   79     void networkReply( QNetworkReply * e )
   80     {
   81         if( e == m_reply ){
   82 
   83             m_timer.stop() ;
   84             this->deleteLater() ;
   85         }
   86     }
   87 private:
   88     QObject * m_object ;
   89     QNetworkReply * m_reply ;
   90     QTimer m_timer ;
   91     std::function< bool( QNetworkReply * ) > m_cancel ;
   92     std::function< void() > m_timeout ;
   93 } ;
   94 
   95 class NetworkAccessManager : public QObject
   96 {
   97     Q_OBJECT
   98 public:
   99     using NetworkReply = std::unique_ptr< QNetworkReply,void( * )( QNetworkReply * ) > ;
  100     using function_t   = std::function< void( QNetworkReply& ) > ;
  101 private:
  102     using entries_t = std::vector< std::tuple< QNetworkReply *,bool,function_t > > ;
  103     entries_t m_entries ;
  104     QNetworkAccessManager m_manager ;
  105 
  106     using position_t = decltype( m_entries.size() ) ;
  107 public:
  108     NetworkAccessManager()
  109     {
  110         connect( &m_manager,SIGNAL( finished( QNetworkReply * ) ),
  111              this,SLOT( networkReply( QNetworkReply * ) ),Qt::QueuedConnection ) ;
  112     }
  113     QNetworkAccessManager& QtNAM()
  114     {
  115         return m_manager ;
  116     }
  117     QNetworkReply * get( int timeOut,const QNetworkRequest& r,function_t f,
  118                          std::function< void() > m = [](){} )
  119     {
  120         auto s = m_manager.get( r ) ;
  121 
  122         m_entries.emplace_back( s,true,std::move( f ) ) ;
  123 
  124         this->setTimeOut( timeOut,s,std::move( m ) ) ;
  125 
  126         return s ;
  127     }
  128     NetworkReply get( int timeOut,const QNetworkRequest& r,std::function< void() > m = [](){} )
  129     {
  130         QNetworkReply * q ;
  131 
  132         QEventLoop s ;
  133 
  134         auto a = m_manager.get( r ) ;
  135 
  136         auto function = [ & ]( QNetworkReply& e ){ q = std::addressof( e ) ; s.quit() ; } ;
  137 
  138         m_entries.emplace_back( std::make_tuple( a,false,std::move( function ) ) ) ;
  139 
  140         this->setTimeOut( timeOut,a,std::move( m ) ) ;
  141 
  142         s.exec() ;
  143 
  144         return { q,[]( QNetworkReply * e ){ e->deleteLater() ; } } ;
  145     }
  146     template< typename T >
  147     QNetworkReply * post( int timeOut,const QNetworkRequest& r,const T& e,function_t f,
  148                   std::function< void() > m = [](){} )
  149     {
  150         auto s = m_manager.post( r,e ) ;
  151 
  152         m_entries.emplace_back( std::make_tuple( s,true,std::move( f ) ) ) ;
  153 
  154         this->setTimeOut( timeOut,s,std::move( m ) ) ;
  155 
  156         return s ;
  157     }
  158     template< typename T >
  159     NetworkReply post( int timeOut,const QNetworkRequest& r,const T& e,
  160                std::function< void() > m = [](){} )
  161     {
  162         QNetworkReply * q ;
  163 
  164         QEventLoop s ;
  165 
  166         auto function = [ & ]( QNetworkReply& e ){ q = std::addressof( e ) ; s.quit() ; } ;
  167 
  168         auto a = m_manager.post( r,e ) ;
  169 
  170         m_entries.emplace_back( std::make_tuple( a,false,std::move( function ) ) ) ;
  171 
  172         this->setTimeOut( timeOut,a,std::move( m ) ) ;
  173 
  174         s.exec() ;
  175 
  176         return { q,[]( QNetworkReply * e ){ e->deleteLater() ; } } ;
  177     }
  178     QNetworkReply * head( int timeOut,const QNetworkRequest& r,function_t f,
  179                   std::function< void() > m = [](){} )
  180     {
  181         auto s = m_manager.head( r ) ;
  182 
  183         m_entries.emplace_back( std::make_tuple( s,true,std::move( f ) ) ) ;
  184 
  185         this->setTimeOut( timeOut,s,std::move( m ) ) ;
  186 
  187         return s ;
  188     }
  189     NetworkReply head( int timeOut,const QNetworkRequest& r,std::function< void() > m = [](){} )
  190     {
  191         QNetworkReply * q ;
  192 
  193         QEventLoop s ;
  194 
  195         auto function = [ & ]( QNetworkReply& e ){ q = std::addressof( e ) ; s.quit() ; } ;
  196 
  197         auto a = m_manager.head( r ) ;
  198 
  199         m_entries.emplace_back( std::make_tuple( a,false,std::move( function ) ) ) ;
  200 
  201         this->setTimeOut( timeOut,a,std::move( m ) ) ;
  202 
  203         s.exec() ;
  204 
  205         return { q,[]( QNetworkReply * e ){ e->deleteLater() ; } } ;
  206     }
  207     bool cancel( QNetworkReply * e )
  208     {
  209         return this->find_network_reply( e,[ this ]( QNetworkReply& e,position_t s ){
  210 
  211             if( std::get< 1 >( m_entries[ s ] ) ){
  212 
  213                 e.deleteLater() ;
  214             }
  215 
  216             m_entries.erase( m_entries.begin() + s ) ;
  217 
  218             e.close() ;
  219             e.abort() ;
  220         } ) ;
  221     }
  222 private:
  223     void setTimeOut( int s,QNetworkReply * e,std::function< void() > m )
  224     {
  225         if( s > 0 ){
  226 
  227             auto a = [ this ]( QNetworkReply * e ){ return this->cancel( e ) ; } ;
  228 
  229             auto u = new NetworkAccessManagerTimeOutManager( std::move( a ),
  230                                      std::move( m ),
  231                                      e,s,&m_manager ) ;
  232 
  233             connect( &m_manager,SIGNAL( finished( QNetworkReply * ) ),
  234                  u,SLOT( networkReply( QNetworkReply * ) ),Qt::QueuedConnection ) ;
  235         }
  236     }
  237     bool find_network_reply( QNetworkReply * e,std::function< void( QNetworkReply&,position_t ) > function )
  238     {
  239         for( position_t s = 0 ; s < m_entries.size() ; s++ ){
  240 
  241             if( std::get< 0 >( m_entries[ s ] ) == e ){
  242 
  243                 function( *e,s ) ;
  244 
  245                 return true ;
  246             }
  247         }
  248 
  249         return false ;
  250     }
  251 private slots:
  252     void networkReply( QNetworkReply * e )
  253     {
  254         this->find_network_reply( e,[ this ]( QNetworkReply& e,position_t s ){
  255 
  256             std::get< 2 >( m_entries[ s ] )( e ) ;
  257 
  258             if( std::get< 1 >( m_entries[ s ] ) ){
  259 
  260                 e.deleteLater() ;
  261             }
  262 
  263             m_entries.erase( m_entries.begin() + s ) ;
  264         } ) ;
  265     }
  266 };
  267 
  268 #endif