process_per_connection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // process_per_connection.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/io_context.hpp>
  11. #include <boost/asio/ip/tcp.hpp>
  12. #include <boost/asio/signal_set.hpp>
  13. #include <boost/asio/write.hpp>
  14. #include <boost/array.hpp>
  15. #include <boost/bind.hpp>
  16. #include <cstdlib>
  17. #include <iostream>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <unistd.h>
  21. using boost::asio::ip::tcp;
  22. class server
  23. {
  24. public:
  25. server(boost::asio::io_context& io_context, unsigned short port)
  26. : io_context_(io_context),
  27. signal_(io_context, SIGCHLD),
  28. acceptor_(io_context, tcp::endpoint(tcp::v4(), port)),
  29. socket_(io_context)
  30. {
  31. start_signal_wait();
  32. start_accept();
  33. }
  34. private:
  35. void start_signal_wait()
  36. {
  37. signal_.async_wait(boost::bind(&server::handle_signal_wait, this));
  38. }
  39. void handle_signal_wait()
  40. {
  41. // Only the parent process should check for this signal. We can determine
  42. // whether we are in the parent by checking if the acceptor is still open.
  43. if (acceptor_.is_open())
  44. {
  45. // Reap completed child processes so that we don't end up with zombies.
  46. int status = 0;
  47. while (waitpid(-1, &status, WNOHANG) > 0) {}
  48. start_signal_wait();
  49. }
  50. }
  51. void start_accept()
  52. {
  53. acceptor_.async_accept(socket_,
  54. boost::bind(&server::handle_accept, this, _1));
  55. }
  56. void handle_accept(const boost::system::error_code& ec)
  57. {
  58. if (!ec)
  59. {
  60. // Inform the io_context that we are about to fork. The io_context cleans
  61. // up any internal resources, such as threads, that may interfere with
  62. // forking.
  63. io_context_.notify_fork(boost::asio::io_context::fork_prepare);
  64. if (fork() == 0)
  65. {
  66. // Inform the io_context that the fork is finished and that this is the
  67. // child process. The io_context uses this opportunity to create any
  68. // internal file descriptors that must be private to the new process.
  69. io_context_.notify_fork(boost::asio::io_context::fork_child);
  70. // The child won't be accepting new connections, so we can close the
  71. // acceptor. It remains open in the parent.
  72. acceptor_.close();
  73. // The child process is not interested in processing the SIGCHLD signal.
  74. signal_.cancel();
  75. start_read();
  76. }
  77. else
  78. {
  79. // Inform the io_context that the fork is finished (or failed) and that
  80. // this is the parent process. The io_context uses this opportunity to
  81. // recreate any internal resources that were cleaned up during
  82. // preparation for the fork.
  83. io_context_.notify_fork(boost::asio::io_context::fork_parent);
  84. socket_.close();
  85. start_accept();
  86. }
  87. }
  88. else
  89. {
  90. std::cerr << "Accept error: " << ec.message() << std::endl;
  91. start_accept();
  92. }
  93. }
  94. void start_read()
  95. {
  96. socket_.async_read_some(boost::asio::buffer(data_),
  97. boost::bind(&server::handle_read, this, _1, _2));
  98. }
  99. void handle_read(const boost::system::error_code& ec, std::size_t length)
  100. {
  101. if (!ec)
  102. start_write(length);
  103. }
  104. void start_write(std::size_t length)
  105. {
  106. boost::asio::async_write(socket_, boost::asio::buffer(data_, length),
  107. boost::bind(&server::handle_write, this, _1));
  108. }
  109. void handle_write(const boost::system::error_code& ec)
  110. {
  111. if (!ec)
  112. start_read();
  113. }
  114. boost::asio::io_context& io_context_;
  115. boost::asio::signal_set signal_;
  116. tcp::acceptor acceptor_;
  117. tcp::socket socket_;
  118. boost::array<char, 1024> data_;
  119. };
  120. int main(int argc, char* argv[])
  121. {
  122. try
  123. {
  124. if (argc != 2)
  125. {
  126. std::cerr << "Usage: process_per_connection <port>\n";
  127. return 1;
  128. }
  129. boost::asio::io_context io_context;
  130. using namespace std; // For atoi.
  131. server s(io_context, atoi(argv[1]));
  132. io_context.run();
  133. }
  134. catch (std::exception& e)
  135. {
  136. std::cerr << "Exception: " << e.what() << std::endl;
  137. }
  138. }