giowin32.c

来自「Linux下的多协议即时通讯程序源代码」· C语言 代码 · 共 858 行 · 第 1/2 页

C
858
字号
/* 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 4096typedef 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 voidg_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 gbooleang_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 voidg_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 voidcreate_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 voidcreate_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 voidinit_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 __stdcallselect_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);  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.   */  return 0;}static gbooleang_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)    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?