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 */
41 #include <winsock.h> /* Not everybody has winsock2 */
50 typedef struct _GIOWin32Channel GIOWin32Channel;
51 typedef struct _GIOWin32Watch GIOWin32Watch;
53 #define BUFFER_SIZE 4096
56 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
57 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
58 * _open() or _pipe(). Read with read().
59 * Have to create separate thread to read.
61 G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked
62 * in select() most of the time.
64 } GIOWin32ChannelType;
66 struct _GIOWin32Channel {
68 gint fd; /* Either a Unix-like file handle as provided
69 * by the Microsoft C runtime, or a SOCKET
70 * as provided by WinSock.
72 GIOWin32ChannelType type;
76 CRITICAL_SECTION mutex;
78 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
79 HWND hwnd; /* handle of window, or NULL */
81 /* Following fields are used by both fd and socket channels. */
82 gboolean running; /* Is reader thread running. FALSE if
83 * EOF has been reached.
85 gboolean needs_close; /* If the channel has been closed while
86 * the reader thread was still running.
88 guint thread_id; /* If non-NULL has a reader thread, or has
90 HANDLE data_avail_event;
94 /* Following fields used by fd channels for input */
96 /* Data is kept in a circular buffer. To be able to distinguish between
97 * empty and full buffer, we cannot fill it completely, but have to
98 * leave a one character gap.
100 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
103 * Full: (wrp + 1) % BUFFER_SIZE == rdp
106 guchar *buffer; /* (Circular) buffer */
107 gint wrp, rdp; /* Buffer indices for writing and reading */
108 HANDLE space_avail_event;
110 /* Following fields used by socket channels */
112 HANDLE data_avail_noticed_event;
113 gint reset_send; /* socket used to send data so select_thread() can reset/re-loop */
114 gint reset_recv; /* socket used to recv data so select_thread() can reset/re-loop */
117 #define LOCK(mutex) EnterCriticalSection (&mutex)
118 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
120 struct _GIOWin32Watch {
124 GIOCondition condition;
128 g_win32_print_access_mode (int flags)
130 g_print ("%s%s%s%s%s%s%s%s%s%s",
131 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
132 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
133 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
134 (flags & _O_APPEND ? "|O_APPEND" : ""),
135 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
136 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
137 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
138 (flags & _O_CREAT ? "|O_CREAT" : ""),
139 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
140 (flags & _O_EXCL ? "|O_EXCL" : ""),
141 (flags & _O_TEXT ? "|O_TEXT" : ""),
142 (flags & _O_BINARY ? "|O_BINARY" : ""));
146 g_win32_print_gioflags (GIOFlags flags)
150 if (flags & G_IO_FLAG_APPEND)
151 bar = "|", g_print ("APPEND");
152 if (flags & G_IO_FLAG_NONBLOCK)
153 g_print ("%sNONBLOCK", bar), bar = "|";
154 if (flags & G_IO_FLAG_IS_READABLE)
155 g_print ("%sREADABLE", bar), bar = "|";
156 if (flags & G_IO_FLAG_IS_WRITEABLE)
157 g_print ("%sWRITEABLE", bar), bar = "|";
158 if (flags & G_IO_FLAG_IS_WRITEABLE)
159 g_print ("%sWRITEABLE", bar), bar = "|";
160 if (flags & G_IO_FLAG_IS_SEEKABLE)
161 g_print ("%sSEEKABLE", bar), bar = "|";
165 g_io_win32_get_debug_flag (void)
167 #ifdef G_IO_WIN32_DEBUG
170 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
178 g_io_channel_win32_init (GIOWin32Channel *channel)
180 channel->debug = g_io_win32_get_debug_flag ();
181 channel->buffer = NULL;
182 channel->running = FALSE;
183 channel->needs_close = FALSE;
184 channel->thread_id = 0;
185 channel->data_avail_event = NULL;
186 channel->revents = 0;
187 channel->space_avail_event = NULL;
188 channel->data_avail_noticed_event = NULL;
189 channel->watches = NULL;
190 InitializeCriticalSection (&channel->mutex);
194 create_events (GIOWin32Channel *channel)
196 SECURITY_ATTRIBUTES sec_attrs;
198 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
199 sec_attrs.lpSecurityDescriptor = NULL;
200 sec_attrs.bInheritHandle = FALSE;
202 /* The data available event is manual reset, the space available event
203 * is automatic reset.
205 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
206 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
207 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
209 gchar *emsg = g_win32_error_message (GetLastError ());
210 g_error ("Error creating event: %s", emsg);
215 static unsigned __stdcall
216 read_thread (void *parameter)
218 GIOWin32Channel *channel = parameter;
222 g_io_channel_ref ((GIOChannel *)channel);
225 g_print ("read_thread %#x: start fd:%d, data_avail:%#x space_avail:%#x\n",
228 (guint) channel->data_avail_event,
229 (guint) channel->space_avail_event);
231 channel->buffer = g_malloc (BUFFER_SIZE);
232 channel->rdp = channel->wrp = 0;
233 channel->running = TRUE;
235 SetEvent (channel->space_avail_event);
237 while (channel->running)
239 LOCK (channel->mutex);
241 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
242 channel->thread_id, channel->rdp, channel->wrp);
243 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
247 g_print ("read_thread %#x: resetting space_avail\n",
249 ResetEvent (channel->space_avail_event);
251 g_print ("read_thread %#x: waiting for space\n",
253 UNLOCK (channel->mutex);
254 WaitForSingleObject (channel->space_avail_event, INFINITE);
255 LOCK (channel->mutex);
257 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
258 channel->thread_id, channel->rdp, channel->wrp);
261 buffer = channel->buffer + channel->wrp;
263 /* Always leave at least one byte unused gap to be able to
264 * distinguish between the full and empty condition...
266 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
267 BUFFER_SIZE - channel->wrp);
270 g_print ("read_thread %#x: calling read() for %d bytes\n",
271 channel->thread_id, nbytes);
273 UNLOCK (channel->mutex);
275 nbytes = read (channel->fd, buffer, nbytes);
277 LOCK (channel->mutex);
279 channel->revents = G_IO_IN;
281 channel->revents |= G_IO_HUP;
283 channel->revents |= G_IO_ERR;
286 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
287 channel->thread_id, nbytes, channel->rdp, channel->wrp);
292 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
294 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
295 channel->thread_id, channel->rdp, channel->wrp);
296 SetEvent (channel->data_avail_event);
297 UNLOCK (channel->mutex);
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;
353 channel->reset_send = (gint) socket(AF_INET, SOCK_DGRAM, 0);
354 if (channel->reset_send == INVALID_SOCKET)
356 g_warning (G_STRLOC ": Error creating reset_send socket: %s\n",
357 g_win32_error_message (WSAGetLastError ()));
360 local.sin_family = AF_INET;
362 local.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
364 if (bind(channel->reset_send, (struct sockaddr *)&local, sizeof(local)) == SOCKET_ERROR)
366 g_warning (G_STRLOC ": Error binding to reset_send socket: %s\n",
367 g_win32_error_message (WSAGetLastError ()));
370 local2.sin_family = AF_INET;
372 local2.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
374 channel->reset_recv = (gint) socket(AF_INET, SOCK_DGRAM, 0);
375 if (channel->reset_recv == INVALID_SOCKET)
377 g_warning (G_STRLOC ": Error creating reset_recv socket: %s\n",
378 g_win32_error_message (WSAGetLastError ()));
381 if (bind(channel->reset_recv, (struct sockaddr *)&local2, sizeof(local)) == SOCKET_ERROR)
383 g_warning (G_STRLOC ": Error binding to reset_recv socket: %s\n",
384 g_win32_error_message (WSAGetLastError ()));
387 len = sizeof(local2);
388 if (getsockname(channel->reset_recv, (struct sockaddr *)&local2, &len) == SOCKET_ERROR)
390 g_warning (G_STRLOC ": Error getsockname with reset_recv socket: %s\n",
391 g_win32_error_message (WSAGetLastError ()));
394 addr = inet_addr("127.0.0.1");
395 hp = gethostbyaddr((char *)&addr,4,AF_INET);
397 memset(&server,0,sizeof(server));
398 memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
399 server.sin_family = hp->h_addrtype;
400 server.sin_port = local2.sin_port;
402 if (connect(channel->reset_send, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
404 g_warning (G_STRLOC ": connect to reset_recv socket: %s\n",
405 g_win32_error_message (WSAGetLastError ()));
411 buffer_read (GIOWin32Channel *channel,
420 LOCK (channel->mutex);
422 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
423 channel->thread_id, count, channel->rdp, channel->wrp);
425 if (channel->wrp == channel->rdp)
427 UNLOCK (channel->mutex);
429 g_print ("waiting for data from thread %#x\n", channel->thread_id);
430 WaitForSingleObject (channel->data_avail_event, INFINITE);
432 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
433 LOCK (channel->mutex);
434 if (channel->wrp == channel->rdp && !channel->running)
437 g_print ("wrp==rdp, !running\n");
438 UNLOCK (channel->mutex);
440 return G_IO_STATUS_EOF;
444 if (channel->rdp < channel->wrp)
445 nbytes = channel->wrp - channel->rdp;
447 nbytes = BUFFER_SIZE - channel->rdp;
448 UNLOCK (channel->mutex);
449 nbytes = MIN (left, nbytes);
451 g_print ("moving %d bytes from thread %#x\n",
452 nbytes, channel->thread_id);
453 memcpy (dest, channel->buffer + channel->rdp, nbytes);
456 LOCK (channel->mutex);
457 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
459 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
460 SetEvent (channel->space_avail_event);
462 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
463 channel->thread_id, channel->rdp, channel->wrp);
464 if (channel->running && channel->wrp == channel->rdp)
467 g_print ("resetting data_avail of thread %#x\n",
469 ResetEvent (channel->data_avail_event);
471 UNLOCK (channel->mutex);
473 /* We have no way to indicate any errors form the actual
474 * read() or recv() call in the reader thread. Should we have?
476 *bytes_read = count - left;
477 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
480 static unsigned __stdcall
481 select_thread (void *parameter)
483 GIOWin32Channel *channel = parameter;
484 fd_set read_fds, write_fds, except_fds;
489 g_io_channel_ref ((GIOChannel *)channel);
492 g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n",
495 (guint) channel->data_avail_event,
496 (guint) channel->data_avail_noticed_event);
498 channel->rdp = channel->wrp = 0;
499 channel->running = TRUE;
501 SetEvent (channel->space_avail_event);
503 while (channel->running)
506 FD_ZERO (&write_fds);
507 FD_ZERO (&except_fds);
508 FD_SET(channel->reset_recv, &read_fds);
510 LOCK (channel->mutex);
511 tmp = channel->watches;
514 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
516 if (watch->condition & (G_IO_IN | G_IO_HUP))
517 FD_SET (channel->fd, &read_fds);
518 if (watch->condition & G_IO_OUT)
519 FD_SET (channel->fd, &write_fds);
520 if (watch->condition & G_IO_ERR)
521 FD_SET (channel->fd, &except_fds);
525 UNLOCK (channel->mutex);
528 g_print ("select_thread %#x: calling select() for%s%s%s\n",
530 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
531 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
532 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
534 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
536 LOCK (channel->mutex);
537 if (channel->needs_close)
539 UNLOCK (channel->mutex);
542 UNLOCK (channel->mutex);
544 if (n == SOCKET_ERROR)
547 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
552 if (FD_ISSET(channel->reset_recv, &read_fds))
555 g_print ("select_thread %#x: re-looping\n",
557 recv(channel->reset_recv, (char *)&buffer, (int) sizeof(buffer), 0);
562 g_print ("select_thread %#x: got%s%s%s\n",
564 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
565 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
566 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
568 if (FD_ISSET (channel->fd, &read_fds))
569 channel->revents |= G_IO_IN;
570 if (FD_ISSET (channel->fd, &write_fds))
571 channel->revents |= G_IO_OUT;
572 if (FD_ISSET (channel->fd, &except_fds))
573 channel->revents |= G_IO_ERR;
576 g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
579 LOCK (channel->mutex);
580 ResetEvent (channel->data_avail_noticed_event);
581 SetEvent (channel->data_avail_event);
582 if (channel->needs_close)
584 UNLOCK (channel->mutex);
587 UNLOCK (channel->mutex);
590 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
593 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
595 g_print ("select_thread %#x: got data_avail_noticed\n",
599 LOCK (channel->mutex);
600 channel->running = FALSE;
602 g_print ("select_thread %#x: got error, setting data_avail\n",
604 SetEvent (channel->data_avail_event);
605 g_io_channel_unref((GIOChannel *)channel);
606 UNLOCK (channel->mutex);
608 /* No need to call _endthreadex(), the actual thread starter routine
609 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
610 * _endthreadex() for us.
617 g_io_win32_prepare (GSource *source,
620 GIOWin32Watch *watch = (GIOWin32Watch *)source;
621 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
622 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
627 g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n"
628 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
629 channel->thread_id, buffer_condition,
630 watch->pollfd.events, watch->pollfd.revents, channel->revents);
632 if (channel->type == G_IO_WIN32_FILE_DESC)
634 LOCK (channel->mutex);
635 if (channel->running && channel->wrp == channel->rdp)
638 g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n",
640 channel->revents = 0;
642 UNLOCK (channel->mutex);
644 else if (channel->type == G_IO_WIN32_SOCKET)
646 LOCK (channel->mutex);
647 channel->revents = 0;
649 g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n",
651 SetEvent (channel->data_avail_noticed_event);
653 g_print ("g_io_win32_prepare: thread %#x, there.\n",
655 UNLOCK (channel->mutex);
658 return ((watch->condition & buffer_condition) == watch->condition);
662 g_io_win32_check (GSource *source)
665 GIOWin32Watch *watch = (GIOWin32Watch *)source;
666 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
667 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
670 g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n"
671 " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
672 channel->thread_id, buffer_condition,
673 watch->pollfd.events, watch->pollfd.revents, channel->revents);
675 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
677 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
681 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
684 if (channel->type == G_IO_WIN32_SOCKET)
686 LOCK (channel->mutex);
688 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
690 ResetEvent (channel->data_avail_event);
692 g_print ("g_io_win32_check: thread %#x, there.\n",
694 UNLOCK (channel->mutex);
697 return ((watch->pollfd.revents | buffer_condition) & watch->condition);
701 g_io_win32_dispatch (GSource *source,
702 GSourceFunc callback,
705 GIOFunc func = (GIOFunc)callback;
706 GIOWin32Watch *watch = (GIOWin32Watch *)source;
707 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
711 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
712 "You must call g_source_connect().");
716 return (*func) (watch->channel,
717 (watch->pollfd.revents | buffer_condition) & watch->condition,
722 g_io_win32_finalize (GSource *source)
724 GIOWin32Watch *watch = (GIOWin32Watch *)source;
725 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
726 char send_buffer[] = "f";
728 LOCK (channel->mutex);
730 g_print ("g_io_win32_finalize: channel with thread %#x\n",
733 channel->watches = g_slist_remove (channel->watches, watch);
735 SetEvent (channel->data_avail_noticed_event);
736 if (channel->type == G_IO_WIN32_SOCKET)
737 send(channel->reset_send, send_buffer, sizeof(send_buffer), 0);
739 g_io_channel_unref (watch->channel);
740 UNLOCK (channel->mutex);
743 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
744 __declspec(dllexport)
746 GSourceFuncs g_io_watch_funcs = {
754 g_io_win32_create_watch (GIOChannel *channel,
755 GIOCondition condition,
756 unsigned (__stdcall *thread) (void *parameter))
758 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
759 GIOWin32Watch *watch;
761 char send_buffer[] = "c";
763 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
764 watch = (GIOWin32Watch *)source;
766 watch->channel = channel;
767 g_io_channel_ref (channel);
769 watch->condition = condition;
771 if (win32_channel->data_avail_event == NULL)
772 create_events (win32_channel);
774 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
775 watch->pollfd.events = condition;
777 if (win32_channel->debug)
778 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
779 win32_channel->fd, condition, watch->pollfd.fd);
781 LOCK (win32_channel->mutex);
782 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
784 if (win32_channel->thread_id == 0)
785 create_thread (win32_channel, condition, thread);
787 send(win32_channel->reset_send, send_buffer, sizeof(send_buffer), 0);
789 g_source_add_poll (source, &watch->pollfd);
790 UNLOCK (win32_channel->mutex);
796 g_io_win32_msg_read (GIOChannel *channel,
802 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
803 MSG msg; /* In case of alignment problems */
805 if (count < sizeof (MSG))
807 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
808 "Incorrect message size"); /* Informative enough error message? */
809 return G_IO_STATUS_ERROR;
812 if (win32_channel->debug)
813 g_print ("g_io_win32_msg_read: for %#x\n",
814 (guint) win32_channel->hwnd);
815 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
816 return G_IO_STATUS_AGAIN;
818 memmove (buf, &msg, sizeof (MSG));
819 *bytes_read = sizeof (MSG);
821 return G_IO_STATUS_NORMAL;
825 g_io_win32_msg_write (GIOChannel *channel,
828 gsize *bytes_written,
831 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
834 if (count != sizeof (MSG))
836 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
837 "Incorrect message size"); /* Informative enough error message? */
838 return G_IO_STATUS_ERROR;
841 /* In case of alignment problems */
842 memmove (&msg, buf, sizeof (MSG));
843 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
845 gchar *emsg = g_win32_error_message (GetLastError ());
846 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
848 return G_IO_STATUS_ERROR;
851 *bytes_written = sizeof (MSG);
853 return G_IO_STATUS_NORMAL;
857 g_io_win32_msg_close (GIOChannel *channel,
860 /* Nothing to be done. Or should we set hwnd to some invalid value? */
862 return G_IO_STATUS_NORMAL;
866 g_io_win32_free (GIOChannel *channel)
868 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
870 if (win32_channel->debug)
871 g_print ("thread %#x: freeing channel, fd: %d\n",
872 win32_channel->thread_id,
875 if (win32_channel->reset_send)
876 closesocket(win32_channel->reset_send);
877 if (win32_channel->reset_recv)
878 closesocket(win32_channel->reset_recv);
879 if (win32_channel->data_avail_event)
880 CloseHandle (win32_channel->data_avail_event);
881 if (win32_channel->space_avail_event)
882 CloseHandle (win32_channel->space_avail_event);
883 if (win32_channel->data_avail_noticed_event)
884 CloseHandle (win32_channel->data_avail_noticed_event);
885 DeleteCriticalSection (&win32_channel->mutex);
887 g_free (win32_channel->buffer);
888 g_slist_free (win32_channel->watches);
889 g_free (win32_channel);
893 g_io_win32_msg_create_watch (GIOChannel *channel,
894 GIOCondition condition)
896 GIOWin32Watch *watch;
899 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
900 watch = (GIOWin32Watch *)source;
902 watch->channel = channel;
903 g_io_channel_ref (channel);
905 watch->condition = condition;
907 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
908 watch->pollfd.events = condition;
910 g_source_add_poll (source, &watch->pollfd);
916 g_io_win32_fd_read (GIOChannel *channel,
922 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
925 if (win32_channel->debug)
926 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
927 win32_channel->fd, count);
929 if (win32_channel->thread_id)
931 return buffer_read (win32_channel, buf, count, bytes_read, err);
934 result = read (win32_channel->fd, buf, count);
936 if (win32_channel->debug)
937 g_print ("g_io_win32_fd_read: read() = %d\n", result);
947 return G_IO_STATUS_AGAIN;
950 g_set_error (err, G_IO_CHANNEL_ERROR,
951 g_io_channel_error_from_errno (errno),
953 return G_IO_STATUS_ERROR;
957 *bytes_read = result;
959 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
963 g_io_win32_fd_write (GIOChannel *channel,
966 gsize *bytes_written,
969 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
972 result = write (win32_channel->fd, buf, count);
973 if (win32_channel->debug)
974 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
975 win32_channel->fd, count, result);
985 return G_IO_STATUS_AGAIN;
988 g_set_error (err, G_IO_CHANNEL_ERROR,
989 g_io_channel_error_from_errno (errno),
991 return G_IO_STATUS_ERROR;
995 *bytes_written = result;
997 return G_IO_STATUS_NORMAL;
1001 g_io_win32_fd_seek (GIOChannel *channel,
1006 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1023 whence = -1; /* Keep the compiler quiet */
1024 g_assert_not_reached();
1027 tmp_offset = offset;
1028 if (tmp_offset != offset)
1030 g_set_error (err, G_IO_CHANNEL_ERROR,
1031 g_io_channel_error_from_errno (EINVAL),
1032 g_strerror (EINVAL));
1033 return G_IO_STATUS_ERROR;
1036 result = lseek (win32_channel->fd, tmp_offset, whence);
1040 g_set_error (err, G_IO_CHANNEL_ERROR,
1041 g_io_channel_error_from_errno (errno),
1042 g_strerror (errno));
1043 return G_IO_STATUS_ERROR;
1046 return G_IO_STATUS_NORMAL;
1050 g_io_win32_fd_close (GIOChannel *channel,
1053 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1055 if (win32_channel->debug)
1056 g_print ("thread %#x: closing fd %d\n",
1057 win32_channel->thread_id,
1059 LOCK (win32_channel->mutex);
1060 if (win32_channel->running)
1062 if (win32_channel->debug)
1063 g_print ("thread %#x: running, marking fd %d for later close\n",
1064 win32_channel->thread_id, win32_channel->fd);
1065 win32_channel->running = FALSE;
1066 win32_channel->needs_close = TRUE;
1067 SetEvent (win32_channel->data_avail_event);
1071 if (win32_channel->debug)
1072 g_print ("closing fd %d\n", win32_channel->fd);
1073 close (win32_channel->fd);
1074 if (win32_channel->debug)
1075 g_print ("closed fd %d, setting to -1\n",
1077 win32_channel->fd = -1;
1079 UNLOCK (win32_channel->mutex);
1081 /* FIXME error detection? */
1083 return G_IO_STATUS_NORMAL;
1087 g_io_win32_fd_create_watch (GIOChannel *channel,
1088 GIOCondition condition)
1090 return g_io_win32_create_watch (channel, condition, read_thread);
1094 g_io_win32_sock_read (GIOChannel *channel,
1100 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1102 GIOChannelError error = G_IO_STATUS_NORMAL;
1103 GIOStatus internal_status = G_IO_STATUS_NORMAL;
1104 char send_buffer[] = "sr";
1106 if (win32_channel->debug)
1107 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
1108 win32_channel->fd, count);
1109 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1112 result = recv (win32_channel->fd, buf, count, 0);
1114 if (win32_channel->debug)
1115 g_print ("g_io_win32_sock_read: recv:%d\n", result);
1117 if (result == SOCKET_ERROR)
1121 switch (WSAGetLastError ())
1124 error = G_IO_CHANNEL_ERROR_INVAL;
1126 case WSAEWOULDBLOCK:
1127 return G_IO_STATUS_AGAIN;
1128 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1133 error = G_IO_CHANNEL_ERROR_FAILED;
1136 g_set_error(err, G_IO_CHANNEL_ERROR, error, "Socket read error");
1137 internal_status = G_IO_STATUS_ERROR;
1138 /* FIXME get all errors, better error messages */
1142 *bytes_read = result;
1144 internal_status = G_IO_STATUS_EOF;
1147 if ((internal_status == G_IO_STATUS_EOF) ||
1148 (internal_status == G_IO_STATUS_ERROR))
1150 LOCK (win32_channel->mutex);
1151 SetEvent(win32_channel->data_avail_noticed_event);
1152 win32_channel->needs_close = 1;
1153 send(win32_channel->reset_send, send_buffer, sizeof(send_buffer), 0);
1154 UNLOCK (win32_channel->mutex);
1156 return internal_status;
1160 g_io_win32_sock_write (GIOChannel *channel,
1163 gsize *bytes_written,
1166 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1168 GIOChannelError error = G_IO_STATUS_NORMAL;
1169 char send_buffer[] = "sw";
1171 if (win32_channel->debug)
1172 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
1173 win32_channel->fd, count);
1174 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1177 result = send (win32_channel->fd, buf, count, 0);
1179 if (win32_channel->debug)
1180 g_print ("g_io_win32_sock_write: send:%d\n", result);
1182 if (result == SOCKET_ERROR)
1186 switch (WSAGetLastError ())
1189 error = G_IO_CHANNEL_ERROR_INVAL;
1191 case WSAEWOULDBLOCK:
1192 return G_IO_STATUS_AGAIN;
1193 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1198 error = G_IO_CHANNEL_ERROR_FAILED;
1201 g_set_error(err, G_IO_CHANNEL_ERROR, error, "Socket write error");
1202 LOCK (win32_channel->mutex);
1203 SetEvent(win32_channel->data_avail_noticed_event);
1204 win32_channel->needs_close = 1;
1205 send(win32_channel->reset_send, send_buffer, sizeof(send_buffer), 0);
1206 UNLOCK (win32_channel->mutex);
1207 return G_IO_STATUS_ERROR;
1208 /* FIXME get all errors, better error messages */
1212 *bytes_written = result;
1214 return G_IO_STATUS_NORMAL;
1219 g_io_win32_sock_close (GIOChannel *channel,
1222 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1224 LOCK(win32_channel->mutex);
1225 if (win32_channel->running)
1227 if (win32_channel->debug)
1228 g_print ("thread %#x: running, marking for later close\n",
1229 win32_channel->thread_id);
1230 win32_channel->running = FALSE;
1231 win32_channel->needs_close = TRUE;
1232 SetEvent(win32_channel->data_avail_noticed_event);
1234 if (win32_channel->fd != -1)
1236 if (win32_channel->debug)
1237 g_print ("thread %#x: closing socket %d\n",
1238 win32_channel->thread_id,
1241 closesocket (win32_channel->fd);
1242 win32_channel->fd = -1;
1244 UNLOCK(win32_channel->mutex);
1246 /* FIXME error detection? */
1248 return G_IO_STATUS_NORMAL;
1252 g_io_win32_sock_create_watch (GIOChannel *channel,
1253 GIOCondition condition)
1255 return g_io_win32_create_watch (channel, condition, select_thread);
1259 g_io_channel_new_file (const gchar *filename,
1263 int fid, flags, pmode;
1264 GIOChannel *channel;
1266 enum { /* Cheesy hack */
1273 g_return_val_if_fail (filename != NULL, NULL);
1274 g_return_val_if_fail (mode != NULL, NULL);
1275 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1289 g_warning ("Invalid GIOFileMode %s.\n", mode);
1298 if (mode[2] == '\0')
1300 mode_num |= MODE_PLUS;
1305 g_warning ("Invalid GIOFileMode %s.\n", mode);
1316 flags = O_WRONLY | O_TRUNC | O_CREAT;
1320 flags = O_WRONLY | O_APPEND | O_CREAT;
1323 case MODE_R | MODE_PLUS:
1325 pmode = _S_IREAD | _S_IWRITE;
1327 case MODE_W | MODE_PLUS:
1328 flags = O_RDWR | O_TRUNC | O_CREAT;
1329 pmode = _S_IREAD | _S_IWRITE;
1331 case MODE_A | MODE_PLUS:
1332 flags = O_RDWR | O_APPEND | O_CREAT;
1333 pmode = _S_IREAD | _S_IWRITE;
1336 g_assert_not_reached ();
1341 /* always open 'untranslated' */
1342 fid = open (filename, flags | _O_BINARY, pmode);
1344 if (g_io_win32_get_debug_flag ())
1346 g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
1347 g_win32_print_access_mode (flags|_O_BINARY);
1348 g_print (",%#o)=%d\n", pmode, fid);
1353 g_set_error (error, G_FILE_ERROR,
1354 g_file_error_from_errno (errno),
1355 g_strerror (errno));
1356 return (GIOChannel *)NULL;
1359 channel = g_io_channel_win32_new_fd (fid);
1361 /* XXX: move this to g_io_channel_win32_new_fd () */
1362 channel->close_on_unref = TRUE;
1363 channel->is_seekable = TRUE;
1365 /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
1366 * correspond to actual readability/writeability. Set to FALSE those
1367 * that mode doesn't allow
1372 channel->is_writeable = FALSE;
1376 channel->is_readable = FALSE;
1378 case MODE_R | MODE_PLUS:
1379 case MODE_W | MODE_PLUS:
1380 case MODE_A | MODE_PLUS:
1383 g_assert_not_reached ();
1390 g_io_win32_set_flags (GIOChannel *channel,
1394 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1396 if (win32_channel->debug)
1398 g_print ("g_io_win32_set_flags: ");
1399 g_win32_print_gioflags (flags);
1403 g_warning ("g_io_win32_set_flags () not implemented.\n");
1405 return G_IO_STATUS_NORMAL;
1409 g_io_win32_fd_get_flags_internal (GIOChannel *channel,
1412 GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
1416 if (st->st_mode & _S_IFIFO)
1418 channel->is_readable =
1419 (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
1420 channel->is_writeable =
1421 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1422 channel->is_seekable = FALSE;
1424 else if (st->st_mode & _S_IFCHR)
1426 /* XXX Seems there is no way to find out the readability of file
1427 * handles to device files (consoles, mostly) without doing a
1428 * blocking read. So punt, use st->st_mode.
1430 channel->is_readable = !!(st->st_mode & _S_IREAD);
1432 channel->is_writeable =
1433 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1435 /* XXX What about devices that actually *are* seekable? But
1436 * those would probably not be handled using the C runtime
1437 * anyway, but using Windows-specific code.
1439 channel->is_seekable = FALSE;
1443 channel->is_readable =
1444 (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1445 channel->is_writeable =
1446 (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
1447 channel->is_seekable = TRUE;
1450 /* XXX: G_IO_FLAG_APPEND */
1451 /* XXX: G_IO_FLAG_NONBLOCK */
1457 g_io_win32_fd_get_flags (GIOChannel *channel)
1460 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1462 g_return_val_if_fail (win32_channel != NULL, 0);
1463 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1465 if (0 == fstat (win32_channel->fd, &st))
1466 return g_io_win32_fd_get_flags_internal (channel, &st);
1472 g_io_win32_msg_get_flags (GIOChannel *channel)
1478 g_io_win32_sock_get_flags (GIOChannel *channel)
1480 /* XXX Could do something here. */
1484 static GIOFuncs win32_channel_msg_funcs = {
1485 g_io_win32_msg_read,
1486 g_io_win32_msg_write,
1488 g_io_win32_msg_close,
1489 g_io_win32_msg_create_watch,
1491 g_io_win32_set_flags,
1492 g_io_win32_msg_get_flags,
1495 static GIOFuncs win32_channel_fd_funcs = {
1497 g_io_win32_fd_write,
1499 g_io_win32_fd_close,
1500 g_io_win32_fd_create_watch,
1502 g_io_win32_set_flags,
1503 g_io_win32_fd_get_flags,
1506 static GIOFuncs win32_channel_sock_funcs = {
1507 g_io_win32_sock_read,
1508 g_io_win32_sock_write,
1510 g_io_win32_sock_close,
1511 g_io_win32_sock_create_watch,
1513 g_io_win32_set_flags,
1514 g_io_win32_sock_get_flags,
1518 g_io_channel_win32_new_messages (guint hwnd)
1520 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1521 GIOChannel *channel = (GIOChannel *)win32_channel;
1523 g_io_channel_init (channel);
1524 g_io_channel_win32_init (win32_channel);
1525 if (win32_channel->debug)
1526 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1527 channel->funcs = &win32_channel_msg_funcs;
1528 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1529 win32_channel->hwnd = (HWND) hwnd;
1531 /* XXX: check this. */
1532 channel->is_readable = IsWindow (win32_channel->hwnd);
1533 channel->is_writeable = IsWindow (win32_channel->hwnd);
1535 channel->is_seekable = FALSE;
1541 g_io_channel_win32_new_fd_internal (gint fd,
1544 GIOWin32Channel *win32_channel;
1545 GIOChannel *channel;
1547 win32_channel = g_new (GIOWin32Channel, 1);
1548 channel = (GIOChannel *)win32_channel;
1550 g_io_channel_init (channel);
1551 g_io_channel_win32_init (win32_channel);
1552 if (win32_channel->debug)
1553 g_print ("g_io_channel_win32_new_fd: %u\n", fd);
1554 channel->funcs = &win32_channel_fd_funcs;
1555 win32_channel->type = G_IO_WIN32_FILE_DESC;
1556 win32_channel->fd = fd;
1558 g_io_win32_fd_get_flags_internal (channel, st);
1564 g_io_channel_win32_new_fd (gint fd)
1568 if (fstat (fd, &st) == -1)
1570 g_warning (G_STRLOC ": %d isn't a C library file descriptor", fd);
1574 return g_io_channel_win32_new_fd_internal (fd, &st);
1578 g_io_channel_win32_get_fd (GIOChannel *channel)
1580 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1582 return win32_channel->fd;
1586 g_io_channel_win32_new_socket (int socket)
1588 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1589 GIOChannel *channel = (GIOChannel *)win32_channel;
1591 g_io_channel_init (channel);
1592 g_io_channel_win32_init (win32_channel);
1593 init_reset_sockets(channel);
1594 if (win32_channel->debug)
1595 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1596 channel->funcs = &win32_channel_sock_funcs;
1597 win32_channel->type = G_IO_WIN32_SOCKET;
1598 win32_channel->fd = socket;
1600 /* XXX: check this */
1601 channel->is_readable = TRUE;
1602 channel->is_writeable = TRUE;
1604 channel->is_seekable = FALSE;
1610 g_io_channel_unix_new (gint fd)
1614 if (fstat (fd, &st) == 0)
1615 return g_io_channel_win32_new_fd_internal (fd, &st);
1617 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SOCKET_ERROR)
1618 return g_io_channel_win32_new_socket(fd);
1620 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1625 g_io_channel_unix_get_fd (GIOChannel *channel)
1627 return g_io_channel_win32_get_fd (channel);
1631 g_io_channel_win32_set_debug (GIOChannel *channel,
1634 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1636 win32_channel->debug = flag;
1640 g_io_channel_win32_poll (GPollFD *fds,
1646 g_return_val_if_fail (n_fds >= 0, 0);
1648 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1654 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1655 GIOCondition condition,
1658 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1660 if (win32_channel->data_avail_event == NULL)
1661 create_events (win32_channel);
1663 fd->fd = (gint) win32_channel->data_avail_event;
1664 fd->events = condition;
1666 if (win32_channel->thread_id == 0)
1668 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1669 create_thread (win32_channel, condition, read_thread);
1670 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1671 create_thread (win32_channel, condition, select_thread);
1675 /* Binary compatibility */
1677 g_io_channel_win32_new_stream_socket (int socket)
1679 return g_io_channel_win32_new_socket (socket);