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 #define LOCK(mutex) EnterCriticalSection (&mutex)
167 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
169 struct _GIOWin32Watch {
173 GIOCondition condition;
177 g_win32_print_access_mode (int flags)
179 g_print ("%s%s%s%s%s%s%s%s%s%s",
180 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
181 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
182 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
183 (flags & _O_APPEND ? "|O_APPEND" : ""),
184 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
185 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
186 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
187 (flags & _O_CREAT ? "|O_CREAT" : ""),
188 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
189 (flags & _O_EXCL ? "|O_EXCL" : ""),
190 (flags & _O_TEXT ? "|O_TEXT" : ""),
191 (flags & _O_BINARY ? "|O_BINARY" : ""));
195 g_win32_print_gioflags (GIOFlags flags)
199 if (flags & G_IO_FLAG_APPEND)
200 bar = "|", g_print ("APPEND");
201 if (flags & G_IO_FLAG_NONBLOCK)
202 g_print ("%sNONBLOCK", bar), bar = "|";
203 if (flags & G_IO_FLAG_IS_READABLE)
204 g_print ("%sREADABLE", bar), bar = "|";
205 if (flags & G_IO_FLAG_IS_WRITEABLE)
206 g_print ("%sWRITEABLE", bar), bar = "|";
207 if (flags & G_IO_FLAG_IS_SEEKABLE)
208 g_print ("%sSEEKABLE", bar), bar = "|";
212 event_mask_to_string (int mask)
215 int checked_bits = 0;
221 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
231 BIT (ROUTING_INTERFACE_CHANGE);
232 BIT (ADDRESS_LIST_CHANGE);
236 if ((mask & ~checked_bits) != 0)
237 bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
239 return g_quark_to_string (g_quark_from_string (buf));
243 condition_to_string (GIOCondition condition)
246 int checked_bits = 0;
252 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
263 if ((condition & ~checked_bits) != 0)
264 bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
266 return g_quark_to_string (g_quark_from_string (buf));
270 g_io_win32_get_debug_flag (void)
272 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
276 g_io_channel_win32_init (GIOWin32Channel *channel)
278 channel->debug = g_io_win32_get_debug_flag ();
279 channel->buffer = NULL;
280 channel->running = FALSE;
281 channel->needs_close = FALSE;
282 channel->thread_id = 0;
283 channel->data_avail_event = NULL;
284 channel->revents = 0;
285 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;
291 InitializeCriticalSection (&channel->mutex);
295 create_events (GIOWin32Channel *channel)
297 SECURITY_ATTRIBUTES sec_attrs;
299 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
300 sec_attrs.lpSecurityDescriptor = NULL;
301 sec_attrs.bInheritHandle = FALSE;
303 /* The data available event is manual reset, the space available event
304 * is automatic reset.
306 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
307 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
309 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 LOCK (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 UNLOCK (channel->mutex);
355 WaitForSingleObject (channel->space_avail_event, INFINITE);
356 LOCK (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 UNLOCK (channel->mutex);
376 nbytes = read (channel->fd, buffer, nbytes);
378 LOCK (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 UNLOCK (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 LOCK (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 UNLOCK (channel->mutex);
474 WaitForSingleObject (channel->space_avail_event, INFINITE);
476 LOCK (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 UNLOCK (channel->mutex);
496 nbytes = write (channel->fd, buffer, nbytes);
497 LOCK (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 UNLOCK (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))
550 g_warning ("Error closing thread handle: %s.\n",
551 g_win32_error_message (GetLastError ()));
553 WaitForSingleObject (channel->space_avail_event, INFINITE);
557 buffer_read (GIOWin32Channel *channel,
566 LOCK (channel->mutex);
568 g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
569 channel->thread_id, count, channel->rdp, channel->wrp);
571 if (channel->wrp == channel->rdp)
573 UNLOCK (channel->mutex);
575 g_print ("waiting for data from thread %#x\n", channel->thread_id);
576 WaitForSingleObject (channel->data_avail_event, INFINITE);
578 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
579 LOCK (channel->mutex);
580 if (channel->wrp == channel->rdp && !channel->running)
583 g_print ("wrp==rdp, !running\n");
584 UNLOCK (channel->mutex);
586 return G_IO_STATUS_EOF;
590 if (channel->rdp < channel->wrp)
591 nbytes = channel->wrp - channel->rdp;
593 nbytes = BUFFER_SIZE - channel->rdp;
594 UNLOCK (channel->mutex);
595 nbytes = MIN (left, nbytes);
597 g_print ("moving %d bytes from thread %#x\n",
598 nbytes, channel->thread_id);
599 memcpy (dest, channel->buffer + channel->rdp, nbytes);
602 LOCK (channel->mutex);
603 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
605 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
606 SetEvent (channel->space_avail_event);
608 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
609 channel->thread_id, channel->rdp, channel->wrp);
610 if (channel->running && channel->wrp == channel->rdp)
613 g_print ("resetting data_avail of thread %#x\n",
615 ResetEvent (channel->data_avail_event);
617 UNLOCK (channel->mutex);
619 /* We have no way to indicate any errors form the actual
620 * read() or recv() call in the reader thread. Should we have?
622 *bytes_read = count - left;
623 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
628 buffer_write (GIOWin32Channel *channel,
631 gsize *bytes_written,
637 LOCK (channel->mutex);
639 g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
640 channel->thread_id, count, channel->rdp, channel->wrp);
642 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
646 g_print ("buffer_write: tid %#x: resetting data_avail\n",
648 ResetEvent (channel->data_avail_event);
650 g_print ("buffer_write: tid %#x: waiting for space\n",
652 UNLOCK (channel->mutex);
653 WaitForSingleObject (channel->data_avail_event, INFINITE);
654 LOCK (channel->mutex);
656 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
657 channel->thread_id, channel->rdp, channel->wrp);
660 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
661 BUFFER_SIZE - channel->wrp);
663 UNLOCK (channel->mutex);
664 nbytes = MIN (left, nbytes);
666 g_print ("buffer_write: tid %#x: writing %d bytes\n",
667 channel->thread_id, nbytes);
668 memcpy (channel->buffer + channel->wrp, dest, nbytes);
671 LOCK (channel->mutex);
673 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
675 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
676 channel->thread_id, channel->rdp, channel->wrp);
677 SetEvent (channel->space_avail_event);
679 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
683 g_print ("buffer_write: tid %#x: resetting data_avail\n",
685 ResetEvent (channel->data_avail_event);
688 UNLOCK (channel->mutex);
690 /* We have no way to indicate any errors form the actual
691 * write() call in the writer thread. Should we have?
693 *bytes_written = count - left;
694 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
699 g_io_win32_prepare (GSource *source,
702 GIOWin32Watch *watch = (GIOWin32Watch *)source;
703 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
704 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
710 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
712 switch (channel->type)
714 case G_IO_WIN32_WINDOWS_MESSAGES:
719 case G_IO_WIN32_CONSOLE:
724 case G_IO_WIN32_FILE_DESC:
726 g_print (" FD thread=%#x buffer_condition:{%s}"
727 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
728 channel->thread_id, condition_to_string (buffer_condition),
729 condition_to_string (watch->pollfd.events),
730 condition_to_string (watch->pollfd.revents),
731 condition_to_string (channel->revents));
733 LOCK (channel->mutex);
734 if (channel->running)
736 if (channel->direction == 0 && channel->wrp == channel->rdp)
739 g_print ("\n setting revents=0");
740 channel->revents = 0;
745 if (channel->direction == 1
746 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
749 g_print ("\n setting revents=0");
750 channel->revents = 0;
753 UNLOCK (channel->mutex);
756 case G_IO_WIN32_SOCKET:
760 if (watch->condition & G_IO_IN)
761 event_mask |= (FD_READ | FD_ACCEPT);
762 if (watch->condition & G_IO_OUT)
763 event_mask |= (FD_WRITE | FD_CONNECT);
764 event_mask |= FD_CLOSE;
766 if (channel->event_mask != event_mask)
769 g_print ("\n WSAEventSelect(%d,%p,{%s})",
770 channel->fd, (HANDLE) watch->pollfd.fd,
771 event_mask_to_string (event_mask));
772 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
773 event_mask) == SOCKET_ERROR)
775 channel->event_mask = event_mask;
777 channel->event = watch->pollfd.fd;
780 g_print ("\n setting last_events=0");
781 channel->last_events = 0;
783 if ((event_mask & FD_WRITE) &&
784 channel->ever_writable &&
785 !channel->write_would_have_blocked)
788 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
789 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
795 g_assert_not_reached ();
801 return ((watch->condition & buffer_condition) == watch->condition);
805 g_io_win32_check (GSource *source)
808 GIOWin32Watch *watch = (GIOWin32Watch *)source;
809 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
810 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
811 WSANETWORKEVENTS events;
814 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
816 switch (channel->type)
818 case G_IO_WIN32_WINDOWS_MESSAGES:
821 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
823 case G_IO_WIN32_FILE_DESC:
825 g_print (" FD thread=%#x buffer_condition=%s\n"
826 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
827 channel->thread_id, condition_to_string (buffer_condition),
828 condition_to_string (watch->pollfd.events),
829 condition_to_string (watch->pollfd.revents),
830 condition_to_string (channel->revents));
832 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
834 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
836 case G_IO_WIN32_CONSOLE:
839 if (watch->channel->is_writeable)
841 else if (watch->channel->is_readable)
845 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
848 /* _kbhit() does quite complex processing to find out
849 * whether at least one of the key events pending corresponds
850 * to a "real" character that can be read.
855 /* Discard all other kinds of events */
856 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
861 case G_IO_WIN32_SOCKET:
864 if (channel->last_events & FD_WRITE)
867 g_print (" sock=%d event=%p last_events has FD_WRITE",
868 channel->fd, (HANDLE) watch->pollfd.fd);
872 WSAEnumNetworkEvents (channel->fd, 0, &events);
875 g_print ("\n revents={%s} condition={%s}"
876 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
877 condition_to_string (watch->pollfd.revents),
878 condition_to_string (watch->condition),
880 event_mask_to_string (events.lNetworkEvents));
882 if (watch->pollfd.revents != 0 &&
883 events.lNetworkEvents == 0 &&
884 !(channel->event_mask & FD_WRITE))
886 channel->event_mask = 0;
888 g_print ("\n WSAEventSelect(%d,%p,{})",
889 channel->fd, (HANDLE) watch->pollfd.fd);
890 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
892 g_print (" ResetEvent(%p)",
893 (HANDLE) watch->pollfd.fd);
894 ResetEvent ((HANDLE) watch->pollfd.fd);
896 else if (events.lNetworkEvents & FD_WRITE)
897 channel->ever_writable = TRUE;
898 channel->last_events = events.lNetworkEvents;
901 watch->pollfd.revents = 0;
902 if (channel->last_events & (FD_READ | FD_ACCEPT))
903 watch->pollfd.revents |= G_IO_IN;
905 if (channel->last_events & FD_WRITE)
906 watch->pollfd.revents |= G_IO_OUT;
909 /* We have called WSAEnumNetworkEvents() above but it didn't
912 if (events.lNetworkEvents & FD_CONNECT)
914 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
915 watch->pollfd.revents |= G_IO_OUT;
917 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
919 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
920 watch->pollfd.revents |= G_IO_HUP;
923 /* Regardless of WSAEnumNetworkEvents() result, if watching for
924 * writability, and if we have ever got a FD_WRITE event, and
925 * unless last write would have blocked, set G_IO_OUT. But never
926 * set both G_IO_OUT and G_IO_HUP.
928 if (!(watch->pollfd.revents & G_IO_HUP) &&
929 channel->ever_writable &&
930 !channel->write_would_have_blocked &&
931 (channel->event_mask & FD_WRITE))
932 watch->pollfd.revents |= G_IO_OUT;
935 g_print ("\n revents={%s} retval={%s}\n",
936 condition_to_string (watch->pollfd.revents),
937 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
939 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
942 g_assert_not_reached ();
948 g_io_win32_dispatch (GSource *source,
949 GSourceFunc callback,
952 GIOFunc func = (GIOFunc)callback;
953 GIOWin32Watch *watch = (GIOWin32Watch *)source;
954 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
955 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
959 g_warning ("IO Watch dispatched without callback\n"
960 "You must call g_source_connect().");
965 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
966 condition_to_string (watch->pollfd.revents),
967 condition_to_string (watch->condition),
968 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
970 return (*func) (watch->channel,
971 (watch->pollfd.revents | buffer_condition) & watch->condition,
976 g_io_win32_finalize (GSource *source)
978 GIOWin32Watch *watch = (GIOWin32Watch *)source;
979 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
982 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
984 switch (channel->type)
986 case G_IO_WIN32_WINDOWS_MESSAGES:
991 case G_IO_WIN32_CONSOLE:
996 case G_IO_WIN32_FILE_DESC:
998 g_print (" FD thread=%#x", channel->thread_id);
1001 case G_IO_WIN32_SOCKET:
1003 g_print (" SOCK sock=%d", channel->fd);
1007 g_assert_not_reached ();
1012 g_io_channel_unref (watch->channel);
1015 GSourceFuncs g_io_watch_funcs = {
1018 g_io_win32_dispatch,
1023 g_io_win32_msg_read (GIOChannel *channel,
1029 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1030 MSG msg; /* In case of alignment problems */
1032 if (count < sizeof (MSG))
1034 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1035 "Incorrect message size"); /* Informative enough error message? */
1036 return G_IO_STATUS_ERROR;
1039 if (win32_channel->debug)
1040 g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1041 channel, win32_channel->hwnd);
1042 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1043 return G_IO_STATUS_AGAIN;
1045 memmove (buf, &msg, sizeof (MSG));
1046 *bytes_read = sizeof (MSG);
1048 return G_IO_STATUS_NORMAL;
1052 g_io_win32_msg_write (GIOChannel *channel,
1055 gsize *bytes_written,
1058 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1061 if (count != sizeof (MSG))
1063 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1064 "Incorrect message size"); /* Informative enough error message? */
1065 return G_IO_STATUS_ERROR;
1068 /* In case of alignment problems */
1069 memmove (&msg, buf, sizeof (MSG));
1070 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1072 gchar *emsg = g_win32_error_message (GetLastError ());
1073 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1075 return G_IO_STATUS_ERROR;
1078 *bytes_written = sizeof (MSG);
1080 return G_IO_STATUS_NORMAL;
1084 g_io_win32_msg_close (GIOChannel *channel,
1087 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1089 return G_IO_STATUS_NORMAL;
1093 g_io_win32_free (GIOChannel *channel)
1095 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1097 if (win32_channel->debug)
1098 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1100 if (win32_channel->data_avail_event)
1101 CloseHandle (win32_channel->data_avail_event);
1102 if (win32_channel->space_avail_event)
1103 CloseHandle (win32_channel->space_avail_event);
1104 if (win32_channel->type == G_IO_WIN32_SOCKET)
1105 WSAEventSelect (win32_channel->fd, NULL, 0);
1106 DeleteCriticalSection (&win32_channel->mutex);
1108 g_free (win32_channel->buffer);
1109 g_free (win32_channel);
1113 g_io_win32_msg_create_watch (GIOChannel *channel,
1114 GIOCondition condition)
1116 GIOWin32Watch *watch;
1119 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1120 watch = (GIOWin32Watch *)source;
1122 watch->channel = channel;
1123 g_io_channel_ref (channel);
1125 watch->condition = condition;
1127 watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1128 watch->pollfd.events = condition;
1130 g_source_add_poll (source, &watch->pollfd);
1136 g_io_win32_fd_and_console_read (GIOChannel *channel,
1142 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1145 if (win32_channel->debug)
1146 g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1147 win32_channel->fd, count);
1149 if (win32_channel->thread_id)
1151 return buffer_read (win32_channel, buf, count, bytes_read, err);
1154 result = read (win32_channel->fd, buf, count);
1156 if (win32_channel->debug)
1157 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1167 return G_IO_STATUS_AGAIN;
1170 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1171 g_io_channel_error_from_errno (errno),
1172 g_strerror (errno));
1173 return G_IO_STATUS_ERROR;
1177 *bytes_read = result;
1179 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1183 g_io_win32_fd_and_console_write (GIOChannel *channel,
1186 gsize *bytes_written,
1189 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1192 if (win32_channel->thread_id)
1194 return buffer_write (win32_channel, buf, count, bytes_written, err);
1197 result = write (win32_channel->fd, buf, count);
1198 if (win32_channel->debug)
1199 g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1200 win32_channel->fd, count, result);
1210 return G_IO_STATUS_AGAIN;
1213 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1214 g_io_channel_error_from_errno (errno),
1215 g_strerror (errno));
1216 return G_IO_STATUS_ERROR;
1220 *bytes_written = result;
1222 return G_IO_STATUS_NORMAL;
1226 g_io_win32_fd_seek (GIOChannel *channel,
1231 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1248 whence = -1; /* Keep the compiler quiet */
1249 g_assert_not_reached ();
1253 tmp_offset = offset;
1254 if (tmp_offset != offset)
1256 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1257 g_io_channel_error_from_errno (EINVAL),
1258 g_strerror (EINVAL));
1259 return G_IO_STATUS_ERROR;
1262 result = lseek (win32_channel->fd, tmp_offset, whence);
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;
1272 return G_IO_STATUS_NORMAL;
1276 g_io_win32_fd_close (GIOChannel *channel,
1279 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1281 if (win32_channel->debug)
1282 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1283 win32_channel->thread_id,
1285 LOCK (win32_channel->mutex);
1286 if (win32_channel->running)
1288 if (win32_channel->debug)
1289 g_print ("thread %#x: running, marking fd %d for later close\n",
1290 win32_channel->thread_id, win32_channel->fd);
1291 win32_channel->running = FALSE;
1292 win32_channel->needs_close = TRUE;
1293 if (win32_channel->direction == 0)
1294 SetEvent (win32_channel->data_avail_event);
1296 SetEvent (win32_channel->space_avail_event);
1300 if (win32_channel->debug)
1301 g_print ("closing fd %d\n", win32_channel->fd);
1302 close (win32_channel->fd);
1303 if (win32_channel->debug)
1304 g_print ("closed fd %d, setting to -1\n",
1306 win32_channel->fd = -1;
1308 UNLOCK (win32_channel->mutex);
1310 /* FIXME error detection? */
1312 return G_IO_STATUS_NORMAL;
1316 g_io_win32_fd_create_watch (GIOChannel *channel,
1317 GIOCondition condition)
1319 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1320 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1321 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1323 watch->channel = channel;
1324 g_io_channel_ref (channel);
1326 watch->condition = condition;
1328 if (win32_channel->data_avail_event == NULL)
1329 create_events (win32_channel);
1331 watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1332 watch->pollfd.events = condition;
1334 if (win32_channel->debug)
1335 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1336 channel, win32_channel->fd,
1337 condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1339 LOCK (win32_channel->mutex);
1340 if (win32_channel->thread_id == 0)
1342 if (condition & G_IO_IN)
1343 create_thread (win32_channel, condition, read_thread);
1344 else if (condition & G_IO_OUT)
1345 create_thread (win32_channel, condition, write_thread);
1348 g_source_add_poll (source, &watch->pollfd);
1349 UNLOCK (win32_channel->mutex);
1355 g_io_win32_console_close (GIOChannel *channel,
1358 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1360 if (close (win32_channel->fd) < 0)
1362 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1363 g_io_channel_error_from_errno (errno),
1364 g_strerror (errno));
1365 return G_IO_STATUS_ERROR;
1368 return G_IO_STATUS_NORMAL;
1372 g_io_win32_console_create_watch (GIOChannel *channel,
1373 GIOCondition condition)
1375 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1376 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1377 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1379 watch->channel = channel;
1380 g_io_channel_ref (channel);
1382 watch->condition = condition;
1384 watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1385 watch->pollfd.events = condition;
1387 g_source_add_poll (source, &watch->pollfd);
1393 g_io_win32_sock_read (GIOChannel *channel,
1399 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1401 GIOChannelError error;
1404 if (win32_channel->debug)
1405 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1406 channel, win32_channel->fd, count);
1408 result = recv (win32_channel->fd, buf, count, 0);
1409 if (result == SOCKET_ERROR)
1410 winsock_error = WSAGetLastError ();
1412 if (win32_channel->debug)
1413 g_print (" recv=%d", result);
1415 if (result == SOCKET_ERROR)
1417 gchar *emsg = g_win32_error_message (winsock_error);
1419 if (win32_channel->debug)
1420 g_print (" %s\n", emsg);
1424 switch (winsock_error)
1427 error = G_IO_CHANNEL_ERROR_INVAL;
1429 case WSAEWOULDBLOCK:
1431 return G_IO_STATUS_AGAIN;
1433 error = G_IO_CHANNEL_ERROR_FAILED;
1436 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1439 return G_IO_STATUS_ERROR;
1443 if (win32_channel->debug)
1445 *bytes_read = result;
1447 return G_IO_STATUS_EOF;
1449 return G_IO_STATUS_NORMAL;
1454 g_io_win32_sock_write (GIOChannel *channel,
1457 gsize *bytes_written,
1460 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1462 GIOChannelError error;
1465 if (win32_channel->debug)
1466 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1467 channel, win32_channel->fd, count);
1469 result = send (win32_channel->fd, buf, count, 0);
1470 if (result == SOCKET_ERROR)
1471 winsock_error = WSAGetLastError ();
1473 if (win32_channel->debug)
1474 g_print (" send=%d", result);
1476 if (result == SOCKET_ERROR)
1478 gchar *emsg = g_win32_error_message (winsock_error);
1480 if (win32_channel->debug)
1481 g_print (" %s\n", emsg);
1485 switch (winsock_error)
1488 error = G_IO_CHANNEL_ERROR_INVAL;
1490 case WSAEWOULDBLOCK:
1491 win32_channel->write_would_have_blocked = TRUE;
1492 win32_channel->last_events = 0;
1494 return G_IO_STATUS_AGAIN;
1496 error = G_IO_CHANNEL_ERROR_FAILED;
1499 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1502 return G_IO_STATUS_ERROR;
1506 if (win32_channel->debug)
1508 *bytes_written = result;
1509 win32_channel->write_would_have_blocked = FALSE;
1511 return G_IO_STATUS_NORMAL;
1516 g_io_win32_sock_close (GIOChannel *channel,
1519 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1521 if (win32_channel->fd != -1)
1523 if (win32_channel->debug)
1524 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1525 channel, win32_channel->fd);
1527 closesocket (win32_channel->fd);
1528 win32_channel->fd = -1;
1531 /* FIXME error detection? */
1533 return G_IO_STATUS_NORMAL;
1537 g_io_win32_sock_create_watch (GIOChannel *channel,
1538 GIOCondition condition)
1540 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1541 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1542 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1544 watch->channel = channel;
1545 g_io_channel_ref (channel);
1547 watch->condition = condition;
1549 if (win32_channel->event == 0)
1550 win32_channel->event = WSACreateEvent ();
1552 watch->pollfd.fd = (gintptr) win32_channel->event;
1553 watch->pollfd.events = condition;
1555 if (win32_channel->debug)
1556 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1557 channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1558 condition_to_string (watch->condition));
1560 g_source_add_poll (source, &watch->pollfd);
1566 g_io_channel_new_file (const gchar *filename,
1570 int fid, flags, pmode;
1571 GIOChannel *channel;
1573 enum { /* Cheesy hack */
1580 g_return_val_if_fail (filename != NULL, NULL);
1581 g_return_val_if_fail (mode != NULL, NULL);
1582 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1596 g_warning ("Invalid GIOFileMode %s.\n", mode);
1605 if (mode[2] == '\0')
1607 mode_num |= MODE_PLUS;
1612 g_warning ("Invalid GIOFileMode %s.\n", mode);
1623 flags = O_WRONLY | O_TRUNC | O_CREAT;
1627 flags = O_WRONLY | O_APPEND | O_CREAT;
1630 case MODE_R | MODE_PLUS:
1632 pmode = _S_IREAD | _S_IWRITE;
1634 case MODE_W | MODE_PLUS:
1635 flags = O_RDWR | O_TRUNC | O_CREAT;
1636 pmode = _S_IREAD | _S_IWRITE;
1638 case MODE_A | MODE_PLUS:
1639 flags = O_RDWR | O_APPEND | O_CREAT;
1640 pmode = _S_IREAD | _S_IWRITE;
1643 g_assert_not_reached ();
1647 /* always open 'untranslated' */
1648 fid = g_open (filename, flags | _O_BINARY, pmode);
1650 if (g_io_win32_get_debug_flag ())
1652 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1653 g_win32_print_access_mode (flags|_O_BINARY);
1654 g_print (",%#o)=%d\n", pmode, fid);
1659 g_set_error_literal (error, G_FILE_ERROR,
1660 g_file_error_from_errno (errno),
1661 g_strerror (errno));
1662 return (GIOChannel *)NULL;
1665 channel = g_io_channel_win32_new_fd (fid);
1667 /* XXX: move this to g_io_channel_win32_new_fd () */
1668 channel->close_on_unref = TRUE;
1669 channel->is_seekable = TRUE;
1671 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1672 * correspond to actual readability/writeability. Set to FALSE those
1673 * that mode doesn't allow
1678 channel->is_writeable = FALSE;
1682 channel->is_readable = FALSE;
1684 case MODE_R | MODE_PLUS:
1685 case MODE_W | MODE_PLUS:
1686 case MODE_A | MODE_PLUS:
1689 g_assert_not_reached ();
1696 #if !defined (_WIN64)
1698 #undef g_io_channel_new_file
1700 /* Binary compatibility version. Not for newly compiled code. */
1703 g_io_channel_new_file (const gchar *filename,
1707 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1710 if (utf8_filename == NULL)
1713 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1715 g_free (utf8_filename);
1723 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1727 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1729 if (win32_channel->debug)
1731 g_print ("g_io_win32_unimpl_set_flags: ");
1732 g_win32_print_gioflags (flags);
1736 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1737 G_IO_CHANNEL_ERROR_FAILED,
1738 "Not implemented on Win32");
1740 return G_IO_STATUS_ERROR;
1744 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1747 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1751 if (st->st_mode & _S_IFIFO)
1753 channel->is_readable =
1754 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1755 channel->is_writeable =
1756 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1757 channel->is_seekable = FALSE;
1761 channel->is_readable =
1762 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1763 channel->is_writeable =
1764 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1765 channel->is_seekable = TRUE;
1768 /* XXX: G_IO_FLAG_APPEND */
1769 /* XXX: G_IO_FLAG_NONBLOCK */
1775 g_io_win32_fd_get_flags (GIOChannel *channel)
1778 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1780 g_return_val_if_fail (win32_channel != NULL, 0);
1781 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1783 if (0 == fstat (win32_channel->fd, &st))
1784 return g_io_win32_fd_get_flags_internal (channel, &st);
1790 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1792 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1793 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1796 INPUT_RECORD record;
1798 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1799 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1800 channel->is_seekable = FALSE;
1806 g_io_win32_console_get_flags (GIOChannel *channel)
1808 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1810 g_return_val_if_fail (win32_channel != NULL, 0);
1811 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1813 return g_io_win32_console_get_flags_internal (channel);
1817 g_io_win32_msg_get_flags (GIOChannel *channel)
1823 g_io_win32_sock_set_flags (GIOChannel *channel,
1827 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1830 if (win32_channel->debug)
1832 g_print ("g_io_win32_sock_set_flags: ");
1833 g_win32_print_gioflags (flags);
1837 if (flags & G_IO_FLAG_NONBLOCK)
1840 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1842 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1844 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1845 G_IO_CHANNEL_ERROR_FAILED,
1849 return G_IO_STATUS_ERROR;
1855 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1857 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1859 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1860 G_IO_CHANNEL_ERROR_FAILED,
1864 return G_IO_STATUS_ERROR;
1868 return G_IO_STATUS_NORMAL;
1872 g_io_win32_sock_get_flags (GIOChannel *channel)
1874 /* Could we do something here? */
1878 static GIOFuncs win32_channel_msg_funcs = {
1879 g_io_win32_msg_read,
1880 g_io_win32_msg_write,
1882 g_io_win32_msg_close,
1883 g_io_win32_msg_create_watch,
1885 g_io_win32_unimpl_set_flags,
1886 g_io_win32_msg_get_flags,
1889 static GIOFuncs win32_channel_fd_funcs = {
1890 g_io_win32_fd_and_console_read,
1891 g_io_win32_fd_and_console_write,
1893 g_io_win32_fd_close,
1894 g_io_win32_fd_create_watch,
1896 g_io_win32_unimpl_set_flags,
1897 g_io_win32_fd_get_flags,
1900 static GIOFuncs win32_channel_console_funcs = {
1901 g_io_win32_fd_and_console_read,
1902 g_io_win32_fd_and_console_write,
1904 g_io_win32_console_close,
1905 g_io_win32_console_create_watch,
1907 g_io_win32_unimpl_set_flags,
1908 g_io_win32_console_get_flags,
1911 static GIOFuncs win32_channel_sock_funcs = {
1912 g_io_win32_sock_read,
1913 g_io_win32_sock_write,
1915 g_io_win32_sock_close,
1916 g_io_win32_sock_create_watch,
1918 g_io_win32_sock_set_flags,
1919 g_io_win32_sock_get_flags,
1923 #if GLIB_SIZEOF_VOID_P == 8
1924 g_io_channel_win32_new_messages (gsize hwnd)
1926 g_io_channel_win32_new_messages (guint hwnd)
1929 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1930 GIOChannel *channel = (GIOChannel *)win32_channel;
1932 g_io_channel_init (channel);
1933 g_io_channel_win32_init (win32_channel);
1934 if (win32_channel->debug)
1935 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1936 channel, (HWND) hwnd);
1937 channel->funcs = &win32_channel_msg_funcs;
1938 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1939 win32_channel->hwnd = (HWND) hwnd;
1941 /* XXX: check this. */
1942 channel->is_readable = IsWindow (win32_channel->hwnd);
1943 channel->is_writeable = IsWindow (win32_channel->hwnd);
1945 channel->is_seekable = FALSE;
1951 g_io_channel_win32_new_fd_internal (gint fd,
1954 GIOWin32Channel *win32_channel;
1955 GIOChannel *channel;
1957 win32_channel = g_new (GIOWin32Channel, 1);
1958 channel = (GIOChannel *)win32_channel;
1960 g_io_channel_init (channel);
1961 g_io_channel_win32_init (win32_channel);
1963 win32_channel->fd = fd;
1965 if (win32_channel->debug)
1966 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
1969 if (st->st_mode & _S_IFCHR) /* console */
1971 channel->funcs = &win32_channel_console_funcs;
1972 win32_channel->type = G_IO_WIN32_CONSOLE;
1973 g_io_win32_console_get_flags_internal (channel);
1977 channel->funcs = &win32_channel_fd_funcs;
1978 win32_channel->type = G_IO_WIN32_FILE_DESC;
1979 g_io_win32_fd_get_flags_internal (channel, st);
1986 g_io_channel_win32_new_fd (gint fd)
1990 if (fstat (fd, &st) == -1)
1992 g_warning ("g_io_channel_win32_new_fd: %d isn't an open file descriptor in the C library GLib uses.", fd);
1996 return g_io_channel_win32_new_fd_internal (fd, &st);
2000 g_io_channel_win32_get_fd (GIOChannel *channel)
2002 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2004 return win32_channel->fd;
2008 g_io_channel_win32_new_socket (int socket)
2010 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2011 GIOChannel *channel = (GIOChannel *)win32_channel;
2013 g_io_channel_init (channel);
2014 g_io_channel_win32_init (win32_channel);
2015 if (win32_channel->debug)
2016 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2018 channel->funcs = &win32_channel_sock_funcs;
2019 win32_channel->type = G_IO_WIN32_SOCKET;
2020 win32_channel->fd = socket;
2022 channel->is_readable = TRUE;
2023 channel->is_writeable = TRUE;
2024 channel->is_seekable = FALSE;
2030 g_io_channel_unix_new (gint fd)
2032 gboolean is_fd, is_socket;
2036 is_fd = (fstat (fd, &st) == 0);
2038 optlen = sizeof (optval);
2039 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2041 if (is_fd && is_socket)
2042 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);
2045 return g_io_channel_win32_new_fd_internal (fd, &st);
2048 return g_io_channel_win32_new_socket(fd);
2050 g_warning ("g_io_channel_unix_new: %d is neither a file descriptor or a socket.", fd);
2056 g_io_channel_unix_get_fd (GIOChannel *channel)
2058 return g_io_channel_win32_get_fd (channel);
2062 g_io_channel_win32_set_debug (GIOChannel *channel,
2065 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2067 win32_channel->debug = flag;
2071 g_io_channel_win32_poll (GPollFD *fds,
2077 g_return_val_if_fail (n_fds >= 0, 0);
2079 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2085 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2086 GIOCondition condition,
2089 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2091 switch (win32_channel->type)
2093 case G_IO_WIN32_FILE_DESC:
2094 if (win32_channel->data_avail_event == NULL)
2095 create_events (win32_channel);
2097 fd->fd = (gintptr) win32_channel->data_avail_event;
2099 if (win32_channel->thread_id == 0)
2101 /* Is it meaningful for a file descriptor to be polled for
2102 * both IN and OUT? For what kind of file descriptor would
2103 * that be? Doesn't seem to make sense, in practise the file
2104 * descriptors handled here are always read or write ends of
2105 * pipes surely, and thus unidirectional.
2107 if (condition & G_IO_IN)
2108 create_thread (win32_channel, condition, read_thread);
2109 else if (condition & G_IO_OUT)
2110 create_thread (win32_channel, condition, write_thread);
2114 case G_IO_WIN32_CONSOLE:
2115 fd->fd = _get_osfhandle (win32_channel->fd);
2118 case G_IO_WIN32_SOCKET:
2119 fd->fd = (gintptr) WSACreateEvent ();
2122 case G_IO_WIN32_WINDOWS_MESSAGES:
2123 fd->fd = G_WIN32_MSG_HANDLE;
2127 g_assert_not_reached ();
2131 fd->events = condition;
2134 /* Binary compatibility */
2136 g_io_channel_win32_new_stream_socket (int socket)
2138 return g_io_channel_win32_new_socket (socket);
2141 #define __G_IO_WIN32_C__
2142 #include "galiasdef.c"