Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Сети > boost::asio::async_accept


Автор: kapusta007 17.7.2020, 23:39
добрый вечер ! использую boost::asio столкнулся с такой проблемой.есть сервер SSL и клиент, при установки соединения async_accept соединение устанавливается но не вызывается handler. причем есть просто код сервера отдельно все работает с тем же клиентом

вот код

Код

SSL_Server=std::make_unique<Server_SSL>(m_io_context, 3333);
m_io_context.run();


Код

Server_SSL::Server_SSL(boost::asio::io_context& io_context, unsigned short port)
    : m_acceptor(io_context, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
    m_context(boost::asio::ssl::context::sslv23), m_socket(io_context, m_context)
{
    m_context.set_options(
        boost::asio::ssl::context::default_workarounds
        | boost::asio::ssl::context::no_sslv2
        | boost::asio::ssl::context::single_dh_use);
    m_context.set_password_callback(std::bind(&Server_SSL::get_password, this));
    m_context.use_certificate_chain_file("server.crt");
    m_context.use_private_key_file("server.key", boost::asio::ssl::context::pem);
    m_context.use_tmp_dh_file("dh2048.pem");

    accept();

    std::cout << m_acceptor.local_endpoint() << '\n';
}

void Server_SSL::accept()
{
    m_acceptor.async_accept([this](const boost::system::error_code& error, boost::asio::ip::tcp::socket socket)
        {
            if (!error)
            {
                //std::make_shared<Session>(std::move(socket), m_context)->start();
                handshake();
            }
        });
}


BOOST_ASIO_ENABLE_HANDLER_TRACKING

старт сервера
https://bbcode0.com https://time2.link/utc.html

старт клиента
https://bbcode0.com https://time2.link/utc.html

принудительное закрытие сервера
https://bbcode0.com https://time2.link/utc.html

закрытие соединения сервера

https://bbcode0.com https://time2.link/utc.html

что делаю не так?

Автор: kapusta007 18.7.2020, 19:43
взял пример из boost

https://www.boost.org/doc/libs/1_70_0/doc/html/boost_asio/example/cpp11/ssl/server.cpp

ровно как есть собрал запустил работает.
затем взял разнес реализацию от интерфейса по SSL_Server.h, SSL_Server.cpp, Session.h, Session.cpp , main вынес в Serv.cpp
 в итоге болт!!! такая же проблема как выше.  не понимаю в чем причина.

SSL_Server.h
Код

#ifndef SSL_SERVER
#define SSL_SERVER

#define BOOST_ASIO_ENABLE_HANDLER_TRACKING

#include "Session.h"
#include <functional>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>


using boost::asio::ip::tcp;

class SSL_Server
{
public:
    SSL_Server(boost::asio::io_context& io_context, unsigned short port);
private:
    void do_accept();
    std::string get_password() const;
private:
    tcp::acceptor acceptor_;
    boost::asio::ssl::context context_;
};

#endif


SSL_Server.cpp
Код

#include "Server_SSL.h"

    SSL_Server::SSL_Server(boost::asio::io_context& io_context, unsigned short port)
        : acceptor_(io_context, tcp::endpoint(tcp::v4(), port)),
        context_(boost::asio::ssl::context::sslv23)
    {
        context_.set_options(
            boost::asio::ssl::context::default_workarounds
            | boost::asio::ssl::context::no_sslv2
            | boost::asio::ssl::context::single_dh_use);
        context_.set_password_callback(std::bind(&SSL_Server::get_password, this));
        context_.use_certificate_chain_file("server.crt");
        context_.use_private_key_file("server.key", boost::asio::ssl::context::pem);
        context_.use_tmp_dh_file("dh2048.pem");

        do_accept();
    }

std::string SSL_Server::get_password() const
{
        return "test";
}

void SSL_Server::do_accept()
{
    acceptor_.async_accept([this](const boost::system::error_code& error, tcp::socket socket)
    {
         if (!error)
         {
             std::make_shared<session>(std::move(socket), context_)->start();
         }
    });
}


Session.h
Код

#ifndef SESSION
#define SESSION

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>

using boost::asio::ip::tcp;

class session : public std::enable_shared_from_this<session>
{
public:
    session(tcp::socket socket, boost::asio::ssl::context& context);

    void start();
private:
    void do_handshake();
    void do_read();
    void do_write(std::size_t length);
private:
    boost::asio::ssl::stream<tcp::socket> socket_;
    char data_[1024];
};

#endif



Session.cpp
Код

#include "Session.h"
session::session(tcp::socket socket, boost::asio::ssl::context& context)
    : socket_(std::move(socket), context)
{
}

void session::start()
{
    do_handshake();
}

void session::do_handshake()
{
    auto self(shared_from_this());
    socket_.async_handshake(boost::asio::ssl::stream_base::server,
        [this, self](const boost::system::error_code& error)
        {
            if (!error)
            {
                do_read();
            }
        });
}

void session::do_read()
{
    auto self(shared_from_this());
    socket_.async_read_some(boost::asio::buffer(data_),
        [this, self](const boost::system::error_code& ec, std::size_t length)
        {
            if (!ec)
            {
                do_write(length);
            }
        });
}
void session::do_write(std::size_t length)
{
    auto self(shared_from_this());
    boost::asio::async_write(socket_, boost::asio::buffer(data_, length),
        [this, self](const boost::system::error_code& ec,
            std::size_t /*length*/)
        {
            if (!ec)
            {
                do_read();
            }
        });
}


Serv.cpp
Код

#include "Server_SSL.h"

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 2)
        {
            std::cerr << "Usage: server <port>\n";
            return 1;
        }

        boost::asio::io_context io_context;

        SSL_Server s(io_context, 3333);

        io_context.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)