@@ -499,14 +499,26 @@ std::unique_ptr<ProxyClient<InitInterface>> ConnectStream(EventLoop& loop, int f
499499 kj::mv (init_client), connection.release (), /* destroy_connection= */ true );
500500}
501501
502- // ! Given stream and a callback to construct a new ProxyServer object that
503- // ! handles requests from the stream, create a new Connection callback, pass it
504- // ! to the callback, use the returned ProxyServer to handle requests, and delete
505- // ! the proxyserver if the connection is disconnected.
506- // ! This should be called from the event loop thread.
507- void ServeStream (EventLoop& loop,
508- kj::Own<kj::AsyncIoStream>&& stream,
509- std::function<capnp::Capability::Client(Connection&)> make_server);
502+ // ! Given stream file descriptor and an init object, construct a new ProxyServer
503+ // ! object that handles requests from the stream calling the init object. Embed
504+ // ! the ProxyServer in a Connection object that is stored and erased if
505+ // ! disconnected. This should be called from the event loop thread.
506+ template <typename InitInterface, typename InitImpl>
507+ void ServeStream (EventLoop& loop, int fd, InitImpl& init)
508+ {
509+ loop.m_incoming_connections .emplace_front (loop,
510+ loop.m_io_context .lowLevelProvider ->wrapSocketFd (fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP),
511+ [&](Connection& connection) {
512+ // Set owned to false so proxy object doesn't attempt to delete init
513+ // object on disconnect/close.
514+ return kj::heap<mp::ProxyServer<InitInterface>>(&init, false , connection);
515+ });
516+ auto it = loop.m_incoming_connections .begin ();
517+ it->onDisconnect ([&loop, it] {
518+ loop.log () << " IPC server: socket disconnected." ;
519+ loop.m_incoming_connections .erase (it);
520+ });
521+ }
510522
511523extern thread_local ThreadContext g_thread_context;
512524
0 commit comments