Re: [asio-users] Files, unix sockets, pipes with io_service? Or boost.signals?
Brought to you by:
chris_kohlhoff
|
From: Casimiro, D. C C. N. N. <dan...@na...> - 2008-08-07 21:00:49
|
> Daniel I've found your unix domain socket endpoint and
> protocol code but unsure how to use them properly. I've
> failed with socketpair() - I get a callback now but it always
> gets called with bytes_transferred == 0!
>
> How does one go about using your endpoint with socket::accept
> and connect? Like this? Will it work like sockpair and/or a
> pipe()? Something like this?
>
> endpoint ep("/tmp/socket");
> acceptor a(service, ep);
> socket s(service);
>
> a.async_accept(s, &handler);
> s.connect(ep)
>
> ??
Here is my code. I removed the classes(mostly) and the error handling.
I initialize the UNIX socket as follows:
dcc::asio::local::domain::socket childcomm_;
char buffer_[8];
int chldpipefd_[2];
socketpair(AF_UNIX, SOCK_STREAM, 0, chldpipefd_);
childcomm_.assign(dcc::asio::local::domain(), chldpipefd_[0]);
childcomm_.async_receive(boost::asio::buffer(buffer_),
boost::bind(&child_events::read_handler,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
------------------------------------------------------------------------
-
The "signal handler" looks like:
if (sig == SIGCHLD) {
ssize_t bytes = 0;
do {
bytes = write(chldpipefd_[1], "A", 1);
} while (bytes != 1);
}
// this is run from a background thread after sigwait returns SIGCHLD.
------------------------------------------------------------------------
--
I hope that helps.
~Dan
|