/*
    GDAM - Geoff & Dave's Audio Mixer
    Copyright (C) 1999    Dave Benson, Geoff Matters.

    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

    Contact:
        daveb@ffem.org <Dave Benson>
        geoff@ugcs.caltech.edu <Geoff Matters>
*/
#ifndef __GDAM_SERVER_H_
#define __GDAM_SERVER_H_

typedef struct _GdamServer GdamServer;
#include <gdam/common/gdamfdpoll.h>
#include "gdamauthmaker.h"
#include <glib.h>
typedef gboolean (*GdamServerAcceptCheckFunc)(int        fd, 
                                              gpointer   accepted_sockaddr,
                                              gpointer   accept_check_data);
typedef gboolean (*GdamServerNewConnectionFunc) (gint               fd,
						 GdamServer        *server,
						 gpointer           data);

struct _GdamServer
{
  /* How to authenticate the incoming connection. */
  GdamAuthMaker                  *auth_maker;

  /* Our listening file descriptor. */
  int                             socket_fd;

  /* Function to call at `accept' time to do
   * system-level authentication */
  GdamServerAcceptCheckFunc       accept_check_func;
  gpointer                        accept_check_data;
  GDestroyNotify                  accept_check_destroy;

  /* Function to handle the newly constructed file descriptor. */
  GdamServerNewConnectionFunc     new_conn_func;
  gpointer                        new_conn_data;
  GDestroyNotify                  new_conn_destroy;

  /* Flag used to prevent destroy from recursing forever. */
  unsigned                        is_destructing : 1;

  /* main loop stuff -- private. */
  GdamFdPoll                     *fd_poll;
};


GdamServer      *gdam_server_new             (int            bound_socket,
                                              GdamAuthMaker *maker);
GdamServer      *gdam_server_new_full        (int            bound_socket,
                                              GdamAuthMaker *maker,
					      GdamServerNewConnectionFunc func,
					      gpointer       new_conn_data,
					      GDestroyNotify new_conn_destroy);
void             gdam_server_attach          (GdamServer    *server);
void             gdam_server_detach          (GdamServer    *server);
void             gdam_server_destroy         (GdamServer    *server);

#endif
