Skip to content

Commit 710238c

Browse files
committed
Add simpler ServeStream function
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.
1 parent 5f42547 commit 710238c

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)