reference_counted.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // reference_counted.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio.hpp>
  11. #include <iostream>
  12. #include <memory>
  13. #include <utility>
  14. #include <vector>
  15. #include <ctime>
  16. using boost::asio::ip::tcp;
  17. // A reference-counted non-modifiable buffer class.
  18. class shared_const_buffer
  19. {
  20. public:
  21. // Construct from a std::string.
  22. explicit shared_const_buffer(const std::string& data)
  23. : data_(new std::vector<char>(data.begin(), data.end())),
  24. buffer_(boost::asio::buffer(*data_))
  25. {
  26. }
  27. // Implement the ConstBufferSequence requirements.
  28. typedef boost::asio::const_buffer value_type;
  29. typedef const boost::asio::const_buffer* const_iterator;
  30. const boost::asio::const_buffer* begin() const { return &buffer_; }
  31. const boost::asio::const_buffer* end() const { return &buffer_ + 1; }
  32. private:
  33. std::shared_ptr<std::vector<char> > data_;
  34. boost::asio::const_buffer buffer_;
  35. };
  36. class session
  37. : public std::enable_shared_from_this<session>
  38. {
  39. public:
  40. session(tcp::socket socket)
  41. : socket_(std::move(socket))
  42. {
  43. }
  44. void start()
  45. {
  46. do_write();
  47. }
  48. private:
  49. void do_write()
  50. {
  51. std::time_t now = std::time(0);
  52. shared_const_buffer buffer(std::ctime(&now));
  53. auto self(shared_from_this());
  54. boost::asio::async_write(socket_, buffer,
  55. [self](boost::system::error_code /*ec*/, std::size_t /*length*/)
  56. {
  57. });
  58. }
  59. // The socket used to communicate with the client.
  60. tcp::socket socket_;
  61. };
  62. class server
  63. {
  64. public:
  65. server(boost::asio::io_context& io_context, short port)
  66. : acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
  67. {
  68. do_accept();
  69. }
  70. private:
  71. void do_accept()
  72. {
  73. acceptor_.async_accept(
  74. [this](boost::system::error_code ec, tcp::socket socket)
  75. {
  76. if (!ec)
  77. {
  78. std::make_shared<session>(std::move(socket))->start();
  79. }
  80. do_accept();
  81. });
  82. }
  83. tcp::acceptor acceptor_;
  84. };
  85. int main(int argc, char* argv[])
  86. {
  87. try
  88. {
  89. if (argc != 2)
  90. {
  91. std::cerr << "Usage: reference_counted <port>\n";
  92. return 1;
  93. }
  94. boost::asio::io_context io_context;
  95. server s(io_context, std::atoi(argv[1]));
  96. io_context.run();
  97. }
  98. catch (std::exception& e)
  99. {
  100. std::cerr << "Exception: " << e.what() << "\n";
  101. }
  102. return 0;
  103. }