/* FDLIST.H - functions and structures used by the fdlist functions

   environ - C programming support functions
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   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 Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
   Contact Martin Gregorie at martin@gregorie.org

*/

#ifndef TRUE
#define TRUE         1
#endif

#ifndef FALSE
#define FALSE        0
#endif

#define MAXFDS       50
#define CONNECTION   0
#define LISTENER     1

#define ADDED        0
#define ON_LIMIT     1
#define ERROR        2

#ifndef SEMAPHORE_H
#include <semaphore.h>
#endif

#ifndef PTHREAD_H
#include <pthread.h>
#endif

typedef struct thread_control_block
{
   int         fd;
   sem_t       *sem_id;
   pthread_t   *thread_id;
   int         debug;
} TCB;

typedef struct fdb
{
   int            count;
   struct pollfd  fds[MAXFDS];
   int            listener[MAXFDS];
   TCB            tcb[MAXFDS];
}  FDL;

void           fdlist_debug(int d);
FDL            *fdlist_create(void);
FDL            *fdlist_release(FDL *pl);
int            fdlist_add(FDL *pl, int fd, int listener_flag);
int            fdlist_del(FDL *pl, int fd);
int            fdlist_getevents(FDL *pl, int fd, short *evflags);
int            fdlist_setevents(FDL *pl, int fd, short evflags);
int            fdlist_islistener(FDL *pl, int fd);
int            fdlist_nextopen(FDL *pl);
int            fdlist_getfd(FDL *pl);
short          fdlist_geteventtype(FDL *pl, int fd);
TCB            *fdlist_get_tcb_ref(FDL *pl, int fd);
struct pollfd  *fdlist_fds(FDL *pl);
int            fdlist_size(FDL *pl);
char           *fdlist_decode(short revents);
