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 */
96 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
97 * _open() or _pipe(), except for console IO.
98 * Have to create separate thread to read.
100 G_IO_WIN32_CONSOLE, /* Console IO (usually stdin, stdout, stderr) */
101 G_IO_WIN32_SOCKET /* Sockets. No separate thread */
102 } GIOWin32ChannelType;
104 struct _GIOWin32Channel {
106 gint fd; /* Either a Unix-like file handle as provided
107 * by the Microsoft C runtime, or a SOCKET
108 * as provided by WinSock.
110 GIOWin32ChannelType type;
114 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
115 HWND hwnd; /* handle of window, or NULL */
117 /* Following fields are used by fd channels. */
118 CRITICAL_SECTION mutex;
120 int direction; /* 0 means we read from it,
121 * 1 means we write to it.
124 gboolean running; /* Is reader thread running. FALSE if
125 * EOF has been reached.
127 gboolean needs_close; /* If the channel has been closed while
128 * the reader thread was still running.
130 guint thread_id; /* If non-NULL has a reader thread, or has
132 HANDLE data_avail_event;
136 /* Following fields used by fd channels for input */
138 /* Data is kept in a circular buffer. To be able to distinguish between
139 * empty and full buffer, we cannot fill it completely, but have to
140 * leave a one character gap.
142 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
145 * Full: (wrp + 1) % BUFFER_SIZE == rdp
148 guchar *buffer; /* (Circular) buffer */
149 gint wrp, rdp; /* Buffer indices for writing and reading */
150 HANDLE space_avail_event;
152 /* Following fields used by socket channels */
156 gboolean write_would_have_blocked;
157 gboolean ever_writable;
160 #define LOCK(mutex) EnterCriticalSection (&mutex)
161 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
163 struct _GIOWin32Watch {
167 GIOCondition condition;
171 g_win32_print_access_mode (int flags)
173 g_print ("%s%s%s%s%s%s%s%s%s%s",
174 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
175 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
176 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
177 (flags & _O_APPEND ? "|O_APPEND" : ""),
178 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
179 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
180 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
181 (flags & _O_CREAT ? "|O_CREAT" : ""),
182 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
183 (flags & _O_EXCL ? "|O_EXCL" : ""),
184 (flags & _O_TEXT ? "|O_TEXT" : ""),
185 (flags & _O_BINARY ? "|O_BINARY" : ""));
189 g_win32_print_gioflags (GIOFlags flags)
193 if (flags & G_IO_FLAG_APPEND)
194 bar = "|", g_print ("APPEND");
195 if (flags & G_IO_FLAG_NONBLOCK)
196 g_print ("%sNONBLOCK", bar), bar = "|";
197 if (flags & G_IO_FLAG_IS_READABLE)
198 g_print ("%sREADABLE", bar), bar = "|";
199 if (flags & G_IO_FLAG_IS_WRITEABLE)
200 g_print ("%sWRITEABLE", bar), bar = "|";
201 if (flags & G_IO_FLAG_IS_SEEKABLE)
202 g_print ("%sSEEKABLE", bar), bar = "|";
206 event_mask_to_string (int mask)
209 int checked_bits = 0;
215 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
225 BIT (ROUTING_INTERFACE_CHANGE);
226 BIT (ADDRESS_LIST_CHANGE);
230 if ((mask & ~checked_bits) != 0)
231 bufp += sprintf (bufp, "|%#x", mask & ~checked_bits);
233 return g_quark_to_string (g_quark_from_string (buf));
237 condition_to_string (GIOCondition condition)
240 int checked_bits = 0;
246 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
257 if ((condition & ~checked_bits) != 0)
258 bufp += sprintf (bufp, "|%#x", condition & ~checked_bits);
260 return g_quark_to_string (g_quark_from_string (buf));
264 g_io_win32_get_debug_flag (void)
266 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
270 g_io_channel_win32_init (GIOWin32Channel *channel)
272 channel->debug = g_io_win32_get_debug_flag ();
273 channel->buffer = NULL;
274 channel->running = FALSE;
275 channel->needs_close = FALSE;
276 channel->thread_id = 0;
277 channel->data_avail_event = NULL;
278 channel->revents = 0;
279 channel->space_avail_event = NULL;
280 channel->event_mask = 0;
281 channel->last_events = 0;
282 channel->event = NULL;
283 channel->write_would_have_blocked = FALSE;
284 channel->ever_writable = FALSE;
285 InitializeCriticalSection (&channel->mutex);
289 create_events (GIOWin32Channel *channel)
291 SECURITY_ATTRIBUTES sec_attrs;
293 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
294 sec_attrs.lpSecurityDescriptor = NULL;
295 sec_attrs.bInheritHandle = FALSE;
297 /* The data available event is manual reset, the space available event
298 * is automatic reset.
300 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
301 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
303 gchar *emsg = g_win32_error_message (GetLastError ());
304 g_error ("Error creating event: %s", emsg);
309 static unsigned __stdcall
310 read_thread (void *parameter)
312 GIOWin32Channel *channel = parameter;
316 g_io_channel_ref ((GIOChannel *)channel);
319 g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
322 channel->data_avail_event,
323 channel->space_avail_event);
325 channel->direction = 0;
326 channel->buffer = g_malloc (BUFFER_SIZE);
327 channel->rdp = channel->wrp = 0;
328 channel->running = TRUE;
330 SetEvent (channel->space_avail_event);
332 LOCK (channel->mutex);
333 while (channel->running)
336 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
337 channel->thread_id, channel->rdp, channel->wrp);
338 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
342 g_print ("read_thread %#x: resetting space_avail\n",
344 ResetEvent (channel->space_avail_event);
346 g_print ("read_thread %#x: waiting for space\n",
348 UNLOCK (channel->mutex);
349 WaitForSingleObject (channel->space_avail_event, INFINITE);
350 LOCK (channel->mutex);
352 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
353 channel->thread_id, channel->rdp, channel->wrp);
356 buffer = channel->buffer + channel->wrp;
358 /* Always leave at least one byte unused gap to be able to
359 * distinguish between the full and empty condition...
361 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
362 BUFFER_SIZE - channel->wrp);
365 g_print ("read_thread %#x: calling read() for %d bytes\n",
366 channel->thread_id, nbytes);
368 UNLOCK (channel->mutex);
370 nbytes = read (channel->fd, buffer, nbytes);
372 LOCK (channel->mutex);
374 channel->revents = G_IO_IN;
376 channel->revents |= G_IO_HUP;
378 channel->revents |= G_IO_ERR;
381 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
382 channel->thread_id, nbytes, channel->rdp, channel->wrp);
387 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
389 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
390 channel->thread_id, channel->rdp, channel->wrp);
391 SetEvent (channel->data_avail_event);
394 channel->running = FALSE;
395 if (channel->needs_close)
398 g_print ("read_thread %#x: channel fd %d needs closing\n",
399 channel->thread_id, channel->fd);
405 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
406 channel->thread_id, channel->rdp, channel->wrp);
407 SetEvent (channel->data_avail_event);
408 UNLOCK (channel->mutex);
410 g_io_channel_unref ((GIOChannel *)channel);
412 /* No need to call _endthreadex(), the actual thread starter routine
413 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
414 * _endthreadex() for us.
420 static unsigned __stdcall
421 write_thread (void *parameter)
423 GIOWin32Channel *channel = parameter;
427 g_io_channel_ref ((GIOChannel *)channel);
430 g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
433 channel->data_avail_event,
434 channel->space_avail_event);
436 channel->direction = 1;
437 channel->buffer = g_malloc (BUFFER_SIZE);
438 channel->rdp = channel->wrp = 0;
439 channel->running = TRUE;
441 SetEvent (channel->space_avail_event);
443 /* We use the same event objects as for a reader thread, but with
444 * reversed meaning. So, space_avail is used if data is available
445 * for writing, and data_avail is used if space is available in the
449 LOCK (channel->mutex);
450 while (channel->running || channel->rdp != channel->wrp)
453 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
454 channel->thread_id, channel->rdp, channel->wrp);
455 if (channel->wrp == channel->rdp)
457 /* Buffer is empty. */
459 g_print ("write_thread %#x: resetting space_avail\n",
461 ResetEvent (channel->space_avail_event);
463 g_print ("write_thread %#x: waiting for data\n",
465 channel->revents = G_IO_OUT;
466 SetEvent (channel->data_avail_event);
467 UNLOCK (channel->mutex);
468 WaitForSingleObject (channel->space_avail_event, INFINITE);
470 LOCK (channel->mutex);
471 if (channel->rdp == channel->wrp)
475 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
476 channel->thread_id, channel->rdp, channel->wrp);
479 buffer = channel->buffer + channel->rdp;
480 if (channel->rdp < channel->wrp)
481 nbytes = channel->wrp - channel->rdp;
483 nbytes = BUFFER_SIZE - channel->rdp;
486 g_print ("write_thread %#x: calling write() for %d bytes\n",
487 channel->thread_id, nbytes);
489 UNLOCK (channel->mutex);
490 nbytes = write (channel->fd, buffer, nbytes);
491 LOCK (channel->mutex);
494 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
495 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
497 channel->revents = 0;
499 channel->revents |= G_IO_OUT;
500 else if (nbytes <= 0)
501 channel->revents |= G_IO_ERR;
503 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
509 g_print ("write_thread: setting data_avail for thread %#x\n",
511 SetEvent (channel->data_avail_event);
514 channel->running = FALSE;
515 if (channel->needs_close)
518 g_print ("write_thread %#x: channel fd %d needs closing\n",
519 channel->thread_id, channel->fd);
524 UNLOCK (channel->mutex);
526 g_io_channel_unref ((GIOChannel *)channel);
532 create_thread (GIOWin32Channel *channel,
533 GIOCondition condition,
534 unsigned (__stdcall *thread) (void *parameter))
536 HANDLE thread_handle;
538 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
539 &channel->thread_id);
540 if (thread_handle == 0)
541 g_warning (G_STRLOC ": Error creating reader thread: %s",
543 else if (!CloseHandle (thread_handle))
544 g_warning (G_STRLOC ": Error closing thread handle: %s\n",
545 g_win32_error_message (GetLastError ()));
547 WaitForSingleObject (channel->space_avail_event, INFINITE);
551 buffer_read (GIOWin32Channel *channel,
560 LOCK (channel->mutex);
562 g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
563 channel->thread_id, count, channel->rdp, channel->wrp);
565 if (channel->wrp == channel->rdp)
567 UNLOCK (channel->mutex);
569 g_print ("waiting for data from thread %#x\n", channel->thread_id);
570 WaitForSingleObject (channel->data_avail_event, INFINITE);
572 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
573 LOCK (channel->mutex);
574 if (channel->wrp == channel->rdp && !channel->running)
577 g_print ("wrp==rdp, !running\n");
578 UNLOCK (channel->mutex);
580 return G_IO_STATUS_EOF;
584 if (channel->rdp < channel->wrp)
585 nbytes = channel->wrp - channel->rdp;
587 nbytes = BUFFER_SIZE - channel->rdp;
588 UNLOCK (channel->mutex);
589 nbytes = MIN (left, nbytes);
591 g_print ("moving %d bytes from thread %#x\n",
592 nbytes, channel->thread_id);
593 memcpy (dest, channel->buffer + channel->rdp, nbytes);
596 LOCK (channel->mutex);
597 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
599 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
600 SetEvent (channel->space_avail_event);
602 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
603 channel->thread_id, channel->rdp, channel->wrp);
604 if (channel->running && channel->wrp == channel->rdp)
607 g_print ("resetting data_avail of thread %#x\n",
609 ResetEvent (channel->data_avail_event);
611 UNLOCK (channel->mutex);
613 /* We have no way to indicate any errors form the actual
614 * read() or recv() call in the reader thread. Should we have?
616 *bytes_read = count - left;
617 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
622 buffer_write (GIOWin32Channel *channel,
625 gsize *bytes_written,
631 LOCK (channel->mutex);
633 g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
634 channel->thread_id, count, channel->rdp, channel->wrp);
636 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
640 g_print ("buffer_write: tid %#x: resetting data_avail\n",
642 ResetEvent (channel->data_avail_event);
644 g_print ("buffer_write: tid %#x: waiting for space\n",
646 UNLOCK (channel->mutex);
647 WaitForSingleObject (channel->data_avail_event, INFINITE);
648 LOCK (channel->mutex);
650 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
651 channel->thread_id, channel->rdp, channel->wrp);
654 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
655 BUFFER_SIZE - channel->wrp);
657 UNLOCK (channel->mutex);
658 nbytes = MIN (left, nbytes);
660 g_print ("buffer_write: tid %#x: writing %d bytes\n",
661 channel->thread_id, nbytes);
662 memcpy (channel->buffer + channel->wrp, dest, nbytes);
665 LOCK (channel->mutex);
667 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
669 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
670 channel->thread_id, channel->rdp, channel->wrp);
671 SetEvent (channel->space_avail_event);
673 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
677 g_print ("buffer_write: tid %#x: resetting data_avail\n",
679 ResetEvent (channel->data_avail_event);
682 UNLOCK (channel->mutex);
684 /* We have no way to indicate any errors form the actual
685 * write() call in the writer thread. Should we have?
687 *bytes_written = count - left;
688 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
693 g_io_win32_prepare (GSource *source,
696 GIOWin32Watch *watch = (GIOWin32Watch *)source;
697 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
698 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
704 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
706 switch (channel->type)
708 case G_IO_WIN32_WINDOWS_MESSAGES:
713 case G_IO_WIN32_CONSOLE:
718 case G_IO_WIN32_FILE_DESC:
720 g_print (" FD thread=%#x buffer_condition:{%s}"
721 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
722 channel->thread_id, condition_to_string (buffer_condition),
723 condition_to_string (watch->pollfd.events),
724 condition_to_string (watch->pollfd.revents),
725 condition_to_string (channel->revents));
727 LOCK (channel->mutex);
728 if (channel->running)
730 if (channel->direction == 0 && channel->wrp == channel->rdp)
733 g_print ("\n setting revents=0");
734 channel->revents = 0;
739 if (channel->direction == 1
740 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
743 g_print ("\n setting revents=0");
744 channel->revents = 0;
747 UNLOCK (channel->mutex);
750 case G_IO_WIN32_SOCKET:
754 if (watch->condition & G_IO_IN)
755 event_mask |= (FD_READ | FD_ACCEPT);
756 if (watch->condition & G_IO_OUT)
757 event_mask |= (FD_WRITE | FD_CONNECT);
758 event_mask |= FD_CLOSE;
760 if (channel->event_mask != event_mask /* || channel->event != watch->pollfd.fd*/)
763 g_print ("\n WSAEventSelect(%d,%p,{%s})",
764 channel->fd, (HANDLE) watch->pollfd.fd,
765 event_mask_to_string (event_mask));
766 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
767 event_mask) == SOCKET_ERROR)
769 channel->event_mask = event_mask;
771 channel->event = watch->pollfd.fd;
774 g_print ("\n setting last_events=0");
775 channel->last_events = 0;
777 if ((event_mask & FD_WRITE) &&
778 channel->ever_writable &&
779 !channel->write_would_have_blocked)
782 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
783 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
789 g_assert_not_reached ();
795 return ((watch->condition & buffer_condition) == watch->condition);
799 g_io_win32_check (GSource *source)
802 GIOWin32Watch *watch = (GIOWin32Watch *)source;
803 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
804 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
805 WSANETWORKEVENTS events;
808 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
810 switch (channel->type)
812 case G_IO_WIN32_WINDOWS_MESSAGES:
815 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
817 case G_IO_WIN32_FILE_DESC:
819 g_print (" FD thread=%#x buffer_condition=%s\n"
820 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
821 channel->thread_id, condition_to_string (buffer_condition),
822 condition_to_string (watch->pollfd.events),
823 condition_to_string (watch->pollfd.revents),
824 condition_to_string (channel->revents));
826 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
828 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
830 case G_IO_WIN32_CONSOLE:
833 if (watch->channel->is_writeable)
835 else if (watch->channel->is_readable)
839 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
842 /* _kbhit() does quite complex processing to find out
843 * whether at least one of the key events pending corresponds
844 * to a "real" character that can be read.
849 /* Discard all other kinds of events */
850 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
855 case G_IO_WIN32_SOCKET:
858 if (channel->last_events & FD_WRITE)
861 g_print (" sock=%d event=%p last_events has FD_WRITE",
862 channel->fd, (HANDLE) watch->pollfd.fd);
866 WSAEnumNetworkEvents (channel->fd, 0, &events);
869 g_print ("\n revents={%s} condition={%s}"
870 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
871 condition_to_string (watch->pollfd.revents),
872 condition_to_string (watch->condition),
874 event_mask_to_string (events.lNetworkEvents));
876 if (watch->pollfd.revents != 0 &&
877 events.lNetworkEvents == 0 &&
878 !(channel->event_mask & FD_WRITE))
880 channel->event_mask = 0;
882 g_print ("\n WSAEventSelect(%d,%p,{})",
883 channel->fd, (HANDLE) watch->pollfd.fd);
884 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
886 g_print (" ResetEvent(%p)",
887 (HANDLE) watch->pollfd.fd);
888 ResetEvent ((HANDLE) watch->pollfd.fd);
890 else if (events.lNetworkEvents & FD_WRITE)
891 channel->ever_writable = TRUE;
892 channel->last_events = events.lNetworkEvents;
895 watch->pollfd.revents = 0;
896 if (channel->last_events & (FD_READ | FD_ACCEPT))
897 watch->pollfd.revents |= G_IO_IN;
899 if (channel->last_events & FD_WRITE)
900 watch->pollfd.revents |= G_IO_OUT;
903 /* We have called WSAEnumNetworkEvents() above but it didn't
906 if (events.lNetworkEvents & FD_CONNECT)
908 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
909 watch->pollfd.revents |= G_IO_OUT;
911 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
913 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
914 watch->pollfd.revents |= G_IO_HUP;
917 /* Regardless of WSAEnumNetworkEvents() result, if watching for
918 * writability, and if we have ever got a FD_WRITE event, and
919 * unless last write would have blocked, set G_IO_OUT. But never
920 * set both G_IO_OUT and G_IO_HUP.
922 if (!(watch->pollfd.revents & G_IO_HUP) &&
923 channel->ever_writable &&
924 !channel->write_would_have_blocked &&
925 (channel->event_mask & FD_WRITE))
926 watch->pollfd.revents |= G_IO_OUT;
929 g_print ("\n revents={%s} retval={%s}\n",
930 condition_to_string (watch->pollfd.revents),
931 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
933 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
936 g_assert_not_reached ();
942 g_io_win32_dispatch (GSource *source,
943 GSourceFunc callback,
946 GIOFunc func = (GIOFunc)callback;
947 GIOWin32Watch *watch = (GIOWin32Watch *)source;
948 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
949 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
953 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
954 "You must call g_source_connect().");
959 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
960 condition_to_string (watch->pollfd.revents),
961 condition_to_string (watch->condition),
962 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
964 return (*func) (watch->channel,
965 (watch->pollfd.revents | buffer_condition) & watch->condition,
970 g_io_win32_finalize (GSource *source)
972 GIOWin32Watch *watch = (GIOWin32Watch *)source;
973 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
976 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
978 switch (channel->type)
980 case G_IO_WIN32_WINDOWS_MESSAGES:
985 case G_IO_WIN32_CONSOLE:
990 case G_IO_WIN32_FILE_DESC:
992 g_print (" FD thread=%#x", channel->thread_id);
995 case G_IO_WIN32_SOCKET:
997 g_print (" SOCK sock=%d", channel->fd);
999 CloseHandle ((HANDLE) watch->pollfd.fd);
1001 channel->event_mask = 0;
1006 g_assert_not_reached ();
1011 g_io_channel_unref (watch->channel);
1014 GSourceFuncs g_io_watch_funcs = {
1017 g_io_win32_dispatch,
1022 g_io_win32_msg_read (GIOChannel *channel,
1028 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1029 MSG msg; /* In case of alignment problems */
1031 if (count < sizeof (MSG))
1033 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1034 "Incorrect message size"); /* Informative enough error message? */
1035 return G_IO_STATUS_ERROR;
1038 if (win32_channel->debug)
1039 g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1040 channel, win32_channel->hwnd);
1041 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1042 return G_IO_STATUS_AGAIN;
1044 memmove (buf, &msg, sizeof (MSG));
1045 *bytes_read = sizeof (MSG);
1047 return G_IO_STATUS_NORMAL;
1051 g_io_win32_msg_write (GIOChannel *channel,
1054 gsize *bytes_written,
1057 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1060 if (count != sizeof (MSG))
1062 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1063 "Incorrect message size"); /* Informative enough error message? */
1064 return G_IO_STATUS_ERROR;
1067 /* In case of alignment problems */
1068 memmove (&msg, buf, sizeof (MSG));
1069 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1071 gchar *emsg = g_win32_error_message (GetLastError ());
1072 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1074 return G_IO_STATUS_ERROR;
1077 *bytes_written = sizeof (MSG);
1079 return G_IO_STATUS_NORMAL;
1083 g_io_win32_msg_close (GIOChannel *channel,
1086 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1088 return G_IO_STATUS_NORMAL;
1092 g_io_win32_free (GIOChannel *channel)
1094 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1096 if (win32_channel->debug)
1097 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1099 if (win32_channel->data_avail_event)
1100 CloseHandle (win32_channel->data_avail_event);
1101 if (win32_channel->space_avail_event)
1102 CloseHandle (win32_channel->space_avail_event);
1103 if (win32_channel->type == G_IO_WIN32_SOCKET)
1104 WSAEventSelect (win32_channel->fd, NULL, 0);
1105 DeleteCriticalSection (&win32_channel->mutex);
1107 g_free (win32_channel->buffer);
1108 g_free (win32_channel);
1112 g_io_win32_msg_create_watch (GIOChannel *channel,
1113 GIOCondition condition)
1115 GIOWin32Watch *watch;
1118 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1119 watch = (GIOWin32Watch *)source;
1121 watch->channel = channel;
1122 g_io_channel_ref (channel);
1124 watch->condition = condition;
1126 watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1127 watch->pollfd.events = condition;
1129 g_source_add_poll (source, &watch->pollfd);
1135 g_io_win32_fd_and_console_read (GIOChannel *channel,
1141 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1144 if (win32_channel->debug)
1145 g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1146 win32_channel->fd, count);
1148 if (win32_channel->thread_id)
1150 return buffer_read (win32_channel, buf, count, bytes_read, err);
1153 result = read (win32_channel->fd, buf, count);
1155 if (win32_channel->debug)
1156 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1166 return G_IO_STATUS_AGAIN;
1169 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1170 g_io_channel_error_from_errno (errno),
1171 g_strerror (errno));
1172 return G_IO_STATUS_ERROR;
1176 *bytes_read = result;
1178 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1182 g_io_win32_fd_and_console_write (GIOChannel *channel,
1185 gsize *bytes_written,
1188 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1191 if (win32_channel->thread_id)
1193 return buffer_write (win32_channel, buf, count, bytes_written, err);
1196 result = write (win32_channel->fd, buf, count);
1197 if (win32_channel->debug)
1198 g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1199 win32_channel->fd, count, result);
1209 return G_IO_STATUS_AGAIN;
1212 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1213 g_io_channel_error_from_errno (errno),
1214 g_strerror (errno));
1215 return G_IO_STATUS_ERROR;
1219 *bytes_written = result;
1221 return G_IO_STATUS_NORMAL;
1225 g_io_win32_fd_seek (GIOChannel *channel,
1230 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1247 whence = -1; /* Keep the compiler quiet */
1248 g_assert_not_reached ();
1252 tmp_offset = offset;
1253 if (tmp_offset != offset)
1255 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1256 g_io_channel_error_from_errno (EINVAL),
1257 g_strerror (EINVAL));
1258 return G_IO_STATUS_ERROR;
1261 result = lseek (win32_channel->fd, tmp_offset, whence);
1265 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1266 g_io_channel_error_from_errno (errno),
1267 g_strerror (errno));
1268 return G_IO_STATUS_ERROR;
1271 return G_IO_STATUS_NORMAL;
1275 g_io_win32_fd_close (GIOChannel *channel,
1278 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1280 if (win32_channel->debug)
1281 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1282 win32_channel->thread_id,
1284 LOCK (win32_channel->mutex);
1285 if (win32_channel->running)
1287 if (win32_channel->debug)
1288 g_print ("thread %#x: running, marking fd %d for later close\n",
1289 win32_channel->thread_id, win32_channel->fd);
1290 win32_channel->running = FALSE;
1291 win32_channel->needs_close = TRUE;
1292 if (win32_channel->direction == 0)
1293 SetEvent (win32_channel->data_avail_event);
1295 SetEvent (win32_channel->space_avail_event);
1299 if (win32_channel->debug)
1300 g_print ("closing fd %d\n", win32_channel->fd);
1301 close (win32_channel->fd);
1302 if (win32_channel->debug)
1303 g_print ("closed fd %d, setting to -1\n",
1305 win32_channel->fd = -1;
1307 UNLOCK (win32_channel->mutex);
1309 /* FIXME error detection? */
1311 return G_IO_STATUS_NORMAL;
1315 g_io_win32_fd_create_watch (GIOChannel *channel,
1316 GIOCondition condition)
1318 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1319 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1320 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1322 watch->channel = channel;
1323 g_io_channel_ref (channel);
1325 watch->condition = condition;
1327 if (win32_channel->data_avail_event == NULL)
1328 create_events (win32_channel);
1330 watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1331 watch->pollfd.events = condition;
1333 if (win32_channel->debug)
1334 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1335 channel, win32_channel->fd,
1336 condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1338 LOCK (win32_channel->mutex);
1339 if (win32_channel->thread_id == 0)
1341 if (condition & G_IO_IN)
1342 create_thread (win32_channel, condition, read_thread);
1343 else if (condition & G_IO_OUT)
1344 create_thread (win32_channel, condition, write_thread);
1347 g_source_add_poll (source, &watch->pollfd);
1348 UNLOCK (win32_channel->mutex);
1354 g_io_win32_console_close (GIOChannel *channel,
1357 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1359 if (close (win32_channel->fd) < 0)
1361 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1362 g_io_channel_error_from_errno (errno),
1363 g_strerror (errno));
1364 return G_IO_STATUS_ERROR;
1367 return G_IO_STATUS_NORMAL;
1371 g_io_win32_console_create_watch (GIOChannel *channel,
1372 GIOCondition condition)
1374 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1375 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1376 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1378 watch->channel = channel;
1379 g_io_channel_ref (channel);
1381 watch->condition = condition;
1383 watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1384 watch->pollfd.events = condition;
1386 g_source_add_poll (source, &watch->pollfd);
1392 g_io_win32_sock_read (GIOChannel *channel,
1398 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1400 GIOChannelError error;
1403 if (win32_channel->debug)
1404 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1405 channel, win32_channel->fd, count);
1407 result = recv (win32_channel->fd, buf, count, 0);
1408 if (result == SOCKET_ERROR)
1409 winsock_error = WSAGetLastError ();
1411 if (win32_channel->debug)
1412 g_print (" recv=%d", result);
1414 if (result == SOCKET_ERROR)
1416 gchar *emsg = g_win32_error_message (winsock_error);
1418 if (win32_channel->debug)
1419 g_print (" %s\n", emsg);
1423 switch (winsock_error)
1426 error = G_IO_CHANNEL_ERROR_INVAL;
1428 case WSAEWOULDBLOCK:
1430 return G_IO_STATUS_AGAIN;
1432 error = G_IO_CHANNEL_ERROR_FAILED;
1435 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1438 return G_IO_STATUS_ERROR;
1442 if (win32_channel->debug)
1444 *bytes_read = result;
1446 return G_IO_STATUS_EOF;
1448 return G_IO_STATUS_NORMAL;
1453 g_io_win32_sock_write (GIOChannel *channel,
1456 gsize *bytes_written,
1459 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1461 GIOChannelError error;
1464 if (win32_channel->debug)
1465 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1466 channel, win32_channel->fd, count);
1468 result = send (win32_channel->fd, buf, count, 0);
1469 if (result == SOCKET_ERROR)
1470 winsock_error = WSAGetLastError ();
1472 if (win32_channel->debug)
1473 g_print (" send=%d", result);
1475 if (result == SOCKET_ERROR)
1477 gchar *emsg = g_win32_error_message (winsock_error);
1479 if (win32_channel->debug)
1480 g_print (" %s\n", emsg);
1484 switch (winsock_error)
1487 error = G_IO_CHANNEL_ERROR_INVAL;
1489 case WSAEWOULDBLOCK:
1490 win32_channel->write_would_have_blocked = TRUE;
1491 win32_channel->last_events = 0;
1493 return G_IO_STATUS_AGAIN;
1495 error = G_IO_CHANNEL_ERROR_FAILED;
1498 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1501 return G_IO_STATUS_ERROR;
1505 if (win32_channel->debug)
1507 *bytes_written = result;
1508 win32_channel->write_would_have_blocked = FALSE;
1510 return G_IO_STATUS_NORMAL;
1515 g_io_win32_sock_close (GIOChannel *channel,
1518 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1520 if (win32_channel->fd != -1)
1522 if (win32_channel->debug)
1523 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1524 channel, win32_channel->fd);
1526 closesocket (win32_channel->fd);
1527 win32_channel->fd = -1;
1530 /* FIXME error detection? */
1532 return G_IO_STATUS_NORMAL;
1536 g_io_win32_sock_create_watch (GIOChannel *channel,
1537 GIOCondition condition)
1539 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1540 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1541 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1543 watch->channel = channel;
1544 g_io_channel_ref (channel);
1546 watch->condition = condition;
1548 if (win32_channel->event == 0)
1549 win32_channel->event = WSACreateEvent ();
1551 watch->pollfd.fd = (gintptr) win32_channel->event;
1552 watch->pollfd.events = condition;
1554 if (win32_channel->debug)
1555 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1556 channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1557 condition_to_string (watch->condition));
1559 g_source_add_poll (source, &watch->pollfd);
1565 g_io_channel_new_file (const gchar *filename,
1569 int fid, flags, pmode;
1570 GIOChannel *channel;
1572 enum { /* Cheesy hack */
1579 g_return_val_if_fail (filename != NULL, NULL);
1580 g_return_val_if_fail (mode != NULL, NULL);
1581 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1595 g_warning ("Invalid GIOFileMode %s.\n", mode);
1604 if (mode[2] == '\0')
1606 mode_num |= MODE_PLUS;
1611 g_warning ("Invalid GIOFileMode %s.\n", mode);
1622 flags = O_WRONLY | O_TRUNC | O_CREAT;
1626 flags = O_WRONLY | O_APPEND | O_CREAT;
1629 case MODE_R | MODE_PLUS:
1631 pmode = _S_IREAD | _S_IWRITE;
1633 case MODE_W | MODE_PLUS:
1634 flags = O_RDWR | O_TRUNC | O_CREAT;
1635 pmode = _S_IREAD | _S_IWRITE;
1637 case MODE_A | MODE_PLUS:
1638 flags = O_RDWR | O_APPEND | O_CREAT;
1639 pmode = _S_IREAD | _S_IWRITE;
1642 g_assert_not_reached ();
1646 /* always open 'untranslated' */
1647 fid = g_open (filename, flags | _O_BINARY, pmode);
1649 if (g_io_win32_get_debug_flag ())
1651 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1652 g_win32_print_access_mode (flags|_O_BINARY);
1653 g_print (",%#o)=%d\n", pmode, fid);
1658 g_set_error_literal (error, G_FILE_ERROR,
1659 g_file_error_from_errno (errno),
1660 g_strerror (errno));
1661 return (GIOChannel *)NULL;
1664 channel = g_io_channel_win32_new_fd (fid);
1666 /* XXX: move this to g_io_channel_win32_new_fd () */
1667 channel->close_on_unref = TRUE;
1668 channel->is_seekable = TRUE;
1670 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1671 * correspond to actual readability/writeability. Set to FALSE those
1672 * that mode doesn't allow
1677 channel->is_writeable = FALSE;
1681 channel->is_readable = FALSE;
1683 case MODE_R | MODE_PLUS:
1684 case MODE_W | MODE_PLUS:
1685 case MODE_A | MODE_PLUS:
1688 g_assert_not_reached ();
1695 #if !defined (_WIN64)
1697 #undef g_io_channel_new_file
1699 /* Binary compatibility version. Not for newly compiled code. */
1702 g_io_channel_new_file (const gchar *filename,
1706 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
1709 if (utf8_filename == NULL)
1712 retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
1714 g_free (utf8_filename);
1722 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1726 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1728 if (win32_channel->debug)
1730 g_print ("g_io_win32_unimpl_set_flags: ");
1731 g_win32_print_gioflags (flags);
1735 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1736 G_IO_CHANNEL_ERROR_FAILED,
1737 "Not implemented on Win32");
1739 return G_IO_STATUS_ERROR;
1743 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1746 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1750 if (st->st_mode & _S_IFIFO)
1752 channel->is_readable =
1753 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1754 channel->is_writeable =
1755 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1756 channel->is_seekable = FALSE;
1760 channel->is_readable =
1761 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1762 channel->is_writeable =
1763 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1764 channel->is_seekable = TRUE;
1767 /* XXX: G_IO_FLAG_APPEND */
1768 /* XXX: G_IO_FLAG_NONBLOCK */
1774 g_io_win32_fd_get_flags (GIOChannel *channel)
1777 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1779 g_return_val_if_fail (win32_channel != NULL, 0);
1780 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1782 if (0 == fstat (win32_channel->fd, &st))
1783 return g_io_win32_fd_get_flags_internal (channel, &st);
1789 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1791 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1792 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1795 INPUT_RECORD record;
1797 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1798 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1799 channel->is_seekable = FALSE;
1805 g_io_win32_console_get_flags (GIOChannel *channel)
1807 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1809 g_return_val_if_fail (win32_channel != NULL, 0);
1810 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1812 return g_io_win32_console_get_flags_internal (channel);
1816 g_io_win32_msg_get_flags (GIOChannel *channel)
1822 g_io_win32_sock_set_flags (GIOChannel *channel,
1826 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1829 if (win32_channel->debug)
1831 g_print ("g_io_win32_sock_set_flags: ");
1832 g_win32_print_gioflags (flags);
1836 if (flags & G_IO_FLAG_NONBLOCK)
1839 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1841 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1843 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1844 G_IO_CHANNEL_ERROR_FAILED,
1848 return G_IO_STATUS_ERROR;
1854 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1856 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1858 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1859 G_IO_CHANNEL_ERROR_FAILED,
1863 return G_IO_STATUS_ERROR;
1867 return G_IO_STATUS_NORMAL;
1871 g_io_win32_sock_get_flags (GIOChannel *channel)
1873 /* Could we do something here? */
1877 static GIOFuncs win32_channel_msg_funcs = {
1878 g_io_win32_msg_read,
1879 g_io_win32_msg_write,
1881 g_io_win32_msg_close,
1882 g_io_win32_msg_create_watch,
1884 g_io_win32_unimpl_set_flags,
1885 g_io_win32_msg_get_flags,
1888 static GIOFuncs win32_channel_fd_funcs = {
1889 g_io_win32_fd_and_console_read,
1890 g_io_win32_fd_and_console_write,
1892 g_io_win32_fd_close,
1893 g_io_win32_fd_create_watch,
1895 g_io_win32_unimpl_set_flags,
1896 g_io_win32_fd_get_flags,
1899 static GIOFuncs win32_channel_console_funcs = {
1900 g_io_win32_fd_and_console_read,
1901 g_io_win32_fd_and_console_write,
1903 g_io_win32_console_close,
1904 g_io_win32_console_create_watch,
1906 g_io_win32_unimpl_set_flags,
1907 g_io_win32_console_get_flags,
1910 static GIOFuncs win32_channel_sock_funcs = {
1911 g_io_win32_sock_read,
1912 g_io_win32_sock_write,
1914 g_io_win32_sock_close,
1915 g_io_win32_sock_create_watch,
1917 g_io_win32_sock_set_flags,
1918 g_io_win32_sock_get_flags,
1922 #if GLIB_SIZEOF_VOID_P == 8
1923 g_io_channel_win32_new_messages (gsize hwnd)
1925 g_io_channel_win32_new_messages (guint hwnd)
1928 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1929 GIOChannel *channel = (GIOChannel *)win32_channel;
1931 g_io_channel_init (channel);
1932 g_io_channel_win32_init (win32_channel);
1933 if (win32_channel->debug)
1934 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1935 channel, (HWND) hwnd);
1936 channel->funcs = &win32_channel_msg_funcs;
1937 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1938 win32_channel->hwnd = (HWND) hwnd;
1940 /* XXX: check this. */
1941 channel->is_readable = IsWindow (win32_channel->hwnd);
1942 channel->is_writeable = IsWindow (win32_channel->hwnd);
1944 channel->is_seekable = FALSE;
1950 g_io_channel_win32_new_fd_internal (gint fd,
1953 GIOWin32Channel *win32_channel;
1954 GIOChannel *channel;
1956 win32_channel = g_new (GIOWin32Channel, 1);
1957 channel = (GIOChannel *)win32_channel;
1959 g_io_channel_init (channel);
1960 g_io_channel_win32_init (win32_channel);
1962 win32_channel->fd = fd;
1964 if (win32_channel->debug)
1965 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
1968 if (st->st_mode & _S_IFCHR) /* console */
1970 channel->funcs = &win32_channel_console_funcs;
1971 win32_channel->type = G_IO_WIN32_CONSOLE;
1972 g_io_win32_console_get_flags_internal (channel);
1976 channel->funcs = &win32_channel_fd_funcs;
1977 win32_channel->type = G_IO_WIN32_FILE_DESC;
1978 g_io_win32_fd_get_flags_internal (channel, st);
1985 g_io_channel_win32_new_fd (gint fd)
1989 if (fstat (fd, &st) == -1)
1991 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1995 return g_io_channel_win32_new_fd_internal (fd, &st);
1999 g_io_channel_win32_get_fd (GIOChannel *channel)
2001 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2003 return win32_channel->fd;
2007 g_io_channel_win32_new_socket (int socket)
2009 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2010 GIOChannel *channel = (GIOChannel *)win32_channel;
2012 g_io_channel_init (channel);
2013 g_io_channel_win32_init (win32_channel);
2014 if (win32_channel->debug)
2015 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2017 channel->funcs = &win32_channel_sock_funcs;
2018 win32_channel->type = G_IO_WIN32_SOCKET;
2019 win32_channel->fd = socket;
2021 channel->is_readable = TRUE;
2022 channel->is_writeable = TRUE;
2023 channel->is_seekable = FALSE;
2029 g_io_channel_unix_new (gint fd)
2031 gboolean is_fd, is_socket;
2035 is_fd = (fstat (fd, &st) == 0);
2037 optlen = sizeof (optval);
2038 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2040 if (is_fd && is_socket)
2041 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
2044 return g_io_channel_win32_new_fd_internal (fd, &st);
2047 return g_io_channel_win32_new_socket(fd);
2049 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
2055 g_io_channel_unix_get_fd (GIOChannel *channel)
2057 return g_io_channel_win32_get_fd (channel);
2061 g_io_channel_win32_set_debug (GIOChannel *channel,
2064 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2066 win32_channel->debug = flag;
2070 g_io_channel_win32_poll (GPollFD *fds,
2076 g_return_val_if_fail (n_fds >= 0, 0);
2078 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
2084 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2085 GIOCondition condition,
2088 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2090 switch (win32_channel->type)
2092 case G_IO_WIN32_FILE_DESC:
2093 if (win32_channel->data_avail_event == NULL)
2094 create_events (win32_channel);
2096 fd->fd = (gintptr) win32_channel->data_avail_event;
2098 if (win32_channel->thread_id == 0)
2100 /* Is it meaningful for a file descriptor to be polled for
2101 * both IN and OUT? For what kind of file descriptor would
2102 * that be? Doesn't seem to make sense, in practise the file
2103 * descriptors handled here are always read or write ends of
2104 * pipes surely, and thus unidirectional.
2106 if (condition & G_IO_IN)
2107 create_thread (win32_channel, condition, read_thread);
2108 else if (condition & G_IO_OUT)
2109 create_thread (win32_channel, condition, write_thread);
2113 case G_IO_WIN32_CONSOLE:
2114 fd->fd = _get_osfhandle (win32_channel->fd);
2117 case G_IO_WIN32_SOCKET:
2118 fd->fd = (gintptr) WSACreateEvent ();
2121 case G_IO_WIN32_WINDOWS_MESSAGES:
2122 fd->fd = G_WIN32_MSG_HANDLE;
2126 g_assert_not_reached ();
2130 fd->events = condition;
2133 /* Binary compatibility */
2135 g_io_channel_win32_new_stream_socket (int socket)
2137 return g_io_channel_win32_new_socket (socket);
2140 #define __G_IO_WIN32_C__
2141 #include "galiasdef.c"