Blame tests/gio-test.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 2000  Tor Lillqvist
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/* A test program for the main loop and IO channel code.
Packit ae235b
 * Just run it. Optional parameter is number of sub-processes.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#undef G_DISABLE_ASSERT
Packit ae235b
#undef G_LOG_DOMAIN
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <math.h>
Packit ae235b
#include <time.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  #include <io.h>
Packit ae235b
  #include <fcntl.h>
Packit ae235b
  #include <process.h>
Packit ae235b
  #define STRICT
Packit ae235b
  #include <windows.h>
Packit ae235b
  #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  #include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static int nrunning;
Packit ae235b
static GMainLoop *main_loop;
Packit ae235b
Packit ae235b
#define BUFSIZE 5000		/* Larger than the circular buffer in
Packit ae235b
				 * giowin32.c on purpose.
Packit ae235b
				 */
Packit ae235b
Packit ae235b
static int nkiddies;
Packit ae235b
Packit ae235b
static struct {
Packit ae235b
  int fd;
Packit ae235b
  int seq;
Packit ae235b
} *seqtab;
Packit ae235b
Packit ae235b
static GIOError
Packit ae235b
read_all (int         fd,
Packit ae235b
	  GIOChannel *channel,
Packit ae235b
	  char       *buffer,
Packit ae235b
	  guint       nbytes,
Packit ae235b
	  guint      *bytes_read)
Packit ae235b
{
Packit ae235b
  guint left = nbytes;
Packit ae235b
  gsize nb;
Packit ae235b
  GIOError error = G_IO_ERROR_NONE;
Packit ae235b
  char *bufp = buffer;
Packit ae235b
Packit ae235b
  /* g_io_channel_read() doesn't necessarily return all the
Packit ae235b
   * data we want at once.
Packit ae235b
   */
Packit ae235b
  *bytes_read = 0;
Packit ae235b
  while (left)
Packit ae235b
    {
Packit ae235b
      error = g_io_channel_read (channel, bufp, left, &nb);
Packit ae235b
      
Packit ae235b
      if (error != G_IO_ERROR_NONE)
Packit ae235b
	{
Packit ae235b
	  g_print ("gio-test: ...from %d: %d\n", fd, error);
Packit ae235b
	  if (error == G_IO_ERROR_AGAIN)
Packit ae235b
	    continue;
Packit ae235b
	  break;
Packit ae235b
	}
Packit ae235b
      if (nb == 0)
Packit ae235b
	return error;
Packit ae235b
      left -= nb;
Packit ae235b
      bufp += nb;
Packit ae235b
      *bytes_read += nb;
Packit ae235b
    }
Packit ae235b
  return error;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
shutdown_source (gpointer data)
Packit ae235b
{
Packit ae235b
  if (g_source_remove (*(guint *) data))
Packit ae235b
    {
Packit ae235b
      nrunning--;
Packit ae235b
      if (nrunning == 0)
Packit ae235b
	g_main_loop_quit (main_loop);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
recv_message (GIOChannel  *channel,
Packit ae235b
	      GIOCondition cond,
Packit ae235b
	      gpointer    data)
Packit ae235b
{
Packit ae235b
  gint fd = g_io_channel_unix_get_fd (channel);
Packit ae235b
  gboolean retval = TRUE;
Packit ae235b
Packit ae235b
#ifdef VERBOSE
Packit ae235b
  g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
Packit ae235b
	   (cond & G_IO_ERR) ? " ERR" : "",
Packit ae235b
	   (cond & G_IO_HUP) ? " HUP" : "",
Packit ae235b
	   (cond & G_IO_IN)  ? " IN"  : "",
Packit ae235b
	   (cond & G_IO_PRI) ? " PRI" : "");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (cond & (G_IO_ERR | G_IO_HUP))
Packit ae235b
    {
Packit ae235b
      shutdown_source (data);
Packit ae235b
      retval = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (cond & G_IO_IN)
Packit ae235b
    {
Packit ae235b
      char buf[BUFSIZE];
Packit ae235b
      guint nbytes;
Packit ae235b
      guint nb;
Packit ae235b
      int i, j, seq;
Packit ae235b
      GIOError error;
Packit ae235b
      
Packit ae235b
      error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
Packit ae235b
      if (error == G_IO_ERROR_NONE)
Packit ae235b
	{
Packit ae235b
	  if (nb == 0)
Packit ae235b
	    {
Packit ae235b
#ifdef VERBOSE
Packit ae235b
	      g_print ("gio-test: ...from %d: EOF\n", fd);
Packit ae235b
#endif
Packit ae235b
	      shutdown_source (data);
Packit ae235b
	      return FALSE;
Packit ae235b
	    }
Packit ae235b
	  
Packit ae235b
	  g_assert (nb == sizeof (nbytes));
Packit ae235b
Packit ae235b
	  for (i = 0; i < nkiddies; i++)
Packit ae235b
	    if (seqtab[i].fd == fd)
Packit ae235b
	      {
Packit ae235b
                g_assert_cmpint (seq, ==, seqtab[i].seq);
Packit ae235b
		seqtab[i].seq++;
Packit ae235b
		break;
Packit ae235b
	      }
Packit ae235b
Packit ae235b
	  error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      if (error != G_IO_ERROR_NONE)
Packit ae235b
	return FALSE;
Packit ae235b
      
Packit ae235b
      if (nb == 0)
Packit ae235b
	{
Packit ae235b
#ifdef VERBOSE
Packit ae235b
	  g_print ("gio-test: ...from %d: EOF\n", fd);
Packit ae235b
#endif
Packit ae235b
	  shutdown_source (data);
Packit ae235b
	  return FALSE;
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      g_assert (nb == sizeof (nbytes));
Packit ae235b
Packit ae235b
      g_assert_cmpint (nbytes, <, BUFSIZE);
Packit ae235b
      g_assert (nbytes >= 0 && nbytes < BUFSIZE);
Packit ae235b
#ifdef VERBOSE      
Packit ae235b
      g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
Packit ae235b
#endif      
Packit ae235b
      if (nbytes > 0)
Packit ae235b
	{
Packit ae235b
	  error = read_all (fd, channel, buf, nbytes, &nb);
Packit ae235b
Packit ae235b
	  if (error != G_IO_ERROR_NONE)
Packit ae235b
	    return FALSE;
Packit ae235b
Packit ae235b
	  if (nb == 0)
Packit ae235b
	    {
Packit ae235b
#ifdef VERBOSE
Packit ae235b
	      g_print ("gio-test: ...from %d: EOF\n", fd);
Packit ae235b
#endif
Packit ae235b
	      shutdown_source (data);
Packit ae235b
	      return FALSE;
Packit ae235b
	    }
Packit ae235b
      
Packit ae235b
	  for (j = 0; j < nbytes; j++)
Packit ae235b
            g_assert (buf[j] == ' ' + ((nbytes + j) % 95));
Packit ae235b
#ifdef VERBOSE
Packit ae235b
	  g_print ("gio-test: ...from %d: OK\n", fd);
Packit ae235b
#endif
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
recv_windows_message (GIOChannel  *channel,
Packit ae235b
		      GIOCondition cond,
Packit ae235b
		      gpointer    data)
Packit ae235b
{
Packit ae235b
  GIOError error;
Packit ae235b
  MSG msg;
Packit ae235b
  guint nb;
Packit ae235b
  
Packit ae235b
  while (1)
Packit ae235b
    {
Packit ae235b
      error = g_io_channel_read (channel, &msg, sizeof (MSG), &nb);
Packit ae235b
      
Packit ae235b
      if (error != G_IO_ERROR_NONE)
Packit ae235b
	{
Packit ae235b
	  g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
Packit ae235b
		   (error == G_IO_ERROR_AGAIN ? "AGAIN" :
Packit ae235b
		    (error == G_IO_ERROR_INVAL ? "INVAL" :
Packit ae235b
		     (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
Packit ae235b
	  if (error == G_IO_ERROR_AGAIN)
Packit ae235b
	    continue;
Packit ae235b
	}
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
Packit ae235b
	   msg.hwnd, msg.message, msg.wParam, msg.lParam);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
LRESULT CALLBACK 
Packit ae235b
window_procedure (HWND hwnd,
Packit ae235b
		  UINT message,
Packit ae235b
		  WPARAM wparam,
Packit ae235b
		  LPARAM lparam)
Packit ae235b
{
Packit ae235b
  g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
Packit ae235b
	   hwnd, message, wparam, lparam);
Packit ae235b
  return DefWindowProc (hwnd, message, wparam, lparam);
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int    argc,
Packit ae235b
      char **argv)
Packit ae235b
{
Packit ae235b
  if (argc < 3)
Packit ae235b
    {
Packit ae235b
      /* Parent */
Packit ae235b
      
Packit ae235b
      GIOChannel *my_read_channel;
Packit ae235b
      gchar *cmdline;
Packit ae235b
      guint *id;
Packit ae235b
      int i;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      GTimeVal start, end;
Packit ae235b
      GPollFD pollfd;
Packit ae235b
      int pollresult;
Packit ae235b
      ATOM klass;
Packit ae235b
      static WNDCLASS wcl;
Packit ae235b
      HWND hwnd;
Packit ae235b
      GIOChannel *windows_messages_channel;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
Packit ae235b
      seqtab = g_malloc (nkiddies * 2 * sizeof (int));
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      wcl.style = 0;
Packit ae235b
      wcl.lpfnWndProc = window_procedure;
Packit ae235b
      wcl.cbClsExtra = 0;
Packit ae235b
      wcl.cbWndExtra = 0;
Packit ae235b
      wcl.hInstance = GetModuleHandle (NULL);
Packit ae235b
      wcl.hIcon = NULL;
Packit ae235b
      wcl.hCursor = NULL;
Packit ae235b
      wcl.hbrBackground = NULL;
Packit ae235b
      wcl.lpszMenuName = NULL;
Packit ae235b
      wcl.lpszClassName = "gio-test";
Packit ae235b
Packit ae235b
      klass = RegisterClass (&wcl;;
Packit ae235b
Packit ae235b
      if (!klass)
Packit ae235b
	{
Packit ae235b
	  g_print ("gio-test: RegisterClass failed\n");
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
Packit ae235b
			   NULL, NULL, wcl.hInstance, NULL);
Packit ae235b
      if (!hwnd)
Packit ae235b
	{
Packit ae235b
	  g_print ("gio-test: CreateWindow failed\n");
Packit ae235b
	  exit (1);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
Packit ae235b
      g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      for (i = 0; i < nkiddies; i++)
Packit ae235b
	{
Packit ae235b
	  int pipe_to_sub[2], pipe_from_sub[2];
Packit ae235b
	  
Packit ae235b
	  if (pipe (pipe_to_sub) == -1 ||
Packit ae235b
	      pipe (pipe_from_sub) == -1)
Packit ae235b
	    perror ("pipe"), exit (1);
Packit ae235b
	  
Packit ae235b
	  seqtab[i].fd = pipe_from_sub[0];
Packit ae235b
	  seqtab[i].seq = 0;
Packit ae235b
Packit ae235b
	  my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
Packit ae235b
	  
Packit ae235b
	  id = g_new (guint, 1);
Packit ae235b
	  *id =
Packit ae235b
	    g_io_add_watch (my_read_channel,
Packit ae235b
			    G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
Packit ae235b
			    recv_message,
Packit ae235b
			    id);
Packit ae235b
	  
Packit ae235b
	  nrunning++;
Packit ae235b
	  
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
	  cmdline = g_strdup_printf ("%d:%d:%d",
Packit ae235b
				     pipe_to_sub[0],
Packit ae235b
				     pipe_from_sub[1],
Packit ae235b
				     hwnd);
Packit ae235b
	  _spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
Packit ae235b
#else
Packit ae235b
	  cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
Packit ae235b
				     pipe_to_sub[0], pipe_from_sub[1]);
Packit ae235b
	  
Packit ae235b
	  system (cmdline);
Packit ae235b
          g_free (cmdline);
Packit ae235b
#endif
Packit ae235b
	  close (pipe_to_sub[0]);
Packit ae235b
	  close (pipe_from_sub [1]);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
	  g_get_current_time (&start;;
Packit ae235b
	  g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
Packit ae235b
	  pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
Packit ae235b
	  g_get_current_time (&end;;
Packit ae235b
	  if (end.tv_usec < start.tv_usec)
Packit ae235b
	    end.tv_sec--, end.tv_usec += 1000000;
Packit ae235b
	  g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
Packit ae235b
		   end.tv_sec - start.tv_sec,
Packit ae235b
		   (end.tv_usec - start.tv_usec) / 1000,
Packit ae235b
		   pollresult);
Packit ae235b
#endif
Packit ae235b
          g_io_channel_unref (my_read_channel);
Packit ae235b
	}
Packit ae235b
      
Packit ae235b
      main_loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
      
Packit ae235b
      g_main_loop_run (main_loop);
Packit ae235b
Packit ae235b
      g_main_loop_unref (main_loop);
Packit ae235b
      g_free (seqtab);
Packit ae235b
      g_free (id);
Packit ae235b
    }
Packit ae235b
  else if (argc == 3)
Packit ae235b
    {
Packit ae235b
      /* Child */
Packit ae235b
      
Packit ae235b
      int readfd, writefd;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      HWND hwnd;
Packit ae235b
#endif
Packit ae235b
      int i, j;
Packit ae235b
      char buf[BUFSIZE];
Packit ae235b
      int buflen;
Packit ae235b
      GTimeVal tv;
Packit ae235b
      int n;
Packit ae235b
  
Packit ae235b
      g_get_current_time (&tv;;
Packit ae235b
      
Packit ae235b
      sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      sscanf (argv[2] + n, ":%d", &hwnd);
Packit ae235b
#endif
Packit ae235b
      
Packit ae235b
      srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
Packit ae235b
  
Packit ae235b
      for (i = 0; i < 20 + rand() % 20; i++)
Packit ae235b
	{
Packit ae235b
	  g_usleep (100 + (rand() % 10) * 5000);
Packit ae235b
	  buflen = rand() % BUFSIZE;
Packit ae235b
	  for (j = 0; j < buflen; j++)
Packit ae235b
	    buf[j] = ' ' + ((buflen + j) % 95);
Packit ae235b
#ifdef VERBOSE
Packit ae235b
	  g_print ("gio-test: child writing %d+%d bytes to %d\n",
Packit ae235b
		   (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
Packit ae235b
#endif
Packit ae235b
	  write (writefd, &i, sizeof (i));
Packit ae235b
	  write (writefd, &buflen, sizeof (buflen));
Packit ae235b
	  write (writefd, buf, buflen);
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
	  if (rand() % 100 < 5)
Packit ae235b
	    {
Packit ae235b
	      int msg = WM_USER + (rand() % 100);
Packit ae235b
	      WPARAM wparam = rand ();
Packit ae235b
	      LPARAM lparam = rand ();
Packit ae235b
	      g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
Packit ae235b
		       msg, wparam, lparam, hwnd);
Packit ae235b
	      PostMessage (hwnd, msg, wparam, lparam);
Packit ae235b
	    }
Packit ae235b
#endif
Packit ae235b
	}
Packit ae235b
#ifdef VERBOSE
Packit ae235b
      g_print ("gio-test: child exiting, closing %d\n", writefd);
Packit ae235b
#endif
Packit ae235b
      close (writefd);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_print ("Huh?\n");
Packit ae235b
  
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b