3 Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
5 Written by Christopher Faylor of Cygnus Solutions
8 This file is part of Cygwin.
10 This software is a copyrighted work licensed under the terms of the
11 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
15 * The following line means that the BSD socket
16 * definitions for fd_set, FD_ISSET etc. are used in this
20 #define __INSIDE_CYGWIN_NET__
24 #include <sys/socket.h>
36 * All these defines below should be in sys/types.h
37 * but because of the includes above, they may not have
38 * been included. We create special UNIX_xxxx versions here.
42 #define NBBY 8 /* number of bits in a byte */
46 * Select uses bit masks of file descriptors in longs.
47 * These macros manipulate such bit fields (the filesystem macros use chars).
48 * FD_SETSIZE may be defined by the user, but the default here
49 * should be >= NOFILE (param.h).
53 #define UNIX_NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */
55 #define unix_howmany(x,y) (((x)+((y)-1))/(y))
58 #define unix_fd_set fd_set
60 #define NULL_fd_set ((fd_set *)NULL)
61 #define sizeof_fd_set(n) \
62 ((unsigned) (NULL_fd_set->fds_bits + unix_howmany((n), UNIX_NFDBITS)))
63 #define UNIX_FD_SET(n, p) \
64 ((p)->fds_bits[(n)/UNIX_NFDBITS] |= (1L << ((n) % UNIX_NFDBITS)))
65 #define UNIX_FD_CLR(n, p) \
66 ((p)->fds_bits[(n)/UNIX_NFDBITS] &= ~(1L << ((n) % UNIX_NFDBITS)))
67 #define UNIX_FD_ISSET(n, p) \
68 ((p)->fds_bits[(n)/UNIX_NFDBITS] & (1L << ((n) % UNIX_NFDBITS)))
69 #define UNIX_FD_ZERO(p, n) \
70 bzero ((caddr_t)(p), sizeof_fd_set ((n)))
72 #define allocfd_set(n) ((fd_set *) alloca (sizeof_fd_set (n)))
73 #define copyfd_set(to, from, n) memcpy (to, from, sizeof_fd_set (n));
75 /* Make a fhandler_foo::ready_for_ready method.
76 Assumption: The "ready_for_read" methods are called with one level of
78 #define MAKEready(what) \
80 fhandler_##what::ready_for_read (int fd, DWORD howlong, int ignra) \
82 select_record me (this); \
84 (void) select_read (&me); \
85 while (!peek_##what (&me, ignra) && howlong == INFINITE) \
86 if (fd >= 0 && dtable.not_open (fd)) \
88 else if (WaitForSingleObject (signal_arrived, 10) == WAIT_OBJECT_0) \
90 return me.read_ready; \
93 #define set_handle_or_return_if_not_open(h, s) \
94 h = (s)->fh->get_handle (); \
95 if (dtable.not_open ((s)->fd)) \
97 (s)->saw_error = TRUE; \
102 /* The main select code.
106 cygwin_select (int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
110 fd_set *dummy_readfds = allocfd_set (n);
111 fd_set *dummy_writefds = allocfd_set (n);
112 fd_set *dummy_exceptfds = allocfd_set (n);
122 select_printf ("%d, %p, %p, %p, %p", n, readfds, writefds, exceptfds, to);
124 memset (&sel, 0, sizeof (sel));
127 UNIX_FD_ZERO (dummy_readfds, n);
128 readfds = dummy_readfds;
132 UNIX_FD_ZERO (dummy_writefds, n);
133 writefds = dummy_writefds;
137 UNIX_FD_ZERO (dummy_exceptfds, n);
138 exceptfds = dummy_exceptfds;
141 for (int i = 0; i < n; i++)
142 if (!sel.test_and_set (i, readfds, writefds, exceptfds))
144 select_printf ("aborting due to test_and_set error");
145 return -1; /* Invalid fd, maybe? */
148 /* Convert to milliseconds or INFINITE if to == NULL */
149 DWORD ms = to ? (to->tv_sec * 1000) + (to->tv_usec / 1000) : INFINITE;
150 if (ms == 0 && to->tv_usec)
151 ms = 1; /* At least 1 ms granularity */
154 select_printf ("to->tv_sec %d, to->tv_usec %d, ms %d", to->tv_sec, to->tv_usec, ms);
156 select_printf ("to NULL, ms %x", ms);
158 select_printf ("sel.total %d, sel.always_ready %d", sel.total, sel.always_ready);
160 /* Degenerate case. No fds to wait for. Just wait. */
163 if (WaitForSingleObject (signal_arrived, ms) == WAIT_OBJECT_0)
165 select_printf ("signal received");
166 set_sig_errno (EINTR);
172 /* If one of the selected fds is "always ready" just poll everything and return
173 the result. There is no need to wait. */
174 if (sel.always_ready || ms == 0)
176 UNIX_FD_ZERO (readfds, n);
177 UNIX_FD_ZERO (writefds, n);
178 UNIX_FD_ZERO (exceptfds, n);
179 return sel.poll (readfds, writefds, exceptfds);
182 /* Wait for an fd to come alive */
183 return sel.wait (readfds, writefds, exceptfds, ms);
187 select_stuff::~select_stuff ()
189 select_record *s = &start;
191 select_printf ("calling cleanup routines");
192 while ((s = s->next))
194 s->cleanup (s, this);
196 select_record *snext = start.next;
198 select_printf ("deleting select records");
206 /* Add a record to the select chain */
208 select_stuff::test_and_set (int i, fd_set *readfds, fd_set *writefds,
211 select_record *s = NULL;
212 if (UNIX_FD_ISSET (i, readfds) && (s = dtable.select_read (i, s)) == NULL)
213 return 0; /* error */
214 if (UNIX_FD_ISSET (i, writefds) && (s = dtable.select_write (i, s)) == NULL)
215 return 0; /* error */
216 if (UNIX_FD_ISSET (i, exceptfds) && (s = dtable.select_except (i, s)) == NULL)
217 return 0; /* error */
219 return 1; /* nothing to do */
221 if (s->read_ready || s->write_ready || s->except_ready)
224 if (s->windows_handle || s->windows_handle || s->windows_handle)
227 s->next = start.next;
233 /* Poll every fd in the select chain. Set appropriate fd in mask. */
235 select_stuff::poll (fd_set *readfds, fd_set *writefds, fd_set *exceptfds)
238 select_record *s = &start;
239 while ((s = s->next))
240 n += s->poll (s, readfds, writefds, exceptfds);
241 select_printf ("returning %d", n);
245 /* The heart of select. Waits for an fd to do something interesting. */
247 select_stuff::wait (fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
251 HANDLE w4[total + 1];
252 select_record *s = &start;
255 w4[m++] = signal_arrived; /* Always wait for the arrival of a signal. */
256 /* Loop through the select chain, starting up anything appropriate and
257 counting the number of active fds. */
258 while ((s = s->next))
260 if (!s->startup (s, this))
267 for (int i = 1; i < m; i++)
276 DWORD start_time = GetTickCount (); /* Record the current time for later use. */
278 /* Allocate some fd_set structures using the number of fds as a guide. */
279 fd_set *r = allocfd_set (n);
280 fd_set *w = allocfd_set (n);
281 fd_set *e = allocfd_set (n);
285 debug_printf ("n %d, ms %u", n, ms);
289 wait_ret = WaitForMultipleObjects (m, w4, FALSE, ms);
291 wait_ret = MsgWaitForMultipleObjects (m, w4, FALSE, ms, QS_ALLINPUT);
296 select_printf ("signal received");
297 set_sig_errno (EINTR);
300 select_printf ("WaitForMultipleObjects failed");
304 select_printf ("timed out");
308 select_printf ("woke up. wait_ret %d. verifying", wait_ret);
311 while ((s = s->next))
313 return -1; /* Somebody detected an error */
314 else if ((((wait_ret >= m && s->windows_handle) || s->h == w4[wait_ret])) &&
315 s->verify (s, r, w, e))
318 select_printf ("gotone %d", gotone);
324 select_printf ("looping");
327 select_printf ("recalculating ms");
329 DWORD now = GetTickCount ();
330 if (now > (start_time + ms))
332 select_printf ("timed out after verification");
335 ms -= (now - start_time);
337 select_printf ("ms now %u", ms);
341 copyfd_set (readfds, r, n);
342 copyfd_set (writefds, w, n);
343 copyfd_set (exceptfds, e, n);
345 return poll (readfds, writefds, exceptfds);
349 set_bits (select_record *me, fd_set *readfds, fd_set *writefds,
353 select_printf ("me %p, testing fd %d (%s)", me, me->fd, me->fh->get_name ());
354 if (me->read_selected && me->read_ready)
356 UNIX_FD_SET (me->fd, readfds);
359 if (me->write_selected && me->write_ready)
361 UNIX_FD_SET (me->fd, writefds);
364 if (me->except_ready && me->except_ready)
366 UNIX_FD_SET (me->fd, exceptfds);
369 select_printf ("ready %d", ready);
374 verify_true (select_record *, fd_set *, fd_set *, fd_set *)
380 verify_ok (select_record *me, fd_set *readfds, fd_set *writefds,
383 return set_bits (me, readfds, writefds, exceptfds);
387 no_startup (select_record *, select_stuff *)
393 no_verify (select_record *, fd_set *, fd_set *, fd_set *)
399 peek_pipe (select_record *s, int ignra)
403 fhandler_base *fh = s->fh;
406 set_handle_or_return_if_not_open (h, s);
408 /* Don't perform complicated tests if we don't need to. */
409 if (!s->read_selected && !s->except_selected)
412 if (s->read_selected && fh->bg_check (SIGTTIN) <= 0)
414 gotone = s->read_ready = 1;
418 if (!ignra && fh->get_readahead_valid ())
420 select_printf ("readahead");
421 gotone = s->read_ready = 1;
425 else if (!PeekNamedPipe (h, NULL, 0, NULL, (DWORD *) &n, NULL))
427 select_printf ("%s, PeekNamedPipe failed, %E", fh->get_name ());
433 select_printf ("%s, n %d", fh->get_name (), n);
434 if (s->except_selected)
435 gotone += s->except_ready = TRUE;
436 if (s->read_selected)
437 gotone += s->read_ready = TRUE;
439 if (n > 0 && s->read_selected)
441 select_printf ("%s, ready for read", fh->get_name ());
442 gotone += s->read_ready = TRUE;
444 if (!gotone && s->fh->hit_eof ())
446 select_printf ("%s, saw EOF", fh->get_name ());
447 if (s->except_selected)
448 gotone = s->except_ready = TRUE;
449 if (s->read_selected)
450 gotone += s->read_ready = TRUE;
454 return gotone || s->write_ready;
458 poll_pipe (select_record *me, fd_set *readfds, fd_set *writefds,
461 return peek_pipe (me, 0) ?
462 set_bits (me, readfds, writefds, exceptfds) :
468 static int start_thread_pipe (select_record *me, select_stuff *stuff);
473 BOOL stop_thread_pipe;
474 select_record *start;
478 thread_pipe (void *arg)
480 pipeinf *pi = (pipeinf *)arg;
485 select_record *s = pi->start;
486 while ((s = s->next))
487 if (s->startup == start_thread_pipe)
489 if (peek_pipe (s, 0))
491 if (pi->stop_thread_pipe)
493 select_printf ("stopping");
506 start_thread_pipe (select_record *me, select_stuff *stuff)
508 if (stuff->device_specific[FHDEVN(FH_PIPE)])
510 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_PIPE)])->thread;
513 pipeinf *pi = new pipeinf;
514 pi->start = &stuff->start;
515 pi->stop_thread_pipe = FALSE;
516 pi->thread = me->h = makethread (thread_pipe, (LPVOID)pi, 0, "select_pipe");
519 stuff->device_specific[FHDEVN(FH_PIPE)] = (void *)pi;
524 pipe_cleanup (select_record *, select_stuff *stuff)
526 pipeinf *pi = (pipeinf *)stuff->device_specific[FHDEVN(FH_PIPE)];
527 if (pi && pi->thread)
529 pi->stop_thread_pipe = TRUE;
530 WaitForSingleObject (pi->thread, INFINITE);
531 CloseHandle (pi->thread);
533 stuff->device_specific[FHDEVN(FH_PIPE)] = NULL;
538 fhandler_pipe::select_read (select_record *s)
541 s = new select_record;
542 s->startup = start_thread_pipe;
544 s->verify = verify_ok;
545 s->read_selected = TRUE;
546 s->cleanup = pipe_cleanup;
551 fhandler_pipe::select_write (select_record *s)
555 s = new select_record;
556 s->startup = no_startup;
558 s->verify = no_verify;
560 s->write_selected = TRUE;
561 s->write_ready = TRUE;
566 fhandler_pipe::select_except (select_record *s)
569 s = new select_record;
570 s->startup = start_thread_pipe;
572 s->verify = verify_ok;
573 s->cleanup = pipe_cleanup;
574 s->except_selected = TRUE;
579 peek_console (select_record *me, int ignra)
581 extern const char * get_nonascii_key (INPUT_RECORD& input_rec);
582 fhandler_console *fh = (fhandler_console *)me->fh;
584 if (!me->read_selected)
585 return me->write_ready;
587 if (!ignra && fh->get_readahead_valid ())
589 select_printf ("readahead");
590 return me->read_ready = 1;
596 set_handle_or_return_if_not_open (h, me);
599 if (fh->bg_check (SIGTTIN) <= 0)
600 return me->read_ready = 1;
601 else if (!PeekConsoleInput (h, &irec, 1, &events_read) || !events_read)
605 if (irec.EventType == WINDOW_BUFFER_SIZE_EVENT)
606 kill_pgrp (fh->tc->getpgid (), SIGWINCH);
607 else if (irec.EventType == KEY_EVENT && irec.Event.KeyEvent.bKeyDown == TRUE &&
608 (irec.Event.KeyEvent.uChar.AsciiChar || get_nonascii_key (irec)))
609 return me->read_ready = 1;
611 /* Read and discard the event */
612 ReadConsoleInput (h, &irec, 1, &events_read);
615 return me->write_ready;
619 poll_console (select_record *me, fd_set *readfds, fd_set *writefds,
622 return peek_console (me, 0) ?
623 set_bits (me, readfds, writefds, exceptfds) :
630 fhandler_console::select_read (select_record *s)
634 s = new select_record;
635 s->startup = no_startup;
636 s->poll = poll_console;
637 s->verify = poll_console;
640 s->h = get_handle ();
641 s->read_selected = TRUE;
646 fhandler_console::select_write (select_record *s)
650 s = new select_record;
651 s->startup = no_startup;
652 s->poll = poll_console;
653 s->verify = no_verify;
656 s->write_selected = TRUE;
657 s->write_ready = TRUE;
662 fhandler_console::select_except (select_record *s)
666 s = new select_record;
667 s->startup = no_startup;
668 s->poll = poll_console;
669 s->verify = no_verify;
672 s->except_selected = TRUE;
677 fhandler_tty_common::ready_for_read (int fd, DWORD howlong, int ignra)
680 if (myself->pgid && get_ttyp ()->getpgid () != myself->pgid &&
681 myself->ctty == ttynum) // background process?
682 return 1; // Yes. Let read return an error
684 return ((fhandler_pipe*)this)->fhandler_pipe::ready_for_read (fd, howlong, ignra);
688 fhandler_tty_common::select_read (select_record *s)
690 return ((fhandler_pipe*)this)->fhandler_pipe::select_read (s);
694 fhandler_tty_common::select_write (select_record *s)
696 return ((fhandler_pipe *)this)->fhandler_pipe::select_write (s);
700 fhandler_tty_common::select_except (select_record *s)
702 return ((fhandler_pipe *)this)->fhandler_pipe::select_except (s);
706 fhandler_dev_null::select_read (select_record *s)
710 s = new select_record;
711 s->startup = no_startup;
713 s->verify = no_verify;
715 s->h = get_handle ();
716 s->read_selected = TRUE;
721 fhandler_dev_null::select_write (select_record *s)
725 s = new select_record;
726 s->startup = no_startup;
728 s->verify = no_verify;
730 s->h = get_handle ();
731 s->write_selected = TRUE;
736 fhandler_dev_null::select_except (select_record *s)
740 s = new select_record;
741 s->startup = no_startup;
743 s->verify = no_verify;
745 s->h = get_handle ();
746 s->except_selected = TRUE;
747 s->except_ready = TRUE;
751 static int start_thread_serial (select_record *me, select_stuff *stuff);
756 BOOL stop_thread_serial;
757 select_record *start;
761 peek_serial (select_record *s, int)
766 fhandler_serial *fh = (fhandler_serial *)s->fh;
768 if (fh->get_readahead_valid () || fh->overlapped_armed < 0)
769 return s->read_ready = 1;
771 select_printf ("fh->overlapped_armed %d", fh->overlapped_armed);
774 set_handle_or_return_if_not_open (h, s);
776 (void) SetCommMask (h, EV_RXCHAR);
778 if (!fh->overlapped_armed)
783 ResetEvent (fh->io_status.hEvent);
785 if (!ClearCommError (h, &ev, &st))
787 debug_printf ("ClearCommError");
791 return s->read_ready = 1;
792 else if (WaitCommEvent (h, &ev, &fh->io_status))
793 return s->read_ready = 1;
794 else if (GetLastError () == ERROR_IO_PENDING)
795 fh->overlapped_armed = 1;
798 debug_printf ("WaitCommEvent");
806 w4[0] = fh->io_status.hEvent;
807 w4[1] = signal_arrived;
810 switch (WaitForMultipleObjects (2, w4, FALSE, to))
813 if (!ClearCommError (h, &ev, &st))
815 debug_printf ("ClearCommError");
818 else if (!st.cbInQue)
822 return s->read_ready = 1;
823 select_printf ("got something");
825 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
827 case WAIT_OBJECT_0 + 1:
828 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
829 select_printf ("interrupt");
830 set_sig_errno (EINTR);
834 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
837 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
838 debug_printf ("WaitForMultipleObjects");
845 if (GetLastError () == ERROR_OPERATION_ABORTED)
847 select_printf ("operation aborted");
853 select_printf ("error %E");
858 thread_serial (void *arg)
860 serialinf *si = (serialinf *)arg;
865 select_record *s = si->start;
866 while ((s = s->next))
867 if (s->startup == start_thread_serial)
869 if (peek_serial (s, 0))
872 if (si->stop_thread_serial)
874 select_printf ("stopping");
881 select_printf ("exiting");
886 start_thread_serial (select_record *me, select_stuff *stuff)
888 if (stuff->device_specific[FHDEVN(FH_SERIAL)])
890 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_SERIAL)])->thread;
893 serialinf *si = new serialinf;
894 si->start = &stuff->start;
895 si->stop_thread_serial = FALSE;
896 si->thread = me->h = makethread (thread_serial, (LPVOID)si, 0, "select_serial");
899 stuff->device_specific[FHDEVN(FH_SERIAL)] = (void *)si;
904 serial_cleanup (select_record *, select_stuff *stuff)
906 serialinf *si = (serialinf *)stuff->device_specific[FHDEVN(FH_SERIAL)];
907 if (si && si->thread)
909 si->stop_thread_serial = TRUE;
910 WaitForSingleObject (si->thread, INFINITE);
911 CloseHandle (si->thread);
913 stuff->device_specific[FHDEVN(FH_SERIAL)] = NULL;
918 poll_serial (select_record *me, fd_set *readfds, fd_set *writefds,
922 return peek_serial (me, 0) ?
923 set_bits (me, readfds, writefds, exceptfds) :
930 fhandler_serial::select_read (select_record *s)
934 s = new select_record;
935 s->startup = start_thread_serial;
936 s->poll = poll_serial;
937 s->verify = verify_ok;
938 s->cleanup = serial_cleanup;
940 s->read_selected = TRUE;
945 fhandler_serial::select_write (select_record *s)
949 s = new select_record;
950 s->startup = no_startup;
952 s->verify = verify_ok;
954 s->h = get_handle ();
955 s->write_selected = TRUE;
956 s->write_ready = TRUE;
961 fhandler_serial::select_except (select_record *s)
965 s = new select_record;
966 s->startup = no_startup;
968 s->verify = verify_ok;
971 s->except_selected = FALSE; // Can't do this
976 fhandler_base::ready_for_read (int, DWORD, int)
982 fhandler_base::select_read (select_record *s)
986 s = new select_record;
987 s->startup = no_startup;
989 s->verify = verify_ok;
991 s->h = get_handle ();
992 s->read_selected = TRUE;
993 s->read_ready = TRUE;
998 fhandler_base::select_write (select_record *s)
1002 s = new select_record;
1003 s->startup = no_startup;
1005 s->verify = verify_ok;
1007 s->h = get_handle ();
1008 s->write_selected = TRUE;
1009 s->write_ready = TRUE;
1014 fhandler_base::select_except (select_record *s)
1018 s = new select_record;
1019 s->startup = no_startup;
1021 s->verify = verify_ok;
1024 s->write_selected = TRUE;
1031 winsock_fd_set readfds, writefds, exceptfds;
1033 struct sockaddr_in sin;
1034 select_record *start;
1038 peek_socket (select_record *me, int)
1040 winsock_fd_set ws_readfds, ws_writefds, ws_exceptfds;
1041 struct timeval tv = {0, 0};
1042 WINSOCK_FD_ZERO (&ws_readfds);
1043 WINSOCK_FD_ZERO (&ws_writefds);
1044 WINSOCK_FD_ZERO (&ws_exceptfds);
1048 set_handle_or_return_if_not_open (h, me);
1049 select_printf ("considering handle %p", h);
1051 if (me->read_selected)
1053 select_printf ("adding read fd_set %s, fd %d", me->fh->get_name (),
1055 WINSOCK_FD_SET (h, &ws_readfds);
1057 if (me->write_selected)
1059 select_printf ("adding write fd_set %s, fd %d", me->fh->get_name (),
1061 WINSOCK_FD_SET (h, &ws_writefds);
1063 if (me->except_selected)
1065 select_printf ("adding except fd_set %s, fd %d", me->fh->get_name (),
1067 WINSOCK_FD_SET (h, &ws_exceptfds);
1069 int r = WINSOCK_SELECT (0, &ws_readfds, &ws_writefds, &ws_exceptfds, &tv);
1070 select_printf ("WINSOCK_SELECT returned %d", r);
1073 select_printf ("error %d", WSAGetLastError ());
1077 if (WINSOCK_FD_ISSET (h, &ws_readfds))
1078 gotone = me->read_ready = TRUE;
1079 if (WINSOCK_FD_ISSET (h, &ws_writefds))
1080 gotone = me->write_ready = TRUE;
1081 if (WINSOCK_FD_ISSET (h, &ws_exceptfds))
1082 gotone = me->except_ready = TRUE;
1087 poll_socket (select_record *me, fd_set *readfds, fd_set *writefds,
1090 return peek_socket (me, 0) ?
1091 set_bits (me, readfds, writefds, exceptfds) :
1097 static int start_thread_socket (select_record *, select_stuff *);
1100 thread_socket (void *arg)
1102 socketinf *si = (socketinf *)arg;
1104 select_printf ("stuff_start %p", &si->start);
1105 int r = WINSOCK_SELECT (0, &si->readfds, &si->writefds, &si->exceptfds, NULL);
1106 select_printf ("Win32 select returned %d", r);
1108 select_printf ("error %d", WSAGetLastError ());
1109 select_record *s = si->start;
1110 while ((s = s->next))
1111 if (s->startup == start_thread_socket)
1113 HANDLE h = s->fh->get_handle ();
1114 select_printf ("s %p, testing fd %d (%s)", s, s->fd, s->fh->get_name ());
1115 if (WINSOCK_FD_ISSET (h, &si->readfds))
1117 select_printf ("read_ready");
1118 s->read_ready = TRUE;
1120 if (WINSOCK_FD_ISSET (h, &si->writefds))
1122 select_printf ("write_ready");
1123 s->write_ready = TRUE;
1125 if (WINSOCK_FD_ISSET (h, &si->exceptfds))
1127 select_printf ("except_ready");
1128 s->except_ready = TRUE;
1132 if (WINSOCK_FD_ISSET (si->exitsock, &si->readfds))
1133 select_printf ("saw exitsock read");
1138 extern "C" unsigned long htonl (unsigned long);
1141 start_thread_socket (select_record *me, select_stuff *stuff)
1145 if ((si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)]))
1152 WINSOCK_FD_ZERO (&si->readfds);
1153 WINSOCK_FD_ZERO (&si->writefds);
1154 WINSOCK_FD_ZERO (&si->exceptfds);
1155 select_record *s = &stuff->start;
1156 while ((s = s->next))
1157 if (s->startup == start_thread_socket)
1159 HANDLE h = s->fh->get_handle ();
1160 select_printf ("Handle %p", h);
1161 if (s->read_selected)
1163 WINSOCK_FD_SET (h, &si->readfds);
1164 select_printf ("Added to readfds");
1166 if (s->write_selected)
1168 WINSOCK_FD_SET (h, &si->writefds);
1169 select_printf ("Added to writefds");
1171 if (s->except_selected)
1173 WINSOCK_FD_SET (h, &si->exceptfds);
1174 select_printf ("Added to exceptfds");
1178 if ((si->exitsock = socket (PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
1180 set_winsock_errno ();
1181 select_printf ("cannot create socket, %E");
1184 /* Allow rapid reuse of the port. */
1186 (void) setsockopt (si->exitsock, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
1188 int sin_len = sizeof(si->sin);
1189 memset (&si->sin, 0, sizeof (si->sin));
1190 si->sin.sin_family = AF_INET;
1191 si->sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1192 if (bind (si->exitsock, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1194 select_printf ("cannot bind socket, %E");
1198 if (getsockname (si->exitsock, (struct sockaddr *) &si->sin, &sin_len) < 0)
1200 select_printf ("getsockname error");
1204 if (listen (si->exitsock, 1))
1206 select_printf ("listen failed, %E");
1210 select_printf ("exitsock %p", si->exitsock);
1211 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->readfds);
1212 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->exceptfds);
1213 stuff->device_specific[FHDEVN(FH_SOCKET)] = (void *) si;
1214 si->start = &stuff->start;
1215 select_printf ("stuff_start %p", &stuff->start);
1216 si->thread = me->h = makethread (thread_socket, (LPVOID)si, 0,
1221 set_winsock_errno ();
1222 closesocket (si->exitsock);
1227 socket_cleanup (select_record *, select_stuff *stuff)
1229 socketinf *si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)];
1230 select_printf ("si %p si->thread %p", si, si ? si->thread : NULL);
1231 if (si && si->thread)
1233 select_printf ("connection to si->exitsock %p", si->exitsock);
1234 SOCKET s = socket (AF_INET, SOCK_STREAM, 0);
1235 /* Connecting to si->exitsock will cause any executing select to wake
1236 up. When this happens then the exitsock condition will cause the
1237 thread to terminate. */
1238 if (connect (s, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1240 set_winsock_errno ();
1241 select_printf ("connect failed");
1242 /* FIXME: now what? */
1246 /* Wait for thread to go away */
1247 WaitForSingleObject (si->thread, INFINITE);
1248 closesocket (si->exitsock);
1249 CloseHandle (si->thread);
1250 stuff->device_specific[FHDEVN(FH_SOCKET)] = NULL;
1253 select_printf ("returning");
1257 fhandler_socket::select_read (select_record *s)
1261 s = new select_record;
1262 s->startup = start_thread_socket;
1263 s->poll = poll_socket;
1264 s->verify = verify_true;
1265 s->cleanup = socket_cleanup;
1267 s->read_selected = TRUE;
1272 fhandler_socket::select_write (select_record *s)
1276 s = new select_record;
1277 s->startup = start_thread_socket;
1278 s->poll = poll_socket;
1279 s->verify = verify_true;
1280 s->cleanup = socket_cleanup;
1282 s->write_selected = TRUE;
1287 fhandler_socket::select_except (select_record *s)
1291 s = new select_record;
1292 s->startup = start_thread_socket;
1293 s->poll = poll_socket;
1294 s->verify = verify_true;
1295 s->cleanup = socket_cleanup;
1297 s->except_selected = TRUE;
1302 peek_windows (select_record *me, int)
1306 set_handle_or_return_if_not_open (h, me);
1307 if (PeekMessage (&m, (HWND) h, 0, 0, PM_NOREMOVE))
1309 me->read_ready = TRUE;
1310 select_printf ("window %d(%p) ready", me->fd, me->fh->get_handle ());
1314 select_printf ("window %d(%p) not ready", me->fd, me->fh->get_handle ());
1315 return me->write_ready;
1319 poll_windows (select_record *me, fd_set *readfds, fd_set *writefds,
1323 return peek_windows (me, 0) ?
1324 set_bits (me, readfds, writefds, exceptfds) :
1331 fhandler_windows::select_read (select_record *s)
1335 s = new select_record;
1336 s->startup = no_startup;
1337 s->poll = poll_windows;
1338 s->verify = poll_windows;
1340 s->h = get_handle ();
1341 s->read_selected = TRUE;
1342 s->h = get_handle ();
1343 s->windows_handle = TRUE;
1348 fhandler_windows::select_write (select_record *s)
1352 s = new select_record;
1353 s->startup = no_startup;
1355 s->verify = verify_ok;
1357 s->h = get_handle ();
1358 s->write_selected = TRUE;
1359 s->write_ready = TRUE;
1360 s->windows_handle = TRUE;
1365 fhandler_windows::select_except (select_record *s)
1369 s = new select_record;
1370 s->startup = no_startup;
1372 s->verify = verify_ok;
1374 s->h = get_handle ();
1375 s->except_selected = TRUE;
1376 s->except_ready = TRUE;
1377 s->windows_handle = TRUE;