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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
26 * file for a list of people on the GLib Team. See the ChangeLog
27 * files for a list of changes. These files are distributed with
28 * GLib at ftp://ftp.gtk.org/pub/gtk/.
31 /* Define this to get (very) verbose logging of all channels */
32 /* #define G_IO_WIN32_DEBUG */
38 #include <winsock.h> /* Not everybody has winsock2 */
45 typedef struct _GIOWin32Channel GIOWin32Channel;
46 typedef struct _GIOWin32Watch GIOWin32Watch;
48 #define BUFFER_SIZE 4096
51 G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */
52 G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from
53 * _open() or _pipe(). Read with read().
54 * Have to create separate thread to read.
56 G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked
57 * in select() most of the time.
59 } GIOWin32ChannelType;
61 struct _GIOWin32Channel {
63 gint fd; /* Either a Unix-like file handle as provided
64 * by the Microsoft C runtime, or a SOCKET
65 * as provided by WinSock.
67 GIOWin32ChannelType type;
71 CRITICAL_SECTION mutex;
73 /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
74 HWND hwnd; /* handle of window, or NULL */
76 /* Following fields are used by both fd and socket channels. */
77 gboolean running; /* Is reader thread running. FALSE if
78 * EOF has been reached.
80 gboolean needs_close; /* If the channel has been closed while
81 * the reader thread was still running.
83 guint thread_id; /* If non-NULL has a reader thread, or has
86 HANDLE data_avail_event;
90 /* Following fields used by fd channels for input */
92 /* Data is kept in a circular buffer. To be able to distinguish between
93 * empty and full buffer, we cannot fill it completely, but have to
94 * leave a one character gap.
96 * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
99 * Full: (wrp + 1) % BUFFER_SIZE == rdp
102 guchar *buffer; /* (Circular) buffer */
103 gint wrp, rdp; /* Buffer indices for writing and reading */
104 HANDLE space_avail_event;
106 /* Following fields used by socket channels */
108 HANDLE data_avail_noticed_event;
111 #define LOCK(mutex) EnterCriticalSection (&mutex)
112 #define UNLOCK(mutex) LeaveCriticalSection (&mutex)
114 struct _GIOWin32Watch {
118 GIOCondition condition;
123 g_io_channel_win32_init (GIOWin32Channel *channel)
125 #ifdef G_IO_WIN32_DEBUG
126 channel->debug = TRUE;
128 if (getenv ("G_IO_WIN32_DEBUG") != NULL)
129 channel->debug = TRUE;
131 channel->debug = FALSE;
133 channel->buffer = NULL;
134 channel->running = FALSE;
135 channel->needs_close = FALSE;
136 channel->thread_id = 0;
137 channel->data_avail_event = NULL;
138 channel->revents = 0;
139 channel->space_avail_event = NULL;
140 channel->data_avail_noticed_event = NULL;
141 channel->watches = NULL;
142 InitializeCriticalSection (&channel->mutex);
146 create_events (GIOWin32Channel *channel)
148 SECURITY_ATTRIBUTES sec_attrs;
150 sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
151 sec_attrs.lpSecurityDescriptor = NULL;
152 sec_attrs.bInheritHandle = FALSE;
154 /* The data available event is manual reset, the space available event
155 * is automatic reset.
157 if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
158 || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
159 || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
161 gchar *msg = g_win32_error_message (GetLastError ());
162 g_error ("Error creating event: %s", msg);
166 static unsigned __stdcall
167 read_thread (void *parameter)
169 GIOWin32Channel *channel = parameter;
174 g_io_channel_ref ((GIOChannel *)channel);
177 g_print ("read_thread %#x: start fd:%d, data_avail:%#x, space_avail:%#x\n",
180 (guint) channel->data_avail_event,
181 (guint) channel->space_avail_event);
183 channel->buffer = g_malloc (BUFFER_SIZE);
184 channel->rdp = channel->wrp = 0;
185 channel->running = TRUE;
187 SetEvent (channel->space_avail_event);
189 while (channel->running)
191 LOCK (channel->mutex);
193 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
194 channel->thread_id, channel->rdp, channel->wrp);
195 if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
199 g_print ("read_thread %#x: resetting space_avail\n",
201 ResetEvent (channel->space_avail_event);
203 g_print ("read_thread %#x: waiting for space\n",
205 UNLOCK (channel->mutex);
206 WaitForSingleObject (channel->space_avail_event, INFINITE);
207 LOCK (channel->mutex);
209 g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
210 channel->thread_id, channel->rdp, channel->wrp);
213 buffer = channel->buffer + channel->wrp;
215 /* Always leave at least one byte unused gap to be able to
216 * distinguish between the full and empty condition...
218 nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
219 BUFFER_SIZE - channel->wrp);
222 g_print ("read_thread %#x: calling read() for %d bytes\n",
223 channel->thread_id, nbytes);
225 UNLOCK (channel->mutex);
227 nbytes = read (channel->fd, buffer, nbytes);
229 LOCK (channel->mutex);
231 channel->revents = G_IO_IN;
233 channel->revents |= G_IO_HUP;
235 channel->revents |= G_IO_ERR;
238 g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
239 channel->thread_id, nbytes, channel->rdp, channel->wrp);
244 channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
246 g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
247 channel->thread_id, channel->rdp, channel->wrp);
248 SetEvent (channel->data_avail_event);
249 UNLOCK (channel->mutex);
252 channel->running = FALSE;
253 if (channel->needs_close)
256 g_print ("read_thread %#x: channel fd %d needs closing\n",
257 channel->thread_id, channel->fd);
263 g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
264 channel->thread_id, channel->rdp, channel->wrp);
265 SetEvent (channel->data_avail_event);
266 UNLOCK (channel->mutex);
268 g_io_channel_unref((GIOChannel *)channel);
270 /* No need to call _endthreadex(), the actual thread starter routine
271 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
272 * _endthreadex() for us.
275 CloseHandle (channel->thread_handle);
281 create_thread (GIOWin32Channel *channel,
282 GIOCondition condition,
283 unsigned (__stdcall *thread) (void *parameter))
285 if ((channel->thread_handle =
286 (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
287 &channel->thread_id)) == 0)
288 g_warning ("Error creating reader thread: %s", strerror (errno));
289 WaitForSingleObject (channel->space_avail_event, INFINITE);
293 buffer_read (GIOWin32Channel *channel,
301 LOCK (channel->mutex);
303 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
304 channel->thread_id, count, channel->rdp, channel->wrp);
306 if (channel->wrp == channel->rdp)
308 UNLOCK (channel->mutex);
310 g_print ("waiting for data from thread %#x\n", channel->thread_id);
311 WaitForSingleObject (channel->data_avail_event, INFINITE);
313 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
314 LOCK (channel->mutex);
315 if (channel->wrp == channel->rdp && !channel->running)
317 UNLOCK (channel->mutex);
322 if (channel->rdp < channel->wrp)
323 nbytes = channel->wrp - channel->rdp;
325 nbytes = BUFFER_SIZE - channel->rdp;
326 UNLOCK (channel->mutex);
327 nbytes = MIN (left, nbytes);
329 g_print ("moving %d bytes from thread %#x\n",
330 nbytes, channel->thread_id);
331 memcpy (dest, channel->buffer + channel->rdp, nbytes);
334 LOCK (channel->mutex);
335 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
337 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
338 SetEvent (channel->space_avail_event);
340 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
341 channel->thread_id, channel->rdp, channel->wrp);
342 if (channel->running && channel->wrp == channel->rdp)
345 g_print ("resetting data_avail of thread %#x\n",
347 ResetEvent (channel->data_avail_event);
349 UNLOCK (channel->mutex);
351 /* We have no way to indicate any errors form the actual
352 * read() or recv() call in the reader thread. Should we have?
354 *error = G_IO_ERROR_NONE;
358 static unsigned __stdcall
359 select_thread (void *parameter)
361 GIOWin32Channel *channel = parameter;
362 fd_set read_fds, write_fds, except_fds;
366 g_io_channel_ref ((GIOChannel *)channel);
369 g_print ("select_thread %#x: start fd:%d,\n\tdata_avail:%#x, data_avail_noticed:%#x\n",
372 (guint) channel->data_avail_event,
373 (guint) channel->data_avail_noticed_event);
375 channel->rdp = channel->wrp = 0;
376 channel->running = TRUE;
378 SetEvent (channel->space_avail_event);
380 while (channel->running)
383 FD_ZERO (&write_fds);
384 FD_ZERO (&except_fds);
386 tmp = channel->watches;
389 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
391 if (watch->condition & (G_IO_IN | G_IO_HUP))
392 FD_SET (channel->fd, &read_fds);
393 if (watch->condition & G_IO_OUT)
394 FD_SET (channel->fd, &write_fds);
395 if (watch->condition & G_IO_ERR)
396 FD_SET (channel->fd, &except_fds);
401 g_print ("select_thread %#x: calling select() for%s%s%s\n",
403 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
404 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
405 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
407 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
409 if (n == SOCKET_ERROR)
412 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
418 g_print ("select_thread %#x: got%s%s%s\n",
420 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
421 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
422 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
424 if (FD_ISSET (channel->fd, &read_fds))
425 channel->revents |= G_IO_IN;
426 if (FD_ISSET (channel->fd, &write_fds))
427 channel->revents |= G_IO_OUT;
428 if (FD_ISSET (channel->fd, &except_fds))
429 channel->revents |= G_IO_ERR;
432 g_print ("select_thread %#x: resetting data_avail_noticed,\n"
433 "\tsetting data_avail\n",
435 ResetEvent (channel->data_avail_noticed_event);
436 SetEvent (channel->data_avail_event);
439 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
442 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
444 g_print ("select_thread %#x: got data_avail_noticed\n",
448 channel->running = FALSE;
449 LOCK (channel->mutex);
450 if (channel->needs_close)
453 g_print ("select_thread %#x: channel fd %d needs closing\n",
454 channel->thread_id, channel->fd);
455 closesocket (channel->fd);
460 g_print ("select_thread %#x: got error, setting data_avail\n",
462 SetEvent (channel->data_avail_event);
463 UNLOCK (channel->mutex);
465 g_io_channel_unref((GIOChannel *)channel);
467 /* No need to call _endthreadex(), the actual thread starter routine
468 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
469 * _endthreadex() for us.
472 CloseHandle (channel->thread_handle);
478 g_io_win32_prepare (GSource *source,
481 GIOWin32Watch *watch = (GIOWin32Watch *)source;
482 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
486 if (channel->type == G_IO_WIN32_FILE_DESC)
488 LOCK (channel->mutex);
489 if (channel->running && channel->wrp == channel->rdp)
490 channel->revents = 0;
491 UNLOCK (channel->mutex);
493 else if (channel->type == G_IO_WIN32_SOCKET)
495 channel->revents = 0;
498 g_print ("g_io_win32_prepare: thread %#x, setting data_avail_noticed\n",
500 SetEvent (channel->data_avail_noticed_event);
502 g_print ("g_io_win32_prepare: thread %#x, there.\n",
510 g_io_win32_check (GSource *source)
512 GIOWin32Watch *watch = (GIOWin32Watch *)source;
513 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
516 g_print ("g_io_win32_check: for thread %#x:\n"
517 "\twatch->pollfd.events:%#x, watch->pollfd.revents:%#x, channel->revents:%#x\n",
519 watch->pollfd.events, watch->pollfd.revents, channel->revents);
521 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
522 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
524 if (channel->type == G_IO_WIN32_SOCKET)
527 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
529 ResetEvent (channel->data_avail_event);
531 g_print ("g_io_win32_check: thread %#x, there.\n",
535 return (watch->pollfd.revents & watch->condition);
539 g_io_win32_dispatch (GSource *source,
540 GSourceFunc callback,
543 GIOFunc func = (GIOFunc)callback;
544 GIOWin32Watch *watch = (GIOWin32Watch *)source;
548 g_warning ("GIOWin32Watch dispatched without callback\n"
549 "You must call g_source_connect().");
553 return (*func) (watch->channel,
554 watch->pollfd.revents & watch->condition,
559 g_io_win32_destroy (GSource *source)
561 GIOWin32Watch *watch = (GIOWin32Watch *)source;
562 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
565 g_print ("g_io_win32_destroy: channel with thread %#x\n",
568 channel->watches = g_slist_remove (channel->watches, watch);
570 g_io_channel_unref (watch->channel);
573 static GSourceFuncs win32_watch_funcs = {
581 g_io_win32_create_watch (GIOChannel *channel,
582 GIOCondition condition,
583 unsigned (__stdcall *thread) (void *parameter))
585 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
586 GIOWin32Watch *watch;
589 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
590 watch = (GIOWin32Watch *)source;
592 watch->channel = channel;
593 g_io_channel_ref (channel);
595 watch->condition = condition;
597 if (win32_channel->data_avail_event == NULL)
598 create_events (win32_channel);
600 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
601 watch->pollfd.events = condition;
603 if (win32_channel->debug)
604 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
605 win32_channel->fd, condition, watch->pollfd.fd);
607 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
609 if (win32_channel->thread_id == 0)
610 create_thread (win32_channel, condition, thread);
612 g_source_add_poll (source, &watch->pollfd);
618 g_io_win32_msg_read (GIOChannel *channel,
623 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
624 MSG msg; /* In case of alignment problems */
626 if (count < sizeof (MSG))
627 return G_IO_ERROR_INVAL;
629 if (win32_channel->debug)
630 g_print ("g_io_win32_msg_read: for %#x\n",
631 win32_channel->hwnd);
632 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
633 return G_IO_ERROR_AGAIN;
635 memmove (buf, &msg, sizeof (MSG));
636 *bytes_read = sizeof (MSG);
637 return G_IO_ERROR_NONE;
641 g_io_win32_msg_write (GIOChannel *channel,
644 guint *bytes_written)
646 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
649 if (count != sizeof (MSG))
650 return G_IO_ERROR_INVAL;
652 /* In case of alignment problems */
653 memmove (&msg, buf, sizeof (MSG));
654 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
655 return G_IO_ERROR_UNKNOWN;
657 *bytes_written = sizeof (MSG);
658 return G_IO_ERROR_NONE;
662 g_io_win32_no_seek (GIOChannel *channel,
666 return G_IO_ERROR_UNKNOWN;
670 g_io_win32_msg_close (GIOChannel *channel)
672 /* Nothing to be done. Or should we set hwnd to some invalid value? */
676 g_io_win32_free (GIOChannel *channel)
678 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
680 if (win32_channel->debug)
681 g_print ("thread %#x: freeing channel, fd: %d\n",
682 win32_channel->thread_id,
685 if (win32_channel->data_avail_event)
686 CloseHandle (win32_channel->data_avail_event);
687 if (win32_channel->space_avail_event)
688 CloseHandle (win32_channel->space_avail_event);
689 if (win32_channel->data_avail_noticed_event)
690 CloseHandle (win32_channel->data_avail_noticed_event);
691 DeleteCriticalSection (&win32_channel->mutex);
693 g_free (win32_channel->buffer);
694 g_slist_free (win32_channel->watches);
695 g_free (win32_channel);
699 g_io_win32_msg_create_watch (GIOChannel *channel,
700 GIOCondition condition)
702 GIOWin32Watch *watch;
705 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
706 watch = (GIOWin32Watch *)source;
708 watch->channel = channel;
709 g_io_channel_ref (channel);
711 watch->condition = condition;
713 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
714 watch->pollfd.events = condition;
716 g_source_add_poll (source, &watch->pollfd);
722 g_io_win32_fd_read (GIOChannel *channel,
727 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
731 if (win32_channel->debug)
732 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
733 win32_channel->fd, count);
735 if (win32_channel->thread_id)
737 result = buffer_read (win32_channel, buf, count, &error);
745 *bytes_read = result;
746 return G_IO_ERROR_NONE;
750 result = read (win32_channel->fd, buf, count);
756 return G_IO_ERROR_INVAL;
758 return G_IO_ERROR_UNKNOWN;
762 *bytes_read = result;
763 return G_IO_ERROR_NONE;
768 g_io_win32_fd_write (GIOChannel *channel,
771 guint *bytes_written)
773 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
776 result = write (win32_channel->fd, buf, count);
777 if (win32_channel->debug)
778 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
779 win32_channel->fd, count, result);
787 return G_IO_ERROR_INVAL;
789 return G_IO_ERROR_AGAIN;
791 return G_IO_ERROR_UNKNOWN;
796 *bytes_written = result;
797 return G_IO_ERROR_NONE;
802 g_io_win32_fd_seek (GIOChannel *channel,
806 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
822 g_warning ("g_io_win32_fd_seek: unknown seek type");
823 return G_IO_ERROR_UNKNOWN;
826 result = lseek (win32_channel->fd, offset, whence);
833 return G_IO_ERROR_INVAL;
835 return G_IO_ERROR_UNKNOWN;
839 return G_IO_ERROR_NONE;
843 g_io_win32_fd_close (GIOChannel *channel)
845 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
847 if (win32_channel->debug)
848 g_print ("thread %#x: closing fd %d\n",
849 win32_channel->thread_id,
851 LOCK (win32_channel->mutex);
852 if (win32_channel->running)
854 if (win32_channel->debug)
855 g_print ("thread %#x: running, marking fd %d for later close\n",
856 win32_channel->thread_id, win32_channel->fd);
857 win32_channel->running = FALSE;
858 win32_channel->needs_close = TRUE;
859 SetEvent (win32_channel->data_avail_event);
863 if (win32_channel->debug)
864 g_print ("closing fd %d\n", win32_channel->fd);
865 close (win32_channel->fd);
866 if (win32_channel->debug)
867 g_print ("closed fd %d, setting to -1\n",
869 win32_channel->fd = -1;
871 UNLOCK (win32_channel->mutex);
875 g_io_win32_fd_create_watch (GIOChannel *channel,
876 GIOCondition condition)
878 return g_io_win32_create_watch (channel, condition, read_thread);
882 g_io_win32_sock_read (GIOChannel *channel,
887 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
891 if (win32_channel->debug)
892 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
893 win32_channel->fd, count);
895 result = recv (win32_channel->fd, buf, count, 0);
897 if (win32_channel->debug)
898 g_print ("g_io_win32_sock_read: recv:%d\n", result);
900 if (result == SOCKET_ERROR)
903 switch (WSAGetLastError ())
906 return G_IO_ERROR_INVAL;
909 return G_IO_ERROR_AGAIN;
911 return G_IO_ERROR_UNKNOWN;
916 *bytes_read = result;
917 return G_IO_ERROR_NONE;
922 g_io_win32_sock_write (GIOChannel *channel,
925 guint *bytes_written)
927 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
930 if (win32_channel->debug)
931 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
932 win32_channel->fd, count);
934 result = send (win32_channel->fd, buf, count, 0);
936 if (win32_channel->debug)
937 g_print ("g_io_win32_sock_write: send:%d\n", result);
939 if (result == SOCKET_ERROR)
942 switch (WSAGetLastError ())
945 return G_IO_ERROR_INVAL;
948 return G_IO_ERROR_AGAIN;
950 return G_IO_ERROR_UNKNOWN;
955 *bytes_written = result;
956 return G_IO_ERROR_NONE;
961 g_io_win32_sock_close (GIOChannel *channel)
963 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
965 if (win32_channel->debug)
966 g_print ("thread %#x: closing socket %d\n",
967 win32_channel->thread_id,
969 closesocket (win32_channel->fd);
970 win32_channel->fd = -1;
974 g_io_win32_sock_create_watch (GIOChannel *channel,
975 GIOCondition condition)
977 return g_io_win32_create_watch (channel, condition, select_thread);
980 static GIOFuncs win32_channel_msg_funcs = {
982 g_io_win32_msg_write,
984 g_io_win32_msg_close,
985 g_io_win32_msg_create_watch,
989 static GIOFuncs win32_channel_fd_funcs = {
994 g_io_win32_fd_create_watch,
998 static GIOFuncs win32_channel_sock_funcs = {
999 g_io_win32_sock_read,
1000 g_io_win32_sock_write,
1002 g_io_win32_sock_close,
1003 g_io_win32_sock_create_watch,
1008 g_io_channel_win32_new_messages (guint hwnd)
1010 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1011 GIOChannel *channel = (GIOChannel *)win32_channel;
1013 g_io_channel_init (channel);
1014 g_io_channel_win32_init (win32_channel);
1015 if (win32_channel->debug)
1016 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1017 channel->funcs = &win32_channel_msg_funcs;
1018 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1019 win32_channel->hwnd = (HWND) hwnd;
1025 g_io_channel_win32_new_fd (gint fd)
1027 GIOWin32Channel *win32_channel;
1028 GIOChannel *channel;
1031 if (fstat (fd, &st) == -1)
1033 g_warning ("%d isn't a (emulated) file descriptor", fd);
1037 win32_channel = g_new (GIOWin32Channel, 1);
1038 channel = (GIOChannel *)win32_channel;
1040 g_io_channel_init (channel);
1041 g_io_channel_win32_init (win32_channel);
1042 if (win32_channel->debug)
1043 g_print ("g_io_channel_win32_new_fd: fd = %d\n", fd);
1044 channel->funcs = &win32_channel_fd_funcs;
1045 win32_channel->type = G_IO_WIN32_FILE_DESC;
1046 win32_channel->fd = fd;
1052 g_io_channel_win32_get_fd (GIOChannel *channel)
1054 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1056 return win32_channel->fd;
1060 g_io_channel_win32_new_socket (int socket)
1062 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1063 GIOChannel *channel = (GIOChannel *)win32_channel;
1065 g_io_channel_init (channel);
1066 g_io_channel_win32_init (win32_channel);
1067 if (win32_channel->debug)
1068 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1069 channel->funcs = &win32_channel_sock_funcs;
1070 win32_channel->type = G_IO_WIN32_SOCKET;
1071 win32_channel->fd = socket;
1077 g_io_channel_unix_new (gint fd)
1081 if (fstat (fd, &st) == 0)
1082 return g_io_channel_win32_new_fd (fd);
1084 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
1085 return g_io_channel_win32_new_socket(fd);
1087 g_warning ("%d is neither a file descriptor or a socket", fd);
1092 g_io_channel_unix_get_fd (GIOChannel *channel)
1094 return g_io_channel_win32_get_fd (channel);
1098 g_io_channel_win32_set_debug (GIOChannel *channel,
1101 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1103 win32_channel->debug = flag;
1107 g_io_channel_win32_poll (GPollFD *fds,
1113 g_return_val_if_fail (n_fds >= 0, 0);
1115 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1121 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1122 GIOCondition condition,
1125 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1127 if (win32_channel->data_avail_event == NULL)
1128 create_events (win32_channel);
1130 fd->fd = (gint) win32_channel->data_avail_event;
1131 fd->events = condition;
1133 if (win32_channel->thread_id == 0)
1134 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1135 create_thread (win32_channel, condition, read_thread);
1136 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1137 create_thread (win32_channel, condition, select_thread);
1140 /* Binary compatibility */
1142 g_io_channel_win32_new_stream_socket (int socket)
1144 return g_io_channel_win32_new_socket (socket);