/***************************************************************************
 *
 * Copyright (c) 2000, 2001 BalaBit IT Ltd, Budapest, Hungary
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: stream.h,v 1.15.2.1.2.2 2001/09/19 19:22:17 bazsi Exp $
 *
 ***************************************************************************/

#ifndef ZORP_STREAM_H_INCLUDED
#define ZORP_STREAM_H_INCLUDED

#include <zorp/zorp.h>

#include <time.h>

#define Z_STREAM_MAX_NAME   64

struct _ZStream;

typedef guint (*ZStreamCallback)(struct _ZStream *, GIOCondition);

/*+

  ZStream encapsulates a Unix file descriptor. It contains some
  attributes and callbacks which are used by ZPoll.

  +*/
typedef struct _ZStream 
{
  GIOChannel super;
  struct _ZStream *next;
  GIOChannel *channel;
  void (*shutdown)(struct _ZStream *self, int how);
  gboolean (*wait)(struct _ZStream *self, guint cond, gint timeout);
  gchar name[Z_STREAM_MAX_NAME];
  gint fd;
  gint timeout;
  time_t time_open;
  guint64 bytes_recvd, bytes_sent;
  gboolean want_read;     /*+ do we want read callbacks? +*/
  gboolean want_pri;      /*+ do we want urgent data callbacks? +*/
  gboolean want_write;    /*+ do we want write callbacks +*/
  gpointer user_data;     /*+ opaque pointer, can be used by callbacks +*/
  ZStreamCallback read;   /*+ pointer to read callback +*/
  ZStreamCallback pri;    /*+ pointer to urgent data callback +*/
  ZStreamCallback write;  /*+ pointer to write callback +*/
} ZStream;

#define ZSTREAM_READ(s, c)  (s)->read(s, c)
#define ZSTREAM_WRITE(s, c) (s)->write(s, c)
#define ZSTREAM_PRI(s, c)   (s)->pri(s, c)

ZStream *z_stream_new(gint fd, gchar *name);

#define z_stream_read(s, b, l, r) g_io_channel_read(&((s)->super), b, l, r)
#define z_stream_write(s, b, l, r) g_io_channel_write(&((s)->super), b, l, r)
#define z_stream_close(s) g_io_channel_close(&((s)->super))
#define z_stream_ref(s) g_io_channel_ref(&((s)->super))
#define z_stream_unref(s) g_io_channel_unref(&((s)->super))
#define z_stream_shutdown(s, i) (s->shutdown(s, i))

GIOError z_stream_send(ZStream *s, gchar *buf, guint buflen, int flags);
gboolean z_stream_wait_fd(ZStream *stream, guint cond, gint timeout);

void z_data_dump(char *prefix, char *buf, guint len);

#endif
