1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giowin32.c: IO Channels for Win32.
5 * Copyright 1998 Owen Taylor and Tor Lillqvist
6 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
7 * Copyright 2001-2003 Andrew Lanoix
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GLib Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 * Bugs that are related to the code in this file:
35 * Bug 137968 - Sometimes a GIOFunc on Win32 is called with zero condition
36 * http://bugzilla.gnome.org/show_bug.cgi?id=137968
38 * Bug 324234 - Using g_io_add_watch_full() to wait for connect() to return on a non-blocking socket returns prematurely
39 * http://bugzilla.gnome.org/show_bug.cgi?id=324234
41 * Bug 331214 - g_io_channel async socket io stalls
42 * http://bugzilla.gnome.org/show_bug.cgi?id=331214
44 * Bug 338943 - Multiple watches on the same socket
45 * http://bugzilla.gnome.org/show_bug.cgi?id=338943
47 * Bug 357674 - 2 serious bugs in giowin32.c making glib iochannels useless
48 * http://bugzilla.gnome.org/show_bug.cgi?id=357674
50 * Bug 425156 - GIOChannel deadlocks on a win32 socket
51 * http://bugzilla.gnome.org/show_bug.cgi?id=425156
53 * Bug 468910 - giofunc condition=0
54 * http://bugzilla.gnome.org/show_bug.cgi?id=468910
56 * Bug 500246 - Bug fixes for giowin32
57 * http://bugzilla.gnome.org/show_bug.cgi?id=500246
59 * Bug 548278 - Async GETs connections are always terminated unexpectedly on windows
60 * http://bugzilla.gnome.org/show_bug.cgi?id=548278
62 * Bug 548536 - giowin32 problem when adding and removing watches
63 * http://bugzilla.gnome.org/show_bug.cgi?id=548536
65 * When fixing bugs related to the code in this file, either the above
66 * bugs or others, make sure that the test programs attached to the
67 * above bugs continue to work.
89 typedef struct _GIOWin32Channel GIOWin32Channel;
90 typedef struct _GIOWin32Watch GIOWin32Watch;
92 #define BUFFER_SIZE 4096
95 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
97 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
98 * _open() or _pipe(), except for
99 * console IO. Separate thread to read
103 G_IO_WIN32_CONSOLE, /* Console IO (usually stdin, stdout, stderr) */
105 G_IO_WIN32_SOCKET /* Sockets. No separate thread. */
106 } GIOWin32ChannelType;
108 struct _GIOWin32Channel {
110 gint fd; /* Either a Unix-like file handle as provided
111 * by the Microsoft C runtime, or a SOCKET
112 * as provided by WinSock.
114 GIOWin32ChannelType type;
118 /* Field used by G_IO_WIN32_WINDOWS_MESSAGES channels */
119 HWND hwnd; /* Handle of window, or NULL */
121 /* Fields used by G_IO_WIN32_FILE_DESC channels. */
122 CRITICAL_SECTION mutex;
124 int direction; /* 0 means we read from it,
125 * 1 means we write to it.
128 gboolean running; /* Is reader or writer thread
129 * running. FALSE if EOF has been
130 * reached by the reader thread.
133 gboolean needs_close; /* If the channel has been closed while
134 * the reader thread was still running.
137 guint thread_id; /* If non-NULL the channel has or has
138 * had a reader or writer thread.
140 HANDLE data_avail_event;
144 /* Data is kept in a circular buffer. To be able to distinguish between
145 * empty and full buffers, we cannot fill it completely, but have to
146 * leave a one character gap.
148 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
151 * Full: (wrp + 1) % BUFFER_SIZE == rdp
154 guchar *buffer; /* (Circular) buffer */
155 gint wrp, rdp; /* Buffer indices for writing and reading */
156 HANDLE space_avail_event;
158 /* Fields used by G_IO_WIN32_SOCKET channels */
162 gboolean write_would_have_blocked;
163 gboolean ever_writable;
166 struct _GIOWin32Watch {
170 GIOCondition condition;
174 g_win32_print_access_mode (int flags)
176 g_print ("%s%s%s%s%s%s%s%s%s%s",
177 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
178 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
179 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
180 (flags & _O_APPEND ? "|O_APPEND" : ""),
181 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
182 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
183 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
184 (flags & _O_CREAT ? "|O_CREAT" : ""),
185 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
186 (flags & _O_EXCL ? "|O_EXCL" : ""),
187 (flags & _O_TEXT ? "|O_TEXT" : ""),
188 (flags & _O_BINARY ? "|O_BINARY" : ""));
192 g_win32_print_gioflags (GIOFlags flags)
196 if (flags & G_IO_FLAG_APPEND)
197 bar = "|", g_print ("APPEND");
198 if (flags & G_IO_FLAG_NONBLOCK)
199 g_print ("%sNONBLOCK", bar), bar = "|";
200 if (flags & G_IO_FLAG_IS_READABLE)
201 g_print ("%sREADABLE", bar), bar = "|";
202 if (flags & G_IO_FLAG_IS_WRITEABLE)
203 g_print ("%sWRITEABLE", bar), bar = "|";
204 if (flags & G_IO_FLAG_IS_SEEKABLE)
205 g_print ("%sSEEKABLE", bar), bar = "|";
209 event_mask_to_string (int mask)
212 int checked_bits = 0;
218 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
228 BIT (ROUTING_INTERFACE_CHANGE);
229 BIT (ADDRESS_LIST_CHANGE);
233 if ((mask & ~checked_bits) != 0)
234 bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
236 return g_quark_to_string (g_quark_from_string (buf));
240 condition_to_string (GIOCondition condition)
243 int checked_bits = 0;
249 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
260 if ((condition & ~checked_bits) != 0)
261 bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
263 return g_quark_to_string (g_quark_from_string (buf));
267 g_io_win32_get_debug_flag (void)
269 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
273 g_io_channel_win32_init (GIOWin32Channel *channel)
275 channel->debug = g_io_win32_get_debug_flag ();
277 InitializeCriticalSection (&channel->mutex);
278 channel->running = FALSE;
279 channel->needs_close = FALSE;
280 channel->thread_id = 0;
281 channel->data_avail_event = NULL;
282 channel->revents = 0;
283 channel->buffer = NULL;
284 channel->space_avail_event = NULL;
286 channel->event_mask = 0;
287 channel->last_events = 0;
288 channel->event = NULL;
289 channel->write_would_have_blocked = FALSE;
290 channel->ever_writable = FALSE;
294 create_events (GIOWin32Channel *channel)
296 SECURITY_ATTRIBUTES sec_attrs;
298 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
299 sec_attrs.lpSecurityDescriptor = NULL;
300 sec_attrs.bInheritHandle = FALSE;
302 /* The data available event is manual reset, the space available event
303 * is automatic reset.
305 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
306 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
308 gchar *emsg = g_win32_error_message (GetLastError ());
310 g_error ("Error creating event: %s", emsg);
315 static unsigned __stdcall
316 read_thread (void *parameter)
318 GIOWin32Channel *channel = parameter;
322 g_io_channel_ref ((GIOChannel *)channel);
325 g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
328 channel->data_avail_event,
329 channel->space_avail_event);
331 channel->direction = 0;
332 channel->buffer = g_malloc (BUFFER_SIZE);
333 channel->rdp = channel->wrp = 0;
334 channel->running = TRUE;
336 SetEvent (channel->space_avail_event);
338 EnterCriticalSection (&channel->mutex);
339 while (channel->running)
342 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
343 channel->thread_id, channel->rdp, channel->wrp);
344 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
348 g_print ("read_thread %#x: resetting space_avail\n",
350 ResetEvent (channel->space_avail_event);
352 g_print ("read_thread %#x: waiting for space\n",
354 LeaveCriticalSection (&channel->mutex);
355 WaitForSingleObject (channel->space_avail_event, INFINITE);
356 EnterCriticalSection (&channel->mutex);
358 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
359 channel->thread_id, channel->rdp, channel->wrp);
362 buffer = channel->buffer + channel->wrp;
364 /* Always leave at least one byte unused gap to be able to
365 * distinguish between the full and empty condition...
367 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
368 BUFFER_SIZE - channel->wrp);
371 g_print ("read_thread %#x: calling read() for %d bytes\n",
372 channel->thread_id, nbytes);
374 LeaveCriticalSection (&channel->mutex);
376 nbytes = read (channel->fd, buffer, nbytes);
378 EnterCriticalSection (&channel->mutex);
380 channel->revents = G_IO_IN;
382 channel->revents |= G_IO_HUP;
384 channel->revents |= G_IO_ERR;
387 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
388 channel->thread_id, nbytes, channel->rdp, channel->wrp);
393 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
395 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
396 channel->thread_id, channel->rdp, channel->wrp);
397 SetEvent (channel->data_avail_event);
400 channel->running = FALSE;
401 if (channel->needs_close)
404 g_print ("read_thread %#x: channel fd %d needs closing\n",
405 channel->thread_id, channel->fd);
411 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
412 channel->thread_id, channel->rdp, channel->wrp);
413 SetEvent (channel->data_avail_event);
414 LeaveCriticalSection (&channel->mutex);
416 g_io_channel_unref ((GIOChannel *)channel);
418 /* No need to call _endthreadex(), the actual thread starter routine
419 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
420 * _endthreadex() for us.
426 static unsigned __stdcall
427 write_thread (void *parameter)
429 GIOWin32Channel *channel = parameter;
433 g_io_channel_ref ((GIOChannel *)channel);
436 g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
439 channel->data_avail_event,
440 channel->space_avail_event);
442 channel->direction = 1;
443 channel->buffer = g_malloc (BUFFER_SIZE);
444 channel->rdp = channel->wrp = 0;
445 channel->running = TRUE;
447 SetEvent (channel->space_avail_event);
449 /* We use the same event objects as for a reader thread, but with
450 * reversed meaning. So, space_avail is used if data is available
451 * for writing, and data_avail is used if space is available in the
455 EnterCriticalSection (&channel->mutex);
456 while (channel->running || channel->rdp != channel->wrp)
459 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
460 channel->thread_id, channel->rdp, channel->wrp);
461 if (channel->wrp == channel->rdp)
463 /* Buffer is empty. */
465 g_print ("write_thread %#x: resetting space_avail\n",
467 ResetEvent (channel->space_avail_event);
469 g_print ("write_thread %#x: waiting for data\n",
471 channel->revents = G_IO_OUT;
472 SetEvent (channel->data_avail_event);
473 LeaveCriticalSection (&channel->mutex);
474 WaitForSingleObject (channel->space_avail_event, INFINITE);
476 EnterCriticalSection (&channel->mutex);
477 if (channel->rdp == channel->wrp)
481 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
482 channel->thread_id, channel->rdp, channel->wrp);
485 buffer = channel->buffer + channel->rdp;
486 if (channel->rdp < channel->wrp)
487 nbytes = channel->wrp - channel->rdp;
489 nbytes = BUFFER_SIZE - channel->rdp;
492 g_print ("write_thread %#x: calling write() for %d bytes\n",
493 channel->thread_id, nbytes);
495 LeaveCriticalSection (&channel->mutex);
496 nbytes = write (channel->fd, buffer, nbytes);
497 EnterCriticalSection (&channel->mutex);
500 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
501 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
503 channel->revents = 0;
505 channel->revents |= G_IO_OUT;
506 else if (nbytes <= 0)
507 channel->revents |= G_IO_ERR;
509 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
515 g_print ("write_thread: setting data_avail for thread %#x\n",
517 SetEvent (channel->data_avail_event);
520 channel->running = FALSE;
521 if (channel->needs_close)
524 g_print ("write_thread %#x: channel fd %d needs closing\n",
525 channel->thread_id, channel->fd);
530 LeaveCriticalSection (&channel->mutex);
532 g_io_channel_unref ((GIOChannel *)channel);
538 create_thread (GIOWin32Channel *channel,
539 GIOCondition condition,
540 unsigned (__stdcall *thread) (void *parameter))
542 HANDLE thread_handle;
544 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
545 &channel->thread_id);
546 if (thread_handle == 0)
547 g_warning ("Error creating thread: %s.",
549 else if (!CloseHandle (thread_handle))
551 gchar *emsg = g_win32_error_message (GetLastError ());
553 g_warning ("Error closing thread handle: %s.", emsg);
557 WaitForSingleObject (channel->space_avail_event, INFINITE);
561 buffer_read (GIOWin32Channel *channel,
570 EnterCriticalSection (&channel->mutex);
572 g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
573 channel->thread_id, count, channel->rdp, channel->wrp);
575 if (channel->wrp == channel->rdp)
577 LeaveCriticalSection (&channel->mutex);
579 g_print ("waiting for data from thread %#x\n", channel->thread_id);
580 WaitForSingleObject (channel->data_avail_event, INFINITE);
582 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
583 EnterCriticalSection (&channel->mutex);
584 if (channel->wrp == channel->rdp && !channel->running)
587 g_print ("wrp==rdp, !running\n");
588 LeaveCriticalSection (&channel->mutex);
590 return G_IO_STATUS_EOF;
594 if (channel->rdp < channel->wrp)
595 nbytes = channel->wrp - channel->rdp;
597 nbytes = BUFFER_SIZE - channel->rdp;
598 LeaveCriticalSection (&channel->mutex);
599 nbytes = MIN (left, nbytes);
601 g_print ("moving %d bytes from thread %#x\n",
602 nbytes, channel->thread_id);
603 memcpy (dest, channel->buffer + channel->rdp, nbytes);
606 EnterCriticalSection (&channel->mutex);
607 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
609 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
610 SetEvent (channel->space_avail_event);
612 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
613 channel->thread_id, channel->rdp, channel->wrp);
614 if (channel->running && channel->wrp == channel->rdp)
617 g_print ("resetting data_avail of thread %#x\n",
619 ResetEvent (channel->data_avail_event);
621 LeaveCriticalSection (&channel->mutex);
623 /* We have no way to indicate any errors form the actual
624 * read() or recv() call in the reader thread. Should we have?
626 *bytes_read = count - left;
627 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
632 buffer_write (GIOWin32Channel *channel,
635 gsize *bytes_written,
641 EnterCriticalSection (&channel->mutex);
643 g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
644 channel->thread_id, count, channel->rdp, channel->wrp);
646 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
650 g_print ("buffer_write: tid %#x: resetting data_avail\n",
652 ResetEvent (channel->data_avail_event);
654 g_print ("buffer_write: tid %#x: waiting for space\n",
656 LeaveCriticalSection (&channel->mutex);
657 WaitForSingleObject (channel->data_avail_event, INFINITE);
658 EnterCriticalSection (&channel->mutex);
660 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
661 channel->thread_id, channel->rdp, channel->wrp);
664 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
665 BUFFER_SIZE - channel->wrp);
667 LeaveCriticalSection (&channel->mutex);
668 nbytes = MIN (left, nbytes);
670 g_print ("buffer_write: tid %#x: writing %d bytes\n",
671 channel->thread_id, nbytes);
672 memcpy (channel->buffer + channel->wrp, dest, nbytes);
675 EnterCriticalSection (&channel->mutex);
677 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
679 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
680 channel->thread_id, channel->rdp, channel->wrp);
681 SetEvent (channel->space_avail_event);
683 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
687 g_print ("buffer_write: tid %#x: resetting data_avail\n",
689 ResetEvent (channel->data_avail_event);
692 LeaveCriticalSection (&channel->mutex);
694 /* We have no way to indicate any errors form the actual
695 * write() call in the writer thread. Should we have?
697 *bytes_written = count - left;
698 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
703 g_io_win32_prepare (GSource *source,
706 GIOWin32Watch *watch = (GIOWin32Watch *)source;
707 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
708 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
714 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
716 switch (channel->type)
718 case G_IO_WIN32_WINDOWS_MESSAGES:
723 case G_IO_WIN32_CONSOLE:
728 case G_IO_WIN32_FILE_DESC:
730 g_print (" FD thread=%#x buffer_condition:{%s}"
731 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
732 channel->thread_id, condition_to_string (buffer_condition),
733 condition_to_string (watch->pollfd.events),
734 condition_to_string (watch->pollfd.revents),
735 condition_to_string (channel->revents));
737 EnterCriticalSection (&channel->mutex);
738 if (channel->running)
740 if (channel->direction == 0 && channel->wrp == channel->rdp)
743 g_print ("\n setting revents=0");
744 channel->revents = 0;
749 if (channel->direction == 1
750 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
753 g_print ("\n setting revents=0");
754 channel->revents = 0;
757 LeaveCriticalSection (&channel->mutex);
760 case G_IO_WIN32_SOCKET:
764 if (watch->condition & G_IO_IN)
765 event_mask |= (FD_READ | FD_ACCEPT);
766 if (watch->condition & G_IO_OUT)
767 event_mask |= (FD_WRITE | FD_CONNECT);
768 event_mask |= FD_CLOSE;
770 if (channel->event_mask != event_mask)
773 g_print ("\n WSAEventSelect(%d,%p,{%s})",
774 channel->fd, (HANDLE) watch->pollfd.fd,
775 event_mask_to_string (event_mask));
776 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
777 event_mask) == SOCKET_ERROR)
780 gchar *emsg = g_win32_error_message (WSAGetLastError ());
782 g_print (" failed: %s", emsg);
785 channel->event_mask = event_mask;
788 g_print ("\n setting last_events=0");
789 channel->last_events = 0;
791 if ((event_mask & FD_WRITE) &&
792 channel->ever_writable &&
793 !channel->write_would_have_blocked)
796 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
797 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
803 g_assert_not_reached ();
809 return ((watch->condition & buffer_condition) == watch->condition);
813 g_io_win32_check (GSource *source)
816 GIOWin32Watch *watch = (GIOWin32Watch *)source;
817 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
818 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
819 WSANETWORKEVENTS events;
822 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
824 switch (channel->type)
826 case G_IO_WIN32_WINDOWS_MESSAGES:
829 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
831 case G_IO_WIN32_FILE_DESC:
833 g_print (" FD thread=%#x buffer_condition=%s\n"
834 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
835 channel->thread_id, condition_to_string (buffer_condition),
836 condition_to_string (watch->pollfd.events),
837 condition_to_string (watch->pollfd.revents),
838 condition_to_string (channel->revents));
840 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
842 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
844 case G_IO_WIN32_CONSOLE:
847 if (watch->channel->is_writeable)
849 else if (watch->channel->is_readable)
853 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
856 /* _kbhit() does quite complex processing to find out
857 * whether at least one of the key events pending corresponds
858 * to a "real" character that can be read.
863 /* Discard all other kinds of events */
864 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
869 case G_IO_WIN32_SOCKET:
872 if (channel->last_events & FD_WRITE)
875 g_print (" sock=%d event=%p last_events has FD_WRITE",
876 channel->fd, (HANDLE) watch->pollfd.fd);
880 WSAEnumNetworkEvents (channel->fd, 0, &events);
883 g_print ("\n revents={%s} condition={%s}"
884 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
885 condition_to_string (watch->pollfd.revents),
886 condition_to_string (watch->condition),
888 event_mask_to_string (events.lNetworkEvents));
890 if (watch->pollfd.revents != 0 &&
891 events.lNetworkEvents == 0 &&
892 !(channel->event_mask & FD_WRITE))
894 channel->event_mask = 0;
896 g_print ("\n WSAEventSelect(%d,%p,{})",
897 channel->fd, (HANDLE) watch->pollfd.fd);
898 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
900 g_print (" ResetEvent(%p)",
901 (HANDLE) watch->pollfd.fd);
902 ResetEvent ((HANDLE) watch->pollfd.fd);
904 else if (events.lNetworkEvents & FD_WRITE)
905 channel->ever_writable = TRUE;
906 channel->last_events = events.lNetworkEvents;
909 watch->pollfd.revents = 0;
910 if (channel->last_events & (FD_READ | FD_ACCEPT))
911 watch->pollfd.revents |= G_IO_IN;
913 if (channel->last_events & FD_WRITE)
914 watch->pollfd.revents |= G_IO_OUT;
917 /* We have called WSAEnumNetworkEvents() above but it didn't
920 if (events.lNetworkEvents & FD_CONNECT)
922 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
923 watch->pollfd.revents |= G_IO_OUT;
925 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
927 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
928 watch->pollfd.revents |= G_IO_HUP;
931 /* Regardless of WSAEnumNetworkEvents() result, if watching for
932 * writability, and if we have ever got a FD_WRITE event, and
933 * unless last write would have blocked, set G_IO_OUT. But never
934 * set both G_IO_OUT and G_IO_HUP.
936 if (!(watch->pollfd.revents & G_IO_HUP) &&
937 channel->ever_writable &&
938 !channel->write_would_have_blocked &&
939 (channel->event_mask & FD_WRITE))
940 watch->pollfd.revents |= G_IO_OUT;
943 g_print ("\n revents={%s} retval={%s}\n",
944 condition_to_string (watch->pollfd.revents),
945 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
947 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
950 g_assert_not_reached ();
956 g_io_win32_dispatch (GSource *source,
957 GSourceFunc callback,
960 GIOFunc func = (GIOFunc)callback;
961 GIOWin32Watch *watch = (GIOWin32Watch *)source;
962 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
963 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
967 g_warning ("IO Watch dispatched without callback\n"
968 "You must call g_source_connect().");
973 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
974 condition_to_string (watch->pollfd.revents),
975 condition_to_string (watch->condition),
976 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
978 return (*func) (watch->channel,
979 (watch->pollfd.revents | buffer_condition) & watch->condition,
984 g_io_win32_finalize (GSource *source)
986 GIOWin32Watch *watch = (GIOWin32Watch *)source;
987 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
990 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
992 switch (channel->type)
994 case G_IO_WIN32_WINDOWS_MESSAGES:
999 case G_IO_WIN32_CONSOLE:
1004 case G_IO_WIN32_FILE_DESC:
1006 g_print (" FD thread=%#x", channel->thread_id);
1009 case G_IO_WIN32_SOCKET:
1011 g_print (" SOCK sock=%d", channel->fd);
1015 g_assert_not_reached ();
1020 g_io_channel_unref (watch->channel);
1023 GSourceFuncs g_io_watch_funcs = {
1026 g_io_win32_dispatch,
1031 g_io_win32_msg_read (GIOChannel *channel,
1037 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1038 MSG msg; /* In case of alignment problems */
1040 if (count < sizeof (MSG))
1042 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1043 "Incorrect message size"); /* Informative enough error message? */
1044 return G_IO_STATUS_ERROR;
1047 if (win32_channel->debug)
1048 g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1049 channel, win32_channel->hwnd);
1050 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1051 return G_IO_STATUS_AGAIN;
1053 memmove (buf, &msg, sizeof (MSG));
1054 *bytes_read = sizeof (MSG);
1056 return G_IO_STATUS_NORMAL;
1060 g_io_win32_msg_write (GIOChannel *channel,
1063 gsize *bytes_written,
1066 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1069 if (count != sizeof (MSG))
1071 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1072 "Incorrect message size"); /* Informative enough error message? */
1073 return G_IO_STATUS_ERROR;
1076 /* In case of alignment problems */
1077 memmove (&msg, buf, sizeof (MSG));
1078 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1080 gchar *emsg = g_win32_error_message (GetLastError ());
1082 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1085 return G_IO_STATUS_ERROR;
1088 *bytes_written = sizeof (MSG);
1090 return G_IO_STATUS_NORMAL;
1094 g_io_win32_msg_close (GIOChannel *channel,
1097 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1099 return G_IO_STATUS_NORMAL;
1103 g_io_win32_free (GIOChannel *channel)
1105 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1107 if (win32_channel->debug)
1108 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1110 DeleteCriticalSection (&win32_channel->mutex);
1112 if (win32_channel->data_avail_event)
1113 if (!CloseHandle (win32_channel->data_avail_event))
1114 if (win32_channel->debug)
1116 gchar *emsg = g_win32_error_message (GetLastError ());
1118 g_print (" CloseHandle(%p) failed: %s\n",
1119 win32_channel->data_avail_event, emsg);
1123 g_free (win32_channel->buffer);
1125 if (win32_channel->space_avail_event)
1126 if (!CloseHandle (win32_channel->space_avail_event))
1127 if (win32_channel->debug)
1129 gchar *emsg = g_win32_error_message (GetLastError ());
1131 g_print (" CloseHandle(%p) failed: %s\n",
1132 win32_channel->space_avail_event, emsg);
1136 if (win32_channel->type == G_IO_WIN32_SOCKET &&
1137 win32_channel->fd != -1)
1138 if (WSAEventSelect (win32_channel->fd, NULL, 0) == SOCKET_ERROR)
1139 if (win32_channel->debug)
1141 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1143 g_print (" WSAEventSelect(%d,NULL,{}) failed: %s\n",
1144 win32_channel->fd, emsg);
1148 if (win32_channel->event)
1149 if (!WSACloseEvent (win32_channel->event))
1150 if (win32_channel->debug)
1152 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1154 g_print (" WSACloseEvent(%p) failed: %s\n",
1155 win32_channel->event, emsg);
1159 g_free (win32_channel);
1163 g_io_win32_msg_create_watch (GIOChannel *channel,
1164 GIOCondition condition)
1166 GIOWin32Watch *watch;
1169 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1170 watch = (GIOWin32Watch *)source;
1172 watch->channel = channel;
1173 g_io_channel_ref (channel);
1175 watch->condition = condition;
1177 watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1178 watch->pollfd.events = condition;
1180 g_source_add_poll (source, &watch->pollfd);
1186 g_io_win32_fd_and_console_read (GIOChannel *channel,
1192 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1195 if (win32_channel->debug)
1196 g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1197 win32_channel->fd, count);
1199 if (win32_channel->thread_id)
1201 return buffer_read (win32_channel, buf, count, bytes_read, err);
1204 result = read (win32_channel->fd, buf, count);
1206 if (win32_channel->debug)
1207 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1217 return G_IO_STATUS_AGAIN;
1220 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1221 g_io_channel_error_from_errno (errno),
1222 g_strerror (errno));
1223 return G_IO_STATUS_ERROR;
1227 *bytes_read = result;
1229 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1233 g_io_win32_fd_and_console_write (GIOChannel *channel,
1236 gsize *bytes_written,
1239 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1242 if (win32_channel->thread_id)
1244 return buffer_write (win32_channel, buf, count, bytes_written, err);
1247 result = write (win32_channel->fd, buf, count);
1248 if (win32_channel->debug)
1249 g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1250 win32_channel->fd, count, result);
1260 return G_IO_STATUS_AGAIN;
1263 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1264 g_io_channel_error_from_errno (errno),
1265 g_strerror (errno));
1266 return G_IO_STATUS_ERROR;
1270 *bytes_written = result;
1272 return G_IO_STATUS_NORMAL;
1276 g_io_win32_fd_seek (GIOChannel *channel,
1281 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1298 whence = -1; /* Keep the compiler quiet */
1299 g_assert_not_reached ();
1303 tmp_offset = offset;
1304 if (tmp_offset != offset)
1306 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1307 g_io_channel_error_from_errno (EINVAL),
1308 g_strerror (EINVAL));
1309 return G_IO_STATUS_ERROR;
1312 result = lseek (win32_channel->fd, tmp_offset, whence);
1316 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1317 g_io_channel_error_from_errno (errno),
1318 g_strerror (errno));
1319 return G_IO_STATUS_ERROR;
1322 return G_IO_STATUS_NORMAL;
1326 g_io_win32_fd_close (GIOChannel *channel,
1329 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1331 if (win32_channel->debug)
1332 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1333 win32_channel->thread_id,
1335 EnterCriticalSection (&win32_channel->mutex);
1336 if (win32_channel->running)
1338 if (win32_channel->debug)
1339 g_print ("thread %#x: running, marking fd %d for later close\n",
1340 win32_channel->thread_id, win32_channel->fd);
1341 win32_channel->running = FALSE;
1342 win32_channel->needs_close = TRUE;
1343 if (win32_channel->direction == 0)
1344 SetEvent (win32_channel->data_avail_event);
1346 SetEvent (win32_channel->space_avail_event);
1350 if (win32_channel->debug)
1351 g_print ("closing fd %d\n", win32_channel->fd);
1352 close (win32_channel->fd);
1353 if (win32_channel->debug)
1354 g_print ("closed fd %d, setting to -1\n",
1356 win32_channel->fd = -1;
1358 LeaveCriticalSection (&win32_channel->mutex);
1360 /* FIXME error detection? */
1362 return G_IO_STATUS_NORMAL;
1366 g_io_win32_fd_create_watch (GIOChannel *channel,
1367 GIOCondition condition)
1369 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1370 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1371 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1373 watch->channel = channel;
1374 g_io_channel_ref (channel);
1376 watch->condition = condition;
1378 if (win32_channel->data_avail_event == NULL)
1379 create_events (win32_channel);
1381 watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1382 watch->pollfd.events = condition;
1384 if (win32_channel->debug)
1385 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1386 channel, win32_channel->fd,
1387 condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1389 EnterCriticalSection (&win32_channel->mutex);
1390 if (win32_channel->thread_id == 0)
1392 if (condition & G_IO_IN)
1393 create_thread (win32_channel, condition, read_thread);
1394 else if (condition & G_IO_OUT)
1395 create_thread (win32_channel, condition, write_thread);
1398 g_source_add_poll (source, &watch->pollfd);
1399 LeaveCriticalSection (&win32_channel->mutex);
1405 g_io_win32_console_close (GIOChannel *channel,
1408 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1410 if (close (win32_channel->fd) < 0)
1412 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1413 g_io_channel_error_from_errno (errno),
1414 g_strerror (errno));
1415 return G_IO_STATUS_ERROR;
1418 return G_IO_STATUS_NORMAL;
1422 g_io_win32_console_create_watch (GIOChannel *channel,
1423 GIOCondition condition)
1425 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1426 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1427 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1429 watch->channel = channel;
1430 g_io_channel_ref (channel);
1432 watch->condition = condition;
1434 watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1435 watch->pollfd.events = condition;
1437 g_source_add_poll (source, &watch->pollfd);
1443 g_io_win32_sock_read (GIOChannel *channel,
1449 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1451 GIOChannelError error;
1454 if (win32_channel->debug)
1455 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1456 channel, win32_channel->fd, count);
1458 result = recv (win32_channel->fd, buf, count, 0);
1459 if (result == SOCKET_ERROR)
1460 winsock_error = WSAGetLastError ();
1462 if (win32_channel->debug)
1463 g_print (" recv=%d", result);
1465 if (result == SOCKET_ERROR)
1467 gchar *emsg = g_win32_error_message (winsock_error);
1469 if (win32_channel->debug)
1470 g_print (" %s\n", emsg);
1474 switch (winsock_error)
1477 error = G_IO_CHANNEL_ERROR_INVAL;
1479 case WSAEWOULDBLOCK:
1481 return G_IO_STATUS_AGAIN;
1483 error = G_IO_CHANNEL_ERROR_FAILED;
1486 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1489 return G_IO_STATUS_ERROR;
1493 if (win32_channel->debug)
1495 *bytes_read = result;
1497 return G_IO_STATUS_EOF;
1499 return G_IO_STATUS_NORMAL;
1504 g_io_win32_sock_write (GIOChannel *channel,
1507 gsize *bytes_written,
1510 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1512 GIOChannelError error;
1515 if (win32_channel->debug)
1516 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1517 channel, win32_channel->fd, count);
1519 result = send (win32_channel->fd, buf, count, 0);
1520 if (result == SOCKET_ERROR)
1521 winsock_error = WSAGetLastError ();
1523 if (win32_channel->debug)
1524 g_print (" send=%d", result);
1526 if (result == SOCKET_ERROR)
1528 gchar *emsg = g_win32_error_message (winsock_error);
1530 if (win32_channel->debug)
1531 g_print (" %s\n", emsg);
1535 switch (winsock_error)
1538 error = G_IO_CHANNEL_ERROR_INVAL;
1540 case WSAEWOULDBLOCK:
1541 win32_channel->write_would_have_blocked = TRUE;
1542 win32_channel->last_events = 0;
1544 return G_IO_STATUS_AGAIN;
1546 error = G_IO_CHANNEL_ERROR_FAILED;
1549 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1552 return G_IO_STATUS_ERROR;
1556 if (win32_channel->debug)
1558 *bytes_written = result;
1559 win32_channel->write_would_have_blocked = FALSE;
1561 return G_IO_STATUS_NORMAL;
1566 g_io_win32_sock_close (GIOChannel *channel,
1569 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1571 if (win32_channel->fd != -1)
1573 if (win32_channel->debug)
1574 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1575 channel, win32_channel->fd);
1577 closesocket (win32_channel->fd);
1578 win32_channel->fd = -1;
1581 /* FIXME error detection? */
1583 return G_IO_STATUS_NORMAL;
1587 g_io_win32_sock_create_watch (GIOChannel *channel,
1588 GIOCondition condition)
1590 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1591 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1592 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1594 watch->channel = channel;
1595 g_io_channel_ref (channel);
1597 watch->condition = condition;
1599 if (win32_channel->event == 0)
1600 win32_channel->event = WSACreateEvent ();
1602 watch->pollfd.fd = (gintptr) win32_channel->event;
1603 watch->pollfd.events = condition;
1605 if (win32_channel->debug)
1606 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1607 channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1608 condition_to_string (watch->condition));
1610 g_source_add_poll (source, &watch->pollfd);
1616 g_io_channel_new_file (const gchar *filename,
1620 int fid, flags, pmode;
1621 GIOChannel *channel;
1623 enum { /* Cheesy hack */
1630 g_return_val_if_fail (filename != NULL, NULL);
1631 g_return_val_if_fail (mode != NULL, NULL);
1632 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1646 g_warning ("Invalid GIOFileMode %s.", mode);
1655 if (mode[2] == '\0')
1657 mode_num |= MODE_PLUS;
1662 g_warning ("Invalid GIOFileMode %s.", mode);
1673 flags = O_WRONLY | O_TRUNC | O_CREAT;
1677 flags = O_WRONLY | O_APPEND | O_CREAT;
1680 case MODE_R | MODE_PLUS:
1682 pmode = _S_IREAD | _S_IWRITE;
1684 case MODE_W | MODE_PLUS:
1685 flags = O_RDWR | O_TRUNC | O_CREAT;
1686 pmode = _S_IREAD | _S_IWRITE;
1688 case MODE_A | MODE_PLUS:
1689 flags = O_RDWR | O_APPEND | O_CREAT;
1690 pmode = _S_IREAD | _S_IWRITE;
1693 g_assert_not_reached ();
1697 /* always open 'untranslated' */
1698 fid = g_open (filename, flags | _O_BINARY, pmode);
1700 if (g_io_win32_get_debug_flag ())
1702 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1703 g_win32_print_access_mode (flags|_O_BINARY);
1704 g_print (",%#o)=%d\n", pmode, fid);
1709 g_set_error_literal (error, G_FILE_ERROR,
1710 g_file_error_from_errno (errno),
1711 g_strerror (errno));
1712 return (GIOChannel *)NULL;
1715 channel = g_io_channel_win32_new_fd (fid);
1717 /* XXX: move this to g_io_channel_win32_new_fd () */
1718 channel->close_on_unref = TRUE;
1719 channel->is_seekable = TRUE;
1721 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1722 * correspond to actual readability/writeability. Set to FALSE those
1723 * that mode doesn't allow
1728 channel->is_writeable = FALSE;
1732 channel->is_readable = FALSE;
1734 case MODE_R | MODE_PLUS:
1735 case MODE_W | MODE_PLUS:
1736 case MODE_A | MODE_PLUS:
1739 g_assert_not_reached ();
1746 #if !defined (_WIN64)
1748 #undef g_io_channel_new_file
1750 /* Binary compatibility version. Not for newly compiled code. */
1753 g_io_channel_new_file (const gchar *filename,
1757 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1760 if (utf8_filename == NULL)
1763 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1765 g_free (utf8_filename);
1773 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1777 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1779 if (win32_channel->debug)
1781 g_print ("g_io_win32_unimpl_set_flags: ");
1782 g_win32_print_gioflags (flags);
1786 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1787 G_IO_CHANNEL_ERROR_FAILED,
1788 "Not implemented on Win32");
1790 return G_IO_STATUS_ERROR;
1794 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1795 struct _stati64 *st)
1797 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1801 if (st->st_mode & _S_IFIFO)
1803 channel->is_readable =
1804 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1805 channel->is_writeable =
1806 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1807 channel->is_seekable = FALSE;
1811 channel->is_readable =
1812 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1813 channel->is_writeable =
1814 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1815 channel->is_seekable = TRUE;
1818 /* XXX: G_IO_FLAG_APPEND */
1819 /* XXX: G_IO_FLAG_NONBLOCK */
1825 g_io_win32_fd_get_flags (GIOChannel *channel)
1828 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1830 g_return_val_if_fail (win32_channel != NULL, 0);
1831 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1833 if (0 == _fstati64 (win32_channel->fd, &st))
1834 return g_io_win32_fd_get_flags_internal (channel, &st);
1840 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1842 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1843 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1846 INPUT_RECORD record;
1848 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1849 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1850 channel->is_seekable = FALSE;
1856 g_io_win32_console_get_flags (GIOChannel *channel)
1858 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1860 g_return_val_if_fail (win32_channel != NULL, 0);
1861 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1863 return g_io_win32_console_get_flags_internal (channel);
1867 g_io_win32_msg_get_flags (GIOChannel *channel)
1873 g_io_win32_sock_set_flags (GIOChannel *channel,
1877 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1880 if (win32_channel->debug)
1882 g_print ("g_io_win32_sock_set_flags: ");
1883 g_win32_print_gioflags (flags);
1887 if (flags & G_IO_FLAG_NONBLOCK)
1890 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1892 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1894 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1895 G_IO_CHANNEL_ERROR_FAILED,
1899 return G_IO_STATUS_ERROR;
1905 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1907 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1909 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1910 G_IO_CHANNEL_ERROR_FAILED,
1914 return G_IO_STATUS_ERROR;
1918 return G_IO_STATUS_NORMAL;
1922 g_io_win32_sock_get_flags (GIOChannel *channel)
1924 /* Could we do something here? */
1928 static GIOFuncs win32_channel_msg_funcs = {
1929 g_io_win32_msg_read,
1930 g_io_win32_msg_write,
1932 g_io_win32_msg_close,
1933 g_io_win32_msg_create_watch,
1935 g_io_win32_unimpl_set_flags,
1936 g_io_win32_msg_get_flags,
1939 static GIOFuncs win32_channel_fd_funcs = {
1940 g_io_win32_fd_and_console_read,
1941 g_io_win32_fd_and_console_write,
1943 g_io_win32_fd_close,
1944 g_io_win32_fd_create_watch,
1946 g_io_win32_unimpl_set_flags,
1947 g_io_win32_fd_get_flags,
1950 static GIOFuncs win32_channel_console_funcs = {
1951 g_io_win32_fd_and_console_read,
1952 g_io_win32_fd_and_console_write,
1954 g_io_win32_console_close,
1955 g_io_win32_console_create_watch,
1957 g_io_win32_unimpl_set_flags,
1958 g_io_win32_console_get_flags,
1961 static GIOFuncs win32_channel_sock_funcs = {
1962 g_io_win32_sock_read,
1963 g_io_win32_sock_write,
1965 g_io_win32_sock_close,
1966 g_io_win32_sock_create_watch,
1968 g_io_win32_sock_set_flags,
1969 g_io_win32_sock_get_flags,
1973 * g_io_channel_win32_new_messages:
1974 * @hwnd: a window handle.
1975 * @Returns: a new #GIOChannel.
1977 * Creates a new #GIOChannel given a window handle on Windows.
1979 * This function creates a #GIOChannel that can be used to poll for
1980 * Windows messages for the window in question.
1983 #if GLIB_SIZEOF_VOID_P == 8
1984 g_io_channel_win32_new_messages (gsize hwnd)
1986 g_io_channel_win32_new_messages (guint hwnd)
1989 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1990 GIOChannel *channel = (GIOChannel *)win32_channel;
1992 g_io_channel_init (channel);
1993 g_io_channel_win32_init (win32_channel);
1994 if (win32_channel->debug)
1995 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1996 channel, (HWND) hwnd);
1997 channel->funcs = &win32_channel_msg_funcs;
1998 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1999 win32_channel->hwnd = (HWND) hwnd;
2001 /* XXX: check this. */
2002 channel->is_readable = IsWindow (win32_channel->hwnd);
2003 channel->is_writeable = IsWindow (win32_channel->hwnd);
2005 channel->is_seekable = FALSE;
2011 g_io_channel_win32_new_fd_internal (gint fd,
2012 struct _stati64 *st)
2014 GIOWin32Channel *win32_channel;
2015 GIOChannel *channel;
2017 win32_channel = g_new (GIOWin32Channel, 1);
2018 channel = (GIOChannel *)win32_channel;
2020 g_io_channel_init (channel);
2021 g_io_channel_win32_init (win32_channel);
2023 win32_channel->fd = fd;
2025 if (win32_channel->debug)
2026 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
2029 if (st->st_mode & _S_IFCHR) /* console */
2031 channel->funcs = &win32_channel_console_funcs;
2032 win32_channel->type = G_IO_WIN32_CONSOLE;
2033 g_io_win32_console_get_flags_internal (channel);
2037 channel->funcs = &win32_channel_fd_funcs;
2038 win32_channel->type = G_IO_WIN32_FILE_DESC;
2039 g_io_win32_fd_get_flags_internal (channel, st);
2046 * g_io_channel_win32_new_fd:
2047 * @fd: a C library file descriptor.
2048 * @Returns: a new #GIOChannel.
2050 * Creates a new #GIOChannel given a file descriptor on Windows. This
2051 * works for file descriptors from the C runtime.
2053 * This function works for file descriptors as returned by the open(),
2054 * creat(), pipe() and fileno() calls in the Microsoft C runtime. In
2055 * order to meaningfully use this function your code should use the
2056 * same C runtime as GLib uses, which is msvcrt.dll. Note that in
2057 * current Microsoft compilers it is near impossible to convince it to
2058 * build code that would use msvcrt.dll. The last Microsoft compiler
2059 * version that supported using msvcrt.dll as the C runtime was version
2060 * 6. The GNU compiler and toolchain for Windows, also known as Mingw,
2061 * fully supports msvcrt.dll.
2063 * If you have created a #GIOChannel for a file descriptor and started
2064 * watching (polling) it, you shouldn't call read() on the file
2065 * descriptor. This is because adding polling for a file descriptor is
2066 * implemented in GLib on Windows by starting a thread that sits
2067 * blocked in a read() from the file descriptor most of the time. All
2068 * reads from the file descriptor should be done by this internal GLib
2069 * thread. Your code should call only g_io_channel_read().
2071 * This function is available only in GLib on Windows.
2074 g_io_channel_win32_new_fd (gint fd)
2078 if (_fstati64 (fd, &st) == -1)
2080 g_warning ("g_io_channel_win32_new_fd: %d isn't an open file descriptor in the C library GLib uses.", fd);
2084 return g_io_channel_win32_new_fd_internal (fd, &st);
2088 g_io_channel_win32_get_fd (GIOChannel *channel)
2090 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2092 return win32_channel->fd;
2096 * g_io_channel_win32_new_socket:
2097 * @socket: a Winsock socket
2098 * @Returns: a new #GIOChannel
2100 * Creates a new #GIOChannel given a socket on Windows.
2102 * This function works for sockets created by Winsock. It's available
2103 * only in GLib on Windows.
2105 * Polling a #GSource created to watch a channel for a socket puts the
2106 * socket in non-blocking mode. This is a side-effect of the
2107 * implementation and unavoidable.
2110 g_io_channel_win32_new_socket (int socket)
2112 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2113 GIOChannel *channel = (GIOChannel *)win32_channel;
2115 g_io_channel_init (channel);
2116 g_io_channel_win32_init (win32_channel);
2117 if (win32_channel->debug)
2118 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2120 channel->funcs = &win32_channel_sock_funcs;
2121 win32_channel->type = G_IO_WIN32_SOCKET;
2122 win32_channel->fd = socket;
2124 channel->is_readable = TRUE;
2125 channel->is_writeable = TRUE;
2126 channel->is_seekable = FALSE;
2132 g_io_channel_unix_new (gint fd)
2134 gboolean is_fd, is_socket;
2138 is_fd = (_fstati64 (fd, &st) == 0);
2140 optlen = sizeof (optval);
2141 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2143 if (is_fd && is_socket)
2144 g_warning ("g_io_channel_unix_new: %d is both a file descriptor and a socket. File descriptor interpretation assumed. To avoid ambiguity, call either g_io_channel_win32_new_fd() or g_io_channel_win32_new_socket() instead.", fd);
2147 return g_io_channel_win32_new_fd_internal (fd, &st);
2150 return g_io_channel_win32_new_socket(fd);
2152 g_warning ("g_io_channel_unix_new: %d is neither a file descriptor or a socket.", fd);
2158 g_io_channel_unix_get_fd (GIOChannel *channel)
2160 return g_io_channel_win32_get_fd (channel);
2164 g_io_channel_win32_set_debug (GIOChannel *channel,
2167 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2169 win32_channel->debug = flag;
2173 g_io_channel_win32_poll (GPollFD *fds,
2177 g_return_val_if_fail (n_fds >= 0, 0);
2179 return g_poll (fds, n_fds, timeout);
2183 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2184 GIOCondition condition,
2187 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2189 switch (win32_channel->type)
2191 case G_IO_WIN32_FILE_DESC:
2192 if (win32_channel->data_avail_event == NULL)
2193 create_events (win32_channel);
2195 fd->fd = (gintptr) win32_channel->data_avail_event;
2197 if (win32_channel->thread_id == 0)
2199 /* Is it meaningful for a file descriptor to be polled for
2200 * both IN and OUT? For what kind of file descriptor would
2201 * that be? Doesn't seem to make sense, in practise the file
2202 * descriptors handled here are always read or write ends of
2203 * pipes surely, and thus unidirectional.
2205 if (condition & G_IO_IN)
2206 create_thread (win32_channel, condition, read_thread);
2207 else if (condition & G_IO_OUT)
2208 create_thread (win32_channel, condition, write_thread);
2212 case G_IO_WIN32_CONSOLE:
2213 fd->fd = _get_osfhandle (win32_channel->fd);
2216 case G_IO_WIN32_SOCKET:
2217 fd->fd = (gintptr) WSACreateEvent ();
2220 case G_IO_WIN32_WINDOWS_MESSAGES:
2221 fd->fd = G_WIN32_MSG_HANDLE;
2225 g_assert_not_reached ();
2229 fd->events = condition;
2234 /* Binary compatibility */
2236 g_io_channel_win32_new_stream_socket (int socket)
2238 return g_io_channel_win32_new_socket (socket);
2243 #define __G_IO_WIN32_C__
2244 #include "galiasdef.c"