Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / giowin32.c
blob3e38e2a81756f5e7fca2a3254a84e41203ab9a9b
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.1 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, see <http://www.gnu.org/licenses/>.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 * Bugs that are related to the code in this file:
33 * Bug 137968 - Sometimes a GIOFunc on Win32 is called with zero condition
34 * http://bugzilla.gnome.org/show_bug.cgi?id=137968
36 * Bug 324234 - Using g_io_add_watch_full() to wait for connect() to return on a non-blocking socket returns prematurely
37 * http://bugzilla.gnome.org/show_bug.cgi?id=324234
39 * Bug 331214 - g_io_channel async socket io stalls
40 * http://bugzilla.gnome.org/show_bug.cgi?id=331214
42 * Bug 338943 - Multiple watches on the same socket
43 * http://bugzilla.gnome.org/show_bug.cgi?id=338943
45 * Bug 357674 - 2 serious bugs in giowin32.c making glib iochannels useless
46 * http://bugzilla.gnome.org/show_bug.cgi?id=357674
48 * Bug 425156 - GIOChannel deadlocks on a win32 socket
49 * http://bugzilla.gnome.org/show_bug.cgi?id=425156
51 * Bug 468910 - giofunc condition=0
52 * http://bugzilla.gnome.org/show_bug.cgi?id=468910
54 * Bug 500246 - Bug fixes for giowin32
55 * http://bugzilla.gnome.org/show_bug.cgi?id=500246
57 * Bug 548278 - Async GETs connections are always terminated unexpectedly on windows
58 * http://bugzilla.gnome.org/show_bug.cgi?id=548278
60 * Bug 548536 - giowin32 problem when adding and removing watches
61 * http://bugzilla.gnome.org/show_bug.cgi?id=548536
63 * When fixing bugs related to the code in this file, either the above
64 * bugs or others, make sure that the test programs attached to the
65 * above bugs continue to work.
68 #include "config.h"
70 #include "glib.h"
72 #include <stdlib.h>
73 #include <winsock2.h>
74 #include <windows.h>
75 #include <conio.h>
76 #include <fcntl.h>
77 #include <io.h>
78 #include <process.h>
79 #include <errno.h>
80 #include <sys/stat.h>
82 #include "gstdio.h"
83 #include "glibintl.h"
86 typedef struct _GIOWin32Channel GIOWin32Channel;
87 typedef struct _GIOWin32Watch GIOWin32Watch;
89 #define BUFFER_SIZE 4096
91 typedef enum {
92 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
94 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
95 * _open() or _pipe(), except for
96 * console IO. Separate thread to read
97 * or write.
100 G_IO_WIN32_CONSOLE, /* Console IO (usually stdin, stdout, stderr) */
102 G_IO_WIN32_SOCKET /* Sockets. No separate thread. */
103 } GIOWin32ChannelType;
105 struct _GIOWin32Channel {
106 GIOChannel channel;
107 gint fd; /* Either a Unix-like file handle as provided
108 * by the Microsoft C runtime, or a SOCKET
109 * as provided by WinSock.
111 GIOWin32ChannelType type;
113 gboolean debug;
115 /* Field used by G_IO_WIN32_WINDOWS_MESSAGES channels */
116 HWND hwnd; /* Handle of window, or NULL */
118 /* Fields used by G_IO_WIN32_FILE_DESC channels. */
119 CRITICAL_SECTION mutex;
121 int direction; /* 0 means we read from it,
122 * 1 means we write to it.
125 gboolean running; /* Is reader or writer thread
126 * running. FALSE if EOF has been
127 * reached by the reader thread.
130 gboolean needs_close; /* If the channel has been closed while
131 * the reader thread was still running.
134 guint thread_id; /* If non-NULL the channel has or has
135 * had a reader or writer thread.
137 HANDLE data_avail_event;
139 gushort revents;
141 /* Data is kept in a circular buffer. To be able to distinguish between
142 * empty and full buffers, we cannot fill it completely, but have to
143 * leave a one character gap.
145 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
147 * Empty: wrp == rdp
148 * Full: (wrp + 1) % BUFFER_SIZE == rdp
149 * Partial: otherwise
151 guchar *buffer; /* (Circular) buffer */
152 gint wrp, rdp; /* Buffer indices for writing and reading */
153 HANDLE space_avail_event;
155 /* Fields used by G_IO_WIN32_SOCKET channels */
156 int event_mask;
157 int last_events;
158 HANDLE event;
159 gboolean write_would_have_blocked;
160 gboolean ever_writable;
163 struct _GIOWin32Watch {
164 GSource source;
165 GPollFD pollfd;
166 GIOChannel *channel;
167 GIOCondition condition;
170 static void
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" : ""));
188 static void
189 g_win32_print_gioflags (GIOFlags flags)
191 char *bar = "";
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_WRITABLE)
200 g_print ("%sWRITABLE", bar), bar = "|";
201 if (flags & G_IO_FLAG_IS_SEEKABLE)
202 g_print ("%sSEEKABLE", bar), bar = "|";
205 static const char *
206 event_mask_to_string (int mask)
208 char buf[100];
209 int checked_bits = 0;
210 char *bufp = buf;
212 if (mask == 0)
213 return "";
215 #define BIT(n) checked_bits |= FD_##n; if (mask & FD_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
217 BIT (READ);
218 BIT (WRITE);
219 BIT (OOB);
220 BIT (ACCEPT);
221 BIT (CONNECT);
222 BIT (CLOSE);
223 BIT (QOS);
224 BIT (GROUP_QOS);
225 BIT (ROUTING_INTERFACE_CHANGE);
226 BIT (ADDRESS_LIST_CHANGE);
228 #undef BIT
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));
236 static const char *
237 condition_to_string (GIOCondition condition)
239 char buf[100];
240 int checked_bits = 0;
241 char *bufp = buf;
243 if (condition == 0)
244 return "";
246 #define BIT(n) checked_bits |= G_IO_##n; if (condition & G_IO_##n) bufp += sprintf (bufp, "%s" #n, (bufp>buf ? "|" : ""))
248 BIT (IN);
249 BIT (OUT);
250 BIT (PRI);
251 BIT (ERR);
252 BIT (HUP);
253 BIT (NVAL);
255 #undef BIT
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));
263 static gboolean
264 g_io_win32_get_debug_flag (void)
266 return (getenv ("G_IO_WIN32_DEBUG") != NULL);
269 static void
270 g_io_channel_win32_init (GIOWin32Channel *channel)
272 channel->debug = g_io_win32_get_debug_flag ();
274 InitializeCriticalSection (&channel->mutex);
275 channel->running = FALSE;
276 channel->needs_close = FALSE;
277 channel->thread_id = 0;
278 channel->data_avail_event = NULL;
279 channel->revents = 0;
280 channel->buffer = NULL;
281 channel->space_avail_event = NULL;
283 channel->event_mask = 0;
284 channel->last_events = 0;
285 channel->event = NULL;
286 channel->write_would_have_blocked = FALSE;
287 channel->ever_writable = FALSE;
290 static void
291 create_events (GIOWin32Channel *channel)
293 SECURITY_ATTRIBUTES sec_attrs;
295 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
296 sec_attrs.lpSecurityDescriptor = NULL;
297 sec_attrs.bInheritHandle = FALSE;
299 /* The data available event is manual reset, the space available event
300 * is automatic reset.
302 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
303 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
305 gchar *emsg = g_win32_error_message (GetLastError ());
307 g_error ("Error creating event: %s", emsg);
308 g_free (emsg);
312 static unsigned __stdcall
313 read_thread (void *parameter)
315 GIOWin32Channel *channel = parameter;
316 guchar *buffer;
317 gint nbytes;
319 g_io_channel_ref ((GIOChannel *)channel);
321 if (channel->debug)
322 g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
323 channel->thread_id,
324 channel->fd,
325 channel->data_avail_event,
326 channel->space_avail_event);
328 channel->direction = 0;
329 channel->buffer = g_malloc (BUFFER_SIZE);
330 channel->rdp = channel->wrp = 0;
331 channel->running = TRUE;
333 SetEvent (channel->space_avail_event);
335 EnterCriticalSection (&channel->mutex);
336 while (channel->running)
338 if (channel->debug)
339 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
340 channel->thread_id, channel->rdp, channel->wrp);
341 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
343 /* Buffer is full */
344 if (channel->debug)
345 g_print ("read_thread %#x: resetting space_avail\n",
346 channel->thread_id);
347 ResetEvent (channel->space_avail_event);
348 if (channel->debug)
349 g_print ("read_thread %#x: waiting for space\n",
350 channel->thread_id);
351 LeaveCriticalSection (&channel->mutex);
352 WaitForSingleObject (channel->space_avail_event, INFINITE);
353 EnterCriticalSection (&channel->mutex);
354 if (channel->debug)
355 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
356 channel->thread_id, channel->rdp, channel->wrp);
359 buffer = channel->buffer + channel->wrp;
361 /* Always leave at least one byte unused gap to be able to
362 * distinguish between the full and empty condition...
364 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
365 BUFFER_SIZE - channel->wrp);
367 if (channel->debug)
368 g_print ("read_thread %#x: calling read() for %d bytes\n",
369 channel->thread_id, nbytes);
371 LeaveCriticalSection (&channel->mutex);
373 nbytes = read (channel->fd, buffer, nbytes);
375 EnterCriticalSection (&channel->mutex);
377 channel->revents = G_IO_IN;
378 if (nbytes == 0)
379 channel->revents |= G_IO_HUP;
380 else if (nbytes < 0)
381 channel->revents |= G_IO_ERR;
383 if (channel->debug)
384 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
385 channel->thread_id, nbytes, channel->rdp, channel->wrp);
387 if (nbytes <= 0)
388 break;
390 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
391 if (channel->debug)
392 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
393 channel->thread_id, channel->rdp, channel->wrp);
394 SetEvent (channel->data_avail_event);
397 channel->running = FALSE;
398 if (channel->needs_close)
400 if (channel->debug)
401 g_print ("read_thread %#x: channel fd %d needs closing\n",
402 channel->thread_id, channel->fd);
403 close (channel->fd);
404 channel->fd = -1;
407 if (channel->debug)
408 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
409 channel->thread_id, channel->rdp, channel->wrp);
410 SetEvent (channel->data_avail_event);
411 LeaveCriticalSection (&channel->mutex);
413 g_io_channel_unref ((GIOChannel *)channel);
415 /* No need to call _endthreadex(), the actual thread starter routine
416 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
417 * _endthreadex() for us.
420 return 0;
423 static unsigned __stdcall
424 write_thread (void *parameter)
426 GIOWin32Channel *channel = parameter;
427 guchar *buffer;
428 gint nbytes;
430 g_io_channel_ref ((GIOChannel *)channel);
432 if (channel->debug)
433 g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
434 channel->thread_id,
435 channel->fd,
436 channel->data_avail_event,
437 channel->space_avail_event);
439 channel->direction = 1;
440 channel->buffer = g_malloc (BUFFER_SIZE);
441 channel->rdp = channel->wrp = 0;
442 channel->running = TRUE;
444 SetEvent (channel->space_avail_event);
446 /* We use the same event objects as for a reader thread, but with
447 * reversed meaning. So, space_avail is used if data is available
448 * for writing, and data_avail is used if space is available in the
449 * write buffer.
452 EnterCriticalSection (&channel->mutex);
453 while (channel->running || channel->rdp != channel->wrp)
455 if (channel->debug)
456 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
457 channel->thread_id, channel->rdp, channel->wrp);
458 if (channel->wrp == channel->rdp)
460 /* Buffer is empty. */
461 if (channel->debug)
462 g_print ("write_thread %#x: resetting space_avail\n",
463 channel->thread_id);
464 ResetEvent (channel->space_avail_event);
465 if (channel->debug)
466 g_print ("write_thread %#x: waiting for data\n",
467 channel->thread_id);
468 channel->revents = G_IO_OUT;
469 SetEvent (channel->data_avail_event);
470 LeaveCriticalSection (&channel->mutex);
471 WaitForSingleObject (channel->space_avail_event, INFINITE);
473 EnterCriticalSection (&channel->mutex);
474 if (channel->rdp == channel->wrp)
475 break;
477 if (channel->debug)
478 g_print ("write_thread %#x: rdp=%d, wrp=%d\n",
479 channel->thread_id, channel->rdp, channel->wrp);
482 buffer = channel->buffer + channel->rdp;
483 if (channel->rdp < channel->wrp)
484 nbytes = channel->wrp - channel->rdp;
485 else
486 nbytes = BUFFER_SIZE - channel->rdp;
488 if (channel->debug)
489 g_print ("write_thread %#x: calling write() for %d bytes\n",
490 channel->thread_id, nbytes);
492 LeaveCriticalSection (&channel->mutex);
493 nbytes = write (channel->fd, buffer, nbytes);
494 EnterCriticalSection (&channel->mutex);
496 if (channel->debug)
497 g_print ("write_thread %#x: write(%i) returned %d, rdp=%d, wrp=%d\n",
498 channel->thread_id, channel->fd, nbytes, channel->rdp, channel->wrp);
500 channel->revents = 0;
501 if (nbytes > 0)
502 channel->revents |= G_IO_OUT;
503 else if (nbytes <= 0)
504 channel->revents |= G_IO_ERR;
506 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
508 if (nbytes <= 0)
509 break;
511 if (channel->debug)
512 g_print ("write_thread: setting data_avail for thread %#x\n",
513 channel->thread_id);
514 SetEvent (channel->data_avail_event);
517 channel->running = FALSE;
518 if (channel->needs_close)
520 if (channel->debug)
521 g_print ("write_thread %#x: channel fd %d needs closing\n",
522 channel->thread_id, channel->fd);
523 close (channel->fd);
524 channel->fd = -1;
527 LeaveCriticalSection (&channel->mutex);
529 g_io_channel_unref ((GIOChannel *)channel);
531 return 0;
534 static void
535 create_thread (GIOWin32Channel *channel,
536 GIOCondition condition,
537 unsigned (__stdcall *thread) (void *parameter))
539 HANDLE thread_handle;
540 int errsv;
542 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
543 &channel->thread_id);
544 errsv = errno;
545 if (thread_handle == 0)
546 g_warning ("Error creating thread: %s.",
547 g_strerror (errsv));
548 else if (!CloseHandle (thread_handle))
550 gchar *emsg = g_win32_error_message (GetLastError ());
552 g_warning ("Error closing thread handle: %s.", emsg);
553 g_free (emsg);
556 WaitForSingleObject (channel->space_avail_event, INFINITE);
559 static GIOStatus
560 buffer_read (GIOWin32Channel *channel,
561 gchar *dest,
562 gsize count,
563 gsize *bytes_read,
564 GError **err)
566 guint nbytes;
567 guint left = count;
569 EnterCriticalSection (&channel->mutex);
570 if (channel->debug)
571 g_print ("reading from thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
572 channel->thread_id, count, channel->rdp, channel->wrp);
574 if (channel->wrp == channel->rdp)
576 LeaveCriticalSection (&channel->mutex);
577 if (channel->debug)
578 g_print ("waiting for data from thread %#x\n", channel->thread_id);
579 WaitForSingleObject (channel->data_avail_event, INFINITE);
580 if (channel->debug)
581 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
582 EnterCriticalSection (&channel->mutex);
583 if (channel->wrp == channel->rdp && !channel->running)
585 if (channel->debug)
586 g_print ("wrp==rdp, !running\n");
587 LeaveCriticalSection (&channel->mutex);
588 *bytes_read = 0;
589 return G_IO_STATUS_EOF;
593 if (channel->rdp < channel->wrp)
594 nbytes = channel->wrp - channel->rdp;
595 else
596 nbytes = BUFFER_SIZE - channel->rdp;
597 LeaveCriticalSection (&channel->mutex);
598 nbytes = MIN (left, nbytes);
599 if (channel->debug)
600 g_print ("moving %d bytes from thread %#x\n",
601 nbytes, channel->thread_id);
602 memcpy (dest, channel->buffer + channel->rdp, nbytes);
603 dest += nbytes;
604 left -= nbytes;
605 EnterCriticalSection (&channel->mutex);
606 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
607 if (channel->debug)
608 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
609 SetEvent (channel->space_avail_event);
610 if (channel->debug)
611 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
612 channel->thread_id, channel->rdp, channel->wrp);
613 if (channel->running && channel->wrp == channel->rdp)
615 if (channel->debug)
616 g_print ("resetting data_avail of thread %#x\n",
617 channel->thread_id);
618 ResetEvent (channel->data_avail_event);
620 LeaveCriticalSection (&channel->mutex);
622 /* We have no way to indicate any errors form the actual
623 * read() or recv() call in the reader thread. Should we have?
625 *bytes_read = count - left;
626 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
630 static GIOStatus
631 buffer_write (GIOWin32Channel *channel,
632 const gchar *dest,
633 gsize count,
634 gsize *bytes_written,
635 GError **err)
637 guint nbytes;
638 guint left = count;
640 EnterCriticalSection (&channel->mutex);
641 if (channel->debug)
642 g_print ("buffer_write: writing to thread %#x %" G_GSIZE_FORMAT " bytes, rdp=%d, wrp=%d\n",
643 channel->thread_id, count, channel->rdp, channel->wrp);
645 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
647 /* Buffer is full */
648 if (channel->debug)
649 g_print ("buffer_write: tid %#x: resetting data_avail\n",
650 channel->thread_id);
651 ResetEvent (channel->data_avail_event);
652 if (channel->debug)
653 g_print ("buffer_write: tid %#x: waiting for space\n",
654 channel->thread_id);
655 LeaveCriticalSection (&channel->mutex);
656 WaitForSingleObject (channel->data_avail_event, INFINITE);
657 EnterCriticalSection (&channel->mutex);
658 if (channel->debug)
659 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d\n",
660 channel->thread_id, channel->rdp, channel->wrp);
663 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
664 BUFFER_SIZE - channel->wrp);
666 LeaveCriticalSection (&channel->mutex);
667 nbytes = MIN (left, nbytes);
668 if (channel->debug)
669 g_print ("buffer_write: tid %#x: writing %d bytes\n",
670 channel->thread_id, nbytes);
671 memcpy (channel->buffer + channel->wrp, dest, nbytes);
672 dest += nbytes;
673 left -= nbytes;
674 EnterCriticalSection (&channel->mutex);
676 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
677 if (channel->debug)
678 g_print ("buffer_write: tid %#x: rdp=%d, wrp=%d, setting space_avail\n",
679 channel->thread_id, channel->rdp, channel->wrp);
680 SetEvent (channel->space_avail_event);
682 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
684 /* Buffer is full */
685 if (channel->debug)
686 g_print ("buffer_write: tid %#x: resetting data_avail\n",
687 channel->thread_id);
688 ResetEvent (channel->data_avail_event);
691 LeaveCriticalSection (&channel->mutex);
693 /* We have no way to indicate any errors form the actual
694 * write() call in the writer thread. Should we have?
696 *bytes_written = count - left;
697 return (*bytes_written > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
701 static gboolean
702 g_io_win32_prepare (GSource *source,
703 gint *timeout)
705 GIOWin32Watch *watch = (GIOWin32Watch *)source;
706 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
707 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
708 int event_mask;
710 *timeout = -1;
712 if (channel->debug)
713 g_print ("g_io_win32_prepare: source=%p channel=%p", source, channel);
715 switch (channel->type)
717 case G_IO_WIN32_WINDOWS_MESSAGES:
718 if (channel->debug)
719 g_print (" MSG");
720 break;
722 case G_IO_WIN32_CONSOLE:
723 if (channel->debug)
724 g_print (" CON");
725 break;
727 case G_IO_WIN32_FILE_DESC:
728 if (channel->debug)
729 g_print (" FD thread=%#x buffer_condition:{%s}"
730 "\n watch->pollfd.events:{%s} watch->pollfd.revents:{%s} channel->revents:{%s}",
731 channel->thread_id, condition_to_string (buffer_condition),
732 condition_to_string (watch->pollfd.events),
733 condition_to_string (watch->pollfd.revents),
734 condition_to_string (channel->revents));
736 EnterCriticalSection (&channel->mutex);
737 if (channel->running)
739 if (channel->direction == 0 && channel->wrp == channel->rdp)
741 if (channel->debug)
742 g_print ("\n setting revents=0");
743 channel->revents = 0;
746 else
748 if (channel->direction == 1
749 && (channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
751 if (channel->debug)
752 g_print ("\n setting revents=0");
753 channel->revents = 0;
756 LeaveCriticalSection (&channel->mutex);
757 break;
759 case G_IO_WIN32_SOCKET:
760 if (channel->debug)
761 g_print (" SOCK");
762 event_mask = 0;
763 if (watch->condition & G_IO_IN)
764 event_mask |= (FD_READ | FD_ACCEPT);
765 if (watch->condition & G_IO_OUT)
766 event_mask |= (FD_WRITE | FD_CONNECT);
767 event_mask |= FD_CLOSE;
769 if (channel->event_mask != event_mask)
771 if (channel->debug)
772 g_print ("\n WSAEventSelect(%d,%p,{%s})",
773 channel->fd, (HANDLE) watch->pollfd.fd,
774 event_mask_to_string (event_mask));
775 if (WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd,
776 event_mask) == SOCKET_ERROR)
777 if (channel->debug)
779 gchar *emsg = g_win32_error_message (WSAGetLastError ());
781 g_print (" failed: %s", emsg);
782 g_free (emsg);
784 channel->event_mask = event_mask;
786 if (channel->debug)
787 g_print ("\n setting last_events=0");
788 channel->last_events = 0;
790 if ((event_mask & FD_WRITE) &&
791 channel->ever_writable &&
792 !channel->write_would_have_blocked)
794 if (channel->debug)
795 g_print (" WSASetEvent(%p)", (WSAEVENT) watch->pollfd.fd);
796 WSASetEvent ((WSAEVENT) watch->pollfd.fd);
799 break;
801 default:
802 g_assert_not_reached ();
803 g_abort ();
805 if (channel->debug)
806 g_print ("\n");
808 return ((watch->condition & buffer_condition) == watch->condition);
811 static gboolean
812 g_io_win32_check (GSource *source)
814 MSG msg;
815 GIOWin32Watch *watch = (GIOWin32Watch *)source;
816 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
817 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
818 WSANETWORKEVENTS events;
820 if (channel->debug)
821 g_print ("g_io_win32_check: source=%p channel=%p", source, channel);
823 switch (channel->type)
825 case G_IO_WIN32_WINDOWS_MESSAGES:
826 if (channel->debug)
827 g_print (" MSG\n");
828 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
830 case G_IO_WIN32_FILE_DESC:
831 if (channel->debug)
832 g_print (" FD thread=%#x buffer_condition=%s\n"
833 " watch->pollfd.events={%s} watch->pollfd.revents={%s} channel->revents={%s}\n",
834 channel->thread_id, condition_to_string (buffer_condition),
835 condition_to_string (watch->pollfd.events),
836 condition_to_string (watch->pollfd.revents),
837 condition_to_string (channel->revents));
839 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
841 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
843 case G_IO_WIN32_CONSOLE:
844 if (channel->debug)
845 g_print (" CON\n");
846 if (watch->channel->is_writeable)
847 return TRUE;
848 else if (watch->channel->is_readable)
850 INPUT_RECORD buffer;
851 DWORD n;
852 if (PeekConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n) &&
853 n == 1)
855 /* _kbhit() does quite complex processing to find out
856 * whether at least one of the key events pending corresponds
857 * to a "real" character that can be read.
859 if (_kbhit ())
860 return TRUE;
862 /* Discard all other kinds of events */
863 ReadConsoleInput ((HANDLE) watch->pollfd.fd, &buffer, 1, &n);
866 return FALSE;
868 case G_IO_WIN32_SOCKET:
869 if (channel->debug)
870 g_print (" SOCK");
871 if (channel->last_events & FD_WRITE)
873 if (channel->debug)
874 g_print (" sock=%d event=%p last_events has FD_WRITE",
875 channel->fd, (HANDLE) watch->pollfd.fd);
877 else
879 WSAEnumNetworkEvents (channel->fd, 0, &events);
881 if (channel->debug)
882 g_print ("\n revents={%s} condition={%s}"
883 "\n WSAEnumNetworkEvents(%d,0) sets events={%s}",
884 condition_to_string (watch->pollfd.revents),
885 condition_to_string (watch->condition),
886 channel->fd,
887 event_mask_to_string (events.lNetworkEvents));
889 if (watch->pollfd.revents != 0 &&
890 events.lNetworkEvents == 0 &&
891 !(channel->event_mask & FD_WRITE))
893 channel->event_mask = 0;
894 if (channel->debug)
895 g_print ("\n WSAEventSelect(%d,%p,{})",
896 channel->fd, (HANDLE) watch->pollfd.fd);
897 WSAEventSelect (channel->fd, (HANDLE) watch->pollfd.fd, 0);
898 if (channel->debug)
899 g_print (" ResetEvent(%p)",
900 (HANDLE) watch->pollfd.fd);
901 ResetEvent ((HANDLE) watch->pollfd.fd);
903 else if (events.lNetworkEvents & FD_WRITE)
904 channel->ever_writable = TRUE;
905 channel->last_events = events.lNetworkEvents;
908 watch->pollfd.revents = 0;
909 if (channel->last_events & (FD_READ | FD_ACCEPT))
910 watch->pollfd.revents |= G_IO_IN;
912 if (channel->last_events & FD_WRITE)
913 watch->pollfd.revents |= G_IO_OUT;
914 else
916 /* We have called WSAEnumNetworkEvents() above but it didn't
917 * set FD_WRITE.
919 if (events.lNetworkEvents & FD_CONNECT)
921 if (events.iErrorCode[FD_CONNECT_BIT] == 0)
922 watch->pollfd.revents |= G_IO_OUT;
923 else
924 watch->pollfd.revents |= (G_IO_HUP | G_IO_ERR);
926 if (watch->pollfd.revents == 0 && (channel->last_events & (FD_CLOSE)))
927 watch->pollfd.revents |= G_IO_HUP;
930 /* Regardless of WSAEnumNetworkEvents() result, if watching for
931 * writability, and if we have ever got a FD_WRITE event, and
932 * unless last write would have blocked, set G_IO_OUT. But never
933 * set both G_IO_OUT and G_IO_HUP.
935 if (!(watch->pollfd.revents & G_IO_HUP) &&
936 channel->ever_writable &&
937 !channel->write_would_have_blocked &&
938 (channel->event_mask & FD_WRITE))
939 watch->pollfd.revents |= G_IO_OUT;
941 if (channel->debug)
942 g_print ("\n revents={%s} retval={%s}\n",
943 condition_to_string (watch->pollfd.revents),
944 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
946 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
948 default:
949 g_assert_not_reached ();
950 g_abort ();
954 static gboolean
955 g_io_win32_dispatch (GSource *source,
956 GSourceFunc callback,
957 gpointer user_data)
959 GIOFunc func = (GIOFunc)callback;
960 GIOWin32Watch *watch = (GIOWin32Watch *)source;
961 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
962 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
964 if (!func)
966 g_warning ("IO Watch dispatched without callback. "
967 "You must call g_source_connect().");
968 return FALSE;
971 if (channel->debug)
972 g_print ("g_io_win32_dispatch: pollfd.revents=%s condition=%s result=%s\n",
973 condition_to_string (watch->pollfd.revents),
974 condition_to_string (watch->condition),
975 condition_to_string ((watch->pollfd.revents | buffer_condition) & watch->condition));
977 return (*func) (watch->channel,
978 (watch->pollfd.revents | buffer_condition) & watch->condition,
979 user_data);
982 static void
983 g_io_win32_finalize (GSource *source)
985 GIOWin32Watch *watch = (GIOWin32Watch *)source;
986 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
988 if (channel->debug)
989 g_print ("g_io_win32_finalize: source=%p channel=%p", source, channel);
991 switch (channel->type)
993 case G_IO_WIN32_WINDOWS_MESSAGES:
994 if (channel->debug)
995 g_print (" MSG");
996 break;
998 case G_IO_WIN32_CONSOLE:
999 if (channel->debug)
1000 g_print (" CON");
1001 break;
1003 case G_IO_WIN32_FILE_DESC:
1004 if (channel->debug)
1005 g_print (" FD thread=%#x", channel->thread_id);
1006 break;
1008 case G_IO_WIN32_SOCKET:
1009 if (channel->debug)
1010 g_print (" SOCK sock=%d", channel->fd);
1011 break;
1013 default:
1014 g_assert_not_reached ();
1015 g_abort ();
1017 if (channel->debug)
1018 g_print ("\n");
1019 g_io_channel_unref (watch->channel);
1022 GSourceFuncs g_io_watch_funcs = {
1023 g_io_win32_prepare,
1024 g_io_win32_check,
1025 g_io_win32_dispatch,
1026 g_io_win32_finalize
1029 static GIOStatus
1030 g_io_win32_msg_read (GIOChannel *channel,
1031 gchar *buf,
1032 gsize count,
1033 gsize *bytes_read,
1034 GError **err)
1036 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1037 MSG msg; /* In case of alignment problems */
1039 *bytes_read = 0;
1041 if (count < sizeof (MSG))
1043 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1044 "Incorrect message size"); /* Informative enough error message? */
1045 return G_IO_STATUS_ERROR;
1048 if (win32_channel->debug)
1049 g_print ("g_io_win32_msg_read: channel=%p hwnd=%p\n",
1050 channel, win32_channel->hwnd);
1051 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
1052 return G_IO_STATUS_AGAIN;
1054 memmove (buf, &msg, sizeof (MSG));
1055 *bytes_read = sizeof (MSG);
1057 return G_IO_STATUS_NORMAL;
1060 static GIOStatus
1061 g_io_win32_msg_write (GIOChannel *channel,
1062 const gchar *buf,
1063 gsize count,
1064 gsize *bytes_written,
1065 GError **err)
1067 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1068 MSG msg;
1070 *bytes_written = 0;
1072 if (count != sizeof (MSG))
1074 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
1075 "Incorrect message size"); /* Informative enough error message? */
1076 return G_IO_STATUS_ERROR;
1079 /* In case of alignment problems */
1080 memmove (&msg, buf, sizeof (MSG));
1081 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
1083 gchar *emsg = g_win32_error_message (GetLastError ());
1085 g_set_error_literal (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
1086 g_free (emsg);
1088 return G_IO_STATUS_ERROR;
1091 *bytes_written = sizeof (MSG);
1093 return G_IO_STATUS_NORMAL;
1096 static GIOStatus
1097 g_io_win32_msg_close (GIOChannel *channel,
1098 GError **err)
1100 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1102 return G_IO_STATUS_NORMAL;
1105 static void
1106 g_io_win32_free (GIOChannel *channel)
1108 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1110 if (win32_channel->debug)
1111 g_print ("g_io_win32_free channel=%p fd=%d\n", channel, win32_channel->fd);
1113 DeleteCriticalSection (&win32_channel->mutex);
1115 if (win32_channel->data_avail_event)
1116 if (!CloseHandle (win32_channel->data_avail_event))
1117 if (win32_channel->debug)
1119 gchar *emsg = g_win32_error_message (GetLastError ());
1121 g_print (" CloseHandle(%p) failed: %s\n",
1122 win32_channel->data_avail_event, emsg);
1123 g_free (emsg);
1126 g_free (win32_channel->buffer);
1128 if (win32_channel->space_avail_event)
1129 if (!CloseHandle (win32_channel->space_avail_event))
1130 if (win32_channel->debug)
1132 gchar *emsg = g_win32_error_message (GetLastError ());
1134 g_print (" CloseHandle(%p) failed: %s\n",
1135 win32_channel->space_avail_event, emsg);
1136 g_free (emsg);
1139 if (win32_channel->type == G_IO_WIN32_SOCKET &&
1140 win32_channel->fd != -1)
1141 if (WSAEventSelect (win32_channel->fd, NULL, 0) == SOCKET_ERROR)
1142 if (win32_channel->debug)
1144 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1146 g_print (" WSAEventSelect(%d,NULL,{}) failed: %s\n",
1147 win32_channel->fd, emsg);
1148 g_free (emsg);
1151 if (win32_channel->event)
1152 if (!WSACloseEvent (win32_channel->event))
1153 if (win32_channel->debug)
1155 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1157 g_print (" WSACloseEvent(%p) failed: %s\n",
1158 win32_channel->event, emsg);
1159 g_free (emsg);
1162 g_free (win32_channel);
1165 static GSource *
1166 g_io_win32_msg_create_watch (GIOChannel *channel,
1167 GIOCondition condition)
1169 GIOWin32Watch *watch;
1170 GSource *source;
1172 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1173 g_source_set_name (source, "GIOChannel (Win32)");
1174 watch = (GIOWin32Watch *)source;
1176 watch->channel = channel;
1177 g_io_channel_ref (channel);
1179 watch->condition = condition;
1181 watch->pollfd.fd = (gintptr) G_WIN32_MSG_HANDLE;
1182 watch->pollfd.events = condition;
1184 g_source_add_poll (source, &watch->pollfd);
1186 return source;
1189 static GIOStatus
1190 g_io_win32_fd_and_console_read (GIOChannel *channel,
1191 gchar *buf,
1192 gsize count,
1193 gsize *bytes_read,
1194 GError **err)
1196 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1197 gint result;
1198 int errsv;
1200 if (win32_channel->debug)
1201 g_print ("g_io_win32_fd_read: fd=%d count=%" G_GSIZE_FORMAT "\n",
1202 win32_channel->fd, count);
1204 if (win32_channel->thread_id)
1206 return buffer_read (win32_channel, buf, count, bytes_read, err);
1209 result = read (win32_channel->fd, buf, count);
1210 errsv = errno;
1212 if (win32_channel->debug)
1213 g_print ("g_io_win32_fd_read: read() => %d\n", result);
1215 if (result < 0)
1217 *bytes_read = 0;
1219 switch (errsv)
1221 #ifdef EAGAIN
1222 case EAGAIN:
1223 return G_IO_STATUS_AGAIN;
1224 #endif
1225 default:
1226 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1227 g_io_channel_error_from_errno (errsv),
1228 g_strerror (errsv));
1229 return G_IO_STATUS_ERROR;
1233 *bytes_read = result;
1235 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1238 static GIOStatus
1239 g_io_win32_fd_and_console_write (GIOChannel *channel,
1240 const gchar *buf,
1241 gsize count,
1242 gsize *bytes_written,
1243 GError **err)
1245 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1246 gint result;
1247 int errsv;
1249 if (win32_channel->thread_id)
1251 return buffer_write (win32_channel, buf, count, bytes_written, err);
1254 result = write (win32_channel->fd, buf, count);
1255 errsv = errno;
1257 if (win32_channel->debug)
1258 g_print ("g_io_win32_fd_write: fd=%d count=%" G_GSIZE_FORMAT " => %d\n",
1259 win32_channel->fd, count, result);
1261 if (result < 0)
1263 *bytes_written = 0;
1265 switch (errsv)
1267 #ifdef EAGAIN
1268 case EAGAIN:
1269 return G_IO_STATUS_AGAIN;
1270 #endif
1271 default:
1272 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1273 g_io_channel_error_from_errno (errsv),
1274 g_strerror (errsv));
1275 return G_IO_STATUS_ERROR;
1279 *bytes_written = result;
1281 return G_IO_STATUS_NORMAL;
1284 static GIOStatus
1285 g_io_win32_fd_seek (GIOChannel *channel,
1286 gint64 offset,
1287 GSeekType type,
1288 GError **err)
1290 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1291 int whence, errsv;
1292 off_t tmp_offset;
1293 off_t result;
1295 switch (type)
1297 case G_SEEK_SET:
1298 whence = SEEK_SET;
1299 break;
1300 case G_SEEK_CUR:
1301 whence = SEEK_CUR;
1302 break;
1303 case G_SEEK_END:
1304 whence = SEEK_END;
1305 break;
1306 default:
1307 whence = -1; /* Keep the compiler quiet */
1308 g_assert_not_reached ();
1309 g_abort ();
1312 tmp_offset = offset;
1313 if (tmp_offset != offset)
1315 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1316 g_io_channel_error_from_errno (EINVAL),
1317 g_strerror (EINVAL));
1318 return G_IO_STATUS_ERROR;
1321 result = lseek (win32_channel->fd, tmp_offset, whence);
1322 errsv = errno;
1324 if (result < 0)
1326 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1327 g_io_channel_error_from_errno (errsv),
1328 g_strerror (errsv));
1329 return G_IO_STATUS_ERROR;
1332 return G_IO_STATUS_NORMAL;
1335 static GIOStatus
1336 g_io_win32_fd_close (GIOChannel *channel,
1337 GError **err)
1339 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1341 if (win32_channel->debug)
1342 g_print ("g_io_win32_fd_close: thread=%#x: fd=%d\n",
1343 win32_channel->thread_id,
1344 win32_channel->fd);
1345 EnterCriticalSection (&win32_channel->mutex);
1346 if (win32_channel->running)
1348 if (win32_channel->debug)
1349 g_print ("thread %#x: running, marking fd %d for later close\n",
1350 win32_channel->thread_id, win32_channel->fd);
1351 win32_channel->running = FALSE;
1352 win32_channel->needs_close = TRUE;
1353 if (win32_channel->direction == 0)
1354 SetEvent (win32_channel->data_avail_event);
1355 else
1356 SetEvent (win32_channel->space_avail_event);
1358 else
1360 if (win32_channel->debug)
1361 g_print ("closing fd %d\n", win32_channel->fd);
1362 close (win32_channel->fd);
1363 if (win32_channel->debug)
1364 g_print ("closed fd %d, setting to -1\n",
1365 win32_channel->fd);
1366 win32_channel->fd = -1;
1368 LeaveCriticalSection (&win32_channel->mutex);
1370 /* FIXME error detection? */
1372 return G_IO_STATUS_NORMAL;
1375 static GSource *
1376 g_io_win32_fd_create_watch (GIOChannel *channel,
1377 GIOCondition condition)
1379 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1380 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1381 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1383 watch->channel = channel;
1384 g_io_channel_ref (channel);
1386 watch->condition = condition;
1388 if (win32_channel->data_avail_event == NULL)
1389 create_events (win32_channel);
1391 watch->pollfd.fd = (gintptr) win32_channel->data_avail_event;
1392 watch->pollfd.events = condition;
1394 if (win32_channel->debug)
1395 g_print ("g_io_win32_fd_create_watch: channel=%p fd=%d condition={%s} event=%p\n",
1396 channel, win32_channel->fd,
1397 condition_to_string (condition), (HANDLE) watch->pollfd.fd);
1399 EnterCriticalSection (&win32_channel->mutex);
1400 if (win32_channel->thread_id == 0)
1402 if (condition & G_IO_IN)
1403 create_thread (win32_channel, condition, read_thread);
1404 else if (condition & G_IO_OUT)
1405 create_thread (win32_channel, condition, write_thread);
1408 g_source_add_poll (source, &watch->pollfd);
1409 LeaveCriticalSection (&win32_channel->mutex);
1411 return source;
1414 static GIOStatus
1415 g_io_win32_console_close (GIOChannel *channel,
1416 GError **err)
1418 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1420 if (close (win32_channel->fd) < 0)
1422 int errsv = errno;
1423 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1424 g_io_channel_error_from_errno (errsv),
1425 g_strerror (errsv));
1426 return G_IO_STATUS_ERROR;
1429 return G_IO_STATUS_NORMAL;
1432 static GSource *
1433 g_io_win32_console_create_watch (GIOChannel *channel,
1434 GIOCondition condition)
1436 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1437 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1438 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1440 watch->channel = channel;
1441 g_io_channel_ref (channel);
1443 watch->condition = condition;
1445 watch->pollfd.fd = _get_osfhandle (win32_channel->fd);
1446 watch->pollfd.events = condition;
1448 g_source_add_poll (source, &watch->pollfd);
1450 return source;
1453 static GIOStatus
1454 g_io_win32_sock_read (GIOChannel *channel,
1455 gchar *buf,
1456 gsize count,
1457 gsize *bytes_read,
1458 GError **err)
1460 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1461 gint result;
1462 GIOChannelError error;
1463 int winsock_error;
1465 if (win32_channel->debug)
1466 g_print ("g_io_win32_sock_read: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1467 channel, win32_channel->fd, count);
1469 result = recv (win32_channel->fd, buf, count, 0);
1470 if (result == SOCKET_ERROR)
1471 winsock_error = WSAGetLastError ();
1473 if (win32_channel->debug)
1474 g_print (" recv=%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);
1483 *bytes_read = 0;
1485 switch (winsock_error)
1487 case WSAEINVAL:
1488 error = G_IO_CHANNEL_ERROR_INVAL;
1489 break;
1490 case WSAEWOULDBLOCK:
1491 g_free (emsg);
1492 return G_IO_STATUS_AGAIN;
1493 default:
1494 error = G_IO_CHANNEL_ERROR_FAILED;
1495 break;
1497 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1498 g_free (emsg);
1500 return G_IO_STATUS_ERROR;
1502 else
1504 if (win32_channel->debug)
1505 g_print ("\n");
1506 *bytes_read = result;
1507 if (result == 0)
1508 return G_IO_STATUS_EOF;
1509 else
1510 return G_IO_STATUS_NORMAL;
1514 static GIOStatus
1515 g_io_win32_sock_write (GIOChannel *channel,
1516 const gchar *buf,
1517 gsize count,
1518 gsize *bytes_written,
1519 GError **err)
1521 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1522 gint result;
1523 GIOChannelError error;
1524 int winsock_error;
1526 if (win32_channel->debug)
1527 g_print ("g_io_win32_sock_write: channel=%p sock=%d count=%" G_GSIZE_FORMAT,
1528 channel, win32_channel->fd, count);
1530 result = send (win32_channel->fd, buf, count, 0);
1531 if (result == SOCKET_ERROR)
1532 winsock_error = WSAGetLastError ();
1534 if (win32_channel->debug)
1535 g_print (" send=%d", result);
1537 if (result == SOCKET_ERROR)
1539 gchar *emsg = g_win32_error_message (winsock_error);
1541 if (win32_channel->debug)
1542 g_print (" %s\n", emsg);
1544 *bytes_written = 0;
1546 switch (winsock_error)
1548 case WSAEINVAL:
1549 error = G_IO_CHANNEL_ERROR_INVAL;
1550 break;
1551 case WSAEWOULDBLOCK:
1552 win32_channel->write_would_have_blocked = TRUE;
1553 win32_channel->last_events = 0;
1554 g_free (emsg);
1555 return G_IO_STATUS_AGAIN;
1556 default:
1557 error = G_IO_CHANNEL_ERROR_FAILED;
1558 break;
1560 g_set_error_literal (err, G_IO_CHANNEL_ERROR, error, emsg);
1561 g_free (emsg);
1563 return G_IO_STATUS_ERROR;
1565 else
1567 if (win32_channel->debug)
1568 g_print ("\n");
1569 *bytes_written = result;
1570 win32_channel->write_would_have_blocked = FALSE;
1572 return G_IO_STATUS_NORMAL;
1576 static GIOStatus
1577 g_io_win32_sock_close (GIOChannel *channel,
1578 GError **err)
1580 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1582 if (win32_channel->fd != -1)
1584 if (win32_channel->debug)
1585 g_print ("g_io_win32_sock_close: channel=%p sock=%d\n",
1586 channel, win32_channel->fd);
1588 closesocket (win32_channel->fd);
1589 win32_channel->fd = -1;
1592 /* FIXME error detection? */
1594 return G_IO_STATUS_NORMAL;
1597 static GSource *
1598 g_io_win32_sock_create_watch (GIOChannel *channel,
1599 GIOCondition condition)
1601 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1602 GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
1603 GIOWin32Watch *watch = (GIOWin32Watch *)source;
1605 watch->channel = channel;
1606 g_io_channel_ref (channel);
1608 watch->condition = condition;
1610 if (win32_channel->event == 0)
1611 win32_channel->event = WSACreateEvent ();
1613 watch->pollfd.fd = (gintptr) win32_channel->event;
1614 watch->pollfd.events = condition;
1616 if (win32_channel->debug)
1617 g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
1618 channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
1619 condition_to_string (watch->condition));
1621 g_source_add_poll (source, &watch->pollfd);
1623 return source;
1626 GIOChannel *
1627 g_io_channel_new_file (const gchar *filename,
1628 const gchar *mode,
1629 GError **error)
1631 int fid, flags, pmode;
1632 GIOChannel *channel;
1634 enum { /* Cheesy hack */
1635 MODE_R = 1 << 0,
1636 MODE_W = 1 << 1,
1637 MODE_A = 1 << 2,
1638 MODE_PLUS = 1 << 3,
1640 int mode_num, errsv;
1642 g_return_val_if_fail (filename != NULL, NULL);
1643 g_return_val_if_fail (mode != NULL, NULL);
1644 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1646 switch (mode[0])
1648 case 'r':
1649 mode_num = MODE_R;
1650 break;
1651 case 'w':
1652 mode_num = MODE_W;
1653 break;
1654 case 'a':
1655 mode_num = MODE_A;
1656 break;
1657 default:
1658 g_warning ("Invalid GIOFileMode %s.", mode);
1659 return NULL;
1662 switch (mode[1])
1664 case '\0':
1665 break;
1666 case '+':
1667 if (mode[2] == '\0')
1669 mode_num |= MODE_PLUS;
1670 break;
1672 /* Fall through */
1673 default:
1674 g_warning ("Invalid GIOFileMode %s.", mode);
1675 return NULL;
1678 switch (mode_num)
1680 case MODE_R:
1681 flags = O_RDONLY;
1682 pmode = _S_IREAD;
1683 break;
1684 case MODE_W:
1685 flags = O_WRONLY | O_TRUNC | O_CREAT;
1686 pmode = _S_IWRITE;
1687 break;
1688 case MODE_A:
1689 flags = O_WRONLY | O_APPEND | O_CREAT;
1690 pmode = _S_IWRITE;
1691 break;
1692 case MODE_R | MODE_PLUS:
1693 flags = O_RDWR;
1694 pmode = _S_IREAD | _S_IWRITE;
1695 break;
1696 case MODE_W | MODE_PLUS:
1697 flags = O_RDWR | O_TRUNC | O_CREAT;
1698 pmode = _S_IREAD | _S_IWRITE;
1699 break;
1700 case MODE_A | MODE_PLUS:
1701 flags = O_RDWR | O_APPEND | O_CREAT;
1702 pmode = _S_IREAD | _S_IWRITE;
1703 break;
1704 default:
1705 g_assert_not_reached ();
1706 g_abort ();
1709 /* always open 'untranslated' */
1710 fid = g_open (filename, flags | _O_BINARY, pmode);
1711 errsv = errno;
1713 if (g_io_win32_get_debug_flag ())
1715 g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
1716 g_win32_print_access_mode (flags|_O_BINARY);
1717 g_print (",%#o)=%d\n", pmode, fid);
1720 if (fid < 0)
1722 g_set_error_literal (error, G_FILE_ERROR,
1723 g_file_error_from_errno (errsv),
1724 g_strerror (errsv));
1725 return (GIOChannel *)NULL;
1728 channel = g_io_channel_win32_new_fd (fid);
1730 /* XXX: move this to g_io_channel_win32_new_fd () */
1731 channel->close_on_unref = TRUE;
1732 channel->is_seekable = TRUE;
1734 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1735 * correspond to actual readability/writeability. Set to FALSE those
1736 * that mode doesn't allow
1738 switch (mode_num)
1740 case MODE_R:
1741 channel->is_writeable = FALSE;
1742 break;
1743 case MODE_W:
1744 case MODE_A:
1745 channel->is_readable = FALSE;
1746 break;
1747 case MODE_R | MODE_PLUS:
1748 case MODE_W | MODE_PLUS:
1749 case MODE_A | MODE_PLUS:
1750 break;
1751 default:
1752 g_assert_not_reached ();
1753 g_abort ();
1756 return channel;
1759 static GIOStatus
1760 g_io_win32_unimpl_set_flags (GIOChannel *channel,
1761 GIOFlags flags,
1762 GError **err)
1764 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1766 if (win32_channel->debug)
1768 g_print ("g_io_win32_unimpl_set_flags: ");
1769 g_win32_print_gioflags (flags);
1770 g_print ("\n");
1773 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1774 G_IO_CHANNEL_ERROR_FAILED,
1775 "Not implemented on Win32");
1777 return G_IO_STATUS_ERROR;
1780 static GIOFlags
1781 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1782 struct _stati64 *st)
1784 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1785 gchar c;
1786 DWORD count;
1788 if (st->st_mode & _S_IFIFO)
1790 channel->is_readable =
1791 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1792 channel->is_writeable =
1793 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1794 channel->is_seekable = FALSE;
1796 else
1798 channel->is_readable =
1799 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1800 channel->is_writeable =
1801 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1802 channel->is_seekable = TRUE;
1805 /* XXX: G_IO_FLAG_APPEND */
1806 /* XXX: G_IO_FLAG_NONBLOCK */
1808 return 0;
1811 static GIOFlags
1812 g_io_win32_fd_get_flags (GIOChannel *channel)
1814 struct _stati64 st;
1815 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1817 g_return_val_if_fail (win32_channel != NULL, 0);
1818 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1820 if (0 == _fstati64 (win32_channel->fd, &st))
1821 return g_io_win32_fd_get_flags_internal (channel, &st);
1822 else
1823 return 0;
1826 static GIOFlags
1827 g_io_win32_console_get_flags_internal (GIOChannel *channel)
1829 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1830 HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
1831 gchar c;
1832 DWORD count;
1833 INPUT_RECORD record;
1835 channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
1836 channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
1837 channel->is_seekable = FALSE;
1839 return 0;
1842 static GIOFlags
1843 g_io_win32_console_get_flags (GIOChannel *channel)
1845 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1847 g_return_val_if_fail (win32_channel != NULL, 0);
1848 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
1850 return g_io_win32_console_get_flags_internal (channel);
1853 static GIOFlags
1854 g_io_win32_msg_get_flags (GIOChannel *channel)
1856 return 0;
1859 static GIOStatus
1860 g_io_win32_sock_set_flags (GIOChannel *channel,
1861 GIOFlags flags,
1862 GError **err)
1864 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1865 u_long arg;
1867 if (win32_channel->debug)
1869 g_print ("g_io_win32_sock_set_flags: ");
1870 g_win32_print_gioflags (flags);
1871 g_print ("\n");
1874 if (flags & G_IO_FLAG_NONBLOCK)
1876 arg = 1;
1877 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1879 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1881 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1882 G_IO_CHANNEL_ERROR_FAILED,
1883 emsg);
1884 g_free (emsg);
1886 return G_IO_STATUS_ERROR;
1889 else
1891 arg = 0;
1892 if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
1894 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1896 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
1897 G_IO_CHANNEL_ERROR_FAILED,
1898 emsg);
1899 g_free (emsg);
1901 return G_IO_STATUS_ERROR;
1905 return G_IO_STATUS_NORMAL;
1908 static GIOFlags
1909 g_io_win32_sock_get_flags (GIOChannel *channel)
1911 /* Could we do something here? */
1912 return 0;
1915 static GIOFuncs win32_channel_msg_funcs = {
1916 g_io_win32_msg_read,
1917 g_io_win32_msg_write,
1918 NULL,
1919 g_io_win32_msg_close,
1920 g_io_win32_msg_create_watch,
1921 g_io_win32_free,
1922 g_io_win32_unimpl_set_flags,
1923 g_io_win32_msg_get_flags,
1926 static GIOFuncs win32_channel_fd_funcs = {
1927 g_io_win32_fd_and_console_read,
1928 g_io_win32_fd_and_console_write,
1929 g_io_win32_fd_seek,
1930 g_io_win32_fd_close,
1931 g_io_win32_fd_create_watch,
1932 g_io_win32_free,
1933 g_io_win32_unimpl_set_flags,
1934 g_io_win32_fd_get_flags,
1937 static GIOFuncs win32_channel_console_funcs = {
1938 g_io_win32_fd_and_console_read,
1939 g_io_win32_fd_and_console_write,
1940 NULL,
1941 g_io_win32_console_close,
1942 g_io_win32_console_create_watch,
1943 g_io_win32_free,
1944 g_io_win32_unimpl_set_flags,
1945 g_io_win32_console_get_flags,
1948 static GIOFuncs win32_channel_sock_funcs = {
1949 g_io_win32_sock_read,
1950 g_io_win32_sock_write,
1951 NULL,
1952 g_io_win32_sock_close,
1953 g_io_win32_sock_create_watch,
1954 g_io_win32_free,
1955 g_io_win32_sock_set_flags,
1956 g_io_win32_sock_get_flags,
1960 * g_io_channel_win32_new_messages:
1961 * @hwnd: a window handle.
1963 * Creates a new #GIOChannel given a window handle on Windows.
1965 * This function creates a #GIOChannel that can be used to poll for
1966 * Windows messages for the window in question.
1968 * Returns: a new #GIOChannel.
1970 GIOChannel *
1971 #if GLIB_SIZEOF_VOID_P == 8
1972 g_io_channel_win32_new_messages (gsize hwnd)
1973 #else
1974 g_io_channel_win32_new_messages (guint hwnd)
1975 #endif
1977 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1978 GIOChannel *channel = (GIOChannel *)win32_channel;
1980 g_io_channel_init (channel);
1981 g_io_channel_win32_init (win32_channel);
1982 if (win32_channel->debug)
1983 g_print ("g_io_channel_win32_new_messages: channel=%p hwnd=%p\n",
1984 channel, (HWND) hwnd);
1985 channel->funcs = &win32_channel_msg_funcs;
1986 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1987 win32_channel->hwnd = (HWND) hwnd;
1989 /* XXX: check this. */
1990 channel->is_readable = IsWindow (win32_channel->hwnd);
1991 channel->is_writeable = IsWindow (win32_channel->hwnd);
1993 channel->is_seekable = FALSE;
1995 return channel;
1998 static GIOChannel *
1999 g_io_channel_win32_new_fd_internal (gint fd,
2000 struct _stati64 *st)
2002 GIOWin32Channel *win32_channel;
2003 GIOChannel *channel;
2005 win32_channel = g_new (GIOWin32Channel, 1);
2006 channel = (GIOChannel *)win32_channel;
2008 g_io_channel_init (channel);
2009 g_io_channel_win32_init (win32_channel);
2011 win32_channel->fd = fd;
2013 if (win32_channel->debug)
2014 g_print ("g_io_channel_win32_new_fd: channel=%p fd=%u\n",
2015 channel, fd);
2017 if (st->st_mode & _S_IFCHR) /* console */
2019 channel->funcs = &win32_channel_console_funcs;
2020 win32_channel->type = G_IO_WIN32_CONSOLE;
2021 g_io_win32_console_get_flags_internal (channel);
2023 else
2025 channel->funcs = &win32_channel_fd_funcs;
2026 win32_channel->type = G_IO_WIN32_FILE_DESC;
2027 g_io_win32_fd_get_flags_internal (channel, st);
2030 return channel;
2034 * g_io_channel_win32_new_fd:
2035 * @fd: a C library file descriptor.
2037 * Creates a new #GIOChannel given a file descriptor on Windows. This
2038 * works for file descriptors from the C runtime.
2040 * This function works for file descriptors as returned by the open(),
2041 * creat(), pipe() and fileno() calls in the Microsoft C runtime. In
2042 * order to meaningfully use this function your code should use the
2043 * same C runtime as GLib uses, which is msvcrt.dll. Note that in
2044 * current Microsoft compilers it is near impossible to convince it to
2045 * build code that would use msvcrt.dll. The last Microsoft compiler
2046 * version that supported using msvcrt.dll as the C runtime was version 6.
2047 * The GNU compiler and toolchain for Windows, also known as Mingw,
2048 * fully supports msvcrt.dll.
2050 * If you have created a #GIOChannel for a file descriptor and started
2051 * watching (polling) it, you shouldn't call read() on the file
2052 * descriptor. This is because adding polling for a file descriptor is
2053 * implemented in GLib on Windows by starting a thread that sits
2054 * blocked in a read() from the file descriptor most of the time. All
2055 * reads from the file descriptor should be done by this internal GLib
2056 * thread. Your code should call only g_io_channel_read().
2058 * This function is available only in GLib on Windows.
2060 * Returns: a new #GIOChannel.
2062 GIOChannel *
2063 g_io_channel_win32_new_fd (gint fd)
2065 struct _stati64 st;
2067 if (_fstati64 (fd, &st) == -1)
2069 g_warning ("g_io_channel_win32_new_fd: %d isn't an open file descriptor in the C library GLib uses.", fd);
2070 return NULL;
2073 return g_io_channel_win32_new_fd_internal (fd, &st);
2076 gint
2077 g_io_channel_win32_get_fd (GIOChannel *channel)
2079 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2081 return win32_channel->fd;
2085 * g_io_channel_win32_new_socket:
2086 * @socket: a Winsock socket
2088 * Creates a new #GIOChannel given a socket on Windows.
2090 * This function works for sockets created by Winsock. It's available
2091 * only in GLib on Windows.
2093 * Polling a #GSource created to watch a channel for a socket puts the
2094 * socket in non-blocking mode. This is a side-effect of the
2095 * implementation and unavoidable.
2097 * Returns: a new #GIOChannel
2099 GIOChannel *
2100 g_io_channel_win32_new_socket (int socket)
2102 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
2103 GIOChannel *channel = (GIOChannel *)win32_channel;
2105 g_io_channel_init (channel);
2106 g_io_channel_win32_init (win32_channel);
2107 if (win32_channel->debug)
2108 g_print ("g_io_channel_win32_new_socket: channel=%p sock=%d\n",
2109 channel, socket);
2110 channel->funcs = &win32_channel_sock_funcs;
2111 win32_channel->type = G_IO_WIN32_SOCKET;
2112 win32_channel->fd = socket;
2114 channel->is_readable = TRUE;
2115 channel->is_writeable = TRUE;
2116 channel->is_seekable = FALSE;
2118 return channel;
2121 GIOChannel *
2122 g_io_channel_unix_new (gint fd)
2124 gboolean is_fd, is_socket;
2125 struct _stati64 st;
2126 int optval, optlen;
2128 is_fd = (_fstati64 (fd, &st) == 0);
2130 optlen = sizeof (optval);
2131 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
2133 if (is_fd && is_socket)
2134 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);
2136 if (is_fd)
2137 return g_io_channel_win32_new_fd_internal (fd, &st);
2139 if (is_socket)
2140 return g_io_channel_win32_new_socket(fd);
2142 g_warning ("g_io_channel_unix_new: %d is neither a file descriptor or a socket.", fd);
2144 return NULL;
2147 gint
2148 g_io_channel_unix_get_fd (GIOChannel *channel)
2150 return g_io_channel_win32_get_fd (channel);
2153 void
2154 g_io_channel_win32_set_debug (GIOChannel *channel,
2155 gboolean flag)
2157 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2159 win32_channel->debug = flag;
2162 gint
2163 g_io_channel_win32_poll (GPollFD *fds,
2164 gint n_fds,
2165 gint timeout)
2167 g_return_val_if_fail (n_fds >= 0, 0);
2169 return g_poll (fds, n_fds, timeout);
2172 void
2173 g_io_channel_win32_make_pollfd (GIOChannel *channel,
2174 GIOCondition condition,
2175 GPollFD *fd)
2177 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
2179 switch (win32_channel->type)
2181 case G_IO_WIN32_FILE_DESC:
2182 if (win32_channel->data_avail_event == NULL)
2183 create_events (win32_channel);
2185 fd->fd = (gintptr) win32_channel->data_avail_event;
2187 if (win32_channel->thread_id == 0)
2189 /* Is it meaningful for a file descriptor to be polled for
2190 * both IN and OUT? For what kind of file descriptor would
2191 * that be? Doesn't seem to make sense, in practise the file
2192 * descriptors handled here are always read or write ends of
2193 * pipes surely, and thus unidirectional.
2195 if (condition & G_IO_IN)
2196 create_thread (win32_channel, condition, read_thread);
2197 else if (condition & G_IO_OUT)
2198 create_thread (win32_channel, condition, write_thread);
2200 break;
2202 case G_IO_WIN32_CONSOLE:
2203 fd->fd = _get_osfhandle (win32_channel->fd);
2204 break;
2206 case G_IO_WIN32_SOCKET:
2207 fd->fd = (gintptr) WSACreateEvent ();
2208 break;
2210 case G_IO_WIN32_WINDOWS_MESSAGES:
2211 fd->fd = G_WIN32_MSG_HANDLE;
2212 break;
2214 default:
2215 g_assert_not_reached ();
2216 g_abort ();
2219 fd->events = condition;
2222 #ifndef _WIN64
2224 /* Binary compatibility */
2225 GIOChannel *
2226 g_io_channel_win32_new_stream_socket (int socket)
2228 return g_io_channel_win32_new_socket (socket);
2231 #endif
2233 #ifdef G_OS_WIN32
2235 /* Binary compatibility versions. Not for newly compiled code. */
2237 _GLIB_EXTERN GIOChannel *g_io_channel_new_file_utf8 (const gchar *filename,
2238 const gchar *mode,
2239 GError **error);
2241 GIOChannel *
2242 g_io_channel_new_file_utf8 (const gchar *filename,
2243 const gchar *mode,
2244 GError **error)
2246 return g_io_channel_new_file (filename, mode, error);
2249 #endif