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.
85 #define fstat(a,b) _fstat(a,b)
93 typedef struct _GIOWin32Channel GIOWin32Channel;
94 typedef struct _GIOWin32Watch GIOWin32Watch;
96 #define BUFFER_SIZE 4096
99 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
101 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
102 * _open() or _pipe(), except for
103 * console IO. Separate thread to read
107 G_IO_WIN32_CONSOLE, /* Console IO (usually stdin, stdout, stderr) */
109 G_IO_WIN32_SOCKET /* Sockets. No separate thread. */
110 } GIOWin32ChannelType;
112 struct _GIOWin32Channel {
114 gint fd; /* Either a Unix-like file handle as provided
115 * by the Microsoft C runtime, or a SOCKET
116 * as provided by WinSock.
118 GIOWin32ChannelType type;
122 /* Field used by G_IO_WIN32_WINDOWS_MESSAGES channels */
123 HWND hwnd; /* Handle of window, or NULL */
125 /* Fields used by G_IO_WIN32_FILE_DESC channels. */
126 CRITICAL_SECTION mutex;
128 int direction; /* 0 means we read from it,
129 * 1 means we write to it.
132 gboolean running; /* Is reader or writer thread
133 * running. FALSE if EOF has been
134 * reached by the reader thread.
137 gboolean needs_close; /* If the channel has been closed while
138 * the reader thread was still running.
141 guint thread_id; /* If non-NULL the channel has or has
142 * had a reader or writer thread.
144 HANDLE data_avail_event;
148 /* Data is kept in a circular buffer. To be able to distinguish between
149 * empty and full buffers, we cannot fill it completely, but have to
150 * leave a one character gap.
152 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
155 * Full: (wrp + 1) % BUFFER_SIZE == rdp
158 guchar *buffer; /* (Circular) buffer */
159 gint wrp, rdp; /* Buffer indices for writing and reading */
160 HANDLE space_avail_event;
162 /* Fields used by G_IO_WIN32_SOCKET channels */
166 gboolean write_would_have_blocked;
167 gboolean ever_writable;
170 struct _GIOWin32Watch {
174 GIOCondition condition;
178 g_win32_print_access_mode (int flags)
180 g_print ("%s%s%s%s%s%s%s%s%s%s",
181 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
182 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
183 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
184 (flags & _O_APPEND ? "|O_APPEND" : ""),
185 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
186 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
187 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
188 (flags & _O_CREAT ? "|O_CREAT" : ""),
189 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
190 (flags & _O_EXCL ? "|O_EXCL" : ""),
191 (flags & _O_TEXT ? "|O_TEXT" : ""),
192 (flags & _O_BINARY ? "|O_BINARY" : ""));
196 g_win32_print_gioflags (GIOFlags flags)
200 if (flags & G_IO_FLAG_APPEND)
201 bar = "|", g_print ("APPEND");
202 if (flags & G_IO_FLAG_NONBLOCK)
203 g_print ("%sNONBLOCK", bar), bar = "|";
204 if (flags & G_IO_FLAG_IS_READABLE)
205 g_print ("%sREADABLE", bar), bar = "|";
206 if (flags & G_IO_FLAG_IS_WRITEABLE)
207 g_print ("%sWRITEABLE", bar), bar = "|";
208 if (flags & G_IO_FLAG_IS_SEEKABLE)
209 g_print ("%sSEEKABLE", bar), bar = "|";
213 event_mask_to_string (int mask)
216 int checked_bits = 0;
222 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
232 BIT (ROUTING_INTERFACE_CHANGE);
233 BIT (ADDRESS_LIST_CHANGE);
237 if ((mask & ~checked_bits) != 0)
238 bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
240 return g_quark_to_string (g_quark_from_string (buf));
244 condition_to_string (GIOCondition condition)
247 int checked_bits = 0;
253 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
264 if ((condition & ~checked_bits) != 0)
265 bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
267 return g_quark_to_string (g_quark_from_string (buf));
271 g_io_win32_get_debug_flag (void)
273 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
277 g_io_channel_win32_init (GIOWin32Channel *channel)
279 channel->debug = g_io_win32_get_debug_flag ();
281 InitializeCriticalSection (&channel->mutex);
282 channel->running = FALSE;
283 channel->needs_close = FALSE;
284 channel->thread_id = 0;
285 channel->data_avail_event = NULL;
286 channel->revents = 0;
287 channel->buffer = NULL;
288 channel->space_avail_event = NULL;
290 channel->event_mask = 0;
291 channel->last_events = 0;
292 channel->event = NULL;
293 channel->write_would_have_blocked = FALSE;
294 channel->ever_writable = FALSE;
298 create_events (GIOWin32Channel *channel)
300 SECURITY_ATTRIBUTES sec_attrs;
302 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
303 sec_attrs.lpSecurityDescriptor = NULL;
304 sec_attrs.bInheritHandle = FALSE;
306 /* The data available event is manual reset, the space available event
307 * is automatic reset.
309 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
310 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
312 gchar *emsg = g_win32_error_message (GetLastError ());
314 g_error ("Error creating event: %s", emsg);
319 static unsigned __stdcall
320 read_thread (void *parameter)
322 GIOWin32Channel *channel = parameter;
326 g_io_channel_ref ((GIOChannel *)channel);
329 g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
332 channel->data_avail_event,
333 channel->space_avail_event);
335 channel->direction = 0;
336 channel->buffer = g_malloc (BUFFER_SIZE);
337 channel->rdp = channel->wrp = 0;
338 channel->running = TRUE;
340 SetEvent (channel->space_avail_event);
342 EnterCriticalSection (&channel->mutex);
343 while (channel->running)
346 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
347 channel->thread_id, channel->rdp, channel->wrp);
348 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
352 g_print ("read_thread %#x: resetting space_avail\n",
354 ResetEvent (channel->space_avail_event);
356 g_print ("read_thread %#x: waiting for space\n",
358 LeaveCriticalSection (&channel->mutex);
359 WaitForSingleObject (channel->space_avail_event, INFINITE);
360 EnterCriticalSection (&channel->mutex);
362 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
363 channel->thread_id, channel->rdp, channel->wrp);
366 buffer = channel->buffer + channel->wrp;
368 /* Always leave at least one byte unused gap to be able to
369 * distinguish between the full and empty condition...
371 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
372 BUFFER_SIZE - channel->wrp);
375 g_print ("read_thread %#x: calling read() for %d bytes\n",
376 channel->thread_id, nbytes);
378 LeaveCriticalSection (&channel->mutex);
380 nbytes = read (channel->fd, buffer, nbytes);
382 EnterCriticalSection (&channel->mutex);
384 channel->revents = G_IO_IN;
386 channel->revents |= G_IO_HUP;
388 channel->revents |= G_IO_ERR;
391 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
392 channel->thread_id, nbytes, channel->rdp, channel->wrp);
397 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
399 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
400 channel->thread_id, channel->rdp, channel->wrp);
401 SetEvent (channel->data_avail_event);
404 channel->running = FALSE;
405 if (channel->needs_close)
408 g_print ("read_thread %#x: channel fd %d needs closing\n",
409 channel->thread_id, channel->fd);
415 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
416 channel->thread_id, channel->rdp, channel->wrp);
417 SetEvent (channel->data_avail_event);
418 LeaveCriticalSection (&channel->mutex);
420 g_io_channel_unref ((GIOChannel *)channel);
422 /* No need to call _endthreadex(), the actual thread starter routine
423 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
424 * _endthreadex() for us.
430 static unsigned __stdcall
431 write_thread (void *parameter)
433 GIOWin32Channel *channel = parameter;
437 g_io_channel_ref ((GIOChannel *)channel);
440 g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
443 channel->data_avail_event,
444 channel->space_avail_event);
446 channel->direction = 1;
447 channel->buffer = g_malloc (BUFFER_SIZE);
448 channel->rdp = channel->wrp = 0;
449 channel->running = TRUE;
451 SetEvent (channel->space_avail_event);
453 /* We use the same event objects as for a reader thread, but with
454 * reversed meaning. So, space_avail is used if data is available
455 * for writing, and data_avail is used if space is available in the
459 EnterCriticalSection (&channel->mutex);
460 while (channel->running || channel->rdp != channel->wrp)
463 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
464 channel->thread_id, channel->rdp, channel->wrp);
465 if (channel->wrp == channel->rdp)
467 /* Buffer is empty. */
469 g_print ("write_thread %#x: resetting space_avail\n",
471 ResetEvent (channel->space_avail_event);
473 g_print ("write_thread %#x: waiting for data\n",
475 channel->revents = G_IO_OUT;
476 SetEvent (channel->data_avail_event);
477 LeaveCriticalSection (&channel->mutex);
478 WaitForSingleObject (channel->space_avail_event, INFINITE);
480 EnterCriticalSection (&channel->mutex);
481 if (channel->rdp == channel->wrp)
485 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
486 channel->thread_id, channel->rdp, channel->wrp);
489 buffer = channel->buffer + channel->rdp;
490 if (channel->rdp < channel->wrp)
491 nbytes = channel->wrp - channel->rdp;
493 nbytes = BUFFER_SIZE - channel->rdp;
496 g_print ("write_thread %#x: calling write() for %d bytes\n",
497 channel->thread_id, nbytes);
499 LeaveCriticalSection (&channel->mutex);
500 nbytes = write (channel->fd, buffer, nbytes);
501 EnterCriticalSection (&channel->mutex);
504 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
505 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
507 channel->revents = 0;
509 channel->revents |= G_IO_OUT;
510 else if (nbytes <= 0)
511 channel->revents |= G_IO_ERR;
513 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
519 g_print ("write_thread: setting data_avail for thread %#x\n",
521 SetEvent (channel->data_avail_event);
524 channel->running = FALSE;
525 if (channel->needs_close)
528 g_print ("write_thread %#x: channel fd %d needs closing\n",
529 channel->thread_id, channel->fd);
534 LeaveCriticalSection (&channel->mutex);
536 g_io_channel_unref ((GIOChannel *)channel);
542 create_thread (GIOWin32Channel *channel,
543 GIOCondition condition,
544 unsigned (__stdcall *thread) (void *parameter))
546 HANDLE thread_handle;
548 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
549 &channel->thread_id);
550 if (thread_handle == 0)
551 g_warning ("Error creating thread: %s.",
553 else if (!CloseHandle (thread_handle))
555 gchar *emsg = g_win32_error_message (GetLastError ());
557 g_warning ("Error closing thread handle: %s.", emsg);
561 WaitForSingleObject (channel->space_avail_event, INFINITE);
565 buffer_read (GIOWin32Channel *channel,
574 EnterCriticalSection (&channel->mutex);
576 g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
577 channel->thread_id, count, channel->rdp, channel->wrp);
579 if (channel->wrp == channel->rdp)
581 LeaveCriticalSection (&channel->mutex);
583 g_print ("waiting for data from thread %#x\n", channel->thread_id);
584 WaitForSingleObject (channel->data_avail_event, INFINITE);
586 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
587 EnterCriticalSection (&channel->mutex);
588 if (channel->wrp == channel->rdp && !channel->running)
591 g_print ("wrp==rdp, !running\n");
592 LeaveCriticalSection (&channel->mutex);
594 return G_IO_STATUS_EOF;
598 if (channel->rdp < channel->wrp)
599 nbytes = channel->wrp - channel->rdp;
601 nbytes = BUFFER_SIZE - channel->rdp;
602 LeaveCriticalSection (&channel->mutex);
603 nbytes = MIN (left, nbytes);
605 g_print ("moving %d bytes from thread %#x\n",
606 nbytes, channel->thread_id);
607 memcpy (dest, channel->buffer + channel->rdp, nbytes);
610 EnterCriticalSection (&channel->mutex);
611 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
613 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
614 SetEvent (channel->space_avail_event);
616 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
617 channel->thread_id, channel->rdp, channel->wrp);
618 if (channel->running && channel->wrp == channel->rdp)
621 g_print ("resetting data_avail of thread %#x\n",
623 ResetEvent (channel->data_avail_event);
625 LeaveCriticalSection (&channel->mutex);
627 /* We have no way to indicate any errors form the actual
628 * read() or recv() call in the reader thread. Should we have?
630 *bytes_read = count - left;
631 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
636 buffer_write (GIOWin32Channel *channel,
639 gsize *bytes_written,
645 EnterCriticalSection (&channel->mutex);
647 g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
648 channel->thread_id, count, channel->rdp, channel->wrp);
650 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
654 g_print ("buffer_write: tid %#x: resetting data_avail\n",
656 ResetEvent (channel->data_avail_event);
658 g_print ("buffer_write: tid %#x: waiting for space\n",
660 LeaveCriticalSection (&channel->mutex);
661 WaitForSingleObject (channel->data_avail_event, INFINITE);
662 EnterCriticalSection (&channel->mutex);
664 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
665 channel->thread_id, channel->rdp, channel->wrp);
668 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
669 BUFFER_SIZE - channel->wrp);
671 LeaveCriticalSection (&channel->mutex);
672 nbytes = MIN (left, nbytes);
674 g_print ("buffer_write: tid %#x: writing %d bytes\n",
675 channel->thread_id, nbytes);
676 memcpy (channel->buffer + channel->wrp, dest, nbytes);
679 EnterCriticalSection (&channel->mutex);
681 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
683 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
684 channel->thread_id, channel->rdp, channel->wrp);
685 SetEvent (channel->space_avail_event);
687 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
691 g_print ("buffer_write: tid %#x: resetting data_avail\n",
693 ResetEvent (channel->data_avail_event);
696 LeaveCriticalSection (&channel->mutex);
698 /* We have no way to indicate any errors form the actual
699 * write() call in the writer thread. Should we have?
701 *bytes_written = count - left;
702 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
707 g_io_win32_prepare (GSource *source,
710 GIOWin32Watch *watch = (GIOWin32Watch *)source;
711 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
712 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
718 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
720 switch (channel->type)
722 case G_IO_WIN32_WINDOWS_MESSAGES:
727 case G_IO_WIN32_CONSOLE:
732 case G_IO_WIN32_FILE_DESC:
734 g_print (" FD thread=%#x buffer_condition:{%s}"
735 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
736 channel->thread_id, condition_to_string (buffer_condition),
737 condition_to_string (watch->pollfd.events),
738 condition_to_string (watch->pollfd.revents),
739 condition_to_string (channel->revents));
741 EnterCriticalSection (&channel->mutex);
742 if (channel->running)
744 if (channel->direction == 0 && channel->wrp == channel->rdp)
747 g_print ("\n setting revents=0");
748 channel->revents = 0;
753 if (channel->direction == 1
754 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
757 g_print ("\n setting revents=0");
758 channel->revents = 0;
761 LeaveCriticalSection (&channel->mutex);
764 case G_IO_WIN32_SOCKET:
768 if (watch->condition & G_IO_IN)
769 event_mask |= (FD_READ | FD_ACCEPT);
770 if (watch->condition & G_IO_OUT)
771 event_mask |= (FD_WRITE | FD_CONNECT);
772 event_mask |= FD_CLOSE;
774 if (channel->event_mask != event_mask)
777 g_print ("\n WSAEventSelect(%d,%p,{%s})",
778 channel->fd, (HANDLE) watch->pollfd.fd,
779 event_mask_to_string (event_mask));
780 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
781 event_mask) == SOCKET_ERROR)
784 gchar *emsg = g_win32_error_message (WSAGetLastError ());
786 g_print (" failed: %s", emsg);
789 channel->event_mask = event_mask;
792 g_print ("\n setting last_events=0");
793 channel->last_events = 0;
795 if ((event_mask & FD_WRITE) &&
796 channel->ever_writable &&
797 !channel->write_would_have_blocked)
800 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
801 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
807 g_assert_not_reached ();
813 return ((watch->condition & buffer_condition) == watch->condition);
817 g_io_win32_check (GSource *source)
820 GIOWin32Watch *watch = (GIOWin32Watch *)source;
821 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
822 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
823 WSANETWORKEVENTS events;
826 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
828 switch (channel->type)
830 case G_IO_WIN32_WINDOWS_MESSAGES:
833 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
835 case G_IO_WIN32_FILE_DESC:
837 g_print (" FD thread=%#x buffer_condition=%s\n"
838 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
839 channel->thread_id, condition_to_string (buffer_condition),
840 condition_to_string (watch->pollfd.events),
841 condition_to_string (watch->pollfd.revents),
842 condition_to_string (channel->revents));
844 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
846 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
848 case G_IO_WIN32_CONSOLE:
851 if (watch->channel->is_writeable)
853 else if (watch->channel->is_readable)
857 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
860 /* _kbhit() does quite complex processing to find out
861 * whether at least one of the key events pending corresponds
862 * to a "real" character that can be read.
867 /* Discard all other kinds of events */
868 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
873 case G_IO_WIN32_SOCKET:
876 if (channel->last_events & FD_WRITE)
879 g_print (" sock=%d event=%p last_events has FD_WRITE",
880 channel->fd, (HANDLE) watch->pollfd.fd);
884 WSAEnumNetworkEvents (channel->fd, 0, &events);
887 g_print ("\n revents={%s} condition={%s}"
888 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
889 condition_to_string (watch->pollfd.revents),
890 condition_to_string (watch->condition),
892 event_mask_to_string (events.lNetworkEvents));
894 if (watch->pollfd.revents != 0 &&
895 events.lNetworkEvents == 0 &&
896 !(channel->event_mask & FD_WRITE))
898 channel->event_mask = 0;
900 g_print ("\n WSAEventSelect(%d,%p,{})",
901 channel->fd, (HANDLE) watch->pollfd.fd);
902 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
904 g_print (" ResetEvent(%p)",
905 (HANDLE) watch->pollfd.fd);
906 ResetEvent ((HANDLE) watch->pollfd.fd);
908 else if (events.lNetworkEvents & FD_WRITE)
909 channel->ever_writable = TRUE;
910 channel->last_events = events.lNetworkEvents;
913 watch->pollfd.revents = 0;
914 if (channel->last_events & (FD_READ | FD_ACCEPT))
915 watch->pollfd.revents |= G_IO_IN;
917 if (channel->last_events & FD_WRITE)
918 watch->pollfd.revents |= G_IO_OUT;
921 /* We have called WSAEnumNetworkEvents() above but it didn't
924 if (events.lNetworkEvents & FD_CONNECT)
926 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
927 watch->pollfd.revents |= G_IO_OUT;
929 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
931 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
932 watch->pollfd.revents |= G_IO_HUP;
935 /* Regardless of WSAEnumNetworkEvents() result, if watching for
936 * writability, and if we have ever got a FD_WRITE event, and
937 * unless last write would have blocked, set G_IO_OUT. But never
938 * set both G_IO_OUT and G_IO_HUP.
940 if (!(watch->pollfd.revents & G_IO_HUP) &&
941 channel->ever_writable &&
942 !channel->write_would_have_blocked &&
943 (channel->event_mask & FD_WRITE))
944 watch->pollfd.revents |= G_IO_OUT;
947 g_print ("\n revents={%s} retval={%s}\n",
948 condition_to_string (watch->pollfd.revents),
949 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
951 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
954 g_assert_not_reached ();
960 g_io_win32_dispatch (GSource *source,
961 GSourceFunc callback,
964 GIOFunc func = (GIOFunc)callback;
965 GIOWin32Watch *watch = (GIOWin32Watch *)source;
966 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
967 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
971 g_warning ("IO Watch dispatched without callback\n"
972 "You must call g_source_connect().");
977 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
978 condition_to_string (watch->pollfd.revents),
979 condition_to_string (watch->condition),
980 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
982 return (*func) (watch->channel,
983 (watch->pollfd.revents | buffer_condition) & watch->condition,
988 g_io_win32_finalize (GSource *source)
990 GIOWin32Watch *watch = (GIOWin32Watch *)source;
991 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
994 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
996 switch (channel->type)
998 case G_IO_WIN32_WINDOWS_MESSAGES:
1003 case G_IO_WIN32_CONSOLE:
1008 case G_IO_WIN32_FILE_DESC:
1010 g_print (" FD thread=%#x", channel->thread_id);
1013 case G_IO_WIN32_SOCKET:
1015 g_print (" SOCK sock=%d", channel->fd);
1019 g_assert_not_reached ();
1024 g_io_channel_unref (watch->channel);
1027 GSourceFuncs g_io_watch_funcs = {
1030 g_io_win32_dispatch,
1035 g_io_win32_msg_read (GIOChannel *channel,
1041 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1042 MSG msg; /* In case of alignment problems */
1044 if (count < sizeof (MSG))
1046 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1047 "Incorrect message size"); /* Informative enough error message? */
1048 return G_IO_STATUS_ERROR;
1051 if (win32_channel->debug)
1052 g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1053 channel, win32_channel->hwnd);
1054 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1055 return G_IO_STATUS_AGAIN;
1057 memmove (buf, &msg, sizeof (MSG));
1058 *bytes_read = sizeof (MSG);
1060 return G_IO_STATUS_NORMAL;
1064 g_io_win32_msg_write (GIOChannel *channel,
1067 gsize *bytes_written,
1070 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1073 if (count != sizeof (MSG))
1075 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1076 "Incorrect message size"); /* Informative enough error message? */
1077 return G_IO_STATUS_ERROR;
1080 /* In case of alignment problems */
1081 memmove (&msg, buf, sizeof (MSG));
1082 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1084 gchar *emsg = g_win32_error_message (GetLastError ());
1086 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1089 return G_IO_STATUS_ERROR;
1092 *bytes_written = sizeof (MSG);
1094 return G_IO_STATUS_NORMAL;
1098 g_io_win32_msg_close (GIOChannel *channel,
1101 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1103 return G_IO_STATUS_NORMAL;
1107 g_io_win32_free (GIOChannel *channel)
1109 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1111 if (win32_channel->debug)
1112 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1114 DeleteCriticalSection (&win32_channel->mutex);
1116 if (win32_channel->data_avail_event)
1117 if (!CloseHandle (win32_channel->data_avail_event))
1118 if (win32_channel->debug)
1120 gchar *emsg = g_win32_error_message (GetLastError ());
1122 g_print (" CloseHandle(%p) failed: %s\n",
1123 win32_channel->data_avail_event, emsg);
1127 g_free (win32_channel->buffer);
1129 if (win32_channel->space_avail_event)
1130 if (!CloseHandle (win32_channel->space_avail_event))
1131 if (win32_channel->debug)
1133 gchar *emsg = g_win32_error_message (GetLastError ());
1135 g_print (" CloseHandle(%p) failed: %s\n",
1136 win32_channel->space_avail_event, emsg);
1140 if (win32_channel->type == G_IO_WIN32_SOCKET)
1141 if (WSAEventSelect (win32_channel->fd, NULL, 0) == SOCKET_ERROR)
1142 if (win32_channel->debug)
1144 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1146 g_print (" WSAEventSelect(%d,NULL,{}) failed: %s\n",
1147 win32_channel->fd, emsg);
1151 if (win32_channel->event)
1152 if (!WSACloseEvent (win32_channel->event))
1153 if (win32_channel->debug)
1155 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1157 g_print (" WSACloseEvent(%p) failed: %s\n",
1158 win32_channel->event, emsg);
1162 g_free (win32_channel);
1166 g_io_win32_msg_create_watch (GIOChannel *channel,
1167 GIOCondition condition)
1169 GIOWin32Watch *watch;
1172 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1173 watch = (GIOWin32Watch *)source;
1175 watch->channel = channel;
1176 g_io_channel_ref (channel);
1178 watch->condition = condition;
1180 watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1181 watch->pollfd.events = condition;
1183 g_source_add_poll (source, &watch->pollfd);
1189 g_io_win32_fd_and_console_read (GIOChannel *channel,
1195 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1198 if (win32_channel->debug)
1199 g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1200 win32_channel->fd, count);
1202 if (win32_channel->thread_id)
1204 return buffer_read (win32_channel, buf, count, bytes_read, err);
1207 result = read (win32_channel->fd, buf, count);
1209 if (win32_channel->debug)
1210 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1220 return G_IO_STATUS_AGAIN;
1223 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1224 g_io_channel_error_from_errno (errno),
1225 g_strerror (errno));
1226 return G_IO_STATUS_ERROR;
1230 *bytes_read = result;
1232 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1236 g_io_win32_fd_and_console_write (GIOChannel *channel,
1239 gsize *bytes_written,
1242 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1245 if (win32_channel->thread_id)
1247 return buffer_write (win32_channel, buf, count, bytes_written, err);
1250 result = write (win32_channel->fd, buf, count);
1251 if (win32_channel->debug)
1252 g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1253 win32_channel->fd, count, result);
1263 return G_IO_STATUS_AGAIN;
1266 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1267 g_io_channel_error_from_errno (errno),
1268 g_strerror (errno));
1269 return G_IO_STATUS_ERROR;
1273 *bytes_written = result;
1275 return G_IO_STATUS_NORMAL;
1279 g_io_win32_fd_seek (GIOChannel *channel,
1284 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1301 whence = -1; /* Keep the compiler quiet */
1302 g_assert_not_reached ();
1306 tmp_offset = offset;
1307 if (tmp_offset != offset)
1309 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1310 g_io_channel_error_from_errno (EINVAL),
1311 g_strerror (EINVAL));
1312 return G_IO_STATUS_ERROR;
1315 result = lseek (win32_channel->fd, tmp_offset, whence);
1319 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1320 g_io_channel_error_from_errno (errno),
1321 g_strerror (errno));
1322 return G_IO_STATUS_ERROR;
1325 return G_IO_STATUS_NORMAL;
1329 g_io_win32_fd_close (GIOChannel *channel,
1332 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1334 if (win32_channel->debug)
1335 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1336 win32_channel->thread_id,
1338 EnterCriticalSection (&win32_channel->mutex);
1339 if (win32_channel->running)
1341 if (win32_channel->debug)
1342 g_print ("thread %#x: running, marking fd %d for later close\n",
1343 win32_channel->thread_id, win32_channel->fd);
1344 win32_channel->running = FALSE;
1345 win32_channel->needs_close = TRUE;
1346 if (win32_channel->direction == 0)
1347 SetEvent (win32_channel->data_avail_event);
1349 SetEvent (win32_channel->space_avail_event);
1353 if (win32_channel->debug)
1354 g_print ("closing fd %d\n", win32_channel->fd);
1355 close (win32_channel->fd);
1356 if (win32_channel->debug)
1357 g_print ("closed fd %d, setting to -1\n",
1359 win32_channel->fd = -1;
1361 LeaveCriticalSection (&win32_channel->mutex);
1363 /* FIXME error detection? */
1365 return G_IO_STATUS_NORMAL;
1369 g_io_win32_fd_create_watch (GIOChannel *channel,
1370 GIOCondition condition)
1372 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1373 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1374 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1376 watch->channel = channel;
1377 g_io_channel_ref (channel);
1379 watch->condition = condition;
1381 if (win32_channel->data_avail_event == NULL)
1382 create_events (win32_channel);
1384 watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1385 watch->pollfd.events = condition;
1387 if (win32_channel->debug)
1388 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1389 channel, win32_channel->fd,
1390 condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1392 EnterCriticalSection (&win32_channel->mutex);
1393 if (win32_channel->thread_id == 0)
1395 if (condition & G_IO_IN)
1396 create_thread (win32_channel, condition, read_thread);
1397 else if (condition & G_IO_OUT)
1398 create_thread (win32_channel, condition, write_thread);
1401 g_source_add_poll (source, &watch->pollfd);
1402 LeaveCriticalSection (&win32_channel->mutex);
1408 g_io_win32_console_close (GIOChannel *channel,
1411 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1413 if (close (win32_channel->fd) < 0)
1415 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1416 g_io_channel_error_from_errno (errno),
1417 g_strerror (errno));
1418 return G_IO_STATUS_ERROR;
1421 return G_IO_STATUS_NORMAL;
1425 g_io_win32_console_create_watch (GIOChannel *channel,
1426 GIOCondition condition)
1428 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1429 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1430 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1432 watch->channel = channel;
1433 g_io_channel_ref (channel);
1435 watch->condition = condition;
1437 watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1438 watch->pollfd.events = condition;
1440 g_source_add_poll (source, &watch->pollfd);
1446 g_io_win32_sock_read (GIOChannel *channel,
1452 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1454 GIOChannelError error;
1457 if (win32_channel->debug)
1458 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1459 channel, win32_channel->fd, count);
1461 result = recv (win32_channel->fd, buf, count, 0);
1462 if (result == SOCKET_ERROR)
1463 winsock_error = WSAGetLastError ();
1465 if (win32_channel->debug)
1466 g_print (" recv=%d", result);
1468 if (result == SOCKET_ERROR)
1470 gchar *emsg = g_win32_error_message (winsock_error);
1472 if (win32_channel->debug)
1473 g_print (" %s\n", emsg);
1477 switch (winsock_error)
1480 error = G_IO_CHANNEL_ERROR_INVAL;
1482 case WSAEWOULDBLOCK:
1484 return G_IO_STATUS_AGAIN;
1486 error = G_IO_CHANNEL_ERROR_FAILED;
1489 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1492 return G_IO_STATUS_ERROR;
1496 if (win32_channel->debug)
1498 *bytes_read = result;
1500 return G_IO_STATUS_EOF;
1502 return G_IO_STATUS_NORMAL;
1507 g_io_win32_sock_write (GIOChannel *channel,
1510 gsize *bytes_written,
1513 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1515 GIOChannelError error;
1518 if (win32_channel->debug)
1519 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1520 channel, win32_channel->fd, count);
1522 result = send (win32_channel->fd, buf, count, 0);
1523 if (result == SOCKET_ERROR)
1524 winsock_error = WSAGetLastError ();
1526 if (win32_channel->debug)
1527 g_print (" send=%d", result);
1529 if (result == SOCKET_ERROR)
1531 gchar *emsg = g_win32_error_message (winsock_error);
1533 if (win32_channel->debug)
1534 g_print (" %s\n", emsg);
1538 switch (winsock_error)
1541 error = G_IO_CHANNEL_ERROR_INVAL;
1543 case WSAEWOULDBLOCK:
1544 win32_channel->write_would_have_blocked = TRUE;
1545 win32_channel->last_events = 0;
1547 return G_IO_STATUS_AGAIN;
1549 error = G_IO_CHANNEL_ERROR_FAILED;
1552 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1555 return G_IO_STATUS_ERROR;
1559 if (win32_channel->debug)
1561 *bytes_written = result;
1562 win32_channel->write_would_have_blocked = FALSE;
1564 return G_IO_STATUS_NORMAL;
1569 g_io_win32_sock_close (GIOChannel *channel,
1572 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1574 if (win32_channel->fd != -1)
1576 if (win32_channel->debug)
1577 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1578 channel, win32_channel->fd);
1580 closesocket (win32_channel->fd);
1581 win32_channel->fd = -1;
1584 /* FIXME error detection? */
1586 return G_IO_STATUS_NORMAL;
1590 g_io_win32_sock_create_watch (GIOChannel *channel,
1591 GIOCondition condition)
1593 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1594 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1595 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1597 watch->channel = channel;
1598 g_io_channel_ref (channel);
1600 watch->condition = condition;
1602 if (win32_channel->event == 0)
1603 win32_channel->event = WSACreateEvent ();
1605 watch->pollfd.fd = (gintptr) win32_channel->event;
1606 watch->pollfd.events = condition;
1608 if (win32_channel->debug)
1609 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1610 channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1611 condition_to_string (watch->condition));
1613 g_source_add_poll (source, &watch->pollfd);
1619 g_io_channel_new_file (const gchar *filename,
1623 int fid, flags, pmode;
1624 GIOChannel *channel;
1626 enum { /* Cheesy hack */
1633 g_return_val_if_fail (filename != NULL, NULL);
1634 g_return_val_if_fail (mode != NULL, NULL);
1635 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1649 g_warning ("Invalid GIOFileMode %s.", mode);
1658 if (mode[2] == '\0')
1660 mode_num |= MODE_PLUS;
1665 g_warning ("Invalid GIOFileMode %s.", mode);
1676 flags = O_WRONLY | O_TRUNC | O_CREAT;
1680 flags = O_WRONLY | O_APPEND | O_CREAT;
1683 case MODE_R | MODE_PLUS:
1685 pmode = _S_IREAD | _S_IWRITE;
1687 case MODE_W | MODE_PLUS:
1688 flags = O_RDWR | O_TRUNC | O_CREAT;
1689 pmode = _S_IREAD | _S_IWRITE;
1691 case MODE_A | MODE_PLUS:
1692 flags = O_RDWR | O_APPEND | O_CREAT;
1693 pmode = _S_IREAD | _S_IWRITE;
1696 g_assert_not_reached ();
1700 /* always open 'untranslated' */
1701 fid = g_open (filename, flags | _O_BINARY, pmode);
1703 if (g_io_win32_get_debug_flag ())
1705 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1706 g_win32_print_access_mode (flags|_O_BINARY);
1707 g_print (",%#o)=%d\n", pmode, fid);
1712 g_set_error_literal (error, G_FILE_ERROR,
1713 g_file_error_from_errno (errno),
1714 g_strerror (errno));
1715 return (GIOChannel *)NULL;
1718 channel = g_io_channel_win32_new_fd (fid);
1720 /* XXX: move this to g_io_channel_win32_new_fd () */
1721 channel->close_on_unref = TRUE;
1722 channel->is_seekable = TRUE;
1724 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1725 * correspond to actual readability/writeability. Set to FALSE those
1726 * that mode doesn't allow
1731 channel->is_writeable = FALSE;
1735 channel->is_readable = FALSE;
1737 case MODE_R | MODE_PLUS:
1738 case MODE_W | MODE_PLUS:
1739 case MODE_A | MODE_PLUS:
1742 g_assert_not_reached ();
1749 #if !defined (_WIN64)
1751 #undef g_io_channel_new_file
1753 /* Binary compatibility version. Not for newly compiled code. */
1756 g_io_channel_new_file (const gchar *filename,
1760 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1763 if (utf8_filename == NULL)
1766 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1768 g_free (utf8_filename);
1776 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1780 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1782 if (win32_channel->debug)
1784 g_print ("g_io_win32_unimpl_set_flags: ");
1785 g_win32_print_gioflags (flags);
1789 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1790 G_IO_CHANNEL_ERROR_FAILED,
1791 "Not implemented on Win32");
1793 return G_IO_STATUS_ERROR;
1797 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1800 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1804 if (st->st_mode & _S_IFIFO)
1806 channel->is_readable =
1807 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1808 channel->is_writeable =
1809 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1810 channel->is_seekable = FALSE;
1814 channel->is_readable =
1815 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1816 channel->is_writeable =
1817 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1818 channel->is_seekable = TRUE;
1821 /* XXX: G_IO_FLAG_APPEND */
1822 /* XXX: G_IO_FLAG_NONBLOCK */
1828 g_io_win32_fd_get_flags (GIOChannel *channel)
1831 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1833 g_return_val_if_fail (win32_channel != NULL, 0);
1834 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1836 if (0 == fstat (win32_channel->fd, &st))
1837 return g_io_win32_fd_get_flags_internal (channel, &st);
1843 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1845 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1846 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1849 INPUT_RECORD record;
1851 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1852 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1853 channel->is_seekable = FALSE;
1859 g_io_win32_console_get_flags (GIOChannel *channel)
1861 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1863 g_return_val_if_fail (win32_channel != NULL, 0);
1864 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1866 return g_io_win32_console_get_flags_internal (channel);
1870 g_io_win32_msg_get_flags (GIOChannel *channel)
1876 g_io_win32_sock_set_flags (GIOChannel *channel,
1880 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1883 if (win32_channel->debug)
1885 g_print ("g_io_win32_sock_set_flags: ");
1886 g_win32_print_gioflags (flags);
1890 if (flags & G_IO_FLAG_NONBLOCK)
1893 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1895 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1897 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1898 G_IO_CHANNEL_ERROR_FAILED,
1902 return G_IO_STATUS_ERROR;
1908 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1910 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1912 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1913 G_IO_CHANNEL_ERROR_FAILED,
1917 return G_IO_STATUS_ERROR;
1921 return G_IO_STATUS_NORMAL;
1925 g_io_win32_sock_get_flags (GIOChannel *channel)
1927 /* Could we do something here? */
1931 static GIOFuncs win32_channel_msg_funcs = {
1932 g_io_win32_msg_read,
1933 g_io_win32_msg_write,
1935 g_io_win32_msg_close,
1936 g_io_win32_msg_create_watch,
1938 g_io_win32_unimpl_set_flags,
1939 g_io_win32_msg_get_flags,
1942 static GIOFuncs win32_channel_fd_funcs = {
1943 g_io_win32_fd_and_console_read,
1944 g_io_win32_fd_and_console_write,
1946 g_io_win32_fd_close,
1947 g_io_win32_fd_create_watch,
1949 g_io_win32_unimpl_set_flags,
1950 g_io_win32_fd_get_flags,
1953 static GIOFuncs win32_channel_console_funcs = {
1954 g_io_win32_fd_and_console_read,
1955 g_io_win32_fd_and_console_write,
1957 g_io_win32_console_close,
1958 g_io_win32_console_create_watch,
1960 g_io_win32_unimpl_set_flags,
1961 g_io_win32_console_get_flags,
1964 static GIOFuncs win32_channel_sock_funcs = {
1965 g_io_win32_sock_read,
1966 g_io_win32_sock_write,
1968 g_io_win32_sock_close,
1969 g_io_win32_sock_create_watch,
1971 g_io_win32_sock_set_flags,
1972 g_io_win32_sock_get_flags,
1976 #if GLIB_SIZEOF_VOID_P == 8
1977 g_io_channel_win32_new_messages (gsize hwnd)
1979 g_io_channel_win32_new_messages (guint hwnd)
1982 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1983 GIOChannel *channel = (GIOChannel *)win32_channel;
1985 g_io_channel_init (channel);
1986 g_io_channel_win32_init (win32_channel);
1987 if (win32_channel->debug)
1988 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1989 channel, (HWND) hwnd);
1990 channel->funcs = &win32_channel_msg_funcs;
1991 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1992 win32_channel->hwnd = (HWND) hwnd;
1994 /* XXX: check this. */
1995 channel->is_readable = IsWindow (win32_channel->hwnd);
1996 channel->is_writeable = IsWindow (win32_channel->hwnd);
1998 channel->is_seekable = FALSE;
2004 g_io_channel_win32_new_fd_internal (gint fd,
2007 GIOWin32Channel *win32_channel;
2008 GIOChannel *channel;
2010 win32_channel = g_new (GIOWin32Channel, 1);
2011 channel = (GIOChannel *)win32_channel;
2013 g_io_channel_init (channel);
2014 g_io_channel_win32_init (win32_channel);
2016 win32_channel->fd = fd;
2018 if (win32_channel->debug)
2019 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
2022 if (st->st_mode & _S_IFCHR) /* console */
2024 channel->funcs = &win32_channel_console_funcs;
2025 win32_channel->type = G_IO_WIN32_CONSOLE;
2026 g_io_win32_console_get_flags_internal (channel);
2030 channel->funcs = &win32_channel_fd_funcs;
2031 win32_channel->type = G_IO_WIN32_FILE_DESC;
2032 g_io_win32_fd_get_flags_internal (channel, st);
2039 g_io_channel_win32_new_fd (gint fd)
2043 if (fstat (fd, &st) == -1)
2045 g_warning ("g_io_channel_win32_new_fd: %d isn't an open file descriptor in the C library GLib uses.", fd);
2049 return g_io_channel_win32_new_fd_internal (fd, &st);
2053 g_io_channel_win32_get_fd (GIOChannel *channel)
2055 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2057 return win32_channel->fd;
2061 g_io_channel_win32_new_socket (int socket)
2063 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2064 GIOChannel *channel = (GIOChannel *)win32_channel;
2066 g_io_channel_init (channel);
2067 g_io_channel_win32_init (win32_channel);
2068 if (win32_channel->debug)
2069 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2071 channel->funcs = &win32_channel_sock_funcs;
2072 win32_channel->type = G_IO_WIN32_SOCKET;
2073 win32_channel->fd = socket;
2075 channel->is_readable = TRUE;
2076 channel->is_writeable = TRUE;
2077 channel->is_seekable = FALSE;
2083 g_io_channel_unix_new (gint fd)
2085 gboolean is_fd, is_socket;
2089 is_fd = (fstat (fd, &st) == 0);
2091 optlen = sizeof (optval);
2092 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2094 if (is_fd && is_socket)
2095 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);
2098 return g_io_channel_win32_new_fd_internal (fd, &st);
2101 return g_io_channel_win32_new_socket(fd);
2103 g_warning ("g_io_channel_unix_new: %d is neither a file descriptor or a socket.", fd);
2109 g_io_channel_unix_get_fd (GIOChannel *channel)
2111 return g_io_channel_win32_get_fd (channel);
2115 g_io_channel_win32_set_debug (GIOChannel *channel,
2118 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2120 win32_channel->debug = flag;
2124 g_io_channel_win32_poll (GPollFD *fds,
2128 g_return_val_if_fail (n_fds >= 0, 0);
2130 return g_poll (fds, n_fds, timeout);
2134 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2135 GIOCondition condition,
2138 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2140 switch (win32_channel->type)
2142 case G_IO_WIN32_FILE_DESC:
2143 if (win32_channel->data_avail_event == NULL)
2144 create_events (win32_channel);
2146 fd->fd = (gintptr) win32_channel->data_avail_event;
2148 if (win32_channel->thread_id == 0)
2150 /* Is it meaningful for a file descriptor to be polled for
2151 * both IN and OUT? For what kind of file descriptor would
2152 * that be? Doesn't seem to make sense, in practise the file
2153 * descriptors handled here are always read or write ends of
2154 * pipes surely, and thus unidirectional.
2156 if (condition & G_IO_IN)
2157 create_thread (win32_channel, condition, read_thread);
2158 else if (condition & G_IO_OUT)
2159 create_thread (win32_channel, condition, write_thread);
2163 case G_IO_WIN32_CONSOLE:
2164 fd->fd = _get_osfhandle (win32_channel->fd);
2167 case G_IO_WIN32_SOCKET:
2168 fd->fd = (gintptr) WSACreateEvent ();
2171 case G_IO_WIN32_WINDOWS_MESSAGES:
2172 fd->fd = G_WIN32_MSG_HANDLE;
2176 g_assert_not_reached ();
2180 fd->events = condition;
2185 /* Binary compatibility */
2187 g_io_channel_win32_new_stream_socket (int socket)
2189 return g_io_channel_win32_new_socket (socket);
2194 #define __G_IO_WIN32_C__
2195 #include "galiasdef.c"