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 *me, fd_set *readfds, fd_set *writefds,
381 verify_ok (select_record *me, fd_set *readfds, fd_set *writefds,
384 return set_bits (me, readfds, writefds, exceptfds);
388 no_startup (select_record *me, select_stuff *stuff)
394 no_verify (select_record *, fd_set *, fd_set *, fd_set *)
400 peek_pipe (select_record *s, int ignra)
404 fhandler_base *fh = s->fh;
407 set_handle_or_return_if_not_open (h, s);
409 /* Don't perform complicated tests if we don't need to. */
410 if (!s->read_selected && !s->except_selected)
413 if (s->read_selected && fh->bg_check (SIGTTIN) <= 0)
415 gotone = s->read_ready = 1;
419 if (!ignra && fh->get_readahead_valid ())
421 select_printf ("readahead");
422 gotone = s->read_ready = 1;
426 else if (!PeekNamedPipe (h, NULL, 0, NULL, (DWORD *) &n, NULL))
428 select_printf ("%s, PeekNamedPipe failed, %E", fh->get_name ());
434 select_printf ("%s, n %d", fh->get_name (), n);
435 if (s->except_selected)
436 gotone += s->except_ready = TRUE;
437 if (s->read_selected)
438 gotone += s->read_ready = TRUE;
440 if (n > 0 && s->read_selected)
442 select_printf ("%s, ready for read", fh->get_name ());
443 gotone += s->read_ready = TRUE;
445 if (!gotone && s->fh->hit_eof ())
447 select_printf ("%s, saw EOF", fh->get_name ());
448 if (s->except_selected)
449 gotone = s->except_ready = TRUE;
450 if (s->read_selected)
451 gotone += s->read_ready = TRUE;
455 return gotone || s->write_ready;
459 poll_pipe (select_record *me, fd_set *readfds, fd_set *writefds,
462 return peek_pipe (me, 0) ?
463 set_bits (me, readfds, writefds, exceptfds) :
469 static int start_thread_pipe (select_record *me, select_stuff *stuff);
474 BOOL stop_thread_pipe;
475 select_record *start;
479 thread_pipe (void *arg)
481 pipeinf *pi = (pipeinf *)arg;
486 select_record *s = pi->start;
487 while ((s = s->next))
488 if (s->startup == start_thread_pipe)
490 if (peek_pipe (s, 0))
492 if (pi->stop_thread_pipe)
494 select_printf ("stopping");
507 start_thread_pipe (select_record *me, select_stuff *stuff)
509 if (stuff->device_specific[FHDEVN(FH_PIPE)])
511 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_PIPE)])->thread;
514 pipeinf *pi = new pipeinf;
515 pi->start = &stuff->start;
516 pi->stop_thread_pipe = FALSE;
517 pi->thread = me->h = makethread (thread_pipe, (LPVOID)pi, 0, "select_pipe");
520 stuff->device_specific[FHDEVN(FH_PIPE)] = (void *)pi;
525 pipe_cleanup (select_record *me, select_stuff *stuff)
527 pipeinf *pi = (pipeinf *)stuff->device_specific[FHDEVN(FH_PIPE)];
528 if (pi && pi->thread)
530 pi->stop_thread_pipe = TRUE;
531 WaitForSingleObject (pi->thread, INFINITE);
532 CloseHandle (pi->thread);
534 stuff->device_specific[FHDEVN(FH_PIPE)] = NULL;
539 fhandler_pipe::select_read (select_record *s)
542 s = new select_record;
543 s->startup = start_thread_pipe;
545 s->verify = verify_ok;
546 s->read_selected = TRUE;
547 s->cleanup = pipe_cleanup;
552 fhandler_pipe::select_write (select_record *s)
556 s = new select_record;
557 s->startup = no_startup;
559 s->verify = no_verify;
561 s->write_selected = TRUE;
562 s->write_ready = TRUE;
567 fhandler_pipe::select_except (select_record *s)
570 s = new select_record;
571 s->startup = start_thread_pipe;
573 s->verify = verify_ok;
574 s->cleanup = pipe_cleanup;
575 s->except_selected = TRUE;
580 peek_console (select_record *me, int ignra)
582 extern const char * get_nonascii_key (INPUT_RECORD& input_rec);
583 fhandler_console *fh = (fhandler_console *)me->fh;
585 if (!me->read_selected)
586 return me->write_ready;
588 if (!ignra && fh->get_readahead_valid ())
590 select_printf ("readahead");
591 return me->read_ready = 1;
597 set_handle_or_return_if_not_open (h, me);
600 if (fh->bg_check (SIGTTIN) <= 0)
601 return me->read_ready = 1;
602 else if (!PeekConsoleInput (h, &irec, 1, &events_read) || !events_read)
606 if (irec.EventType == WINDOW_BUFFER_SIZE_EVENT)
607 kill_pgrp (fh->tc->getpgid (), SIGWINCH);
608 else if (irec.EventType == KEY_EVENT && irec.Event.KeyEvent.bKeyDown == TRUE &&
609 (irec.Event.KeyEvent.uChar.AsciiChar || get_nonascii_key (irec)))
610 return me->read_ready = 1;
612 /* Read and discard the event */
613 ReadConsoleInput (h, &irec, 1, &events_read);
616 return me->write_ready;
620 poll_console (select_record *me, fd_set *readfds, fd_set *writefds,
623 return peek_console (me, 0) ?
624 set_bits (me, readfds, writefds, exceptfds) :
631 fhandler_console::select_read (select_record *s)
635 s = new select_record;
636 s->startup = no_startup;
637 s->poll = poll_console;
638 s->verify = poll_console;
641 s->h = get_handle ();
642 s->read_selected = TRUE;
647 fhandler_console::select_write (select_record *s)
651 s = new select_record;
652 s->startup = no_startup;
653 s->poll = poll_console;
654 s->verify = no_verify;
657 s->write_selected = TRUE;
658 s->write_ready = TRUE;
663 fhandler_console::select_except (select_record *s)
667 s = new select_record;
668 s->startup = no_startup;
669 s->poll = poll_console;
670 s->verify = no_verify;
673 s->except_selected = TRUE;
678 fhandler_tty_common::ready_for_read (int fd, DWORD howlong, int ignra)
681 if (myself->pgid && get_ttyp ()->getpgid () != myself->pgid &&
682 myself->ctty == ttynum) // background process?
683 return 1; // Yes. Let read return an error
685 return ((fhandler_pipe*)this)->fhandler_pipe::ready_for_read (fd, howlong, ignra);
689 fhandler_tty_common::select_read (select_record *s)
691 return ((fhandler_pipe*)this)->fhandler_pipe::select_read (s);
695 fhandler_tty_common::select_write (select_record *s)
697 return ((fhandler_pipe *)this)->fhandler_pipe::select_write (s);
701 fhandler_tty_common::select_except (select_record *s)
703 return ((fhandler_pipe *)this)->fhandler_pipe::select_except (s);
707 fhandler_dev_null::select_read (select_record *s)
711 s = new select_record;
712 s->startup = no_startup;
714 s->verify = no_verify;
716 s->h = get_handle ();
717 s->read_selected = TRUE;
722 fhandler_dev_null::select_write (select_record *s)
726 s = new select_record;
727 s->startup = no_startup;
729 s->verify = no_verify;
731 s->h = get_handle ();
732 s->write_selected = TRUE;
737 fhandler_dev_null::select_except (select_record *s)
741 s = new select_record;
742 s->startup = no_startup;
744 s->verify = no_verify;
746 s->h = get_handle ();
747 s->except_selected = TRUE;
748 s->except_ready = TRUE;
752 static int start_thread_serial (select_record *me, select_stuff *stuff);
757 BOOL stop_thread_serial;
758 select_record *start;
762 peek_serial (select_record *s, int)
767 fhandler_serial *fh = (fhandler_serial *)s->fh;
769 if (fh->get_readahead_valid () || fh->overlapped_armed < 0)
770 return s->read_ready = 1;
772 select_printf ("fh->overlapped_armed %d", fh->overlapped_armed);
775 set_handle_or_return_if_not_open (h, s);
777 (void) SetCommMask (h, EV_RXCHAR);
779 if (!fh->overlapped_armed)
784 ResetEvent (fh->io_status.hEvent);
786 if (!ClearCommError (h, &ev, &st))
788 debug_printf ("ClearCommError");
792 return s->read_ready = 1;
793 else if (WaitCommEvent (h, &ev, &fh->io_status))
794 return s->read_ready = 1;
795 else if (GetLastError () == ERROR_IO_PENDING)
796 fh->overlapped_armed = 1;
799 debug_printf ("WaitCommEvent");
807 w4[0] = fh->io_status.hEvent;
808 w4[1] = signal_arrived;
811 switch (WaitForMultipleObjects (2, w4, FALSE, to))
814 if (!ClearCommError (h, &ev, &st))
816 debug_printf ("ClearCommError");
819 else if (!st.cbInQue)
823 return s->read_ready = 1;
824 select_printf ("got something");
826 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
828 case WAIT_OBJECT_0 + 1:
829 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
830 select_printf ("interrupt");
831 set_sig_errno (EINTR);
835 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
838 PurgeComm (h, PURGE_TXABORT | PURGE_RXABORT);
839 debug_printf ("WaitForMultipleObjects");
846 if (GetLastError () == ERROR_OPERATION_ABORTED)
848 select_printf ("operation aborted");
854 select_printf ("error %E");
859 thread_serial (void *arg)
861 serialinf *si = (serialinf *)arg;
866 select_record *s = si->start;
867 while ((s = s->next))
868 if (s->startup == start_thread_serial)
870 if (peek_serial (s, 0))
873 if (si->stop_thread_serial)
875 select_printf ("stopping");
882 select_printf ("exiting");
887 start_thread_serial (select_record *me, select_stuff *stuff)
889 if (stuff->device_specific[FHDEVN(FH_SERIAL)])
891 me->h = ((pipeinf *) stuff->device_specific[FHDEVN(FH_SERIAL)])->thread;
894 serialinf *si = new serialinf;
895 si->start = &stuff->start;
896 si->stop_thread_serial = FALSE;
897 si->thread = me->h = makethread (thread_serial, (LPVOID)si, 0, "select_serial");
900 stuff->device_specific[FHDEVN(FH_SERIAL)] = (void *)si;
905 serial_cleanup (select_record *me, select_stuff *stuff)
907 serialinf *si = (serialinf *)stuff->device_specific[FHDEVN(FH_SERIAL)];
908 if (si && si->thread)
910 si->stop_thread_serial = TRUE;
911 WaitForSingleObject (si->thread, INFINITE);
912 CloseHandle (si->thread);
914 stuff->device_specific[FHDEVN(FH_SERIAL)] = NULL;
919 poll_serial (select_record *me, fd_set *readfds, fd_set *writefds,
923 return peek_serial (me, 0) ?
924 set_bits (me, readfds, writefds, exceptfds) :
931 fhandler_serial::select_read (select_record *s)
935 s = new select_record;
936 s->startup = start_thread_serial;
937 s->poll = poll_serial;
938 s->verify = verify_ok;
939 s->cleanup = serial_cleanup;
941 s->read_selected = TRUE;
946 fhandler_serial::select_write (select_record *s)
950 s = new select_record;
951 s->startup = no_startup;
953 s->verify = verify_ok;
955 s->h = get_handle ();
956 s->write_selected = TRUE;
957 s->write_ready = TRUE;
962 fhandler_serial::select_except (select_record *s)
966 s = new select_record;
967 s->startup = no_startup;
969 s->verify = verify_ok;
972 s->except_selected = FALSE; // Can't do this
977 fhandler_base::ready_for_read (int, DWORD, int)
983 fhandler_base::select_read (select_record *s)
987 s = new select_record;
988 s->startup = no_startup;
990 s->verify = verify_ok;
992 s->h = get_handle ();
993 s->read_selected = TRUE;
994 s->read_ready = TRUE;
999 fhandler_base::select_write (select_record *s)
1003 s = new select_record;
1004 s->startup = no_startup;
1006 s->verify = verify_ok;
1008 s->h = get_handle ();
1009 s->write_selected = TRUE;
1010 s->write_ready = TRUE;
1015 fhandler_base::select_except (select_record *s)
1019 s = new select_record;
1020 s->startup = no_startup;
1022 s->verify = verify_ok;
1025 s->write_selected = TRUE;
1032 winsock_fd_set readfds, writefds, exceptfds;
1034 struct sockaddr_in sin;
1035 select_record *start;
1039 peek_socket (select_record *me, int)
1041 winsock_fd_set ws_readfds, ws_writefds, ws_exceptfds;
1042 struct timeval tv = {0};
1043 WINSOCK_FD_ZERO (&ws_readfds);
1044 WINSOCK_FD_ZERO (&ws_writefds);
1045 WINSOCK_FD_ZERO (&ws_exceptfds);
1049 set_handle_or_return_if_not_open (h, me);
1050 select_printf ("considering handle %p", h);
1052 if (me->read_selected)
1054 select_printf ("adding read fd_set %s, fd %d", me->fh->get_name (),
1056 WINSOCK_FD_SET (h, &ws_readfds);
1058 if (me->write_selected)
1060 select_printf ("adding write fd_set %s, fd %d", me->fh->get_name (),
1062 WINSOCK_FD_SET (h, &ws_writefds);
1064 if (me->except_selected)
1066 select_printf ("adding except fd_set %s, fd %d", me->fh->get_name (),
1068 WINSOCK_FD_SET (h, &ws_exceptfds);
1070 int r = WINSOCK_SELECT (0, &ws_readfds, &ws_writefds, &ws_exceptfds, &tv);
1071 select_printf ("WINSOCK_SELECT returned %d", r);
1074 select_printf ("error %d", WSAGetLastError ());
1078 if (WINSOCK_FD_ISSET (h, &ws_readfds))
1079 gotone = me->read_ready = TRUE;
1080 if (WINSOCK_FD_ISSET (h, &ws_writefds))
1081 gotone = me->write_ready = TRUE;
1082 if (WINSOCK_FD_ISSET (h, &ws_exceptfds))
1083 gotone = me->except_ready = TRUE;
1088 poll_socket (select_record *me, fd_set *readfds, fd_set *writefds,
1091 return peek_socket (me, 0) ?
1092 set_bits (me, readfds, writefds, exceptfds) :
1098 static int start_thread_socket (select_record *, select_stuff *);
1101 thread_socket (void *arg)
1103 socketinf *si = (socketinf *)arg;
1105 select_printf ("stuff_start %p", &si->start);
1106 int r = WINSOCK_SELECT (0, &si->readfds, &si->writefds, &si->exceptfds, NULL);
1107 select_printf ("Win32 select returned %d", r);
1109 select_printf ("error %d", WSAGetLastError ());
1110 select_record *s = si->start;
1111 while ((s = s->next))
1112 if (s->startup == start_thread_socket)
1114 HANDLE h = s->fh->get_handle ();
1115 select_printf ("s %p, testing fd %d (%s)", s, s->fd, s->fh->get_name ());
1116 if (WINSOCK_FD_ISSET (h, &si->readfds))
1118 select_printf ("read_ready");
1119 s->read_ready = TRUE;
1121 if (WINSOCK_FD_ISSET (h, &si->writefds))
1123 select_printf ("write_ready");
1124 s->write_ready = TRUE;
1126 if (WINSOCK_FD_ISSET (h, &si->exceptfds))
1128 select_printf ("except_ready");
1129 s->except_ready = TRUE;
1133 if (WINSOCK_FD_ISSET (si->exitsock, &si->readfds))
1134 select_printf ("saw exitsock read");
1139 extern "C" unsigned long htonl (unsigned long);
1142 start_thread_socket (select_record *me, select_stuff *stuff)
1146 if ((si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)]))
1153 WINSOCK_FD_ZERO (&si->readfds);
1154 WINSOCK_FD_ZERO (&si->writefds);
1155 WINSOCK_FD_ZERO (&si->exceptfds);
1156 select_record *s = &stuff->start;
1157 while ((s = s->next))
1158 if (s->startup == start_thread_socket)
1160 HANDLE h = s->fh->get_handle ();
1161 select_printf ("Handle %p", h);
1162 if (s->read_selected)
1164 WINSOCK_FD_SET (h, &si->readfds);
1165 select_printf ("Added to readfds");
1167 if (s->write_selected)
1169 WINSOCK_FD_SET (h, &si->writefds);
1170 select_printf ("Added to writefds");
1172 if (s->except_selected)
1174 WINSOCK_FD_SET (h, &si->exceptfds);
1175 select_printf ("Added to exceptfds");
1179 if ((si->exitsock = socket (PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
1181 set_winsock_errno ();
1182 select_printf ("cannot create socket, %E");
1185 /* Allow rapid reuse of the port. */
1187 (void) setsockopt (si->exitsock, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
1189 int sin_len = sizeof(si->sin);
1190 memset (&si->sin, 0, sizeof (si->sin));
1191 si->sin.sin_family = AF_INET;
1192 si->sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1193 if (bind (si->exitsock, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1195 select_printf ("cannot bind socket, %E");
1199 if (getsockname (si->exitsock, (struct sockaddr *) &si->sin, &sin_len) < 0)
1201 select_printf ("getsockname error");
1205 if (listen (si->exitsock, 1))
1207 select_printf ("listen failed, %E");
1211 select_printf ("exitsock %p", si->exitsock);
1212 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->readfds);
1213 WINSOCK_FD_SET ((HANDLE) si->exitsock, &si->exceptfds);
1214 stuff->device_specific[FHDEVN(FH_SOCKET)] = (void *) si;
1215 si->start = &stuff->start;
1216 select_printf ("stuff_start %p", &stuff->start);
1217 si->thread = me->h = makethread (thread_socket, (LPVOID)si, 0,
1222 set_winsock_errno ();
1223 closesocket (si->exitsock);
1228 socket_cleanup (select_record *me, select_stuff *stuff)
1230 socketinf *si = (socketinf *)stuff->device_specific[FHDEVN(FH_SOCKET)];
1231 select_printf ("si %p si->thread %p", si, si ? si->thread : NULL);
1232 if (si && si->thread)
1234 select_printf ("connection to si->exitsock %p", si->exitsock);
1235 SOCKET s = socket (AF_INET, SOCK_STREAM, 0);
1236 /* Connecting to si->exitsock will cause any executing select to wake
1237 up. When this happens then the exitsock condition will cause the
1238 thread to terminate. */
1239 if (connect (s, (struct sockaddr *) &si->sin, sizeof (si->sin)) < 0)
1241 set_winsock_errno ();
1242 select_printf ("connect failed");
1243 /* FIXME: now what? */
1247 /* Wait for thread to go away */
1248 WaitForSingleObject (si->thread, INFINITE);
1249 closesocket (si->exitsock);
1250 CloseHandle (si->thread);
1251 stuff->device_specific[FHDEVN(FH_SOCKET)] = NULL;
1254 select_printf ("returning");
1258 fhandler_socket::select_read (select_record *s)
1262 s = new select_record;
1263 s->startup = start_thread_socket;
1264 s->poll = poll_socket;
1265 s->verify = verify_true;
1266 s->cleanup = socket_cleanup;
1268 s->read_selected = TRUE;
1273 fhandler_socket::select_write (select_record *s)
1277 s = new select_record;
1278 s->startup = start_thread_socket;
1279 s->poll = poll_socket;
1280 s->verify = verify_true;
1281 s->cleanup = socket_cleanup;
1283 s->write_selected = TRUE;
1288 fhandler_socket::select_except (select_record *s)
1292 s = new select_record;
1293 s->startup = start_thread_socket;
1294 s->poll = poll_socket;
1295 s->verify = verify_true;
1296 s->cleanup = socket_cleanup;
1298 s->except_selected = TRUE;
1303 peek_windows (select_record *me, int)
1307 set_handle_or_return_if_not_open (h, me);
1308 if (PeekMessage (&m, (HWND) h, 0, 0, PM_NOREMOVE))
1310 me->read_ready = TRUE;
1311 select_printf ("window %d(%p) ready", me->fd, me->fh->get_handle ());
1315 select_printf ("window %d(%p) not ready", me->fd, me->fh->get_handle ());
1316 return me->write_ready;
1320 poll_windows (select_record *me, fd_set *readfds, fd_set *writefds,
1324 return peek_windows (me, 0) ?
1325 set_bits (me, readfds, writefds, exceptfds) :
1332 fhandler_windows::select_read (select_record *s)
1336 s = new select_record;
1337 s->startup = no_startup;
1338 s->poll = poll_windows;
1339 s->verify = poll_windows;
1341 s->h = get_handle ();
1342 s->read_selected = TRUE;
1343 s->h = get_handle ();
1344 s->windows_handle = TRUE;
1349 fhandler_windows::select_write (select_record *s)
1353 s = new select_record;
1354 s->startup = no_startup;
1356 s->verify = verify_ok;
1358 s->h = get_handle ();
1359 s->write_selected = TRUE;
1360 s->write_ready = TRUE;
1361 s->windows_handle = TRUE;
1366 fhandler_windows::select_except (select_record *s)
1370 s = new select_record;
1371 s->startup = no_startup;
1373 s->verify = verify_ok;
1375 s->h = get_handle ();
1376 s->except_selected = TRUE;
1377 s->except_ready = TRUE;
1378 s->windows_handle = TRUE;