Menu

Boost serveer examples C++11

Help
Soren Bro
2020-12-14
2021-11-21
  • Soren Bro

    Soren Bro - 2020-12-14

    I'm tinkering around with the boost examples using asio and beast to write a rudimentary http server.

    Many of them, for instance:

    https://www.boost.org/doc/libs/develop/libs/beast/example/http/server/async/http_server_async.cpp

    and even later ones, make use of an improvised C11 lambda-isdh struct:

    // Handles an HTTP server connection
    class session : public std::enable_shared_from_this<session>
    {
       [B] // This is the C++11 equivalent of a generic lambda.
        // The function object is used to send an HTTP message.
        [/B]
        struct send_lambda
        {
            session& self_;
    
            explicit
            send_lambda(session& self)
                : self_(self)
            {
            }
    
            template<bool isRequest, class Body, class Fields>
            void
            operator()(http::message<isRequest, Body, Fields>&& msg) const
            {
                // The lifetime of the message has to extend
                // for the duration of the async operation so
                // we use a shared_ptr to manage it.
                auto sp = std::make_shared<
                    http::message<isRequest, Body, Fields>>(std::move(msg));
    
                // Store a type-erased version of the shared
                // pointer in the class to keep it alive.
                self_.res_ = sp;
    
                // Write the response
                http::async_write(
                    self_.stream_,
                    *sp,
                    beast::bind_front_handler(
                        &session::on_write,
                        self_.shared_from_this(),
                        sp->need_eof()));
            }
        };
    
        beast::tcp_stream stream_;
        beast::flat_buffer buffer_;
        std::shared_ptr<std::string const> doc_root_;
        http::request<http::string_body> req_;
        std::shared_ptr<void> res_;
    [B]    send_lambda lambda_;[/B]
    
    public:
        // Take ownership of the stream
        session(
            tcp::socket&& socket,
            std::shared_ptr<std::string const> const& doc_root)
            : stream_(std::move(socket))
            , doc_root_(doc_root)
    [B]        , lambda_(*this)[/B]
        {
        }
    

    as a workaround, which, as I understand, shouldn't be necessary beyond C++14. Now my question is (for the sake of sheer academic discussion):

    Shouldn't it be possible to rewrite this struct as a true generic lambda in C++ 14/17 and beyond? And is it worth the task at all (apart from trying to understand lambdas)?

    I understand lambdas readily enough but the fact that the "this" pointer is involved (in a constructor initializer list no less) makes it alittle more opaque. (I know can just move it to the body of the C-tor but stil..

     
  • Soren Bro

    Soren Bro - 2020-12-14

    Even the boost 1.70 exampåles uses the same construct, so maybe there's something going on here that just can' be solved by a generic lambda. It just seems to be it shouldn't be necessary.

    Just for the sake of argument I start my precompiled header along these lines:

    //------------------------------------------------------------------------------
    // This is pretty academic. But since we *can* check let's do it.
    //------------------------------------------------------------------------------
    
    #ifndef __cplusplus
    #error Not using C+
    #endif
    
    #define __CPLUSPLUS_PRE_98      1
    #define __CPLUSPLUS_98_(TRI)    199711L
    #define __CPLUSPLUS_11          201103L
    #define __CPLUSPLUS_14          201402L
    #define __CPLUSPLUS_17          201703L
    
    #if (__cplusplus < __CPLUSPLUS_17)
    #error We make heavy use of the auto keyword and lambdas. We need version 17 at least.
    #endif
    

    and add this switch to my g++ compile command:

    g++ -std=c++17 [...]
    
     
  • Soren Bro

    Soren Bro - 2020-12-14

    Any thought?. I'm not sking for someone to solve it for me. Solving the problem is much of the fun, Just a general pointer, please.

     
  • if ber

    if ber - 2021-11-21

    If you are not the usage of the "default personal IP Ranges such as 10.X.X.X. Or 192.168.X.X. Or 172.Sixteen.X.X you need to add your IP Range as Trusted Range as follows as you can check.

     
  • if ber

    if ber - 2021-11-21

    If you are not the usage of the "default personal IP Ranges such as 10.X.X.X. Or 192.168.X.X. Or 172.Sixteen.X.X you need to add your IP Range as Trusted Range as follows as you can check.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.