summaryrefslogtreecommitdiff
path: root/libgaim/win32/giowin32.c
blob: e94cb4e75133d6efbb0626e2ccce5c0014f7fd85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * giowin32.c: IO Channels for Win32.
 * Copyright 1998 Owen Taylor and Tor Lillqvist
 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
 * Copyright 2001-2003 Andrew Lanoix
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
 */

/* Define this to get (very) verbose logging of all channels */
/* #define G_IO_WIN32_DEBUG */

//#include "config.h"

#include <glib.h>

#include <stdlib.h>
#include <windows.h>
#include <winsock.h>          /* Not everybody has winsock2 */
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <errno.h>
#include <sys/stat.h>

#include <glib/gstdio.h>

typedef struct _GIOWin32Channel GIOWin32Channel;
typedef struct _GIOWin32Watch GIOWin32Watch;

#define BUFFER_SIZE 4096

typedef enum {
  G_IO_WIN32_WINDOWS_MESSAGES,	/* Windows messages */
  G_IO_WIN32_FILE_DESC,		/* Unix-like file descriptors from
				 * _open() or _pipe(). Read with read().
				 * Have to create separate thread to read.
				 */
  G_IO_WIN32_SOCKET		/* Sockets. A separate thread is blocked
				 * in select() most of the time.
				 */
} GIOWin32ChannelType;

struct _GIOWin32Channel {
  GIOChannel channel;
  gint fd;			/* Either a Unix-like file handle as provided
				 * by the Microsoft C runtime, or a SOCKET
				 * as provided by WinSock.
				 */
  GIOWin32ChannelType type;
  
  gboolean debug;

  CRITICAL_SECTION mutex;

  /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
  HWND hwnd;			/* handle of window, or NULL */
  
  /* Following fields are used by both fd and socket channels. */
  gboolean running;		/* Is reader thread running. FALSE if
				 * EOF has been reached.
				 */
  gboolean needs_close;		/* If the channel has been closed while
				 * the reader thread was still running.
				 */
  guint thread_id;		/* If non-NULL has a reader thread, or has
				 * had.*/
  HANDLE data_avail_event;

  gushort revents;

  /* Following fields used by fd channels for input */
  
  /* Data is kept in a circular buffer. To be able to distinguish between
   * empty and full buffer, we cannot fill it completely, but have to
   * leave a one character gap.
   *
   * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE).
   *
   * Empty:    wrp == rdp
   * Full:     (wrp + 1) % BUFFER_SIZE == rdp
   * Partial:  otherwise
   */
  guchar *buffer;		/* (Circular) buffer */
  gint wrp, rdp;		/* Buffer indices for writing and reading */
  HANDLE space_avail_event;

  /* Following fields used by socket channels */
  GSList *watches;
  HANDLE data_avail_noticed_event;
  gint reset_send; /* socket used to send data so select_thread() can reset/re-loop */
  gint reset_recv; /* socket used to recv data so select_thread() can reset/re-loop */
};

#define LOCK(mutex) EnterCriticalSection (&mutex)
#define UNLOCK(mutex) LeaveCriticalSection (&mutex)

struct _GIOWin32Watch {
  GSource       source;
  GPollFD       pollfd;
  GIOChannel   *channel;
  GIOCondition  condition;
};

static void
g_win32_print_gioflags (GIOFlags flags)
{
  char *bar = "";

  if (flags & G_IO_FLAG_APPEND)
    bar = "|", g_print ("APPEND");
  if (flags & G_IO_FLAG_NONBLOCK)
    g_print ("%sNONBLOCK", bar), bar = "|";
  if (flags & G_IO_FLAG_IS_READABLE)
    g_print ("%sREADABLE", bar), bar = "|";
  if (flags & G_IO_FLAG_IS_WRITEABLE)
    g_print ("%sWRITEABLE", bar), bar = "|";
  if (flags & G_IO_FLAG_IS_SEEKABLE)
    g_print ("%sSEEKABLE", bar), bar = "|";
}

static gboolean
g_io_win32_get_debug_flag (void)
{
#ifdef G_IO_WIN32_DEBUG
  return TRUE;
#else
  if (getenv ("G_IO_WIN32_DEBUG") != NULL)
    return TRUE;
  else
    return FALSE;
#endif
}  

static void
g_io_channel_win32_init (GIOWin32Channel *channel)
{
  channel->debug = g_io_win32_get_debug_flag ();
  channel->buffer = NULL;
  channel->running = FALSE;
  channel->needs_close = FALSE;
  channel->thread_id = 0;
  channel->data_avail_event = NULL;
  channel->revents = 0;
  channel->space_avail_event = NULL;
  channel->reset_send = INVALID_SOCKET;
  channel->reset_recv = INVALID_SOCKET;
  channel->data_avail_noticed_event = NULL;
  channel->watches = NULL;
  InitializeCriticalSection (&channel->mutex);
}

static void
create_events (GIOWin32Channel *channel)
{
  SECURITY_ATTRIBUTES sec_attrs;
  
  sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES);
  sec_attrs.lpSecurityDescriptor = NULL;
  sec_attrs.bInheritHandle = FALSE;

  /* The data available event is manual reset, the space available event
   * is automatic reset.
   */
  if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL))
      || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
      || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
    {
      gchar *emsg = g_win32_error_message (GetLastError ());
      g_error ("Error creating event: %s", emsg);
      g_free (emsg);
    }
}

static void
create_thread (GIOWin32Channel     *channel,
	       GIOCondition         condition,
	       unsigned (__stdcall *thread) (void *parameter))
{
  HANDLE thread_handle;

  thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
					   &channel->thread_id);
  if (thread_handle == 0)
    g_warning (G_STRLOC ": Error creating reader thread: %s",
	       g_strerror (errno));
  else if (!CloseHandle (thread_handle))
    g_warning (G_STRLOC ": Error closing thread handle: %s\n",
	       g_win32_error_message (GetLastError ()));

  WaitForSingleObject (channel->space_avail_event, INFINITE);
}

static void
init_reset_sockets (GIOWin32Channel *channel)
{
  struct sockaddr_in local, local2, server;
  int len;

  channel->reset_send = (gint) socket (AF_INET, SOCK_DGRAM, 0);
  if (channel->reset_send == INVALID_SOCKET)
    {
      g_warning (G_STRLOC ": Error creating reset_send socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
    }

  local.sin_family = AF_INET;
  local.sin_port = 0;
  local.sin_addr.s_addr = htonl (INADDR_LOOPBACK);

  if (bind (channel->reset_send, (struct sockaddr *)&local, sizeof (local)) == SOCKET_ERROR)
    {
      g_warning (G_STRLOC ": Error binding to reset_send socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
  }

  local2.sin_family = AF_INET;
  local2.sin_port = 0;
  local2.sin_addr.s_addr = htonl (INADDR_LOOPBACK);

  channel->reset_recv = (gint) socket (AF_INET, SOCK_DGRAM, 0);
  if (channel->reset_recv == INVALID_SOCKET)
    {
      g_warning (G_STRLOC ": Error creating reset_recv socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
  }

  if (bind (channel->reset_recv, (struct sockaddr *)&local2, sizeof (local)) == SOCKET_ERROR)
    {
      g_warning (G_STRLOC ": Error binding to reset_recv socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
    }
  
  len = sizeof (local2);
  if (getsockname (channel->reset_recv, (struct sockaddr *)&local2, &len) == SOCKET_ERROR)
    {
      g_warning (G_STRLOC ": Error getsockname with reset_recv socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
    }

  memset (&server, 0, sizeof (server));
  server.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  server.sin_family = AF_INET;
  server.sin_port = local2.sin_port;

  if (connect (channel->reset_send, (struct sockaddr  *)&server, sizeof (server)) == SOCKET_ERROR)
    {
      g_warning (G_STRLOC ": connect to reset_recv socket: %s\n",
		 g_win32_error_message (WSAGetLastError ()));
  }

}

static unsigned __stdcall
select_thread (void *parameter)
{
  GIOWin32Channel *channel = parameter;
  fd_set read_fds, write_fds, except_fds;
  GSList *tmp;
  int n;
  char buffer[8];

  g_io_channel_ref ((GIOChannel *)channel);

  if (channel->debug)
    g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n",
	     channel->thread_id,
	     channel->fd,
	     (guint) channel->data_avail_event,
	     (guint) channel->data_avail_noticed_event);
  
  channel->rdp = channel->wrp = 0;
  channel->running = TRUE;

  SetEvent (channel->space_avail_event);
  
  while (channel->running)
    {
      FD_ZERO (&read_fds);
      FD_ZERO (&write_fds);
      FD_ZERO (&except_fds);
      FD_SET (channel->reset_recv, &read_fds);

      LOCK (channel->mutex);
      tmp = channel->watches;
      while (tmp)
	{
	  GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data;

	  if (watch->condition & (G_IO_IN | G_IO_HUP))
	    FD_SET (channel->fd, &read_fds);
	  if (watch->condition & G_IO_OUT)
	    FD_SET (channel->fd, &write_fds);
	  if (watch->condition & G_IO_ERR)
	    FD_SET (channel->fd, &except_fds);
	  
	  tmp = tmp->next;
	}
      UNLOCK (channel->mutex);

      if (channel->debug)
	g_print ("select_thread %#x: calling select() for%s%s%s\n",
		 channel->thread_id,
		 (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
		 (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
		 (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
      
      n = select (1, &read_fds, &write_fds, &except_fds, NULL);
      
      LOCK (channel->mutex);
      if (channel->needs_close)
	{
	  UNLOCK (channel->mutex);
	  break;
	}
      UNLOCK (channel->mutex);

      if (n == SOCKET_ERROR)
	{
	  if (channel->debug)
	    g_print ("select_thread %#x: select returned SOCKET_ERROR\n",
		     channel->thread_id);
	  break;
	}

    if (FD_ISSET (channel->reset_recv, &read_fds))
    {
      if (channel->debug)
        g_print ("select_thread %#x: re-looping\n",
            channel->thread_id);
      recv (channel->reset_recv,  (char *)&buffer, (int) sizeof (buffer), 0);
      continue;
    }

    if (channel->debug)
      g_print ("select_thread %#x: got%s%s%s\n",
	       channel->thread_id,
	       (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""),
	       (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""),
	       (FD_ISSET (channel->fd, &except_fds) ? " ERR" : ""));
    
    if (FD_ISSET (channel->fd, &read_fds))
      channel->revents |= G_IO_IN;
    if (FD_ISSET (channel->fd, &write_fds))
      channel->revents |= G_IO_OUT;
    if (FD_ISSET (channel->fd, &except_fds))
      channel->revents |= G_IO_ERR;

    if (channel->debug)
      g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n",
	       channel->thread_id);

    LOCK (channel->mutex);
    ResetEvent (channel->data_avail_noticed_event);
    SetEvent (channel->data_avail_event);
    if (channel->needs_close)
      {
	UNLOCK (channel->mutex);
	break;
      }
    UNLOCK (channel->mutex);

    if (channel->debug)
      g_print ("select_thread %#x: waiting for data_avail_noticed\n",
        channel->thread_id);

    WaitForSingleObject (channel->data_avail_noticed_event, INFINITE);
    if (channel->debug)
      g_print ("select_thread %#x: got data_avail_noticed\n",
		 channel->thread_id);
    }

  LOCK (channel->mutex);
  channel->running = FALSE;
  if (channel->debug)
    g_print ("select_thread %#x: got error, setting data_avail\n",
	     channel->thread_id);
  SetEvent (channel->data_avail_event);
  g_io_channel_unref ((GIOChannel *)channel);
  UNLOCK (channel->mutex);

  /* No need to call _endthreadex(), the actual thread starter routine
   * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
   * _endthreadex() for us.
   */

  return 0;
}

static gboolean
g_io_win32_prepare (GSource *source,
		    gint    *timeout)
{
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
  
  *timeout = -1;
  
  if (channel->debug)
    g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n"
	     "  watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
	     channel->thread_id, buffer_condition,
	     watch->pollfd.events, watch->pollfd.revents, channel->revents);

  if (channel->type == G_IO_WIN32_FILE_DESC)
    {
      LOCK (channel->mutex);
      if (channel->running && channel->wrp == channel->rdp)
	{
	  if (channel->debug)
	    g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n",
		     channel->thread_id);
	  channel->revents = 0;
	}
      UNLOCK (channel->mutex);
    }
  else if (channel->type == G_IO_WIN32_SOCKET)
    {
      LOCK (channel->mutex);
      channel->revents = 0;
      if (channel->debug)
	g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n",
		 channel->thread_id);
      SetEvent (channel->data_avail_noticed_event);
      if (channel->debug)
	g_print ("g_io_win32_prepare: thread %#x, there.\n",
		 channel->thread_id);
      UNLOCK (channel->mutex);
    }

  return ((watch->condition & buffer_condition) == watch->condition);
}

static gboolean
g_io_win32_check (GSource *source)
{
  MSG msg;
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);

  if (channel->debug)
    g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n"
	     "  watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n",
	     channel->thread_id, buffer_condition,
	     watch->pollfd.events, watch->pollfd.revents, channel->revents);

  if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
    {
      watch->pollfd.revents = (watch->pollfd.events & channel->revents);
    }
  else
    {
      return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
    }
  
  if (channel->type == G_IO_WIN32_SOCKET)
    {
      LOCK (channel->mutex);
      if (channel->debug)
	g_print ("g_io_win32_check: thread %#x, resetting data_avail\n",
		 channel->thread_id);
      ResetEvent (channel->data_avail_event);
      if (channel->debug)
	g_print ("g_io_win32_check: thread %#x, there.\n",
		 channel->thread_id);
      UNLOCK (channel->mutex);
    }

  return ((watch->pollfd.revents | buffer_condition) & watch->condition);
}

static gboolean
g_io_win32_dispatch (GSource     *source,
		     GSourceFunc  callback,
		     gpointer     user_data)
{
  GIOFunc func = (GIOFunc)callback;
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
  
  if (!func)
    {
      g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
		 "You must call g_source_connect().");
      return FALSE;
    }
  
  return (*func) (watch->channel,
		  (watch->pollfd.revents | buffer_condition) & watch->condition,
		  user_data);
}

static void
g_io_win32_finalize (GSource *source)
{
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
  char send_buffer[] = "f";
  
  LOCK (channel->mutex);
  if (channel->debug)
    g_print ("g_io_win32_finalize: channel with thread %#x\n",
	     channel->thread_id);

  channel->watches = g_slist_remove (channel->watches, watch);

  SetEvent (channel->data_avail_noticed_event);
  if (channel->type == G_IO_WIN32_SOCKET)
  {
    /* Tell select_thread() to exit */
    channel->needs_close = 1;
    /* Wake up select_thread() from its blocking select() */
    send (channel->reset_send, send_buffer, sizeof (send_buffer), 0);
  }

  UNLOCK (channel->mutex);
  g_io_channel_unref (watch->channel);
}

GSourceFuncs g_io_watch_funcs = {
  g_io_win32_prepare,
  g_io_win32_check,
  g_io_win32_dispatch,
  g_io_win32_finalize,
  NULL, NULL
};

static GSource *
g_io_win32_create_watch (GIOChannel    *channel,
			 GIOCondition   condition,
			 unsigned (__stdcall *thread) (void *parameter))
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
  GIOWin32Watch *watch;
  GSource *source;
  char send_buffer[] = "c";

  source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
  watch = (GIOWin32Watch *)source;
  
  watch->channel = channel;
  g_io_channel_ref (channel);
  
  watch->condition = condition;
  
  if (win32_channel->data_avail_event == NULL)
    create_events (win32_channel);

  watch->pollfd.fd = (gint) win32_channel->data_avail_event;
  watch->pollfd.events = condition;
  
  if (win32_channel->debug)
    g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
	     win32_channel->fd, condition, watch->pollfd.fd);

  LOCK (win32_channel->mutex);
  win32_channel->watches = g_slist_append (win32_channel->watches, watch);

  if (win32_channel->thread_id == 0)
    create_thread (win32_channel, condition, thread);
  else
    send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);

  g_source_add_poll (source, &watch->pollfd);
  UNLOCK (win32_channel->mutex);

  return source;
}

static void
g_io_win32_free (GIOChannel *channel)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
  
  if (win32_channel->debug)
    g_print ("thread %#x: freeing channel, fd: %d\n",
	     win32_channel->thread_id,
	     win32_channel->fd);

  if (win32_channel->reset_send && win32_channel->reset_send != INVALID_SOCKET)
    closesocket (win32_channel->reset_send);
  if (win32_channel->reset_recv && win32_channel->reset_recv != INVALID_SOCKET)
    closesocket (win32_channel->reset_recv);
  if (win32_channel->data_avail_event)
    CloseHandle (win32_channel->data_avail_event);
  if (win32_channel->space_avail_event)
    CloseHandle (win32_channel->space_avail_event);
  if (win32_channel->data_avail_noticed_event)
    CloseHandle (win32_channel->data_avail_noticed_event);
  DeleteCriticalSection (&win32_channel->mutex);

  g_free (win32_channel->buffer);
  g_slist_free (win32_channel->watches);
  g_free (win32_channel);
}

static GIOStatus
g_io_win32_sock_read (GIOChannel *channel,
		      gchar      *buf,
		      gsize       count,
		      gsize      *bytes_read,
		      GError    **err)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
  gint result;
  GIOChannelError error = G_IO_STATUS_NORMAL;
  GIOStatus internal_status = G_IO_STATUS_NORMAL;
  char send_buffer[] = "sr";

  if (win32_channel->debug)
    g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
	     win32_channel->fd, count);
#ifdef WE_NEED_TO_HANDLE_WSAEINTR
repeat:
#endif
  result = recv (win32_channel->fd, buf, count, 0);

  if (win32_channel->debug)
    g_print ("g_io_win32_sock_read: recv:%d\n", result);
  
  if (result == SOCKET_ERROR)
    {
      *bytes_read = 0;

      switch (WSAGetLastError ())
	{
	case WSAEINVAL:
          error = G_IO_CHANNEL_ERROR_INVAL;
          break;
	case WSAEWOULDBLOCK:
          return G_IO_STATUS_AGAIN;
#ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
	case WSAEINTR:
          goto repeat;
#endif
	default:
	  error = G_IO_CHANNEL_ERROR_FAILED;
          break;
	}
      g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket read error");
      internal_status = G_IO_STATUS_ERROR;
      /* FIXME get all errors, better error messages */
    }
  else
    {
      *bytes_read = result;
      if (result == 0)
	internal_status = G_IO_STATUS_EOF;
    }

  if ((internal_status == G_IO_STATUS_EOF) || 
      (internal_status == G_IO_STATUS_ERROR))
    {
      LOCK (win32_channel->mutex);
      SetEvent (win32_channel->data_avail_noticed_event);
      win32_channel->needs_close = 1;
      send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
      UNLOCK (win32_channel->mutex);
    }
  return internal_status;
}

static GIOStatus
g_io_win32_sock_write (GIOChannel  *channel,
		       const gchar *buf,
		       gsize        count,
		       gsize       *bytes_written,
		       GError     **err)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
  gint result;
  GIOChannelError error = G_IO_STATUS_NORMAL;
  char send_buffer[] = "sw";
  
  if (win32_channel->debug)
    g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n",
	     win32_channel->fd, count);
#ifdef WE_NEED_TO_HANDLE_WSAEINTR
repeat:
#endif
  result = send (win32_channel->fd, buf, count, 0);
  
  if (win32_channel->debug)
    g_print ("g_io_win32_sock_write: send:%d\n", result);
  
  if (result == SOCKET_ERROR)
    {
      *bytes_written = 0;

      switch (WSAGetLastError ())
	{
	case WSAEINVAL:
	  error = G_IO_CHANNEL_ERROR_INVAL;
          break;
	case WSAEWOULDBLOCK:
          return G_IO_STATUS_AGAIN;
#ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
	case WSAEINTR:
          goto repeat;
#endif
	default:
	  error = G_IO_CHANNEL_ERROR_FAILED;
          break;
	}
      g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket write error");
      LOCK (win32_channel->mutex);
      SetEvent (win32_channel->data_avail_noticed_event);
      win32_channel->needs_close = 1;
      send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0);
      UNLOCK (win32_channel->mutex);
      return G_IO_STATUS_ERROR;
      /* FIXME get all errors, better error messages */
    }
  else
    {
      *bytes_written = result;

      return G_IO_STATUS_NORMAL;
    }
}

static GIOStatus
g_io_win32_sock_close (GIOChannel *channel,
		       GError    **err)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;

  LOCK (win32_channel->mutex);
  if (win32_channel->running)
    {
      if (win32_channel->debug)
	g_print ("thread %#x: running, marking for later close\n",
		 win32_channel->thread_id);
      win32_channel->running = FALSE;
      win32_channel->needs_close = TRUE;
      SetEvent(win32_channel->data_avail_noticed_event);
    }
  if (win32_channel->fd != -1)
    {
      if (win32_channel->debug)
	g_print ("thread %#x: closing socket %d\n",
		 win32_channel->thread_id,
		 win32_channel->fd);
      
      closesocket (win32_channel->fd);
      win32_channel->fd = -1;
    }
  UNLOCK (win32_channel->mutex);

  /* FIXME error detection? */

  return G_IO_STATUS_NORMAL;
}

static GSource *
g_io_win32_sock_create_watch (GIOChannel    *channel,
			      GIOCondition   condition)
{
  return g_io_win32_create_watch (channel, condition, select_thread);
}

static GIOStatus
g_io_win32_set_flags (GIOChannel *channel,
                      GIOFlags    flags,
                      GError    **err)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;

  if (win32_channel->debug)
    {
      g_print ("g_io_win32_set_flags: ");
      g_win32_print_gioflags (flags);
      g_print ("\n");
    }

  g_warning ("g_io_win32_set_flags () not implemented.\n");

  return G_IO_STATUS_NORMAL;
}

static GIOFlags
g_io_win32_sock_get_flags (GIOChannel *channel)
{
  /* XXX Could do something here. */
  return 0;
}

static GIOFuncs win32_channel_sock_funcs = {
  g_io_win32_sock_read,
  g_io_win32_sock_write,
  NULL,
  g_io_win32_sock_close,
  g_io_win32_sock_create_watch,
  g_io_win32_free,
  g_io_win32_set_flags,
  g_io_win32_sock_get_flags,
};

GIOChannel *
wgaim_g_io_channel_win32_new_socket (int socket)
{
  GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1);
  GIOChannel *channel = (GIOChannel *)win32_channel;

  g_io_channel_init (channel);
  g_io_channel_win32_init (win32_channel);
  init_reset_sockets (win32_channel);
  if (win32_channel->debug)
    g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket);
  channel->funcs = &win32_channel_sock_funcs;
  win32_channel->type = G_IO_WIN32_SOCKET;
  win32_channel->fd = socket;

  /* XXX: check this */
  channel->is_readable = TRUE;
  channel->is_writeable = TRUE;

  channel->is_seekable = FALSE;

  return channel;
}

#if 0
void
g_io_channel_win32_set_debug (GIOChannel *channel,
			      gboolean    flag)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;

  win32_channel->debug = flag;
}
#endif