|
Boost Users : |
Subject: Re: [Boost-users] Networking TS + Beast, NEW Tutorials, Read this to learn std::net !!!
From: Cristian Morales Vega (cristian_at_[hidden])
Date: 2019-03-15 14:57:58
On Fri, 15 Mar 2019 at 14:44, Vinnie Falco via Boost-users
<boost-users_at_[hidden]> wrote:
> The documentation on services is particularly scant. There is no explanation of what they are for, how to use them, best practices, or even "Frequently Asked Questions." I was able to figure it out though, but I am able to focus all of my time on it.
How much time I did lose looking at "services" when I actually only
needed a "composed operation"... (at that point the ASIO docs had one
example of a service, but none of a "composed operation"; now it has
of both, so it's improving).
> If you define
>
> struct sync_socket
> {
> net::io_context ioc_;
> net::ip::tcp::socket_;
> net::steady_timer timer_;
> ...
> template<class Buffers>
> std::size_t write_some (Buffers const& b);
> };
>
> You can easily implement timeouts in `write_some` thusly:
>
> template<class Buffers>
> std::size_t
> sync_socket::
> write_some (Buffers const& b, error_code& ec)
> {
> std::size_t n;
> timer_.expires_after(seconds(30));
> timer_.async_wait(
> [&](error_code ec)
> {
> socket_.cancel(ec):
> });
> socket_.async_write_some(b,
> [&](error_code ec_, std::size_t n_)
> {
> ec = ec_; n = n_;
> });
> ioc_.run();
> return n;
> }
>
> Now, you can use `sync_socket` with all the usual sync write operations like `net::write`, and get timeouts.
When you explain it it actually sounds obvious... and I feel silly for
not seeing it. You do that a lot to me.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net