MCPcopy Create free account
hub / github.com/boostorg/asio / accept

Method accept

example/cpp11/fork/process_per_connection.cpp:57–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55 }
56
57 void accept()
58 {
59 acceptor_.async_accept(
60 [this](boost::system::error_code ec, tcp::socket new_socket)
61 {
62 if (!ec)
63 {
64 // Take ownership of the newly accepted socket.
65 socket_ = std::move(new_socket);
66
67 // Inform the io_context that we are about to fork. The io_context
68 // cleans up any internal resources, such as threads, that may
69 // interfere with forking.
70 io_context_.notify_fork(boost::asio::io_context::fork_prepare);
71
72 if (fork() == 0)
73 {
74 // Inform the io_context that the fork is finished and that this
75 // is the child process. The io_context uses this opportunity to
76 // create any internal file descriptors that must be private to
77 // the new process.
78 io_context_.notify_fork(boost::asio::io_context::fork_child);
79
80 // The child won't be accepting new connections, so we can close
81 // the acceptor. It remains open in the parent.
82 acceptor_.close();
83
84 // The child process is not interested in processing the SIGCHLD
85 // signal.
86 signal_.cancel();
87
88 read();
89 }
90 else
91 {
92
93 // Inform the io_context that the fork is finished (or failed)
94 // and that this is the parent process. The io_context uses this
95 // opportunity to recreate any internal resources that were
96 // cleaned up during preparation for the fork.
97 io_context_.notify_fork(boost::asio::io_context::fork_parent);
98
99 // The parent process can now close the newly accepted socket. It
100 // remains open in the child.
101 socket_.close();
102
103 accept();
104 }
105 }
106 else
107 {
108 std::cerr << "Accept error: " << ec.message() << std::endl;
109 accept();
110 }
111 });
112 }
113
114 void read()

Callers

nothing calls this directly

Calls 6

readFunction · 0.85
acceptFunction · 0.85
notify_forkMethod · 0.80
async_acceptMethod · 0.45
closeMethod · 0.45
cancelMethod · 0.45

Tested by

no test coverage detected