-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-blocking accept #88
Comments
Apologies. I could have sworn I responded to this a few weeks ago... Perhaps I have a window with an unsent response buried somewhere on my desktop! My first question would be... What is the purpose of the non-blocking accept? The purpose for If we were to add this for Trying to do that and optimize it for all systems via So I'm worried about the slippery slope, but am curious about the need for |
I am currently using a timeout in my extended class to permit two things:
constexpr auto listen_duration = std::chrono::seconds(15);
std::atomic<bool> running{true}; // Modified by main thread when shutdown signal is handled
tcp_acceptor listener{};
inet_address peer{};
/*...*/
while(running) {
listener.accept(&peer, listen_duration);
/* do watchdog checking */
}
/* perform clean shutdown */ For my use case, a timeout is sufficient. I understand your concern about expanding the library to be fully asynchronous. One of the reasons why I selected this library for my application was because it was relatively lightweight. I had initially looked at using an asio based wrapper, but my compile times went through the roof. Since I only need to listen for a single expected incoming TCP connection I didn't want to pay this price for asio. |
Those are both pretty reasonable. But there should be a better way, in general, to do both of those things. Especially the second one. Certainly a timeout would work, but it's not the cleanest solution. You really want something that responds immediately and definitively to say "quit now" rather than polling in a loop. Ironically, one way is to use a But I wonder if there is a portable way to knock the socket off the |
But long and short... An overloaded |
Hey all! I see that the overload suggestion was marked to be added in v0.9 but that I agree with the conclusion but because I never found a portable solution to the "stopping issue" either, I like the polling pattern @gotnone has posted a lot. Also thanks a lot for the C++ bindings in general! It makes managing the C-resource lifetimes from socket.h a breeze! |
I'm on the fence. I just wanted to push out the 1.0 release to preserve a snapshot of the API that's been available for the last few years before a bunch of breaking changes were introduced (which will now be v2.0). So I just tossed out v1.0 rather abruptly without adding any new features or merging the PR's that could have shipped. I didn't want to risk any destabilization at the last minute. The v0.8 line seemed pretty stable. The plan is to maintain v1.x line for a little while... certainly to fix any bad bugs so people can keep using it indefinitely. But I can probably add some more features if there's a demand. But to be clear... v2.0 is going to be what the v0.9 release was supposed to be. I just renamed everything to be less confusing. I was hoping to get most of it done over the holiday break, and though I made a lot of progress, the TLS support is nowhere near ready. So I may put out a v2.0 with just the new error handling, and hold off on TLS until v2.1. |
Hey, thank you for the update. I was able to spend some time on the issue I was facing that related unix domain sockets and as it turned out I had to use the For tcp-sockets this all was way easier in the past I remember... Anyway very exciting stuff, I know. But it all made me question the design proposed in this myself. As I discovered, The Windows-implementation using the Windows overlapped API socket functions (shudder) and something like I think I see the pains now to decide on this for a generic library like Relevant `poll()` code block from my code/* code released into public domain */
/* author's note: this function does not do error checking */
void ConnectionHandler :: threadMain() {
struct pollfd polldata;
polldata.fd = acceptor->handle(); // acceptor is a std::shared_ptr<sockpp::unix_acceptor>
polldata.events = POLLERR | POLLIN | POLLPRI | POLLHUP;
while (alive) {
polldata.revents = 0;
int pollres = poll(&polldata, 1, 500); // 500 ms delay
if (polldata.revents || pollres) {
if (polldata.revents & POLLHUP) {
// Socket connection died, so we escape the thread
alive = false;
socket.close();
continue;
} else if (sockpp::unix_socket sock = acceptor->accept()) {
// do something with the shiny new sock
}
}
}
}
|
Are there any plans to add a non-blocking
accept()
for thetcp_acceptor
class?I attempted to add this to my own derived class, but I think it may be useful to add this to the main
tcp_acceptor
class.I based this off of the non-blocking
connect()
code, but I am far from an expert at socket programming, there are probably some issues in this implementation.The text was updated successfully, but these errors were encountered: