websocket_6_timeouts.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #include <boost/beast/_experimental/unit_test/suite.hpp>
  10. #ifdef BOOST_MSVC
  11. #pragma warning(push)
  12. #pragma warning(disable: 4459) // declaration hides global declaration
  13. #endif
  14. #include <boost/beast.hpp>
  15. #include <boost/beast/ssl.hpp>
  16. #include <boost/asio.hpp>
  17. #include <boost/asio/ssl.hpp>
  18. #include <iostream>
  19. namespace {
  20. #include "websocket_common.ipp"
  21. void
  22. snippets()
  23. {
  24. {
  25. stream<tcp_stream> ws(ioc);
  26. {
  27. //[code_websocket_6_1
  28. // Apply suggested timeout options for the server role to the stream
  29. ws.set_option(stream_base::timeout::suggested(role_type::server));
  30. //]
  31. }
  32. {
  33. //[code_websocket_6_2
  34. stream_base::timeout opt{
  35. std::chrono::seconds(30), // handshake timeout
  36. stream_base::none(), // idle timeout
  37. false
  38. };
  39. // Set the timeout options on the stream.
  40. ws.set_option(opt);
  41. //]
  42. }
  43. {
  44. flat_buffer b;
  45. //[code_websocket_6_3
  46. ws.async_read(b,
  47. [](error_code ec, std::size_t)
  48. {
  49. if(ec == beast::error::timeout)
  50. std::cerr << "timeout, connection closed!";
  51. });
  52. //]
  53. }
  54. }
  55. {
  56. //[code_websocket_6_4
  57. // Disable any timeouts on the tcp_stream
  58. sock.expires_never();
  59. // Construct the websocket stream, taking ownership of the existing tcp_stream
  60. stream<tcp_stream> ws(std::move(sock));
  61. //]
  62. }
  63. }
  64. struct websocket_6_test
  65. : public boost::beast::unit_test::suite
  66. {
  67. void
  68. run() override
  69. {
  70. BEAST_EXPECT(&snippets);
  71. }
  72. };
  73. BEAST_DEFINE_TESTSUITE(beast,doc,websocket_6);
  74. } // (anon)
  75. #ifdef BOOST_MSVC
  76. #pragma warning(pop)
  77. #endif