/* gcc -Wall -g socket.c `pkg-config --cflags --libs glib-2.0 gio-2.0 gio-unix-2.0` -o socket && ./socket */

#include <glib.h>
#include <glib-unix.h>
#include <gio/gio.h>
#include <gio/gunixoutputstream.h>
#include <sys/types.h>
#include <sys/socket.h>

#define BUFFER_SIZE (254*1024)

void
cb (GObject *source, GAsyncResult *res, gpointer user_data)
{
  g_print ("NEVER CALLED\n");
}

int
main (int argc, const char* argv[])
{
  int fds[2], ret;
  GOutputStream *os;
  GError *error = NULL;
  gchar *buf;

  ret = socketpair (AF_UNIX, SOCK_STREAM, 0, fds);
  g_assert (ret == 0);

  /* it works correctly with pipe even if nonblocking is not set, shouldn't we use it instead of socketpair? */
  //g_unix_open_pipe (fds, 0, &error);
  //g_assert_no_error (error);

  /* the same as fcntl (socket_fds[0], F_SETFL, O_NONBLOCK); */
  //g_unix_set_fd_nonblocking (fds[0], TRUE, &error);
  //g_assert_no_error (error);

  os = g_unix_output_stream_new (fds[0], TRUE);
  g_assert (os);

  buf = g_malloc (BUFFER_SIZE);
  g_assert (buf);

  g_output_stream_write_async (os, buf, BUFFER_SIZE, 0, NULL, cb, NULL);

  g_print ("NEVER CALLED\n");
}