Skip to content

Commit d24cae6

Browse files
committed
Merge #14: Add simpler ServeStream function
710238c Add simpler ServeStream function (Russell Yanofsky) Pull request description: Be more consistent with ConnectStream and just require Init message type instead of ProxyServer constructing callback, and file descriptor number instead of a stream object. Top commit has no ACKs. Tree-SHA512: afb7c755cf8c1977b5e6a1ea0f87df4790f307cfe128c6236da7579eb4ac585bbcbddeb0d83a08ea9fa6ed09d8b92b14612eed73f99afd071e1211633abfc855
2 parents 5f42547 + 710238c commit d24cae6

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

include/mp/proxy-io.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

511523
extern thread_local ThreadContext g_thread_context;
512524

src/mp/proxy.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,4 @@ std::string LongThreadName(const char* exe_name)
305305
return g_thread_context.thread_name.empty() ? ThreadName(exe_name) : g_thread_context.thread_name;
306306
}
307307

308-
void ServeStream(EventLoop& loop,
309-
kj::Own<kj::AsyncIoStream>&& stream,
310-
std::function<capnp::Capability::Client(Connection&)> make_server)
311-
{
312-
loop.m_incoming_connections.emplace_front(loop, kj::mv(stream), make_server);
313-
auto it = loop.m_incoming_connections.begin();
314-
it->onDisconnect([&loop, it] {
315-
loop.log() << "IPC server: socket disconnected.";
316-
loop.m_incoming_connections.erase(it);
317-
});
318-
}
319-
320-
void ServeStream(EventLoop& loop, int fd, std::function<capnp::Capability::Client(Connection&)> make_server)
321-
{
322-
ServeStream(loop,
323-
loop.m_io_context.lowLevelProvider->wrapSocketFd(fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP),
324-
std::move(make_server));
325-
}
326-
327308
} // namespace mp

0 commit comments

Comments
 (0)