1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giowin32.c: IO Channels for Win32.
5 * Copyright 1998 Owen Taylor and Tor Lillqvist
6 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
7 * Copyright 2001-2003 Andrew Lanoix
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GLib Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GLib at ftp://ftp.gtk.org/pub/gtk/.
32 /* Define this to get (very) verbose logging of all channels */
33 /* #define G_IO_WIN32_DEBUG */
42 #include <winsock.h> /* Not everybody has winsock2 */
51 typedef struct _GIOWin32Channel GIOWin32Channel;
52 typedef struct _GIOWin32Watch GIOWin32Watch;
54 #define BUFFER_SIZE 4096
57 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
58 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
59 * _open() or _pipe(). Read with read().
60 * Have to create separate thread to read.
62 G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked
63 * in select() most of the time.
65 } GIOWin32ChannelType;
67 struct _GIOWin32Channel {
69 gint fd; /* Either a Unix-like file handle as provided
70 * by the Microsoft C runtime, or a SOCKET
71 * as provided by WinSock.
73 GIOWin32ChannelType type;
77 CRITICAL_SECTION mutex;
79 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
80 HWND hwnd; /* handle of window, or NULL */
82 /* Following fields are used by both fd and socket channels. */
83 gboolean running; /* Is reader thread running. FALSE if
84 * EOF has been reached.
86 gboolean needs_close; /* If the channel has been closed while
87 * the reader thread was still running.
89 guint thread_id; /* If non-NULL has a reader thread, or has
91 HANDLE data_avail_event;
95 /* Following fields used by fd channels for input */
97 /* Data is kept in a circular buffer. To be able to distinguish between
98 * empty and full buffer, we cannot fill it completely, but have to
99 * leave a one character gap.
101 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
104 * Full: (wrp + 1) % BUFFER_SIZE == rdp
107 guchar *buffer; /* (Circular) buffer */
108 gint wrp, rdp; /* Buffer indices for writing and reading */
109 HANDLE space_avail_event;
111 /* Following fields used by socket channels */
113 HANDLE data_avail_noticed_event;
114 gint reset_send; /* socket used to send data so select_thread() can reset/re-loop */
115 gint reset_recv; /* socket used to recv data so select_thread() can reset/re-loop */
118 #define LOCK(mutex) EnterCriticalSection (&mutex)
119 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
121 struct _GIOWin32Watch {
125 GIOCondition condition;
129 g_win32_print_access_mode (int flags)
131 g_print ("%s%s%s%s%s%s%s%s%s%s",
132 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
133 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
134 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
135 (flags & _O_APPEND ? "|O_APPEND" : ""),
136 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
137 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
138 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
139 (flags & _O_CREAT ? "|O_CREAT" : ""),
140 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
141 (flags & _O_EXCL ? "|O_EXCL" : ""),
142 (flags & _O_TEXT ? "|O_TEXT" : ""),
143 (flags & _O_BINARY ? "|O_BINARY" : ""));
147 g_win32_print_gioflags (GIOFlags flags)
151 if (flags & G_IO_FLAG_APPEND)
152 bar = "|", g_print ("APPEND");
153 if (flags & G_IO_FLAG_NONBLOCK)
154 g_print ("%sNONBLOCK", bar), bar = "|";
155 if (flags & G_IO_FLAG_IS_READABLE)
156 g_print ("%sREADABLE", bar), bar = "|";
157 if (flags & G_IO_FLAG_IS_WRITEABLE)
158 g_print ("%sWRITEABLE", bar), bar = "|";
159 if (flags & G_IO_FLAG_IS_SEEKABLE)
160 g_print ("%sSEEKABLE", bar), bar = "|";
164 g_io_win32_get_debug_flag (void)
166 #ifdef G_IO_WIN32_DEBUG
169 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
177 g_io_channel_win32_init (GIOWin32Channel *channel)
179 channel->debug = g_io_win32_get_debug_flag ();
180 channel->buffer = NULL;
181 channel->running = FALSE;
182 channel->needs_close = FALSE;
183 channel->thread_id = 0;
184 channel->data_avail_event = NULL;
185 channel->revents = 0;
186 channel->space_avail_event = NULL;
187 channel->reset_send = INVALID_SOCKET;
188 channel->reset_recv = INVALID_SOCKET;
189 channel->data_avail_noticed_event = NULL;
190 channel->watches = NULL;
191 InitializeCriticalSection (&channel->mutex);
195 create_events (GIOWin32Channel *channel)
197 SECURITY_ATTRIBUTES sec_attrs;
199 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
200 sec_attrs.lpSecurityDescriptor = NULL;
201 sec_attrs.bInheritHandle = FALSE;
203 /* The data available event is manual reset, the space available event
204 * is automatic reset.
206 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
207 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
208 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
210 gchar *emsg = g_win32_error_message (GetLastError ());
211 g_error ("Error creating event: %s", emsg);
216 static unsigned __stdcall
217 read_thread (void *parameter)
219 GIOWin32Channel *channel = parameter;
223 g_io_channel_ref ((GIOChannel *)channel);
226 g_print ("read_thread %#x: start fd:%d, data_avail:%#x space_avail:%#x\n",
229 (guint) channel->data_avail_event,
230 (guint) channel->space_avail_event);
232 channel->buffer = g_malloc (BUFFER_SIZE);
233 channel->rdp = channel->wrp = 0;
234 channel->running = TRUE;
236 SetEvent (channel->space_avail_event);
238 LOCK (channel->mutex);
239 while (channel->running)
242 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
243 channel->thread_id, channel->rdp, channel->wrp);
244 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
248 g_print ("read_thread %#x: resetting space_avail\n",
250 ResetEvent (channel->space_avail_event);
252 g_print ("read_thread %#x: waiting for space\n",
254 UNLOCK (channel->mutex);
255 WaitForSingleObject (channel->space_avail_event, INFINITE);
256 LOCK (channel->mutex);
258 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
259 channel->thread_id, channel->rdp, channel->wrp);
262 buffer = channel->buffer + channel->wrp;
264 /* Always leave at least one byte unused gap to be able to
265 * distinguish between the full and empty condition...
267 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
268 BUFFER_SIZE - channel->wrp);
271 g_print ("read_thread %#x: calling read() for %d bytes\n",
272 channel->thread_id, nbytes);
274 UNLOCK (channel->mutex);
276 nbytes = read (channel->fd, buffer, nbytes);
278 LOCK (channel->mutex);
280 channel->revents = G_IO_IN;
282 channel->revents |= G_IO_HUP;
284 channel->revents |= G_IO_ERR;
287 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
288 channel->thread_id, nbytes, channel->rdp, channel->wrp);
293 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
295 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
296 channel->thread_id, channel->rdp, channel->wrp);
297 SetEvent (channel->data_avail_event);
300 channel->running = FALSE;
301 if (channel->needs_close)
304 g_print ("read_thread %#x: channel fd %d needs closing\n",
305 channel->thread_id, channel->fd);
311 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
312 channel->thread_id, channel->rdp, channel->wrp);
313 SetEvent (channel->data_avail_event);
314 UNLOCK (channel->mutex);
316 g_io_channel_unref ((GIOChannel *)channel);
318 /* No need to call _endthreadex(), the actual thread starter routine
319 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
320 * _endthreadex() for us.
327 create_thread (GIOWin32Channel *channel,
328 GIOCondition condition,
329 unsigned (__stdcall *thread) (void *parameter))
331 HANDLE thread_handle;
333 thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
334 &channel->thread_id);
335 if (thread_handle == 0)
336 g_warning (G_STRLOC ": Error creating reader thread: %s",
338 else if (!CloseHandle (thread_handle))
339 g_warning (G_STRLOC ": Error closing thread handle: %s\n",
340 g_win32_error_message (GetLastError ()));
342 WaitForSingleObject (channel->space_avail_event, INFINITE);
346 init_reset_sockets (GIOWin32Channel *channel)
348 struct sockaddr_in local, local2, server;
351 channel->reset_send = (gint) socket (AF_INET, SOCK_DGRAM, 0);
352 if (channel->reset_send == INVALID_SOCKET)
354 g_warning (G_STRLOC ": Error creating reset_send socket: %s\n",
355 g_win32_error_message (WSAGetLastError ()));
358 local.sin_family = AF_INET;
360 local.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
362 if (bind (channel->reset_send, (struct sockaddr *)&local, sizeof (local)) == SOCKET_ERROR)
364 g_warning (G_STRLOC ": Error binding to reset_send socket: %s\n",
365 g_win32_error_message (WSAGetLastError ()));
368 local2.sin_family = AF_INET;
370 local2.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
372 channel->reset_recv = (gint) socket (AF_INET, SOCK_DGRAM, 0);
373 if (channel->reset_recv == INVALID_SOCKET)
375 g_warning (G_STRLOC ": Error creating reset_recv socket: %s\n",
376 g_win32_error_message (WSAGetLastError ()));
379 if (bind (channel->reset_recv, (struct sockaddr *)&local2, sizeof (local)) == SOCKET_ERROR)
381 g_warning (G_STRLOC ": Error binding to reset_recv socket: %s\n",
382 g_win32_error_message (WSAGetLastError ()));
385 len = sizeof (local2);
386 if (getsockname (channel->reset_recv, (struct sockaddr *)&local2, &len) == SOCKET_ERROR)
388 g_warning (G_STRLOC ": Error getsockname with reset_recv socket: %s\n",
389 g_win32_error_message (WSAGetLastError ()));
392 memset (&server, 0, sizeof (server));
393 server.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
394 server.sin_family = AF_INET;
395 server.sin_port = local2.sin_port;
397 if (connect (channel->reset_send, (struct sockaddr *)&server, sizeof (server)) == SOCKET_ERROR)
399 g_warning (G_STRLOC ": connect to reset_recv socket: %s\n",
400 g_win32_error_message (WSAGetLastError ()));
406 buffer_read (GIOWin32Channel *channel,
415 LOCK (channel->mutex);
417 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
418 channel->thread_id, count, channel->rdp, channel->wrp);
420 if (channel->wrp == channel->rdp)
422 UNLOCK (channel->mutex);
424 g_print ("waiting for data from thread %#x\n", channel->thread_id);
425 WaitForSingleObject (channel->data_avail_event, INFINITE);
427 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
428 LOCK (channel->mutex);
429 if (channel->wrp == channel->rdp && !channel->running)
432 g_print ("wrp==rdp, !running\n");
433 UNLOCK (channel->mutex);
435 return G_IO_STATUS_EOF;
439 if (channel->rdp < channel->wrp)
440 nbytes = channel->wrp - channel->rdp;
442 nbytes = BUFFER_SIZE - channel->rdp;
443 UNLOCK (channel->mutex);
444 nbytes = MIN (left, nbytes);
446 g_print ("moving %d bytes from thread %#x\n",
447 nbytes, channel->thread_id);
448 memcpy (dest, channel->buffer + channel->rdp, nbytes);
451 LOCK (channel->mutex);
452 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
454 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
455 SetEvent (channel->space_avail_event);
457 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
458 channel->thread_id, channel->rdp, channel->wrp);
459 if (channel->running && channel->wrp == channel->rdp)
462 g_print ("resetting data_avail of thread %#x\n",
464 ResetEvent (channel->data_avail_event);
466 UNLOCK (channel->mutex);
468 /* We have no way to indicate any errors form the actual
469 * read() or recv() call in the reader thread. Should we have?
471 *bytes_read = count - left;
472 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
475 static unsigned __stdcall
476 select_thread (void *parameter)
478 GIOWin32Channel *channel = parameter;
479 fd_set read_fds, write_fds, except_fds;
484 g_io_channel_ref ((GIOChannel *)channel);
487 g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n",
490 (guint) channel->data_avail_event,
491 (guint) channel->data_avail_noticed_event);
493 channel->rdp = channel->wrp = 0;
494 channel->running = TRUE;
496 SetEvent (channel->space_avail_event);
498 while (channel->running)
501 FD_ZERO (&write_fds);
502 FD_ZERO (&except_fds);
503 FD_SET (channel->reset_recv, &read_fds);
505 LOCK (channel->mutex);
506 tmp = channel->watches;
509 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
511 if (watch->condition & (G_IO_IN | G_IO_HUP))
512 FD_SET (channel->fd, &read_fds);
513 if (watch->condition & G_IO_OUT)
514 FD_SET (channel->fd, &write_fds);
515 if (watch->condition & G_IO_ERR)
516 FD_SET (channel->fd, &except_fds);
520 UNLOCK (channel->mutex);
523 g_print ("select_thread %#x: calling select() for%s%s%s\n",
525 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
526 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
527 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
529 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
531 LOCK (channel->mutex);
532 if (channel->needs_close)
534 UNLOCK (channel->mutex);
537 UNLOCK (channel->mutex);
539 if (n == SOCKET_ERROR)
542 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
547 if (FD_ISSET (channel->reset_recv, &read_fds))
550 g_print ("select_thread %#x: re-looping\n",
552 recv (channel->reset_recv, (char *)&buffer, (int) sizeof (buffer), 0);
557 g_print ("select_thread %#x: got%s%s%s\n",
559 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
560 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
561 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
563 if (FD_ISSET (channel->fd, &read_fds))
564 channel->revents |= G_IO_IN;
565 if (FD_ISSET (channel->fd, &write_fds))
566 channel->revents |= G_IO_OUT;
567 if (FD_ISSET (channel->fd, &except_fds))
568 channel->revents |= G_IO_ERR;
571 g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
574 LOCK (channel->mutex);
575 ResetEvent (channel->data_avail_noticed_event);
576 SetEvent (channel->data_avail_event);
577 if (channel->needs_close)
579 UNLOCK (channel->mutex);
582 UNLOCK (channel->mutex);
585 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
588 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
590 g_print ("select_thread %#x: got data_avail_noticed\n",
594 LOCK (channel->mutex);
595 channel->running = FALSE;
597 g_print ("select_thread %#x: got error, setting data_avail\n",
599 SetEvent (channel->data_avail_event);
600 g_io_channel_unref ((GIOChannel *)channel);
601 UNLOCK (channel->mutex);
603 /* No need to call _endthreadex(), the actual thread starter routine
604 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
605 * _endthreadex() for us.
612 g_io_win32_prepare (GSource *source,
615 GIOWin32Watch *watch = (GIOWin32Watch *)source;
616 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
617 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
622 g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n"
623 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
624 channel->thread_id, buffer_condition,
625 watch->pollfd.events, watch->pollfd.revents, channel->revents);
627 if (channel->type == G_IO_WIN32_FILE_DESC)
629 LOCK (channel->mutex);
630 if (channel->running && channel->wrp == channel->rdp)
633 g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n",
635 channel->revents = 0;
637 UNLOCK (channel->mutex);
639 else if (channel->type == G_IO_WIN32_SOCKET)
641 LOCK (channel->mutex);
642 channel->revents = 0;
644 g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n",
646 SetEvent (channel->data_avail_noticed_event);
648 g_print ("g_io_win32_prepare: thread %#x, there.\n",
650 UNLOCK (channel->mutex);
653 return ((watch->condition & buffer_condition) == watch->condition);
657 g_io_win32_check (GSource *source)
660 GIOWin32Watch *watch = (GIOWin32Watch *)source;
661 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
662 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
665 g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n"
666 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
667 channel->thread_id, buffer_condition,
668 watch->pollfd.events, watch->pollfd.revents, channel->revents);
670 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
672 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
676 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
679 if (channel->type == G_IO_WIN32_SOCKET)
681 LOCK (channel->mutex);
683 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
685 ResetEvent (channel->data_avail_event);
687 g_print ("g_io_win32_check: thread %#x, there.\n",
689 UNLOCK (channel->mutex);
692 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
696 g_io_win32_dispatch (GSource *source,
697 GSourceFunc callback,
700 GIOFunc func = (GIOFunc)callback;
701 GIOWin32Watch *watch = (GIOWin32Watch *)source;
702 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
706 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
707 "You must call g_source_connect().");
711 return (*func) (watch->channel,
712 (watch->pollfd.revents | buffer_condition) & watch->condition,
717 g_io_win32_finalize (GSource *source)
719 GIOWin32Watch *watch = (GIOWin32Watch *)source;
720 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
721 char send_buffer[] = "f";
723 LOCK (channel->mutex);
725 g_print ("g_io_win32_finalize: channel with thread %#x\n",
728 channel->watches = g_slist_remove (channel->watches, watch);
730 SetEvent (channel->data_avail_noticed_event);
731 if (channel->type == G_IO_WIN32_SOCKET)
733 /* Tell select_thread() to exit */
734 channel->needs_close = 1;
735 /* Wake up select_thread() from its blocking select() */
736 send (channel->reset_send, send_buffer, sizeof (send_buffer), 0);
739 g_io_channel_unref (watch->channel);
740 UNLOCK (channel->mutex);
743 GSourceFuncs g_io_watch_funcs = {
751 g_io_win32_create_watch (GIOChannel *channel,
752 GIOCondition condition,
753 unsigned (__stdcall *thread) (void *parameter))
755 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
756 GIOWin32Watch *watch;
758 char send_buffer[] = "c";
760 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
761 watch = (GIOWin32Watch *)source;
763 watch->channel = channel;
764 g_io_channel_ref (channel);
766 watch->condition = condition;
768 if (win32_channel->data_avail_event == NULL)
769 create_events (win32_channel);
771 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
772 watch->pollfd.events = condition;
774 if (win32_channel->debug)
775 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
776 win32_channel->fd, condition, watch->pollfd.fd);
778 LOCK (win32_channel->mutex);
779 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
781 if (win32_channel->thread_id == 0)
782 create_thread (win32_channel, condition, thread);
784 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
786 g_source_add_poll (source, &watch->pollfd);
787 UNLOCK (win32_channel->mutex);
793 g_io_win32_msg_read (GIOChannel *channel,
799 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
800 MSG msg; /* In case of alignment problems */
802 if (count < sizeof (MSG))
804 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
805 "Incorrect message size"); /* Informative enough error message? */
806 return G_IO_STATUS_ERROR;
809 if (win32_channel->debug)
810 g_print ("g_io_win32_msg_read: for %#x\n",
811 (guint) win32_channel->hwnd);
812 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
813 return G_IO_STATUS_AGAIN;
815 memmove (buf, &msg, sizeof (MSG));
816 *bytes_read = sizeof (MSG);
818 return G_IO_STATUS_NORMAL;
822 g_io_win32_msg_write (GIOChannel *channel,
825 gsize *bytes_written,
828 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
831 if (count != sizeof (MSG))
833 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
834 "Incorrect message size"); /* Informative enough error message? */
835 return G_IO_STATUS_ERROR;
838 /* In case of alignment problems */
839 memmove (&msg, buf, sizeof (MSG));
840 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
842 gchar *emsg = g_win32_error_message (GetLastError ());
843 g_set_error (err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
845 return G_IO_STATUS_ERROR;
848 *bytes_written = sizeof (MSG);
850 return G_IO_STATUS_NORMAL;
854 g_io_win32_msg_close (GIOChannel *channel,
857 /* Nothing to be done. Or should we set hwnd to some invalid value? */
859 return G_IO_STATUS_NORMAL;
863 g_io_win32_free (GIOChannel *channel)
865 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
867 if (win32_channel->debug)
868 g_print ("thread %#x: freeing channel, fd: %d\n",
869 win32_channel->thread_id,
872 if (win32_channel->reset_send && win32_channel->reset_send != INVALID_SOCKET)
873 closesocket (win32_channel->reset_send);
874 if (win32_channel->reset_recv && win32_channel->reset_recv != INVALID_SOCKET)
875 closesocket (win32_channel->reset_recv);
876 if (win32_channel->data_avail_event)
877 CloseHandle (win32_channel->data_avail_event);
878 if (win32_channel->space_avail_event)
879 CloseHandle (win32_channel->space_avail_event);
880 if (win32_channel->data_avail_noticed_event)
881 CloseHandle (win32_channel->data_avail_noticed_event);
882 DeleteCriticalSection (&win32_channel->mutex);
884 g_free (win32_channel->buffer);
885 g_slist_free (win32_channel->watches);
886 g_free (win32_channel);
890 g_io_win32_msg_create_watch (GIOChannel *channel,
891 GIOCondition condition)
893 GIOWin32Watch *watch;
896 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
897 watch = (GIOWin32Watch *)source;
899 watch->channel = channel;
900 g_io_channel_ref (channel);
902 watch->condition = condition;
904 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
905 watch->pollfd.events = condition;
907 g_source_add_poll (source, &watch->pollfd);
913 g_io_win32_fd_read (GIOChannel *channel,
919 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
922 if (win32_channel->debug)
923 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
924 win32_channel->fd, count);
926 if (win32_channel->thread_id)
928 return buffer_read (win32_channel, buf, count, bytes_read, err);
931 result = read (win32_channel->fd, buf, count);
933 if (win32_channel->debug)
934 g_print ("g_io_win32_fd_read: read() = %d\n", result);
944 return G_IO_STATUS_AGAIN;
947 g_set_error (err, G_IO_CHANNEL_ERROR,
948 g_io_channel_error_from_errno (errno),
950 return G_IO_STATUS_ERROR;
954 *bytes_read = result;
956 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
960 g_io_win32_fd_write (GIOChannel *channel,
963 gsize *bytes_written,
966 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
969 result = write (win32_channel->fd, buf, count);
970 if (win32_channel->debug)
971 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
972 win32_channel->fd, count, result);
982 return G_IO_STATUS_AGAIN;
985 g_set_error (err, G_IO_CHANNEL_ERROR,
986 g_io_channel_error_from_errno (errno),
988 return G_IO_STATUS_ERROR;
992 *bytes_written = result;
994 return G_IO_STATUS_NORMAL;
998 g_io_win32_fd_seek (GIOChannel *channel,
1003 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1020 whence = -1; /* Keep the compiler quiet */
1021 g_assert_not_reached ();
1024 tmp_offset = offset;
1025 if (tmp_offset != offset)
1027 g_set_error (err, G_IO_CHANNEL_ERROR,
1028 g_io_channel_error_from_errno (EINVAL),
1029 g_strerror (EINVAL));
1030 return G_IO_STATUS_ERROR;
1033 result = lseek (win32_channel->fd, tmp_offset, whence);
1037 g_set_error (err, G_IO_CHANNEL_ERROR,
1038 g_io_channel_error_from_errno (errno),
1039 g_strerror (errno));
1040 return G_IO_STATUS_ERROR;
1043 return G_IO_STATUS_NORMAL;
1047 g_io_win32_fd_close (GIOChannel *channel,
1050 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1052 if (win32_channel->debug)
1053 g_print ("thread %#x: closing fd %d\n",
1054 win32_channel->thread_id,
1056 LOCK (win32_channel->mutex);
1057 if (win32_channel->running)
1059 if (win32_channel->debug)
1060 g_print ("thread %#x: running, marking fd %d for later close\n",
1061 win32_channel->thread_id, win32_channel->fd);
1062 win32_channel->running = FALSE;
1063 win32_channel->needs_close = TRUE;
1064 SetEvent (win32_channel->data_avail_event);
1068 if (win32_channel->debug)
1069 g_print ("closing fd %d\n", win32_channel->fd);
1070 close (win32_channel->fd);
1071 if (win32_channel->debug)
1072 g_print ("closed fd %d, setting to -1\n",
1074 win32_channel->fd = -1;
1076 UNLOCK (win32_channel->mutex);
1078 /* FIXME error detection? */
1080 return G_IO_STATUS_NORMAL;
1084 g_io_win32_fd_create_watch (GIOChannel *channel,
1085 GIOCondition condition)
1087 return g_io_win32_create_watch (channel, condition, read_thread);
1091 g_io_win32_sock_read (GIOChannel *channel,
1097 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1099 GIOChannelError error = G_IO_STATUS_NORMAL;
1100 GIOStatus internal_status = G_IO_STATUS_NORMAL;
1101 char send_buffer[] = "sr";
1103 if (win32_channel->debug)
1104 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
1105 win32_channel->fd, count);
1106 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1109 result = recv (win32_channel->fd, buf, count, 0);
1111 if (win32_channel->debug)
1112 g_print ("g_io_win32_sock_read: recv:%d\n", result);
1114 if (result == SOCKET_ERROR)
1118 switch (WSAGetLastError ())
1121 error = G_IO_CHANNEL_ERROR_INVAL;
1123 case WSAEWOULDBLOCK:
1124 return G_IO_STATUS_AGAIN;
1125 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1130 error = G_IO_CHANNEL_ERROR_FAILED;
1133 g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket read error");
1134 internal_status = G_IO_STATUS_ERROR;
1135 /* FIXME get all errors, better error messages */
1139 *bytes_read = result;
1141 internal_status = G_IO_STATUS_EOF;
1144 if ((internal_status == G_IO_STATUS_EOF) ||
1145 (internal_status == G_IO_STATUS_ERROR))
1147 LOCK (win32_channel->mutex);
1148 SetEvent (win32_channel->data_avail_noticed_event);
1149 win32_channel->needs_close = 1;
1150 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
1151 UNLOCK (win32_channel->mutex);
1153 return internal_status;
1157 g_io_win32_sock_write (GIOChannel *channel,
1160 gsize *bytes_written,
1163 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1165 GIOChannelError error = G_IO_STATUS_NORMAL;
1166 char send_buffer[] = "sw";
1168 if (win32_channel->debug)
1169 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
1170 win32_channel->fd, count);
1171 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1174 result = send (win32_channel->fd, buf, count, 0);
1176 if (win32_channel->debug)
1177 g_print ("g_io_win32_sock_write: send:%d\n", result);
1179 if (result == SOCKET_ERROR)
1183 switch (WSAGetLastError ())
1186 error = G_IO_CHANNEL_ERROR_INVAL;
1188 case WSAEWOULDBLOCK:
1189 return G_IO_STATUS_AGAIN;
1190 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1195 error = G_IO_CHANNEL_ERROR_FAILED;
1198 g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket write error");
1199 LOCK (win32_channel->mutex);
1200 SetEvent (win32_channel->data_avail_noticed_event);
1201 win32_channel->needs_close = 1;
1202 send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
1203 UNLOCK (win32_channel->mutex);
1204 return G_IO_STATUS_ERROR;
1205 /* FIXME get all errors, better error messages */
1209 *bytes_written = result;
1211 return G_IO_STATUS_NORMAL;
1216 g_io_win32_sock_close (GIOChannel *channel,
1219 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1221 LOCK (win32_channel->mutex);
1222 if (win32_channel->running)
1224 if (win32_channel->debug)
1225 g_print ("thread %#x: running, marking for later close\n",
1226 win32_channel->thread_id);
1227 win32_channel->running = FALSE;
1228 win32_channel->needs_close = TRUE;
1229 SetEvent(win32_channel->data_avail_noticed_event);
1231 if (win32_channel->fd != -1)
1233 if (win32_channel->debug)
1234 g_print ("thread %#x: closing socket %d\n",
1235 win32_channel->thread_id,
1238 closesocket (win32_channel->fd);
1239 win32_channel->fd = -1;
1241 UNLOCK (win32_channel->mutex);
1243 /* FIXME error detection? */
1245 return G_IO_STATUS_NORMAL;
1249 g_io_win32_sock_create_watch (GIOChannel *channel,
1250 GIOCondition condition)
1252 return g_io_win32_create_watch (channel, condition, select_thread);
1256 g_io_channel_new_file (const gchar *filename,
1260 int fid, flags, pmode;
1261 GIOChannel *channel;
1263 enum { /* Cheesy hack */
1270 g_return_val_if_fail (filename != NULL, NULL);
1271 g_return_val_if_fail (mode != NULL, NULL);
1272 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1286 g_warning ("Invalid GIOFileMode %s.\n", mode);
1295 if (mode[2] == '\0')
1297 mode_num |= MODE_PLUS;
1302 g_warning ("Invalid GIOFileMode %s.\n", mode);
1313 flags = O_WRONLY | O_TRUNC | O_CREAT;
1317 flags = O_WRONLY | O_APPEND | O_CREAT;
1320 case MODE_R | MODE_PLUS:
1322 pmode = _S_IREAD | _S_IWRITE;
1324 case MODE_W | MODE_PLUS:
1325 flags = O_RDWR | O_TRUNC | O_CREAT;
1326 pmode = _S_IREAD | _S_IWRITE;
1328 case MODE_A | MODE_PLUS:
1329 flags = O_RDWR | O_APPEND | O_CREAT;
1330 pmode = _S_IREAD | _S_IWRITE;
1333 g_assert_not_reached ();
1338 /* always open 'untranslated' */
1339 fid = open (filename, flags | _O_BINARY, pmode);
1341 if (g_io_win32_get_debug_flag ())
1343 g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
1344 g_win32_print_access_mode (flags|_O_BINARY);
1345 g_print (",%#o)=%d\n", pmode, fid);
1350 g_set_error (error, G_FILE_ERROR,
1351 g_file_error_from_errno (errno),
1352 g_strerror (errno));
1353 return (GIOChannel *)NULL;
1356 channel = g_io_channel_win32_new_fd (fid);
1358 /* XXX: move this to g_io_channel_win32_new_fd () */
1359 channel->close_on_unref = TRUE;
1360 channel->is_seekable = TRUE;
1362 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1363 * correspond to actual readability/writeability. Set to FALSE those
1364 * that mode doesn't allow
1369 channel->is_writeable = FALSE;
1373 channel->is_readable = FALSE;
1375 case MODE_R | MODE_PLUS:
1376 case MODE_W | MODE_PLUS:
1377 case MODE_A | MODE_PLUS:
1380 g_assert_not_reached ();
1387 g_io_win32_set_flags (GIOChannel *channel,
1391 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1393 if (win32_channel->debug)
1395 g_print ("g_io_win32_set_flags: ");
1396 g_win32_print_gioflags (flags);
1400 g_warning ("g_io_win32_set_flags () not implemented.\n");
1402 return G_IO_STATUS_NORMAL;
1406 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1409 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1413 if (st->st_mode & _S_IFIFO)
1415 channel->is_readable =
1416 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1417 channel->is_writeable =
1418 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1419 channel->is_seekable = FALSE;
1421 else if (st->st_mode & _S_IFCHR)
1423 /* XXX Seems there is no way to find out the readability of file
1424 * handles to device files (consoles, mostly) without doing a
1425 * blocking read. So punt, use st->st_mode.
1427 channel->is_readable = !!(st->st_mode & _S_IREAD);
1429 channel->is_writeable =
1430 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1432 /* XXX What about devices that actually *are* seekable? But
1433 * those would probably not be handled using the C runtime
1434 * anyway, but using Windows-specific code.
1436 channel->is_seekable = FALSE;
1440 channel->is_readable =
1441 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1442 channel->is_writeable =
1443 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1444 channel->is_seekable = TRUE;
1447 /* XXX: G_IO_FLAG_APPEND */
1448 /* XXX: G_IO_FLAG_NONBLOCK */
1454 g_io_win32_fd_get_flags (GIOChannel *channel)
1457 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1459 g_return_val_if_fail (win32_channel != NULL, 0);
1460 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1462 if (0 == fstat (win32_channel->fd, &st))
1463 return g_io_win32_fd_get_flags_internal (channel, &st);
1469 g_io_win32_msg_get_flags (GIOChannel *channel)
1475 g_io_win32_sock_get_flags (GIOChannel *channel)
1477 /* XXX Could do something here. */
1481 static GIOFuncs win32_channel_msg_funcs = {
1482 g_io_win32_msg_read,
1483 g_io_win32_msg_write,
1485 g_io_win32_msg_close,
1486 g_io_win32_msg_create_watch,
1488 g_io_win32_set_flags,
1489 g_io_win32_msg_get_flags,
1492 static GIOFuncs win32_channel_fd_funcs = {
1494 g_io_win32_fd_write,
1496 g_io_win32_fd_close,
1497 g_io_win32_fd_create_watch,
1499 g_io_win32_set_flags,
1500 g_io_win32_fd_get_flags,
1503 static GIOFuncs win32_channel_sock_funcs = {
1504 g_io_win32_sock_read,
1505 g_io_win32_sock_write,
1507 g_io_win32_sock_close,
1508 g_io_win32_sock_create_watch,
1510 g_io_win32_set_flags,
1511 g_io_win32_sock_get_flags,
1515 g_io_channel_win32_new_messages (guint hwnd)
1517 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1518 GIOChannel *channel = (GIOChannel *)win32_channel;
1520 g_io_channel_init (channel);
1521 g_io_channel_win32_init (win32_channel);
1522 if (win32_channel->debug)
1523 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1524 channel->funcs = &win32_channel_msg_funcs;
1525 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1526 win32_channel->hwnd = (HWND) hwnd;
1528 /* XXX: check this. */
1529 channel->is_readable = IsWindow (win32_channel->hwnd);
1530 channel->is_writeable = IsWindow (win32_channel->hwnd);
1532 channel->is_seekable = FALSE;
1538 g_io_channel_win32_new_fd_internal (gint fd,
1541 GIOWin32Channel *win32_channel;
1542 GIOChannel *channel;
1544 win32_channel = g_new (GIOWin32Channel, 1);
1545 channel = (GIOChannel *)win32_channel;
1547 g_io_channel_init (channel);
1548 g_io_channel_win32_init (win32_channel);
1549 if (win32_channel->debug)
1550 g_print ("g_io_channel_win32_new_fd: %u\n", fd);
1551 channel->funcs = &win32_channel_fd_funcs;
1552 win32_channel->type = G_IO_WIN32_FILE_DESC;
1553 win32_channel->fd = fd;
1555 g_io_win32_fd_get_flags_internal (channel, st);
1561 g_io_channel_win32_new_fd (gint fd)
1565 if (fstat (fd, &st) == -1)
1567 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1571 return g_io_channel_win32_new_fd_internal (fd, &st);
1575 g_io_channel_win32_get_fd (GIOChannel *channel)
1577 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1579 return win32_channel->fd;
1583 g_io_channel_win32_new_socket (int socket)
1585 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1586 GIOChannel *channel = (GIOChannel *)win32_channel;
1588 g_io_channel_init (channel);
1589 g_io_channel_win32_init (win32_channel);
1590 init_reset_sockets (win32_channel);
1591 if (win32_channel->debug)
1592 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1593 channel->funcs = &win32_channel_sock_funcs;
1594 win32_channel->type = G_IO_WIN32_SOCKET;
1595 win32_channel->fd = socket;
1597 /* XXX: check this */
1598 channel->is_readable = TRUE;
1599 channel->is_writeable = TRUE;
1601 channel->is_seekable = FALSE;
1607 g_io_channel_unix_new (gint fd)
1609 gboolean is_fd, is_socket;
1613 is_fd = (fstat (fd, &st) == 0);
1615 optlen = sizeof (optval);
1616 is_socket = (getsockopt (fd, SOL_SOCKET, SO_TYPE, (char *) &optval, &optlen) != SOCKET_ERROR);
1618 if (is_fd && is_socket)
1619 g_warning (G_STRLOC ": %d is both a file descriptor and a socket, file descriptor interpretation assumed.", fd);
1622 return g_io_channel_win32_new_fd_internal (fd, &st);
1625 return g_io_channel_win32_new_socket(fd);
1627 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1633 g_io_channel_unix_get_fd (GIOChannel *channel)
1635 return g_io_channel_win32_get_fd (channel);
1639 g_io_channel_win32_set_debug (GIOChannel *channel,
1642 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1644 win32_channel->debug = flag;
1648 g_io_channel_win32_poll (GPollFD *fds,
1654 g_return_val_if_fail (n_fds >= 0, 0);
1656 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1662 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1663 GIOCondition condition,
1666 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1668 if (win32_channel->data_avail_event == NULL)
1669 create_events (win32_channel);
1671 fd->fd = (gint) win32_channel->data_avail_event;
1672 fd->events = condition;
1674 if (win32_channel->thread_id == 0)
1676 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1677 create_thread (win32_channel, condition, read_thread);
1678 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1679 create_thread (win32_channel, condition, select_thread);
1683 /* Binary compatibility */
1685 g_io_channel_win32_new_stream_socket (int socket)
1687 return g_io_channel_win32_new_socket (socket);