3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Library General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
13 * You should have received a copy of the GNU Library General Public
14 * License along with this library; if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 * $Id: pthr_listener.c,v 1.5 2002/12/08 13:41:07 rich Exp $
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
37 #ifdef HAVE_NETINET_IN_H
38 #include <netinet/in.h>
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
49 #include "pthr_pseudothread.h"
50 #include "pthr_listener.h"
56 void (*processor_fn) (int sock, void *data);
60 static void run (void *vp);
63 new_listener (int sock,
64 void (*processor_fn) (int sock, void *data), void *data)
70 p = pmalloc (pool, sizeof *p);
73 p->processor_fn = processor_fn;
75 p->pth = new_pseudothread (pool, run, p, "listener");
85 listener p = (listener) vp;
89 struct sockaddr_in addr;
92 /* Wait for the new connection. */
94 ns = pth_accept (p->sock, (struct sockaddr *) &addr, &sz);
96 if (ns < 0) { perror ("accept"); continue; }
98 /* Set the new socket to non-blocking. */
99 if (fcntl (ns, F_SETFL, O_NONBLOCK) < 0) abort ();
101 /* Create a new processor thread to handle this connection. */
102 p->processor_fn (ns, p->data);