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 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 */
39 #include <winsock.h> /* Not everybody has winsock2 */
48 typedef struct _GIOWin32Channel GIOWin32Channel;
49 typedef struct _GIOWin32Watch GIOWin32Watch;
51 #define BUFFER_SIZE 4096
54 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
55 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
56 * _open() or _pipe(). Read with read().
57 * Have to create separate thread to read.
59 G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked
60 * in select() most of the time.
62 } GIOWin32ChannelType;
64 struct _GIOWin32Channel {
66 gint fd; /* Either a Unix-like file handle as provided
67 * by the Microsoft C runtime, or a SOCKET
68 * as provided by WinSock.
70 GIOWin32ChannelType type;
74 CRITICAL_SECTION mutex;
76 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
77 HWND hwnd; /* handle of window, or NULL */
79 /* Following fields are used by both fd and socket channels. */
80 gboolean running; /* Is reader thread running. FALSE if
81 * EOF has been reached.
83 gboolean needs_close; /* If the channel has been closed while
84 * the reader thread was still running.
86 guint thread_id; /* If non-NULL has a reader thread, or has
89 HANDLE data_avail_event;
93 /* Following fields used by fd channels for input */
95 /* Data is kept in a circular buffer. To be able to distinguish between
96 * empty and full buffer, we cannot fill it completely, but have to
97 * leave a one character gap.
99 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
102 * Full: (wrp + 1) % BUFFER_SIZE == rdp
105 guchar *buffer; /* (Circular) buffer */
106 gint wrp, rdp; /* Buffer indices for writing and reading */
107 HANDLE space_avail_event;
109 /* Following fields used by socket channels */
111 HANDLE data_avail_noticed_event;
114 #define LOCK(mutex) EnterCriticalSection (&mutex)
115 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
117 struct _GIOWin32Watch {
121 GIOCondition condition;
126 g_win32_print_access_mode (int flags)
128 g_print ("%s%s%s%s%s%s%s%s%s%s",
129 ((flags & 0x3) == _O_RDWR ? "O_RDWR" :
130 ((flags & 0x3) == _O_RDONLY ? "O_RDONLY" :
131 ((flags & 0x3) == _O_WRONLY ? "O_WRONLY" : "0"))),
132 (flags & _O_APPEND ? "|O_APPEND" : ""),
133 (flags & _O_RANDOM ? "|O_RANDOM" : ""),
134 (flags & _O_SEQUENTIAL ? "|O_SEQUENTIAL" : ""),
135 (flags & _O_TEMPORARY ? "|O_TEMPORARY" : ""),
136 (flags & _O_CREAT ? "|O_CREAT" : ""),
137 (flags & _O_TRUNC ? "|O_TRUNC" : ""),
138 (flags & _O_EXCL ? "|O_EXCL" : ""),
139 (flags & _O_TEXT ? "|O_TEXT" : ""),
140 (flags & _O_BINARY ? "|O_BINARY" : ""));
144 g_win32_print_gioflags (GIOFlags flags)
148 if (flags & G_IO_FLAG_APPEND)
149 bar = "|", g_print ("APPEND");
150 if (flags & G_IO_FLAG_NONBLOCK)
151 g_print ("%sNONBLOCK", bar), bar = "|";
152 if (flags & G_IO_FLAG_IS_READABLE)
153 g_print ("%sREADABLE", bar), bar = "|";
154 if (flags & G_IO_FLAG_IS_WRITEABLE)
155 g_print ("%sWRITEABLE", bar), bar = "|";
156 if (flags & G_IO_FLAG_IS_WRITEABLE)
157 g_print ("%sWRITEABLE", bar), bar = "|";
158 if (flags & G_IO_FLAG_IS_SEEKABLE)
159 g_print ("%sSEEKABLE", bar), bar = "|";
163 g_io_win32_get_debug_flag (void)
165 #ifdef G_IO_WIN32_DEBUG
168 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
176 g_io_channel_win32_init (GIOWin32Channel *channel)
178 channel->debug = g_io_win32_get_debug_flag ();
179 channel->buffer = NULL;
180 channel->running = FALSE;
181 channel->needs_close = FALSE;
182 channel->thread_id = 0;
183 channel->data_avail_event = NULL;
184 channel->revents = 0;
185 channel->space_avail_event = NULL;
186 channel->data_avail_noticed_event = NULL;
187 channel->watches = NULL;
188 InitializeCriticalSection (&channel->mutex);
192 create_events (GIOWin32Channel *channel)
194 SECURITY_ATTRIBUTES sec_attrs;
196 sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
197 sec_attrs.lpSecurityDescriptor = NULL;
198 sec_attrs.bInheritHandle = FALSE;
200 /* The data available event is manual reset, the space available event
201 * is automatic reset.
203 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
204 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
205 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
207 gchar *emsg = g_win32_error_message (GetLastError ());
208 g_error ("Error creating event: %s", emsg);
213 static unsigned __stdcall
214 read_thread (void *parameter)
216 GIOWin32Channel *channel = parameter;
220 g_io_channel_ref ((GIOChannel *)channel);
223 g_print ("read_thread %#x: start fd:%d, data_avail:%#x, space_avail:%#x\n",
226 (guint) channel->data_avail_event,
227 (guint) channel->space_avail_event);
229 channel->buffer = g_malloc (BUFFER_SIZE);
230 channel->rdp = channel->wrp = 0;
231 channel->running = TRUE;
233 SetEvent (channel->space_avail_event);
235 while (channel->running)
237 LOCK (channel->mutex);
239 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
240 channel->thread_id, channel->rdp, channel->wrp);
241 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
245 g_print ("read_thread %#x: resetting space_avail\n",
247 ResetEvent (channel->space_avail_event);
249 g_print ("read_thread %#x: waiting for space\n",
251 UNLOCK (channel->mutex);
252 WaitForSingleObject (channel->space_avail_event, INFINITE);
253 LOCK (channel->mutex);
255 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
256 channel->thread_id, channel->rdp, channel->wrp);
259 buffer = channel->buffer + channel->wrp;
261 /* Always leave at least one byte unused gap to be able to
262 * distinguish between the full and empty condition...
264 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
265 BUFFER_SIZE - channel->wrp);
268 g_print ("read_thread %#x: calling read() for %d bytes\n",
269 channel->thread_id, nbytes);
271 UNLOCK (channel->mutex);
273 nbytes = read (channel->fd, buffer, nbytes);
275 LOCK (channel->mutex);
277 channel->revents = G_IO_IN;
279 channel->revents |= G_IO_HUP;
281 channel->revents |= G_IO_ERR;
284 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
285 channel->thread_id, nbytes, channel->rdp, channel->wrp);
290 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
292 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
293 channel->thread_id, channel->rdp, channel->wrp);
294 SetEvent (channel->data_avail_event);
295 UNLOCK (channel->mutex);
298 channel->running = FALSE;
299 if (channel->needs_close)
302 g_print ("read_thread %#x: channel fd %d needs closing\n",
303 channel->thread_id, channel->fd);
309 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
310 channel->thread_id, channel->rdp, channel->wrp);
311 SetEvent (channel->data_avail_event);
312 UNLOCK (channel->mutex);
314 g_io_channel_unref((GIOChannel *)channel);
316 /* No need to call _endthreadex(), the actual thread starter routine
317 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
318 * _endthreadex() for us.
321 CloseHandle (channel->thread_handle);
327 create_thread (GIOWin32Channel *channel,
328 GIOCondition condition,
329 unsigned (__stdcall *thread) (void *parameter))
331 channel->thread_handle =
332 (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
333 &channel->thread_id);
334 if (channel->thread_handle == 0)
335 g_warning (G_STRLOC ": Error creating reader thread: %s",
337 WaitForSingleObject (channel->space_avail_event, INFINITE);
341 buffer_read (GIOWin32Channel *channel,
350 LOCK (channel->mutex);
352 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
353 channel->thread_id, count, channel->rdp, channel->wrp);
355 if (channel->wrp == channel->rdp)
357 UNLOCK (channel->mutex);
359 g_print ("waiting for data from thread %#x\n", channel->thread_id);
360 WaitForSingleObject (channel->data_avail_event, INFINITE);
362 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
363 LOCK (channel->mutex);
364 if (channel->wrp == channel->rdp && !channel->running)
366 UNLOCK (channel->mutex);
368 return G_IO_STATUS_NORMAL; /* as before, normal case ? */
372 if (channel->rdp < channel->wrp)
373 nbytes = channel->wrp - channel->rdp;
375 nbytes = BUFFER_SIZE - channel->rdp;
376 UNLOCK (channel->mutex);
377 nbytes = MIN (left, nbytes);
379 g_print ("moving %d bytes from thread %#x\n",
380 nbytes, channel->thread_id);
381 memcpy (dest, channel->buffer + channel->rdp, nbytes);
384 LOCK (channel->mutex);
385 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
387 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
388 SetEvent (channel->space_avail_event);
390 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
391 channel->thread_id, channel->rdp, channel->wrp);
392 if (channel->running && channel->wrp == channel->rdp)
395 g_print ("resetting data_avail of thread %#x\n",
397 ResetEvent (channel->data_avail_event);
399 UNLOCK (channel->mutex);
401 /* We have no way to indicate any errors form the actual
402 * read() or recv() call in the reader thread. Should we have?
404 *bytes_read = count - left;
405 return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
408 static unsigned __stdcall
409 select_thread (void *parameter)
411 GIOWin32Channel *channel = parameter;
412 fd_set read_fds, write_fds, except_fds;
416 g_io_channel_ref ((GIOChannel *)channel);
419 g_print ("select_thread %#x: start fd:%d,\n\tdata_avail:%#x, data_avail_noticed:%#x\n",
422 (guint) channel->data_avail_event,
423 (guint) channel->data_avail_noticed_event);
425 channel->rdp = channel->wrp = 0;
426 channel->running = TRUE;
428 SetEvent (channel->space_avail_event);
430 while (channel->running)
433 FD_ZERO (&write_fds);
434 FD_ZERO (&except_fds);
436 tmp = channel->watches;
439 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
441 if (watch->condition & (G_IO_IN | G_IO_HUP))
442 FD_SET (channel->fd, &read_fds);
443 if (watch->condition & G_IO_OUT)
444 FD_SET (channel->fd, &write_fds);
445 if (watch->condition & G_IO_ERR)
446 FD_SET (channel->fd, &except_fds);
451 g_print ("select_thread %#x: calling select() for%s%s%s\n",
453 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
454 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
455 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
457 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
459 if (n == SOCKET_ERROR)
462 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
468 g_print ("select_thread %#x: got%s%s%s\n",
470 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
471 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
472 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
474 if (FD_ISSET (channel->fd, &read_fds))
475 channel->revents |= G_IO_IN;
476 if (FD_ISSET (channel->fd, &write_fds))
477 channel->revents |= G_IO_OUT;
478 if (FD_ISSET (channel->fd, &except_fds))
479 channel->revents |= G_IO_ERR;
482 g_print ("select_thread %#x: resetting data_avail_noticed,\n"
483 "\tsetting data_avail\n",
485 ResetEvent (channel->data_avail_noticed_event);
486 SetEvent (channel->data_avail_event);
488 LOCK (channel->mutex);
489 if (channel->needs_close)
491 UNLOCK (channel->mutex);
494 UNLOCK (channel->mutex);
497 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
500 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
502 g_print ("select_thread %#x: got data_avail_noticed\n",
506 channel->running = FALSE;
507 LOCK (channel->mutex);
508 if (channel->fd != -1)
510 /* DO NOT close the fd here */
515 g_print ("select_thread %#x: got error, setting data_avail\n",
517 SetEvent (channel->data_avail_event);
518 UNLOCK (channel->mutex);
520 g_io_channel_unref((GIOChannel *)channel);
522 /* No need to call _endthreadex(), the actual thread starter routine
523 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
524 * _endthreadex() for us.
527 CloseHandle (channel->thread_handle);
533 g_io_win32_prepare (GSource *source,
536 GIOWin32Watch *watch = (GIOWin32Watch *)source;
537 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
541 if (channel->type == G_IO_WIN32_FILE_DESC)
543 LOCK (channel->mutex);
544 if (channel->running && channel->wrp == channel->rdp)
545 channel->revents = 0;
546 UNLOCK (channel->mutex);
548 else if (channel->type == G_IO_WIN32_SOCKET)
550 channel->revents = 0;
553 g_print ("g_io_win32_prepare: thread %#x, setting data_avail_noticed\n",
555 SetEvent (channel->data_avail_noticed_event);
557 g_print ("g_io_win32_prepare: thread %#x, there.\n",
562 /* XXX: why should we want to do this ? */
563 watch->condition = g_io_channel_get_buffer_condition (watch->channel);
565 return (watch->pollfd.revents & (G_IO_IN | G_IO_OUT)) == watch->condition;
569 g_io_win32_check (GSource *source)
572 GIOWin32Watch *watch = (GIOWin32Watch *)source;
573 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
574 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
577 g_print ("g_io_win32_check: for thread %#x:\n"
578 "\twatch->pollfd.events:%#x, watch->pollfd.revents:%#x, channel->revents:%#x\n",
580 watch->pollfd.events, watch->pollfd.revents, channel->revents);
582 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
584 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
588 return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
591 if (channel->type == G_IO_WIN32_SOCKET)
594 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
596 ResetEvent (channel->data_avail_event);
598 g_print ("g_io_win32_check: thread %#x, there.\n",
602 return (watch->pollfd.revents & watch->condition);
606 g_io_win32_dispatch (GSource *source,
607 GSourceFunc callback,
610 GIOFunc func = (GIOFunc)callback;
611 GIOWin32Watch *watch = (GIOWin32Watch *)source;
615 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
616 "You must call g_source_connect().");
620 return (*func) (watch->channel,
621 watch->pollfd.revents & watch->condition,
626 g_io_win32_finalize (GSource *source)
628 GIOWin32Watch *watch = (GIOWin32Watch *)source;
629 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
632 g_print ("g_io_win32_finalize: channel with thread %#x\n",
635 channel->watches = g_slist_remove (channel->watches, watch);
637 SetEvent (channel->data_avail_noticed_event);
638 g_io_channel_unref (watch->channel);
641 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
642 __declspec(dllexport)
644 GSourceFuncs g_io_watch_funcs = {
652 g_io_win32_create_watch (GIOChannel *channel,
653 GIOCondition condition,
654 unsigned (__stdcall *thread) (void *parameter))
656 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
657 GIOWin32Watch *watch;
660 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
661 watch = (GIOWin32Watch *)source;
663 watch->channel = channel;
664 g_io_channel_ref (channel);
666 watch->condition = condition;
668 if (win32_channel->data_avail_event == NULL)
669 create_events (win32_channel);
671 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
672 watch->pollfd.events = condition;
674 if (win32_channel->debug)
675 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
676 win32_channel->fd, condition, watch->pollfd.fd);
678 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
680 if (win32_channel->thread_id == 0)
681 create_thread (win32_channel, condition, thread);
683 g_source_add_poll (source, &watch->pollfd);
689 g_io_win32_msg_read (GIOChannel *channel,
695 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
696 MSG msg; /* In case of alignment problems */
698 if (count < sizeof (MSG))
700 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
701 _("Incorrect message size")); /* Informative enough error message? */
702 return G_IO_STATUS_ERROR;
705 if (win32_channel->debug)
706 g_print ("g_io_win32_msg_read: for %#x\n",
707 (guint) win32_channel->hwnd);
708 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
709 return G_IO_STATUS_AGAIN;
711 memmove (buf, &msg, sizeof (MSG));
712 *bytes_read = sizeof (MSG);
714 return G_IO_STATUS_NORMAL;
718 g_io_win32_msg_write (GIOChannel *channel,
721 gsize *bytes_written,
724 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
727 if (count != sizeof (MSG))
729 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
730 _("Incorrect message size")); /* Informative enough error message? */
731 return G_IO_STATUS_ERROR;
734 /* In case of alignment problems */
735 memmove (&msg, buf, sizeof (MSG));
736 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
738 gchar *emsg = g_win32_error_message (GetLastError ());
739 g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED, emsg);
741 return G_IO_STATUS_ERROR;
744 *bytes_written = sizeof (MSG);
746 return G_IO_STATUS_NORMAL;
750 g_io_win32_no_seek (GIOChannel *channel,
755 g_assert_not_reached ();
757 return G_IO_STATUS_ERROR;
761 g_io_win32_msg_close (GIOChannel *channel,
764 /* Nothing to be done. Or should we set hwnd to some invalid value? */
766 return G_IO_STATUS_NORMAL;
770 g_io_win32_free (GIOChannel *channel)
772 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
774 if (win32_channel->debug)
775 g_print ("thread %#x: freeing channel, fd: %d\n",
776 win32_channel->thread_id,
779 if (win32_channel->data_avail_event)
780 CloseHandle (win32_channel->data_avail_event);
781 if (win32_channel->space_avail_event)
782 CloseHandle (win32_channel->space_avail_event);
783 if (win32_channel->data_avail_noticed_event)
784 CloseHandle (win32_channel->data_avail_noticed_event);
785 DeleteCriticalSection (&win32_channel->mutex);
787 g_free (win32_channel->buffer);
788 g_slist_free (win32_channel->watches);
789 g_free (win32_channel);
793 g_io_win32_msg_create_watch (GIOChannel *channel,
794 GIOCondition condition)
796 GIOWin32Watch *watch;
799 source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
800 watch = (GIOWin32Watch *)source;
802 watch->channel = channel;
803 g_io_channel_ref (channel);
805 watch->condition = condition;
807 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
808 watch->pollfd.events = condition;
810 g_source_add_poll (source, &watch->pollfd);
816 g_io_win32_fd_read (GIOChannel *channel,
822 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
825 if (win32_channel->debug)
826 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
827 win32_channel->fd, count);
829 if (win32_channel->thread_id)
831 return buffer_read (win32_channel, buf, count, bytes_read, err);
834 result = read (win32_channel->fd, buf, count);
836 if (win32_channel->debug)
837 g_print ("g_io_win32_fd_read: read() = %d\n", result);
847 return G_IO_STATUS_AGAIN;
850 g_set_error (err, G_IO_CHANNEL_ERROR,
851 g_io_channel_error_from_errno (errno),
853 return G_IO_STATUS_ERROR;
857 *bytes_read = result;
859 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
863 g_io_win32_fd_write (GIOChannel *channel,
866 gsize *bytes_written,
869 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
872 result = write (win32_channel->fd, buf, count);
873 if (win32_channel->debug)
874 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
875 win32_channel->fd, count, result);
885 return G_IO_STATUS_AGAIN;
888 g_set_error (err, G_IO_CHANNEL_ERROR,
889 g_io_channel_error_from_errno (errno),
891 return G_IO_STATUS_ERROR;
895 *bytes_written = result;
897 return G_IO_STATUS_NORMAL;
901 g_io_win32_fd_seek (GIOChannel *channel,
906 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
922 whence = -1; /* Keep the compiler quiet */
923 g_assert_not_reached();
926 result = lseek (win32_channel->fd, offset, whence);
930 g_set_error (err, G_IO_CHANNEL_ERROR,
931 g_io_channel_error_from_errno (errno),
933 return G_IO_STATUS_ERROR;
936 return G_IO_STATUS_NORMAL;
940 g_io_win32_fd_close (GIOChannel *channel,
943 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
945 if (win32_channel->debug)
946 g_print ("thread %#x: closing fd %d\n",
947 win32_channel->thread_id,
949 LOCK (win32_channel->mutex);
950 if (win32_channel->running)
952 if (win32_channel->debug)
953 g_print ("thread %#x: running, marking fd %d for later close\n",
954 win32_channel->thread_id, win32_channel->fd);
955 win32_channel->running = FALSE;
956 win32_channel->needs_close = TRUE;
957 SetEvent (win32_channel->data_avail_event);
961 if (win32_channel->debug)
962 g_print ("closing fd %d\n", win32_channel->fd);
963 close (win32_channel->fd);
964 if (win32_channel->debug)
965 g_print ("closed fd %d, setting to -1\n",
967 win32_channel->fd = -1;
969 UNLOCK (win32_channel->mutex);
971 /* FIXME error detection? */
973 return G_IO_STATUS_NORMAL;
977 g_io_win32_fd_create_watch (GIOChannel *channel,
978 GIOCondition condition)
980 return g_io_win32_create_watch (channel, condition, read_thread);
984 g_io_win32_sock_read (GIOChannel *channel,
990 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
992 GIOChannelError error;
994 if (win32_channel->debug)
995 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
996 win32_channel->fd, count);
997 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1000 result = recv (win32_channel->fd, buf, count, 0);
1002 if (win32_channel->debug)
1003 g_print ("g_io_win32_sock_read: recv:%d\n", result);
1005 if (result == SOCKET_ERROR)
1009 switch (WSAGetLastError ())
1012 error = G_IO_CHANNEL_ERROR_INVAL;
1014 case WSAEWOULDBLOCK:
1015 return G_IO_STATUS_AGAIN;
1016 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1021 error = G_IO_CHANNEL_ERROR_FAILED;
1024 g_set_error(err, G_IO_CHANNEL_ERROR, error, _("Socket error"));
1025 return G_IO_STATUS_ERROR;
1026 /* FIXME get all errors, better error messages */
1030 *bytes_read = result;
1032 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
1037 g_io_win32_sock_write (GIOChannel *channel,
1040 gsize *bytes_written,
1043 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1045 GIOChannelError error;
1047 if (win32_channel->debug)
1048 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
1049 win32_channel->fd, count);
1050 #ifdef WE_NEED_TO_HANDLE_WSAEINTR
1053 result = send (win32_channel->fd, buf, count, 0);
1055 if (win32_channel->debug)
1056 g_print ("g_io_win32_sock_write: send:%d\n", result);
1058 if (result == SOCKET_ERROR)
1062 switch (WSAGetLastError ())
1065 error = G_IO_CHANNEL_ERROR_INVAL;
1067 case WSAEWOULDBLOCK:
1068 return G_IO_STATUS_AGAIN;
1069 #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
1074 error = G_IO_CHANNEL_ERROR_FAILED;
1077 g_set_error(err, G_IO_CHANNEL_ERROR, error, _("Socket error"));
1078 return G_IO_STATUS_ERROR;
1079 /* FIXME get all errors, better error messages */
1083 *bytes_written = result;
1085 return G_IO_STATUS_NORMAL;
1090 g_io_win32_sock_close (GIOChannel *channel,
1093 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1095 LOCK(win32_channel->mutex);
1096 if (win32_channel->running)
1098 if (win32_channel->debug)
1099 g_print ("thread %#x: running, marking for later close\n",
1100 win32_channel->thread_id);
1101 win32_channel->running = FALSE;
1102 win32_channel->needs_close = TRUE;
1103 SetEvent(win32_channel->data_avail_noticed_event);
1105 if (win32_channel->fd != -1)
1107 if (win32_channel->debug)
1108 g_print ("thread %#x: closing socket %d\n",
1109 win32_channel->thread_id,
1112 closesocket (win32_channel->fd);
1113 win32_channel->fd = -1;
1115 UNLOCK(win32_channel->mutex);
1117 /* FIXME error detection? */
1119 return G_IO_STATUS_NORMAL;
1123 g_io_win32_sock_create_watch (GIOChannel *channel,
1124 GIOCondition condition)
1126 return g_io_win32_create_watch (channel, condition, select_thread);
1130 g_io_channel_new_file (const gchar *filename,
1134 int fid, flags, pmode;
1135 GIOChannel *channel;
1137 enum { /* Cheesy hack */
1144 g_return_val_if_fail (filename != NULL, NULL);
1145 g_return_val_if_fail (mode != NULL, NULL);
1146 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
1160 g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
1169 if (mode[2] == '\0')
1171 mode_num |= MODE_PLUS;
1176 g_warning (G_STRLOC ": Invalid GIOFileMode %s.\n", mode);
1187 flags = O_WRONLY | O_TRUNC | O_CREAT;
1191 flags = O_WRONLY | O_APPEND | O_CREAT;
1194 case MODE_R | MODE_PLUS:
1196 pmode = _S_IREAD | _S_IWRITE;
1198 case MODE_W | MODE_PLUS:
1199 flags = O_RDWR | O_TRUNC | O_CREAT;
1200 pmode = _S_IREAD | _S_IWRITE;
1202 case MODE_A | MODE_PLUS:
1203 flags = O_RDWR | O_APPEND | O_CREAT;
1204 pmode = _S_IREAD | _S_IWRITE;
1207 g_assert_not_reached ();
1212 /* always open 'untranslated' */
1213 fid = open (filename, flags | _O_BINARY, pmode);
1215 if (g_io_win32_get_debug_flag ())
1217 g_print ("g_io_channel_win32_new_file: open(\"%s\", ", filename);
1218 g_win32_print_access_mode (flags|_O_BINARY);
1219 g_print (",%#o)=%d\n", pmode, fid);
1224 g_set_error (error, G_FILE_ERROR,
1225 g_file_error_from_errno (errno),
1227 return (GIOChannel *)NULL;
1230 channel = g_io_channel_win32_new_fd (fid);
1232 /* XXX: move this to g_io_channel_win32_new_fd () */
1233 channel->close_on_unref = TRUE;
1234 channel->is_seekable = TRUE;
1239 channel->is_readable = TRUE;
1240 channel->is_writeable = FALSE;
1244 channel->is_readable = FALSE;
1245 channel->is_writeable = TRUE;
1247 case MODE_R | MODE_PLUS:
1248 case MODE_W | MODE_PLUS:
1249 case MODE_A | MODE_PLUS:
1250 channel->is_readable = TRUE;
1251 channel->is_writeable = TRUE;
1254 g_assert_not_reached ();
1261 g_io_win32_set_flags (GIOChannel *channel,
1265 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1267 if (win32_channel->debug)
1269 g_print ("g_io_win32_set_flags: ");
1270 g_win32_print_gioflags (flags);
1276 g_file_error_from_errno (EACCES),
1277 _("Channel set flags unsupported"));
1278 return G_IO_STATUS_ERROR;
1282 g_io_win32_fd_get_flags (GIOChannel *channel)
1286 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1288 g_return_val_if_fail (win32_channel != NULL, 0);
1289 g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
1291 if (0 == _fstat (win32_channel->fd, &st))
1293 /* XXX: G_IO_FLAG_APPEND */
1294 /* XXX: G_IO_FLAG_NONBLOCK */
1295 if (st.st_mode & _S_IREAD) flags |= G_IO_FLAG_IS_READABLE;
1296 if (st.st_mode & _S_IWRITE) flags |= G_IO_FLAG_IS_WRITEABLE;
1298 if (!(st.st_mode & _S_IFIFO)) flags |= G_IO_FLAG_IS_SEEKABLE;
1305 * Generic implementation, just translating createion flags
1308 g_io_win32_get_flags (GIOChannel *channel)
1312 flags = (channel->is_readable ? G_IO_FLAG_IS_READABLE : 0)
1313 | (channel->is_writeable ? G_IO_FLAG_IS_READABLE : 0)
1314 | (channel->is_seekable ? G_IO_FLAG_IS_SEEKABLE : 0);
1319 static GIOFuncs win32_channel_msg_funcs = {
1320 g_io_win32_msg_read,
1321 g_io_win32_msg_write,
1323 g_io_win32_msg_close,
1324 g_io_win32_msg_create_watch,
1326 g_io_win32_set_flags,
1327 g_io_win32_get_flags,
1330 static GIOFuncs win32_channel_fd_funcs = {
1332 g_io_win32_fd_write,
1334 g_io_win32_fd_close,
1335 g_io_win32_fd_create_watch,
1337 g_io_win32_set_flags,
1338 g_io_win32_fd_get_flags,
1341 static GIOFuncs win32_channel_sock_funcs = {
1342 g_io_win32_sock_read,
1343 g_io_win32_sock_write,
1345 g_io_win32_sock_close,
1346 g_io_win32_sock_create_watch,
1348 g_io_win32_set_flags,
1349 g_io_win32_get_flags,
1353 g_io_channel_win32_new_messages (guint hwnd)
1355 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1356 GIOChannel *channel = (GIOChannel *)win32_channel;
1358 g_io_channel_init (channel);
1359 g_io_channel_win32_init (win32_channel);
1360 if (win32_channel->debug)
1361 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1362 channel->funcs = &win32_channel_msg_funcs;
1363 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1364 win32_channel->hwnd = (HWND) hwnd;
1366 /* XXX: check this. */
1367 channel->is_readable = IsWindow (win32_channel->hwnd);
1368 channel->is_writeable = IsWindow (win32_channel->hwnd);
1370 channel->is_seekable = FALSE;
1376 g_io_channel_win32_new_fd (gint fd)
1378 GIOWin32Channel *win32_channel;
1379 GIOChannel *channel;
1382 if (fstat (fd, &st) == -1)
1384 g_warning (G_STRLOC ": %d isn't a (emulated) file descriptor", fd);
1388 win32_channel = g_new (GIOWin32Channel, 1);
1389 channel = (GIOChannel *)win32_channel;
1391 g_io_channel_init (channel);
1392 g_io_channel_win32_init (win32_channel);
1393 if (win32_channel->debug)
1394 g_print ("g_io_channel_win32_new_fd: %u\n", fd);
1395 channel->funcs = &win32_channel_fd_funcs;
1396 win32_channel->type = G_IO_WIN32_FILE_DESC;
1397 win32_channel->fd = fd;
1400 /* fstat doesn't deliver senseful values, but
1401 * fcntl isn't available, so guess ...
1403 if (st.st_mode & _S_IFIFO)
1405 channel->is_readable = TRUE;
1406 channel->is_writeable = TRUE;
1407 channel->is_seekable = FALSE;
1411 channel->is_readable = !!(st.st_mode & _S_IREAD);
1412 channel->is_writeable = !!(st.st_mode & _S_IWRITE);
1413 /* XXX What about "device files" (COM1: and the like) */
1414 channel->is_seekable = TRUE;
1421 g_io_channel_win32_get_fd (GIOChannel *channel)
1423 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1425 return win32_channel->fd;
1429 g_io_channel_win32_new_socket (int socket)
1431 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1432 GIOChannel *channel = (GIOChannel *)win32_channel;
1434 g_io_channel_init (channel);
1435 g_io_channel_win32_init (win32_channel);
1436 if (win32_channel->debug)
1437 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1438 channel->funcs = &win32_channel_sock_funcs;
1439 win32_channel->type = G_IO_WIN32_SOCKET;
1440 win32_channel->fd = socket;
1442 /* XXX: check this */
1443 channel->is_readable = TRUE;
1444 channel->is_writeable = TRUE;
1445 channel->is_seekable = FALSE;
1451 g_io_channel_unix_new (gint fd)
1455 if (fstat (fd, &st) == 0)
1456 return g_io_channel_win32_new_fd (fd);
1458 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
1459 return g_io_channel_win32_new_socket(fd);
1461 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1466 g_io_channel_unix_get_fd (GIOChannel *channel)
1468 return g_io_channel_win32_get_fd (channel);
1472 g_io_channel_win32_set_debug (GIOChannel *channel,
1475 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1477 win32_channel->debug = flag;
1481 g_io_channel_win32_poll (GPollFD *fds,
1487 g_return_val_if_fail (n_fds >= 0, 0);
1489 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1495 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1496 GIOCondition condition,
1499 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1501 if (win32_channel->data_avail_event == NULL)
1502 create_events (win32_channel);
1504 fd->fd = (gint) win32_channel->data_avail_event;
1505 fd->events = condition;
1507 if (win32_channel->thread_id == 0)
1509 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1510 create_thread (win32_channel, condition, read_thread);
1511 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1512 create_thread (win32_channel, condition, select_thread);
1516 /* Binary compatibility */
1518 g_io_channel_win32_new_stream_socket (int socket)
1520 return g_io_channel_win32_new_socket (socket);