Exemplo n.º 1
0
        virtual void accept()
        {
            //Create new socket for this connection
            //Shared_ptr is used to pass temporary objects to the asynchronous functions
            auto socket = std::make_shared<HTTPS>(*io_service, context);

            acceptor->async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code &ec)
            {
                //Immediately start accepting a new connection (if io_service hasn't been stopped)
                if (ec != boost::asio::error::operation_aborted)
                    accept();


                if (!ec)
                {
                    boost::asio::ip::tcp::no_delay option(true);
                    socket->lowest_layer().set_option(option);

                    //Set timeout on the following boost::asio::ssl::stream::async_handshake
                    auto timer = get_timeout_timer(socket, config.timeout_request);
                    socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer]
                            (const boost::system::error_code &ec)
                    {
                        if (timer)
                            timer->cancel();
                        if (!ec)
                            read_request_and_content(socket);
                        else if (on_error)
                            on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
                    });
                }
                else if (on_error)
                    on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
            });
        }
Exemplo n.º 2
0
#if defined(ASIO_HAS_MOVE)
    socket1 = ip::udp::socket(ioc);
    socket1 = std::move(socket2);
#endif // defined(ASIO_HAS_MOVE)

    // basic_io_object functions.

    io_context& ioc_ref = socket1.get_io_context();
    (void)ioc_ref;

    ip::udp::socket::executor_type ex = socket1.get_executor();
    (void)ex;

    // basic_socket functions.

    ip::udp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
    (void)lowest_layer;

    const ip::udp::socket& socket8 = socket1;
    const ip::udp::socket::lowest_layer_type& lowest_layer2
      = socket8.lowest_layer();
    (void)lowest_layer2;

    socket1.open(ip::udp::v4());
    socket1.open(ip::udp::v6());
    socket1.open(ip::udp::v4(), ec);
    socket1.open(ip::udp::v6(), ec);

#if !defined(ASIO_WINDOWS_RUNTIME)
    ip::udp::socket::native_handle_type native_socket2
      = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Exemplo n.º 3
0
bool TLSClient::BeginConnect(const IPEndpoint& remote, const connect_callback_t& callback)
{
    if (canceled)
        return false;

    auto stream = std::make_shared<asio::ssl::stream<asio::ip::tcp::socket>>(this->executor->strand.get_io_service(),
                                                                             this->ctx.value);

    auto verify = [self = shared_from_this()](bool preverified, asio::ssl::verify_context& ctx) -> bool {
        self->LogVerifyCallback(preverified, ctx);
        return preverified;
    };

    std::error_code ec;
    stream->set_verify_callback(verify, ec);

    if (ec)
    {
        auto cb = [self = shared_from_this(), callback, stream, ec] {
            if (!self->canceled)
            {
                callback(self->executor, stream, ec);
            }
        };

        this->executor->strand.post(cb);
        return true;
    }

    SocketHelpers::BindToLocalAddress(this->adapter, this->localEndpoint, stream->lowest_layer(), ec);

    if (ec)
    {
        auto cb = [self = shared_from_this(), callback, stream, ec] {
            if (!self->canceled)
            {
                callback(self->executor, stream, ec);
            }
        };

        this->executor->strand.post(cb);
        return true;
    }

    const auto address = asio::ip::address::from_string(remote.address, ec);
    auto self = this->shared_from_this();
    if (ec)
    {
        // Try DNS resolution instead
        auto cb = [self, callback, stream](const std::error_code& ec, asio::ip::tcp::resolver::iterator endpoints) {
            self->HandleResolveResult(callback, stream, endpoints, ec);
        };

        std::stringstream portstr;
        portstr << remote.port;

        resolver.async_resolve(asio::ip::tcp::resolver::query(remote.address, portstr.str()),
                               executor->strand.wrap(cb));

        return true;
    }

    asio::ip::tcp::endpoint remoteEndpoint(address, remote.port);
    auto cb = [self, stream, callback](const std::error_code& ec) { self->HandleConnectResult(callback, stream, ec); };

    stream->lowest_layer().async_connect(remoteEndpoint, executor->strand.wrap(cb));
    return true;
}
	endpoint_type local_endpoint(error_code &ec) const { return lowest_layer().local_endpoint(ec); }
	endpoint_type local_endpoint() const { return lowest_layer().local_endpoint(); }
	error_code close(error_code &ec) { return lowest_layer().close(ec); }
	void close() { lowest_layer().close(); }
	error_code do_connect(const endpoint_type &peer_endpoint, error_code &ec)
	{
		return lowest_layer().connect(peer_endpoint, ec);		
	}
	void do_connect(const endpoint_type &peer_endpoint) { lowest_layer().connect(peer_endpoint); }
	void do_async_connect(const endpoint_type& peer_endpoint, ConnectHandler handler)
	{
		lowest_layer().async_connect(peer_endpoint, handler);		
	}
	endpoint_type remote_endpoint(error_code &ec) { return lowest_layer().remote_endpoint(); }
	endpoint_type remote_endpoint() { return lowest_layer().remote_endpoint(); }