giowin32.c 37.4 KB
Newer Older
1 2 3 4 5
/* 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
6
 * Copyright 1999-2000 Tor Lillqvist and Craig Setera
7
 * Copyright 2001 Andrew Lanoix
8 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 12 13 14 15
 * 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
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

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

35
#include "glib.h"
36 37

#include <stdlib.h>
38
#include <windows.h>
39
#include <winsock.h>          /* Not everybody has winsock2 */
40 41
#include <fcntl.h>
#include <io.h>
42
#include <process.h>
43
#include <errno.h>
44
#include <sys/stat.h>
45

46 47
#include "glibintl.h"

48 49 50
typedef struct _GIOWin32Channel GIOWin32Channel;
typedef struct _GIOWin32Watch GIOWin32Watch;

51
#define BUFFER_SIZE 4096
52 53

typedef enum {
54 55
  G_IO_WIN32_WINDOWS_MESSAGES,	/* Windows messages */
  G_IO_WIN32_FILE_DESC,		/* Unix-like file descriptors from
56 57 58
				 * _open() or _pipe(). Read with read().
				 * Have to create separate thread to read.
				 */
59 60
  G_IO_WIN32_SOCKET		/* Sockets. A separate thread is blocked
				 * in select() most of the time.
61
				 */
62 63 64 65 66 67 68 69 70
} 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;
71
  
72 73
  gboolean debug;

74 75 76
  CRITICAL_SECTION mutex;

  /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */
77
  HWND hwnd;			/* handle of window, or NULL */
78
  
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  /* 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 thread_handle;
  HANDLE data_avail_event;

  gushort revents;

  /* Following fields used by fd channels for input */
94 95 96 97 98 99 100 101 102 103 104 105 106 107
  
  /* 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;
108 109 110 111

  /* Following fields used by socket channels */
  GSList *watches;
  HANDLE data_avail_noticed_event;
112 113
};

114 115 116
#define LOCK(mutex) EnterCriticalSection (&mutex)
#define UNLOCK(mutex) LeaveCriticalSection (&mutex)

117
struct _GIOWin32Watch {
118
  GSource       source;
119 120 121 122 123 124
  GPollFD       pollfd;
  GIOChannel   *channel;
  GIOCondition  condition;
  GIOFunc       callback;
};

125 126 127 128 129 130 131 132 133 134 135 136 137
static void
g_io_channel_win32_init (GIOWin32Channel *channel)
{
#ifdef G_IO_WIN32_DEBUG
  channel->debug = TRUE;
#else
  if (getenv ("G_IO_WIN32_DEBUG") != NULL)
    channel->debug = TRUE;
  else
    channel->debug = FALSE;
#endif
  channel->buffer = NULL;
  channel->running = FALSE;
138
  channel->needs_close = FALSE;
139
  channel->thread_id = 0;
140
  channel->data_avail_event = NULL;
141
  channel->revents = 0;
142
  channel->space_avail_event = NULL;
143 144
  channel->data_avail_noticed_event = NULL;
  channel->watches = NULL;
145
  InitializeCriticalSection (&channel->mutex);
146 147
}

148 149 150 151 152 153 154 155
static void
create_events (GIOWin32Channel *channel)
{
  SECURITY_ATTRIBUTES sec_attrs;
  
  sec_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
  sec_attrs.lpSecurityDescriptor = NULL;
  sec_attrs.bInheritHandle = FALSE;
156

157 158 159 160
  /* 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))
161 162
      || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))
      || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)))
163 164 165 166 167
    {
      gchar *msg = g_win32_error_message (GetLastError ());
      g_error ("Error creating event: %s", msg);
    }
}
168

169
static unsigned __stdcall
170
read_thread (void *parameter)
171 172 173 174
{
  GIOWin32Channel *channel = parameter;
  guchar *buffer;
  guint nbytes;
175

176
  g_io_channel_ref ((GIOChannel *)channel);
177

178
  if (channel->debug)
179
    g_print ("read_thread %#x: start fd:%d, data_avail:%#x, space_avail:%#x\n",
180 181 182 183
	     channel->thread_id,
	     channel->fd,
	     (guint) channel->data_avail_event,
	     (guint) channel->space_avail_event);
184 185 186 187
  
  channel->buffer = g_malloc (BUFFER_SIZE);
  channel->rdp = channel->wrp = 0;
  channel->running = TRUE;
188

189 190 191 192 193
  SetEvent (channel->space_avail_event);
  
  while (channel->running)
    {
      LOCK (channel->mutex);
194
      if (channel->debug)
195
	g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
196
		 channel->thread_id, channel->rdp, channel->wrp);
197 198 199
      if ((channel->wrp + 1) % BUFFER_SIZE == channel->rdp)
	{
	  /* Buffer is full */
200
	  if (channel->debug)
201
	    g_print ("read_thread %#x: resetting space_avail\n",
202
		     channel->thread_id);
203
	  ResetEvent (channel->space_avail_event);
204
	  if (channel->debug)
205 206
	    g_print ("read_thread %#x: waiting for space\n",
		     channel->thread_id);
207 208 209
	  UNLOCK (channel->mutex);
	  WaitForSingleObject (channel->space_avail_event, INFINITE);
	  LOCK (channel->mutex);
210
	  if (channel->debug)
211
	    g_print ("read_thread %#x: rdp=%d, wrp=%d\n",
212
		     channel->thread_id, channel->rdp, channel->wrp);
213 214 215 216 217 218 219 220 221
	}
      
      buffer = channel->buffer + channel->wrp;
      
      /* Always leave at least one byte unused gap to be able to
       * distinguish between the full and empty condition...
       */
      nbytes = MIN ((channel->rdp + BUFFER_SIZE - channel->wrp - 1) % BUFFER_SIZE,
		    BUFFER_SIZE - channel->wrp);
222

223
      if (channel->debug)
224
	g_print ("read_thread %#x: calling read() for %d bytes\n",
225 226
		 channel->thread_id, nbytes);

227
      UNLOCK (channel->mutex);
228

229
      nbytes = read (channel->fd, buffer, nbytes);
230 231
      
      LOCK (channel->mutex);
232

233 234 235 236 237 238
      channel->revents = G_IO_IN;
      if (nbytes == 0)
	channel->revents |= G_IO_HUP;
      else if (nbytes < 0)
	channel->revents |= G_IO_ERR;

239
      if (channel->debug)
240
	g_print ("read_thread %#x: read() returned %d, rdp=%d, wrp=%d\n",
241
		 channel->thread_id, nbytes, channel->rdp, channel->wrp);
242 243 244 245

      if (nbytes <= 0)
	break;

246
      channel->wrp = (channel->wrp + nbytes) % BUFFER_SIZE;
247
      if (channel->debug)
248
	g_print ("read_thread %#x: rdp=%d, wrp=%d, setting data_avail\n",
249
		 channel->thread_id, channel->rdp, channel->wrp);
250 251 252 253 254
      SetEvent (channel->data_avail_event);
      UNLOCK (channel->mutex);
    }
  
  channel->running = FALSE;
255 256 257
  if (channel->needs_close)
    {
      if (channel->debug)
258
	g_print ("read_thread %#x: channel fd %d needs closing\n",
259
		 channel->thread_id, channel->fd);
260
      close (channel->fd);
261 262 263
      channel->fd = -1;
    }

264 265 266
  if (channel->debug)
    g_print ("read_thread %#x: EOF, rdp=%d, wrp=%d, setting data_avail\n",
	     channel->thread_id, channel->rdp, channel->wrp);
267 268 269
  SetEvent (channel->data_avail_event);
  UNLOCK (channel->mutex);
  
270
  g_io_channel_unref((GIOChannel *)channel);
271
  
272 273 274
  /* No need to call _endthreadex(), the actual thread starter routine
   * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
   * _endthreadex() for us.
275
   */
276 277 278

  CloseHandle (channel->thread_handle);

279 280
  return 0;
}
281

282
static void
283 284 285
create_thread (GIOWin32Channel     *channel,
	       GIOCondition         condition,
	       unsigned (__stdcall *thread) (void *parameter))
286
{
287 288 289 290 291 292
  channel->thread_handle =
    (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0,
			     &channel->thread_id);
  if (channel->thread_handle == 0)
    g_warning (G_STRLOC ": Error creating reader thread: %s",
	       strerror (errno));
293 294
  WaitForSingleObject (channel->space_avail_event, INFINITE);
}
295

296
static GIOStatus
297 298
buffer_read (GIOWin32Channel *channel,
	     guchar          *dest,
299 300 301
	     gsize            count,
	     gsize           *bytes_read,
	     GError         **err)
302
{
303 304 305 306
  guint nbytes;
  guint left = count;
  
  LOCK (channel->mutex);
307 308 309
  if (channel->debug)
    g_print ("reading from thread %#x %d bytes, rdp=%d, wrp=%d\n",
	     channel->thread_id, count, channel->rdp, channel->wrp);
310
  
311
  if (channel->wrp == channel->rdp)
312 313
    {
      UNLOCK (channel->mutex);
314 315 316
      if (channel->debug)
	g_print ("waiting for data from thread %#x\n", channel->thread_id);
      WaitForSingleObject (channel->data_avail_event, INFINITE);
317 318
      if (channel->debug)
	g_print ("done waiting for data from thread %#x\n", channel->thread_id);
319
      LOCK (channel->mutex);
320
      if (channel->wrp == channel->rdp && !channel->running)
321 322
	{
	  UNLOCK (channel->mutex);
323
          *bytes_read = 0;
324
	  return G_IO_STATUS_NORMAL; /* as before, normal case ? */
325
	}
326
    }
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  
  if (channel->rdp < channel->wrp)
    nbytes = channel->wrp - channel->rdp;
  else
    nbytes = BUFFER_SIZE - channel->rdp;
  UNLOCK (channel->mutex);
  nbytes = MIN (left, nbytes);
  if (channel->debug)
    g_print ("moving %d bytes from thread %#x\n",
	     nbytes, channel->thread_id);
  memcpy (dest, channel->buffer + channel->rdp, nbytes);
  dest += nbytes;
  left -= nbytes;
  LOCK (channel->mutex);
  channel->rdp = (channel->rdp + nbytes) % BUFFER_SIZE;
  if (channel->debug)
343
    g_print ("setting space_avail for thread %#x\n", channel->thread_id);
344 345 346 347
  SetEvent (channel->space_avail_event);
  if (channel->debug)
    g_print ("for thread %#x: rdp=%d, wrp=%d\n",
	     channel->thread_id, channel->rdp, channel->wrp);
348
  if (channel->running && channel->wrp == channel->rdp)
349 350
    {
      if (channel->debug)
351
	g_print ("resetting data_avail of thread %#x\n",
352 353 354
		 channel->thread_id);
      ResetEvent (channel->data_avail_event);
    };
355 356
  UNLOCK (channel->mutex);
  
357 358 359
  /* We have no way to indicate any errors form the actual
   * read() or recv() call in the reader thread. Should we have?
   */
360 361
  *bytes_read = count - left;
  return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
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
static unsigned __stdcall
select_thread (void *parameter)
{
  GIOWin32Channel *channel = parameter;
  fd_set read_fds, write_fds, except_fds;
  GSList *tmp;
  int n;

  g_io_channel_ref ((GIOChannel *)channel);

  if (channel->debug)
    g_print ("select_thread %#x: start fd:%d,\n\tdata_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);

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

      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,\n"
		 "\tsetting data_avail\n",
		 channel->thread_id);
      ResetEvent (channel->data_avail_noticed_event);
      SetEvent (channel->data_avail_event);

444 445 446 447 448 449 450 451
      LOCK (channel->mutex);
      if (channel->needs_close)
	{
	  UNLOCK (channel->mutex);
	  break;
	}
      UNLOCK (channel->mutex);

452 453 454 455 456 457 458 459 460 461 462 463
      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);
    }
  
  channel->running = FALSE;
  LOCK (channel->mutex);
464
  if (channel->fd != -1)
465
    {
466
      /* DO NOT close the fd here */
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
      channel->fd = -1;
    }

  if (channel->debug)
    g_print ("select_thread %#x: got error, setting data_avail\n",
	     channel->thread_id);
  SetEvent (channel->data_avail_event);
  UNLOCK (channel->mutex);
  
  g_io_channel_unref((GIOChannel *)channel);
  
  /* No need to call _endthreadex(), the actual thread starter routine
   * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls
   * _endthreadex() for us.
   */

  CloseHandle (channel->thread_handle);

  return 0;
}

488
static gboolean
489 490
g_io_win32_prepare (GSource *source,
		    gint    *timeout)
491
{
492 493 494
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
  
495
  *timeout = -1;
496
  
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  if (channel->type == G_IO_WIN32_FILE_DESC)
    {
      LOCK (channel->mutex);
      if (channel->running && channel->wrp == channel->rdp)
	channel->revents = 0;
      UNLOCK (channel->mutex);
    }
  else if (channel->type == G_IO_WIN32_SOCKET)
    {
      channel->revents = 0;

      if (channel->debug)
	g_print ("g_io_win32_prepare: 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);
    }

517 518
  return FALSE;
  /* XXX: why should we want to do this ? */
519 520 521
  watch->condition = g_io_channel_get_buffer_condition (watch->channel);

  return (watch->pollfd.revents & (G_IO_IN | G_IO_OUT)) == watch->condition;
522 523
}

524
static gboolean
525
g_io_win32_check (GSource *source)
526
{
527
	MSG msg;
528
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
529
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
530 531
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);

532
  
533 534 535 536 537 538 539
  if (channel->debug)
    g_print ("g_io_win32_check: for thread %#x:\n"
	     "\twatch->pollfd.events:%#x, watch->pollfd.revents:%#x, channel->revents:%#x\n",
	     channel->thread_id,
	     watch->pollfd.events, watch->pollfd.revents, channel->revents);

  if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES)
540 541 542 543 544 545 546
	{
     watch->pollfd.revents = (watch->pollfd.events & channel->revents);
	}
	else
	{
    return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE));
	}
547 548

  if (channel->type == G_IO_WIN32_SOCKET)
549
    {
550
      if (channel->debug)
551 552 553 554 555 556
	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);
557
    }
558

559
  return (watch->pollfd.revents & watch->condition);
560 561 562
}

static gboolean
563 564 565
g_io_win32_dispatch (GSource     *source,
		     GSourceFunc  callback,
		     gpointer     user_data)
566
{
567
  GIOFunc func = (GIOFunc)callback;
568
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
569
  
570 571
  if (!func)
    {
572
      g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n"
573 574 575 576 577 578 579
		 "You must call g_source_connect().");
      return FALSE;
    }
  
  return (*func) (watch->channel,
		  watch->pollfd.revents & watch->condition,
		  user_data);
580 581 582
}

static void
583
g_io_win32_finalize (GSource *source)
584
{
585
  GIOWin32Watch *watch = (GIOWin32Watch *)source;
586
  GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel;
587
  
588
  if (channel->debug)
589
    g_print ("g_io_win32_finalize: channel with thread %#x\n",
590 591 592 593
	     channel->thread_id);

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

594
  SetEvent (channel->data_avail_noticed_event);
595
  g_io_channel_unref (watch->channel);
596 597
}

598
GSourceFuncs g_io_watch_funcs = {
599 600 601
  g_io_win32_prepare,
  g_io_win32_check,
  g_io_win32_dispatch,
602
  g_io_win32_finalize
603
};
604

605 606 607
static GSource *
g_io_win32_create_watch (GIOChannel    *channel,
			 GIOCondition   condition,
608
			 unsigned (__stdcall *thread) (void *parameter))
609
{
610
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
611 612 613
  GIOWin32Watch *watch;
  GSource *source;

614
  source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
615
  watch = (GIOWin32Watch *)source;
616 617 618 619 620 621 622 623 624 625 626 627 628
  
  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)
629 630
    g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n",
	     win32_channel->fd, condition, watch->pollfd.fd);
631
  
632 633
  win32_channel->watches = g_slist_append (win32_channel->watches, watch);

634
  if (win32_channel->thread_id == 0)
635
    create_thread (win32_channel, condition, thread);
636

637
  g_source_add_poll (source, &watch->pollfd);
638
  
639
  return source;
640 641
}

642
static GIOStatus
643 644
g_io_win32_msg_read (GIOChannel *channel,
		     gchar      *buf,
645 646 647
		     gsize       count,
		     gsize      *bytes_read,
		     GError    **err)
648
{
649
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
650 651
  MSG msg;               /* In case of alignment problems */
  
652
  if (count < sizeof (MSG))
653 654 655 656 657
    {
      g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
        _("Incorrect message size")); /* Correct error message? FIXME */
      return G_IO_STATUS_ERROR;
    }
658
  
659 660 661
  if (win32_channel->debug)
    g_print ("g_io_win32_msg_read: for %#x\n",
	     win32_channel->hwnd);
662
  if (!PeekMessage (&msg, win32_channel->hwnd, 0, 0, PM_REMOVE))
663
    return G_IO_STATUS_AGAIN;
664

665 666
  memmove (buf, &msg, sizeof (MSG));
  *bytes_read = sizeof (MSG);
667 668

  return (*bytes_read > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
669
}
670

671 672 673 674 675 676
static GIOStatus
g_io_win32_msg_write (GIOChannel  *channel,
		      const gchar *buf,
		      gsize        count,
		      gsize       *bytes_written,
		      GError     **err)
677
{
678
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
679
  MSG msg;
680
  
681
  if (count != sizeof (MSG))
682 683 684 685 686
    {
      g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_INVAL,
        _("Incorrect message size")); /* Correct error message? FIXME */
      return G_IO_STATUS_ERROR;
    }
687
  
688 689 690
  /* In case of alignment problems */
  memmove (&msg, buf, sizeof (MSG));
  if (!PostMessage (win32_channel->hwnd, msg.message, msg.wParam, msg.lParam))
691
    {
692
      g_set_error(err, G_IO_CHANNEL_ERROR, G_IO_CHANNEL_ERROR_FAILED,
693 694 695 696
        _("Unknown error")); /* Correct error message? FIXME */
      return G_IO_STATUS_ERROR;
    }

697
  *bytes_written = sizeof (MSG);
698 699

  return G_IO_STATUS_NORMAL;
700 701
}

702
static GIOStatus
703
g_io_win32_no_seek (GIOChannel *channel,
704
		    glong       offset,
705 706
		    GSeekType   type,
		    GError     **err)
707
{
708
  g_assert_not_reached ();
709 710

  return G_IO_STATUS_ERROR;
711 712
}

713 714 715
static GIOStatus
g_io_win32_msg_close (GIOChannel *channel,
		      GError    **err)
716 717
{
  /* Nothing to be done. Or should we set hwnd to some invalid value? */
718 719

  return G_IO_STATUS_NORMAL;
720 721
}

722
static void
723 724
g_io_win32_free (GIOChannel *channel)
{
725
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
726
  
727 728 729 730 731
  if (win32_channel->debug)
    g_print ("thread %#x: freeing channel, fd: %d\n",
	     win32_channel->thread_id,
	     win32_channel->fd);

732 733 734 735 736 737 738
  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);
739

740
  g_free (win32_channel->buffer);
741
  g_slist_free (win32_channel->watches);
742 743 744
  g_free (win32_channel);
}

745 746 747
static GSource *
g_io_win32_msg_create_watch (GIOChannel    *channel,
			     GIOCondition   condition)
748
{
749 750 751
  GIOWin32Watch *watch;
  GSource *source;

752
  source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
753
  watch = (GIOWin32Watch *)source;
754 755 756
  
  watch->channel = channel;
  g_io_channel_ref (channel);
757
  
758
  watch->condition = condition;
759
  
760 761
  watch->pollfd.fd = G_WIN32_MSG_HANDLE;
  watch->pollfd.events = condition;
762
  
763
  g_source_add_poll (source, &watch->pollfd);
764
  
765
  return source;
766 767
}

768
static GIOStatus
769
g_io_win32_fd_read (GIOChannel *channel,
770
		    gchar      *buf,
771 772 773
		    gsize       count,
		    gsize      *bytes_read,
		    GError    **err)
774
{
775
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
776
  gint result;
777
  
778 779 780 781
  if (win32_channel->debug)
    g_print ("g_io_win32_fd_read: fd:%d count:%d\n",
	     win32_channel->fd, count);
  
782 783
  if (win32_channel->thread_id)
    {
784
      return buffer_read (win32_channel, buf, count, bytes_read, err);
785
    }
786 787

  result = read (win32_channel->fd, buf, count);
788

789 790 791
  if (result < 0)
    {
      *bytes_read = 0;
792 793 794 795 796 797 798 799 800

      switch(errno)
        {
#ifdef EAGAIN
          case EAGAIN:
            return G_IO_STATUS_AGAIN;
#endif
          default:
            g_set_error (err, G_IO_CHANNEL_ERROR,
801
                         g_io_channel_error_from_errno (errno),
802 803 804
                         strerror (errno));
            return G_IO_STATUS_ERROR;
        }
805
    }
806 807 808

  *bytes_read = result;

809
  return G_IO_STATUS_NORMAL; /* XXX: 0 byte read an error ?? */
810
  return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
811
}
812

813 814 815 816 817 818
static GIOStatus
g_io_win32_fd_write (GIOChannel  *channel,
		     const gchar *buf,
		     gsize        count,
		     gsize       *bytes_written,
		     GError     **err)
819
{
820
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
821
  gint result;
822
  
823
  result = write (win32_channel->fd, buf, count);
824 825 826
  if (win32_channel->debug)
    g_print ("g_io_win32_fd_write: fd:%d count:%d = %d\n",
	     win32_channel->fd, count, result);
827

828 829 830
  if (result < 0)
    {
      *bytes_written = 0;
831 832 833 834 835 836 837 838 839

      switch(errno)
        {
#ifdef EAGAIN
          case EAGAIN:
            return G_IO_STATUS_AGAIN;
#endif
          default:
            g_set_error (err, G_IO_CHANNEL_ERROR,
840
                         g_io_channel_error_from_errno (errno),
841 842 843
                         strerror (errno));
            return G_IO_STATUS_ERROR;
        }
844
    }
845 846 847 848

  *bytes_written = result;

  return G_IO_STATUS_NORMAL;
849 850
}

851
static GIOStatus
852
g_io_win32_fd_seek (GIOChannel *channel,
853
		    glong       offset,
854 855
		    GSeekType   type,
		    GError    **err)
856
{
857
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
858 859
  int whence;
  off_t result;
860
  
861 862 863 864 865 866 867 868 869 870 871 872
  switch (type)
    {
    case G_SEEK_SET:
      whence = SEEK_SET;
      break;
    case G_SEEK_CUR:
      whence = SEEK_CUR;
      break;
    case G_SEEK_END:
      whence = SEEK_END;
      break;
    default:
873 874
      whence = -1; /* Keep the compiler quiet */
      g_assert_not_reached();
875 876 877 878 879 880
    }
  
  result = lseek (win32_channel->fd, offset, whence);
  
  if (result < 0)
    {
881
      g_set_error (err, G_IO_CHANNEL_ERROR,
882
		   g_io_channel_error_from_errno (errno),
883 884
		   strerror (errno));
      return G_IO_STATUS_ERROR;
885
    }
886 887

  return G_IO_STATUS_NORMAL;
888 889
}

890 891 892
static GIOStatus
g_io_win32_fd_close (GIOChannel *channel,
	             GError    **err)
893
{
894
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
895
  
896 897 898 899 900 901 902 903
  if (win32_channel->debug)
    g_print ("thread %#x: closing fd %d\n",
	     win32_channel->thread_id,
	     win32_channel->fd);
  LOCK (win32_channel->mutex);
  if (win32_channel->running)
    {
      if (win32_channel->debug)
904 905
	g_print ("thread %#x: running, marking fd %d for later close\n",
		 win32_channel->thread_id, win32_channel->fd);
906
      win32_channel->running = FALSE;
907
      win32_channel->needs_close = TRUE;
908 909
      SetEvent (win32_channel->data_avail_event);
    }
910 911 912 913 914 915 916 917 918 919
  else
    {
      if (win32_channel->debug)
	g_print ("closing fd %d\n", win32_channel->fd);
      close (win32_channel->fd);
      if (win32_channel->debug)
	g_print ("closed fd %d, setting to -1\n",
		 win32_channel->fd);
      win32_channel->fd = -1;
    }
920
  UNLOCK (win32_channel->mutex);
921 922 923 924

  /* FIXME error detection? */

  return G_IO_STATUS_NORMAL;
925 926
}

927 928 929
static GSource *
g_io_win32_fd_create_watch (GIOChannel    *channel,
			    GIOCondition   condition)
930
{
931
  return g_io_win32_create_watch (channel, condition, read_thread);
932 933
}

934
static GIOStatus
935 936
g_io_win32_sock_read (GIOChannel *channel,
		      gchar      *buf,
937 938 939
		      gsize       count,
		      gsize      *bytes_read,
		      GError    **err)
940
{
941
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
942
  gint result;
943
  GIOChannelError error;
944

945 946 947
  if (win32_channel->debug)
    g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n",
	     win32_channel->fd, count);
948
repeat: 
949
  result = recv (win32_channel->fd, buf, count, 0);
950

951 952 953 954
  if (win32_channel->debug)
    g_print ("g_io_win32_sock_read: recv:%d\n", result);
  
  if (result == SOCKET_ERROR)
955 956
    {
      *bytes_read = 0;
957

958 959 960
      switch (WSAGetLastError ())
	{
	case WSAEINVAL:
961 962
          error = G_IO_CHANNEL_ERROR_INVAL;
          break;
963
	case WSAEWOULDBLOCK:
964
          return G_IO_STATUS_AGAIN;
Hans Breuer's avatar
Hans Breuer committed
965
#ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */
966
	case WSAEINTR:
967
          goto repeat;
Hans Breuer's avatar
Hans Breuer committed
968
#endif
969
	default:
970 971
	  error = G_IO_CHANNEL_ERROR_FAILED;
          break;
972
	}
973 974 975
      g_set_error(err, G_IO_CHANNEL_ERROR, error, _("Socket error"));
      return G_IO_STATUS_ERROR;
      /* FIXME get all errors, better error messages */