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 channel->thread_handle =
286 (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
287 &channel->thread_id);
288 if (channel->thread_handle == 0)
289 g_warning (G_STRLOC ": Error creating reader thread: %s",
291 WaitForSingleObject (channel->space_avail_event, INFINITE);
295 buffer_read (GIOWin32Channel *channel,
303 LOCK (channel->mutex);
305 g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
306 channel->thread_id, count, channel->rdp, channel->wrp);
308 if (channel->wrp == channel->rdp)
310 UNLOCK (channel->mutex);
312 g_print ("waiting for data from thread %#x\n", channel->thread_id);
313 WaitForSingleObject (channel->data_avail_event, INFINITE);
315 g_print ("done waiting for data from thread %#x\n", channel->thread_id);
316 LOCK (channel->mutex);
317 if (channel->wrp == channel->rdp && !channel->running)
319 UNLOCK (channel->mutex);
324 if (channel->rdp < channel->wrp)
325 nbytes = channel->wrp - channel->rdp;
327 nbytes = BUFFER_SIZE - channel->rdp;
328 UNLOCK (channel->mutex);
329 nbytes = MIN (left, nbytes);
331 g_print ("moving %d bytes from thread %#x\n",
332 nbytes, channel->thread_id);
333 memcpy (dest, channel->buffer + channel->rdp, nbytes);
336 LOCK (channel->mutex);
337 channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
339 g_print ("setting space_avail for thread %#x\n", channel->thread_id);
340 SetEvent (channel->space_avail_event);
342 g_print ("for thread %#x: rdp=%d, wrp=%d\n",
343 channel->thread_id, channel->rdp, channel->wrp);
344 if (channel->running && channel->wrp == channel->rdp)
347 g_print ("resetting data_avail of thread %#x\n",
349 ResetEvent (channel->data_avail_event);
351 UNLOCK (channel->mutex);
353 /* We have no way to indicate any errors form the actual
354 * read() or recv() call in the reader thread. Should we have?
356 *error = G_IO_ERROR_NONE;
360 static unsigned __stdcall
361 select_thread (void *parameter)
363 GIOWin32Channel *channel = parameter;
364 fd_set read_fds, write_fds, except_fds;
368 g_io_channel_ref ((GIOChannel *)channel);
371 g_print ("select_thread %#x: start fd:%d,\n\tdata_avail:%#x, data_avail_noticed:%#x\n",
374 (guint) channel->data_avail_event,
375 (guint) channel->data_avail_noticed_event);
377 channel->rdp = channel->wrp = 0;
378 channel->running = TRUE;
380 SetEvent (channel->space_avail_event);
382 while (channel->running)
385 FD_ZERO (&write_fds);
386 FD_ZERO (&except_fds);
388 tmp = channel->watches;
391 GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;
393 if (watch->condition & (G_IO_IN | G_IO_HUP))
394 FD_SET (channel->fd, &read_fds);
395 if (watch->condition & G_IO_OUT)
396 FD_SET (channel->fd, &write_fds);
397 if (watch->condition & G_IO_ERR)
398 FD_SET (channel->fd, &except_fds);
403 g_print ("select_thread %#x: calling select() for%s%s%s\n",
405 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
406 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
407 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
409 n = select (1, &read_fds, &write_fds, &except_fds, NULL);
411 if (n == SOCKET_ERROR)
414 g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
420 g_print ("select_thread %#x: got%s%s%s\n",
422 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
423 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
424 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
426 if (FD_ISSET (channel->fd, &read_fds))
427 channel->revents |= G_IO_IN;
428 if (FD_ISSET (channel->fd, &write_fds))
429 channel->revents |= G_IO_OUT;
430 if (FD_ISSET (channel->fd, &except_fds))
431 channel->revents |= G_IO_ERR;
434 g_print ("select_thread %#x: resetting data_avail_noticed,\n"
435 "\tsetting data_avail\n",
437 ResetEvent (channel->data_avail_noticed_event);
438 SetEvent (channel->data_avail_event);
441 g_print ("select_thread %#x: waiting for data_avail_noticed\n",
444 WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
446 g_print ("select_thread %#x: got data_avail_noticed\n",
450 channel->running = FALSE;
451 LOCK (channel->mutex);
452 if (channel->needs_close)
455 g_print ("select_thread %#x: channel fd %d needs closing\n",
456 channel->thread_id, channel->fd);
457 closesocket (channel->fd);
462 g_print ("select_thread %#x: got error, setting data_avail\n",
464 SetEvent (channel->data_avail_event);
465 UNLOCK (channel->mutex);
467 g_io_channel_unref((GIOChannel *)channel);
469 /* No need to call _endthreadex(), the actual thread starter routine
470 * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
471 * _endthreadex() for us.
474 CloseHandle (channel->thread_handle);
480 g_io_win32_prepare (GSource *source,
483 GIOWin32Watch *watch = (GIOWin32Watch *)source;
484 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
488 if (channel->type == G_IO_WIN32_FILE_DESC)
490 LOCK (channel->mutex);
491 if (channel->running && channel->wrp == channel->rdp)
492 channel->revents = 0;
493 UNLOCK (channel->mutex);
495 else if (channel->type == G_IO_WIN32_SOCKET)
497 channel->revents = 0;
500 g_print ("g_io_win32_prepare: thread %#x, setting data_avail_noticed\n",
502 SetEvent (channel->data_avail_noticed_event);
504 g_print ("g_io_win32_prepare: thread %#x, there.\n",
512 g_io_win32_check (GSource *source)
514 GIOWin32Watch *watch = (GIOWin32Watch *)source;
515 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
518 g_print ("g_io_win32_check: for thread %#x:\n"
519 "\twatch->pollfd.events:%#x, watch->pollfd.revents:%#x, channel->revents:%#x\n",
521 watch->pollfd.events, watch->pollfd.revents, channel->revents);
523 if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
524 watch->pollfd.revents = (watch->pollfd.events & channel->revents);
526 if (channel->type == G_IO_WIN32_SOCKET)
529 g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
531 ResetEvent (channel->data_avail_event);
533 g_print ("g_io_win32_check: thread %#x, there.\n",
537 return (watch->pollfd.revents & watch->condition);
541 g_io_win32_dispatch (GSource *source,
542 GSourceFunc callback,
545 GIOFunc func = (GIOFunc)callback;
546 GIOWin32Watch *watch = (GIOWin32Watch *)source;
550 g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
551 "You must call g_source_connect().");
555 return (*func) (watch->channel,
556 watch->pollfd.revents & watch->condition,
561 g_io_win32_destroy (GSource *source)
563 GIOWin32Watch *watch = (GIOWin32Watch *)source;
564 GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
567 g_print ("g_io_win32_destroy: channel with thread %#x\n",
570 channel->watches = g_slist_remove (channel->watches, watch);
572 g_io_channel_unref (watch->channel);
575 static GSourceFuncs win32_watch_funcs = {
583 g_io_win32_create_watch (GIOChannel *channel,
584 GIOCondition condition,
585 unsigned (__stdcall *thread) (void *parameter))
587 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
588 GIOWin32Watch *watch;
591 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
592 watch = (GIOWin32Watch *)source;
594 watch->channel = channel;
595 g_io_channel_ref (channel);
597 watch->condition = condition;
599 if (win32_channel->data_avail_event == NULL)
600 create_events (win32_channel);
602 watch->pollfd.fd = (gint) win32_channel->data_avail_event;
603 watch->pollfd.events = condition;
605 if (win32_channel->debug)
606 g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
607 win32_channel->fd, condition, watch->pollfd.fd);
609 win32_channel->watches = g_slist_append (win32_channel->watches, watch);
611 if (win32_channel->thread_id == 0)
612 create_thread (win32_channel, condition, thread);
614 g_source_add_poll (source, &watch->pollfd);
620 g_io_win32_msg_read (GIOChannel *channel,
625 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
626 MSG msg; /* In case of alignment problems */
628 if (count < sizeof (MSG))
629 return G_IO_ERROR_INVAL;
631 if (win32_channel->debug)
632 g_print ("g_io_win32_msg_read: for %#x\n",
633 win32_channel->hwnd);
634 if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
635 return G_IO_ERROR_AGAIN;
637 memmove (buf, &msg, sizeof (MSG));
638 *bytes_read = sizeof (MSG);
639 return G_IO_ERROR_NONE;
643 g_io_win32_msg_write (GIOChannel *channel,
646 guint *bytes_written)
648 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
651 if (count != sizeof (MSG))
652 return G_IO_ERROR_INVAL;
654 /* In case of alignment problems */
655 memmove (&msg, buf, sizeof (MSG));
656 if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
657 return G_IO_ERROR_UNKNOWN;
659 *bytes_written = sizeof (MSG);
660 return G_IO_ERROR_NONE;
664 g_io_win32_no_seek (GIOChannel *channel,
668 return G_IO_ERROR_UNKNOWN;
672 g_io_win32_msg_close (GIOChannel *channel)
674 /* Nothing to be done. Or should we set hwnd to some invalid value? */
678 g_io_win32_free (GIOChannel *channel)
680 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
682 if (win32_channel->debug)
683 g_print ("thread %#x: freeing channel, fd: %d\n",
684 win32_channel->thread_id,
687 if (win32_channel->data_avail_event)
688 CloseHandle (win32_channel->data_avail_event);
689 if (win32_channel->space_avail_event)
690 CloseHandle (win32_channel->space_avail_event);
691 if (win32_channel->data_avail_noticed_event)
692 CloseHandle (win32_channel->data_avail_noticed_event);
693 DeleteCriticalSection (&win32_channel->mutex);
695 g_free (win32_channel->buffer);
696 g_slist_free (win32_channel->watches);
697 g_free (win32_channel);
701 g_io_win32_msg_create_watch (GIOChannel *channel,
702 GIOCondition condition)
704 GIOWin32Watch *watch;
707 source = g_source_new (&win32_watch_funcs, sizeof (GIOWin32Watch));
708 watch = (GIOWin32Watch *)source;
710 watch->channel = channel;
711 g_io_channel_ref (channel);
713 watch->condition = condition;
715 watch->pollfd.fd = G_WIN32_MSG_HANDLE;
716 watch->pollfd.events = condition;
718 g_source_add_poll (source, &watch->pollfd);
724 g_io_win32_fd_read (GIOChannel *channel,
729 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
733 if (win32_channel->debug)
734 g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
735 win32_channel->fd, count);
737 if (win32_channel->thread_id)
739 result = buffer_read (win32_channel, buf, count, &error);
747 *bytes_read = result;
748 return G_IO_ERROR_NONE;
752 result = read (win32_channel->fd, buf, count);
758 return G_IO_ERROR_INVAL;
760 return G_IO_ERROR_UNKNOWN;
764 *bytes_read = result;
765 return G_IO_ERROR_NONE;
770 g_io_win32_fd_write (GIOChannel *channel,
773 guint *bytes_written)
775 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
778 result = write (win32_channel->fd, buf, count);
779 if (win32_channel->debug)
780 g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
781 win32_channel->fd, count, result);
789 return G_IO_ERROR_INVAL;
791 return G_IO_ERROR_AGAIN;
793 return G_IO_ERROR_UNKNOWN;
798 *bytes_written = result;
799 return G_IO_ERROR_NONE;
804 g_io_win32_fd_seek (GIOChannel *channel,
808 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
824 g_warning (G_STRLOC ": Unknown seek type %d", (int) type);
825 return G_IO_ERROR_UNKNOWN;
828 result = lseek (win32_channel->fd, offset, whence);
835 return G_IO_ERROR_INVAL;
837 return G_IO_ERROR_UNKNOWN;
841 return G_IO_ERROR_NONE;
845 g_io_win32_fd_close (GIOChannel *channel)
847 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
849 if (win32_channel->debug)
850 g_print ("thread %#x: closing fd %d\n",
851 win32_channel->thread_id,
853 LOCK (win32_channel->mutex);
854 if (win32_channel->running)
856 if (win32_channel->debug)
857 g_print ("thread %#x: running, marking fd %d for later close\n",
858 win32_channel->thread_id, win32_channel->fd);
859 win32_channel->running = FALSE;
860 win32_channel->needs_close = TRUE;
861 SetEvent (win32_channel->data_avail_event);
865 if (win32_channel->debug)
866 g_print ("closing fd %d\n", win32_channel->fd);
867 close (win32_channel->fd);
868 if (win32_channel->debug)
869 g_print ("closed fd %d, setting to -1\n",
871 win32_channel->fd = -1;
873 UNLOCK (win32_channel->mutex);
877 g_io_win32_fd_create_watch (GIOChannel *channel,
878 GIOCondition condition)
880 return g_io_win32_create_watch (channel, condition, read_thread);
884 g_io_win32_sock_read (GIOChannel *channel,
889 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
893 if (win32_channel->debug)
894 g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
895 win32_channel->fd, count);
897 result = recv (win32_channel->fd, buf, count, 0);
899 if (win32_channel->debug)
900 g_print ("g_io_win32_sock_read: recv:%d\n", result);
902 if (result == SOCKET_ERROR)
905 switch (WSAGetLastError ())
908 return G_IO_ERROR_INVAL;
911 return G_IO_ERROR_AGAIN;
913 return G_IO_ERROR_UNKNOWN;
918 *bytes_read = result;
919 return G_IO_ERROR_NONE;
924 g_io_win32_sock_write (GIOChannel *channel,
927 guint *bytes_written)
929 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
932 if (win32_channel->debug)
933 g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
934 win32_channel->fd, count);
936 result = send (win32_channel->fd, buf, count, 0);
938 if (win32_channel->debug)
939 g_print ("g_io_win32_sock_write: send:%d\n", result);
941 if (result == SOCKET_ERROR)
944 switch (WSAGetLastError ())
947 return G_IO_ERROR_INVAL;
950 return G_IO_ERROR_AGAIN;
952 return G_IO_ERROR_UNKNOWN;
957 *bytes_written = result;
958 return G_IO_ERROR_NONE;
963 g_io_win32_sock_close (GIOChannel *channel)
965 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
967 if (win32_channel->debug)
968 g_print ("thread %#x: closing socket %d\n",
969 win32_channel->thread_id,
971 closesocket (win32_channel->fd);
972 win32_channel->fd = -1;
976 g_io_win32_sock_create_watch (GIOChannel *channel,
977 GIOCondition condition)
979 return g_io_win32_create_watch (channel, condition, select_thread);
982 static GIOFuncs win32_channel_msg_funcs = {
984 g_io_win32_msg_write,
986 g_io_win32_msg_close,
987 g_io_win32_msg_create_watch,
991 static GIOFuncs win32_channel_fd_funcs = {
996 g_io_win32_fd_create_watch,
1000 static GIOFuncs win32_channel_sock_funcs = {
1001 g_io_win32_sock_read,
1002 g_io_win32_sock_write,
1004 g_io_win32_sock_close,
1005 g_io_win32_sock_create_watch,
1010 g_io_channel_win32_new_messages (guint hwnd)
1012 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1013 GIOChannel *channel = (GIOChannel *)win32_channel;
1015 g_io_channel_init (channel);
1016 g_io_channel_win32_init (win32_channel);
1017 if (win32_channel->debug)
1018 g_print ("g_io_channel_win32_new_messages: hwnd = %ud\n", hwnd);
1019 channel->funcs = &win32_channel_msg_funcs;
1020 win32_channel->type = G_IO_WIN32_WINDOWS_MESSAGES;
1021 win32_channel->hwnd = (HWND) hwnd;
1027 g_io_channel_win32_new_fd (gint fd)
1029 GIOWin32Channel *win32_channel;
1030 GIOChannel *channel;
1033 if (fstat (fd, &st) == -1)
1035 g_warning (G_STRLOC ": %d isn't a (emulated) file descriptor", fd);
1039 win32_channel = g_new (GIOWin32Channel, 1);
1040 channel = (GIOChannel *)win32_channel;
1042 g_io_channel_init (channel);
1043 g_io_channel_win32_init (win32_channel);
1044 if (win32_channel->debug)
1045 g_print ("g_io_channel_win32_new_fd: fd = %d\n", fd);
1046 channel->funcs = &win32_channel_fd_funcs;
1047 win32_channel->type = G_IO_WIN32_FILE_DESC;
1048 win32_channel->fd = fd;
1054 g_io_channel_win32_get_fd (GIOChannel *channel)
1056 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1058 return win32_channel->fd;
1062 g_io_channel_win32_new_socket (int socket)
1064 GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
1065 GIOChannel *channel = (GIOChannel *)win32_channel;
1067 g_io_channel_init (channel);
1068 g_io_channel_win32_init (win32_channel);
1069 if (win32_channel->debug)
1070 g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
1071 channel->funcs = &win32_channel_sock_funcs;
1072 win32_channel->type = G_IO_WIN32_SOCKET;
1073 win32_channel->fd = socket;
1079 g_io_channel_unix_new (gint fd)
1083 if (fstat (fd, &st) == 0)
1084 return g_io_channel_win32_new_fd (fd);
1086 if (getsockopt (fd, SOL_SOCKET, SO_TYPE, NULL, NULL) != SO_ERROR)
1087 return g_io_channel_win32_new_socket(fd);
1089 g_warning (G_STRLOC ": %d is neither a file descriptor or a socket", fd);
1094 g_io_channel_unix_get_fd (GIOChannel *channel)
1096 return g_io_channel_win32_get_fd (channel);
1100 g_io_channel_win32_set_debug (GIOChannel *channel,
1103 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1105 win32_channel->debug = flag;
1109 g_io_channel_win32_poll (GPollFD *fds,
1115 g_return_val_if_fail (n_fds >= 0, 0);
1117 result = (*g_main_context_get_poll_func (NULL)) (fds, n_fds, timeout);
1123 g_io_channel_win32_make_pollfd (GIOChannel *channel,
1124 GIOCondition condition,
1127 GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
1129 if (win32_channel->data_avail_event == NULL)
1130 create_events (win32_channel);
1132 fd->fd = (gint) win32_channel->data_avail_event;
1133 fd->events = condition;
1135 if (win32_channel->thread_id == 0)
1136 if ((condition & G_IO_IN) && win32_channel->type == G_IO_WIN32_FILE_DESC)
1137 create_thread (win32_channel, condition, read_thread);
1138 else if (win32_channel->type == G_IO_WIN32_SOCKET)
1139 create_thread (win32_channel, condition, select_thread);
1142 /* Binary compatibility */
1144 g_io_channel_win32_new_stream_socket (int socket)
1146 return g_io_channel_win32_new_socket (socket);