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.
86 typedef struct _GIOWin32Channel GIOWin32Channel
;
87 typedef struct _GIOWin32Watch GIOWin32Watch
;
89 #define BUFFER_SIZE 4096
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
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
{
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
;
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
;
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).
148 * Full: (wrp + 1) % BUFFER_SIZE == rdp
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 */
159 gboolean write_would_have_blocked
;
160 gboolean ever_writable
;
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_WRITABLE
)
200 g_print ("%sWRITABLE", 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 ();
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
;
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
);
312 static unsigned __stdcall
313 read_thread (void *parameter
)
315 GIOWin32Channel
*channel
= parameter
;
319 g_io_channel_ref ((GIOChannel
*)channel
);
322 g_print ("read_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
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
)
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
)
345 g_print ("read_thread %#x: resetting space_avail\n",
347 ResetEvent (channel
->space_avail_event
);
349 g_print ("read_thread %#x: waiting for space\n",
351 LeaveCriticalSection (&channel
->mutex
);
352 WaitForSingleObject (channel
->space_avail_event
, INFINITE
);
353 EnterCriticalSection (&channel
->mutex
);
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
);
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
;
379 channel
->revents
|= G_IO_HUP
;
381 channel
->revents
|= G_IO_ERR
;
384 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
385 channel
->thread_id
, nbytes
, channel
->rdp
, channel
->wrp
);
390 channel
->wrp
= (channel
->wrp
+ nbytes
) % BUFFER_SIZE
;
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
)
401 g_print ("read_thread %#x: channel fd %d needs closing\n",
402 channel
->thread_id
, channel
->fd
);
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.
423 static unsigned __stdcall
424 write_thread (void *parameter
)
426 GIOWin32Channel
*channel
= parameter
;
430 g_io_channel_ref ((GIOChannel
*)channel
);
433 g_print ("write_thread %#x: start fd=%d, data_avail=%p space_avail=%p\n",
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
452 EnterCriticalSection (&channel
->mutex
);
453 while (channel
->running
|| channel
->rdp
!= channel
->wrp
)
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. */
462 g_print ("write_thread %#x: resetting space_avail\n",
464 ResetEvent (channel
->space_avail_event
);
466 g_print ("write_thread %#x: waiting for data\n",
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
)
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
;
486 nbytes
= BUFFER_SIZE
- channel
->rdp
;
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
);
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;
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
;
512 g_print ("write_thread: setting data_avail for thread %#x\n",
514 SetEvent (channel
->data_avail_event
);
517 channel
->running
= FALSE
;
518 if (channel
->needs_close
)
521 g_print ("write_thread %#x: channel fd %d needs closing\n",
522 channel
->thread_id
, channel
->fd
);
527 LeaveCriticalSection (&channel
->mutex
);
529 g_io_channel_unref ((GIOChannel
*)channel
);
535 create_thread (GIOWin32Channel
*channel
,
536 GIOCondition condition
,
537 unsigned (__stdcall
*thread
) (void *parameter
))
539 HANDLE thread_handle
;
542 thread_handle
= (HANDLE
) _beginthreadex (NULL
, 0, thread
, channel
, 0,
543 &channel
->thread_id
);
545 if (thread_handle
== 0)
546 g_warning ("Error creating thread: %s.",
548 else if (!CloseHandle (thread_handle
))
550 gchar
*emsg
= g_win32_error_message (GetLastError ());
552 g_warning ("Error closing thread handle: %s.", emsg
);
556 WaitForSingleObject (channel
->space_avail_event
, INFINITE
);
560 buffer_read (GIOWin32Channel
*channel
,
569 EnterCriticalSection (&channel
->mutex
);
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
);
578 g_print ("waiting for data from thread %#x\n", channel
->thread_id
);
579 WaitForSingleObject (channel
->data_avail_event
, INFINITE
);
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
)
586 g_print ("wrp==rdp, !running\n");
587 LeaveCriticalSection (&channel
->mutex
);
589 return G_IO_STATUS_EOF
;
593 if (channel
->rdp
< channel
->wrp
)
594 nbytes
= channel
->wrp
- channel
->rdp
;
596 nbytes
= BUFFER_SIZE
- channel
->rdp
;
597 LeaveCriticalSection (&channel
->mutex
);
598 nbytes
= MIN (left
, nbytes
);
600 g_print ("moving %d bytes from thread %#x\n",
601 nbytes
, channel
->thread_id
);
602 memcpy (dest
, channel
->buffer
+ channel
->rdp
, nbytes
);
605 EnterCriticalSection (&channel
->mutex
);
606 channel
->rdp
= (channel
->rdp
+ nbytes
) % BUFFER_SIZE
;
608 g_print ("setting space_avail for thread %#x\n", channel
->thread_id
);
609 SetEvent (channel
->space_avail_event
);
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
)
616 g_print ("resetting data_avail of thread %#x\n",
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
;
631 buffer_write (GIOWin32Channel
*channel
,
634 gsize
*bytes_written
,
640 EnterCriticalSection (&channel
->mutex
);
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
)
649 g_print ("buffer_write: tid %#x: resetting data_avail\n",
651 ResetEvent (channel
->data_avail_event
);
653 g_print ("buffer_write: tid %#x: waiting for space\n",
655 LeaveCriticalSection (&channel
->mutex
);
656 WaitForSingleObject (channel
->data_avail_event
, INFINITE
);
657 EnterCriticalSection (&channel
->mutex
);
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
);
669 g_print ("buffer_write: tid %#x: writing %d bytes\n",
670 channel
->thread_id
, nbytes
);
671 memcpy (channel
->buffer
+ channel
->wrp
, dest
, nbytes
);
674 EnterCriticalSection (&channel
->mutex
);
676 channel
->wrp
= (channel
->wrp
+ nbytes
) % BUFFER_SIZE
;
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
)
686 g_print ("buffer_write: tid %#x: resetting data_avail\n",
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
;
702 g_io_win32_prepare (GSource
*source
,
705 GIOWin32Watch
*watch
= (GIOWin32Watch
*)source
;
706 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
707 GIOWin32Channel
*channel
= (GIOWin32Channel
*)watch
->channel
;
713 g_print ("g_io_win32_prepare: source=%p channel=%p", source
, channel
);
715 switch (channel
->type
)
717 case G_IO_WIN32_WINDOWS_MESSAGES
:
722 case G_IO_WIN32_CONSOLE
:
727 case G_IO_WIN32_FILE_DESC
:
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
)
742 g_print ("\n setting revents=0");
743 channel
->revents
= 0;
748 if (channel
->direction
== 1
749 && (channel
->wrp
+ 1) % BUFFER_SIZE
== channel
->rdp
)
752 g_print ("\n setting revents=0");
753 channel
->revents
= 0;
756 LeaveCriticalSection (&channel
->mutex
);
759 case G_IO_WIN32_SOCKET
:
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
)
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
)
779 gchar
*emsg
= g_win32_error_message (WSAGetLastError ());
781 g_print (" failed: %s", emsg
);
784 channel
->event_mask
= event_mask
;
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
)
795 g_print (" WSASetEvent(%p)", (WSAEVENT
) watch
->pollfd
.fd
);
796 WSASetEvent ((WSAEVENT
) watch
->pollfd
.fd
);
802 g_assert_not_reached ();
808 return ((watch
->condition
& buffer_condition
) == watch
->condition
);
812 g_io_win32_check (GSource
*source
)
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
;
821 g_print ("g_io_win32_check: source=%p channel=%p", source
, channel
);
823 switch (channel
->type
)
825 case G_IO_WIN32_WINDOWS_MESSAGES
:
828 return (PeekMessage (&msg
, channel
->hwnd
, 0, 0, PM_NOREMOVE
));
830 case G_IO_WIN32_FILE_DESC
:
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
:
846 if (watch
->channel
->is_writeable
)
848 else if (watch
->channel
->is_readable
)
852 if (PeekConsoleInput ((HANDLE
) watch
->pollfd
.fd
, &buffer
, 1, &n
) &&
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.
862 /* Discard all other kinds of events */
863 ReadConsoleInput ((HANDLE
) watch
->pollfd
.fd
, &buffer
, 1, &n
);
868 case G_IO_WIN32_SOCKET
:
871 if (channel
->last_events
& FD_WRITE
)
874 g_print (" sock=%d event=%p last_events has FD_WRITE",
875 channel
->fd
, (HANDLE
) watch
->pollfd
.fd
);
879 WSAEnumNetworkEvents (channel
->fd
, 0, &events
);
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
),
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;
895 g_print ("\n WSAEventSelect(%d,%p,{})",
896 channel
->fd
, (HANDLE
) watch
->pollfd
.fd
);
897 WSAEventSelect (channel
->fd
, (HANDLE
) watch
->pollfd
.fd
, 0);
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
;
916 /* We have called WSAEnumNetworkEvents() above but it didn't
919 if (events
.lNetworkEvents
& FD_CONNECT
)
921 if (events
.iErrorCode
[FD_CONNECT_BIT
] == 0)
922 watch
->pollfd
.revents
|= G_IO_OUT
;
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
;
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
);
949 g_assert_not_reached ();
955 g_io_win32_dispatch (GSource
*source
,
956 GSourceFunc callback
,
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
);
966 g_warning ("IO Watch dispatched without callback. "
967 "You must call g_source_connect().");
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
,
983 g_io_win32_finalize (GSource
*source
)
985 GIOWin32Watch
*watch
= (GIOWin32Watch
*)source
;
986 GIOWin32Channel
*channel
= (GIOWin32Channel
*)watch
->channel
;
989 g_print ("g_io_win32_finalize: source=%p channel=%p", source
, channel
);
991 switch (channel
->type
)
993 case G_IO_WIN32_WINDOWS_MESSAGES
:
998 case G_IO_WIN32_CONSOLE
:
1003 case G_IO_WIN32_FILE_DESC
:
1005 g_print (" FD thread=%#x", channel
->thread_id
);
1008 case G_IO_WIN32_SOCKET
:
1010 g_print (" SOCK sock=%d", channel
->fd
);
1014 g_assert_not_reached ();
1019 g_io_channel_unref (watch
->channel
);
1022 GSourceFuncs g_io_watch_funcs
= {
1025 g_io_win32_dispatch
,
1030 g_io_win32_msg_read (GIOChannel
*channel
,
1036 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1037 MSG msg
; /* In case of alignment problems */
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
;
1061 g_io_win32_msg_write (GIOChannel
*channel
,
1064 gsize
*bytes_written
,
1067 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
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
);
1088 return G_IO_STATUS_ERROR
;
1091 *bytes_written
= sizeof (MSG
);
1093 return G_IO_STATUS_NORMAL
;
1097 g_io_win32_msg_close (GIOChannel
*channel
,
1100 /* Nothing to be done. Or should we set hwnd to some invalid value? */
1102 return G_IO_STATUS_NORMAL
;
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
);
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
);
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
);
1151 if (win32_channel
->event
)
1152 if (!WSACloseEvent (win32_channel
->event
))
1153 if (win32_channel
->debug
)
1155 gchar
*emsg
= g_win32_error_message (WSAGetLastError ());
1157 g_print (" WSACloseEvent(%p) failed: %s\n",
1158 win32_channel
->event
, emsg
);
1162 g_free (win32_channel
);
1166 g_io_win32_msg_create_watch (GIOChannel
*channel
,
1167 GIOCondition condition
)
1169 GIOWin32Watch
*watch
;
1172 source
= g_source_new (&g_io_watch_funcs
, sizeof (GIOWin32Watch
));
1173 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
);
1190 g_io_win32_fd_and_console_read (GIOChannel
*channel
,
1196 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
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
);
1212 if (win32_channel
->debug
)
1213 g_print ("g_io_win32_fd_read: read() => %d\n", result
);
1223 return G_IO_STATUS_AGAIN
;
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
;
1239 g_io_win32_fd_and_console_write (GIOChannel
*channel
,
1242 gsize
*bytes_written
,
1245 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
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
);
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
);
1269 return G_IO_STATUS_AGAIN
;
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
;
1285 g_io_win32_fd_seek (GIOChannel
*channel
,
1290 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1307 whence
= -1; /* Keep the compiler quiet */
1308 g_assert_not_reached ();
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
);
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
;
1336 g_io_win32_fd_close (GIOChannel
*channel
,
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
,
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
);
1356 SetEvent (win32_channel
->space_avail_event
);
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",
1366 win32_channel
->fd
= -1;
1368 LeaveCriticalSection (&win32_channel
->mutex
);
1370 /* FIXME error detection? */
1372 return G_IO_STATUS_NORMAL
;
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
);
1415 g_io_win32_console_close (GIOChannel
*channel
,
1418 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1420 if (close (win32_channel
->fd
) < 0)
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
;
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
);
1454 g_io_win32_sock_read (GIOChannel
*channel
,
1460 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1462 GIOChannelError 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
);
1485 switch (winsock_error
)
1488 error
= G_IO_CHANNEL_ERROR_INVAL
;
1490 case WSAEWOULDBLOCK
:
1492 return G_IO_STATUS_AGAIN
;
1494 error
= G_IO_CHANNEL_ERROR_FAILED
;
1497 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
, error
, emsg
);
1500 return G_IO_STATUS_ERROR
;
1504 if (win32_channel
->debug
)
1506 *bytes_read
= result
;
1508 return G_IO_STATUS_EOF
;
1510 return G_IO_STATUS_NORMAL
;
1515 g_io_win32_sock_write (GIOChannel
*channel
,
1518 gsize
*bytes_written
,
1521 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1523 GIOChannelError 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
);
1546 switch (winsock_error
)
1549 error
= G_IO_CHANNEL_ERROR_INVAL
;
1551 case WSAEWOULDBLOCK
:
1552 win32_channel
->write_would_have_blocked
= TRUE
;
1553 win32_channel
->last_events
= 0;
1555 return G_IO_STATUS_AGAIN
;
1557 error
= G_IO_CHANNEL_ERROR_FAILED
;
1560 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
, error
, emsg
);
1563 return G_IO_STATUS_ERROR
;
1567 if (win32_channel
->debug
)
1569 *bytes_written
= result
;
1570 win32_channel
->write_would_have_blocked
= FALSE
;
1572 return G_IO_STATUS_NORMAL
;
1577 g_io_win32_sock_close (GIOChannel
*channel
,
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
;
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
);
1627 g_io_channel_new_file (const gchar
*filename
,
1631 int fid
, flags
, pmode
;
1632 GIOChannel
*channel
;
1634 enum { /* Cheesy hack */
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
);
1658 g_warning ("Invalid GIOFileMode %s.", mode
);
1667 if (mode
[2] == '\0')
1669 mode_num
|= MODE_PLUS
;
1674 g_warning ("Invalid GIOFileMode %s.", mode
);
1685 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
1689 flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1692 case MODE_R
| MODE_PLUS
:
1694 pmode
= _S_IREAD
| _S_IWRITE
;
1696 case MODE_W
| MODE_PLUS
:
1697 flags
= O_RDWR
| O_TRUNC
| O_CREAT
;
1698 pmode
= _S_IREAD
| _S_IWRITE
;
1700 case MODE_A
| MODE_PLUS
:
1701 flags
= O_RDWR
| O_APPEND
| O_CREAT
;
1702 pmode
= _S_IREAD
| _S_IWRITE
;
1705 g_assert_not_reached ();
1709 /* always open 'untranslated' */
1710 fid
= g_open (filename
, flags
| _O_BINARY
, pmode
);
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
);
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
1741 channel
->is_writeable
= FALSE
;
1745 channel
->is_readable
= FALSE
;
1747 case MODE_R
| MODE_PLUS
:
1748 case MODE_W
| MODE_PLUS
:
1749 case MODE_A
| MODE_PLUS
:
1752 g_assert_not_reached ();
1760 g_io_win32_unimpl_set_flags (GIOChannel
*channel
,
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
);
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
;
1781 g_io_win32_fd_get_flags_internal (GIOChannel
*channel
,
1782 struct _stati64
*st
)
1784 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*) channel
;
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
;
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 */
1812 g_io_win32_fd_get_flags (GIOChannel
*channel
)
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
);
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
);
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
;
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
);
1854 g_io_win32_msg_get_flags (GIOChannel
*channel
)
1860 g_io_win32_sock_set_flags (GIOChannel
*channel
,
1864 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
1867 if (win32_channel
->debug
)
1869 g_print ("g_io_win32_sock_set_flags: ");
1870 g_win32_print_gioflags (flags
);
1874 if (flags
& G_IO_FLAG_NONBLOCK
)
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
,
1886 return G_IO_STATUS_ERROR
;
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
,
1901 return G_IO_STATUS_ERROR
;
1905 return G_IO_STATUS_NORMAL
;
1909 g_io_win32_sock_get_flags (GIOChannel
*channel
)
1911 /* Could we do something here? */
1915 static GIOFuncs win32_channel_msg_funcs
= {
1916 g_io_win32_msg_read
,
1917 g_io_win32_msg_write
,
1919 g_io_win32_msg_close
,
1920 g_io_win32_msg_create_watch
,
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
,
1930 g_io_win32_fd_close
,
1931 g_io_win32_fd_create_watch
,
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
,
1941 g_io_win32_console_close
,
1942 g_io_win32_console_create_watch
,
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
,
1952 g_io_win32_sock_close
,
1953 g_io_win32_sock_create_watch
,
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.
1971 #if GLIB_SIZEOF_VOID_P == 8
1972 g_io_channel_win32_new_messages (gsize hwnd
)
1974 g_io_channel_win32_new_messages (guint hwnd
)
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
;
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",
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
);
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
);
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.
2063 g_io_channel_win32_new_fd (gint fd
)
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
);
2073 return g_io_channel_win32_new_fd_internal (fd
, &st
);
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
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",
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
;
2122 g_io_channel_unix_new (gint fd
)
2124 gboolean is_fd
, is_socket
;
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
);
2137 return g_io_channel_win32_new_fd_internal (fd
, &st
);
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
);
2148 g_io_channel_unix_get_fd (GIOChannel
*channel
)
2150 return g_io_channel_win32_get_fd (channel
);
2154 g_io_channel_win32_set_debug (GIOChannel
*channel
,
2157 GIOWin32Channel
*win32_channel
= (GIOWin32Channel
*)channel
;
2159 win32_channel
->debug
= flag
;
2163 g_io_channel_win32_poll (GPollFD
*fds
,
2167 g_return_val_if_fail (n_fds
>= 0, 0);
2169 return g_poll (fds
, n_fds
, timeout
);
2173 g_io_channel_win32_make_pollfd (GIOChannel
*channel
,
2174 GIOCondition condition
,
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
);
2202 case G_IO_WIN32_CONSOLE
:
2203 fd
->fd
= _get_osfhandle (win32_channel
->fd
);
2206 case G_IO_WIN32_SOCKET
:
2207 fd
->fd
= (gintptr
) WSACreateEvent ();
2210 case G_IO_WIN32_WINDOWS_MESSAGES
:
2211 fd
->fd
= G_WIN32_MSG_HANDLE
;
2215 g_assert_not_reached ();
2219 fd
->events
= condition
;
2224 /* Binary compatibility */
2226 g_io_channel_win32_new_stream_socket (int socket
)
2228 return g_io_channel_win32_new_socket (socket
);
2235 /* Binary compatibility versions. Not for newly compiled code. */
2237 _GLIB_EXTERN GIOChannel
*g_io_channel_new_file_utf8 (const gchar
*filename
,
2242 g_io_channel_new_file_utf8 (const gchar
*filename
,
2246 return g_io_channel_new_file (filename
, mode
, error
);