The loop with the accept should loop infinetly, with no timeout. This will cause your program to listen to the port, and everytime a connection is received, it will be handled by a new thread. The loop immideately goes back to listening for subsequent requests.while(1) { my $conn = $self->{daemon}->accept; # blocks return undef unless $conn; my $thread = new Thread \&handle_request, $conn; # Thread away, next please! } sub handle_request { my $conn = shift; my $req = $conn->get_request; # The rest of your code here }
Since I do not know the rest of your code, I will take some wild shots here. It seems like you want to call accept_messages for each message you want to receive, and get some answer back, to do something with. Well, with my kind of approach, you want to call this method once, and then let it run, doing the processing of the message in the thread you spawn. That way you can handle a lot of messages. But this will probably trigger quite a rewrite, depening on what you do with the message ($content) you previously got back from accept_messages?
You will also need to implement some way of shutting down the server, I usually have a command I can send to it via the port, if it isn't public. Or you could just trap CTRL-C and do some cleaning up (letting the threads finish first via join, perhaps, if you store them in an array) before a graceful shutdown.
Anyhow, this is, more or less, how most servers do operate. Sorry if I am being unclear on something here. And do remember that this is more like pseudo-code than anything you can use and trust off the bat. Also you will want to read up on all the quirks with threading, so you know what you are doing. Done right, they are indeed very powerful tools. :)
In reply to Re: HTTP::Daemon and broken pipes
by Dog and Pony
in thread HTTP::Daemon and broken pipes
by dash2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |