/*
 * $Id: dinet.h,v 1.11.1.1 1998/03/30 18:19:15 abakun Exp $
 * header file for the darkiron abstracted network layer
 */


#ifndef DI_NET_H
#define DI_NET_H

#include "func_private.h"

/* for the NETSOCKET type, our abstract network handle */

typedef struct handle_s NETSOCKET;

typedef int         netsocket;

typedef void        (*net_func_ptr) (netsocket);

struct handle_s {
    int                 fd;
    long                user_storage;
    int                 type;
    int                 listen_port;	/* used only for HANDLETYPE_LISTENING */
    net_func_ptr        on_read;
    net_func_ptr        on_accept;
    net_func_ptr        on_close;
    net_func_ptr        on_fullbuffer;
    FILE               *fileptr;
    char               *buffer;	/* this will change if the buffer is not full */
    int                 buffer_size;	/* this will change if the buffer is not full */
};

#define HANDLETYPE_NONE		0
#define HANDLETYPE_NETSTREAM	1
#define HANDLETYPE_FILE         2
#define HANDLETYPE_LISTENING	3


/* our error types */

#define NET_SUCCESS       0
#define NET_EINVARG	 -2
#define NET_ENOMEM	 -3
#define NET_ENOTFOUND	 -4
#define NET_ENOTHOST     -5
#define NET_EBADIOCTL    -6
#define NET_ESOCKET      -7
#define NET_ECONNECT     -8
#define NET_ENOSERVICE   -9
#define NET_ENOHOST     -10
#define NET_EUNSUPPORT  -11

typedef int         error_t;


/* our interface */
func_public void    net_end(void);
func_public void    net_start();
func_public error_t net_listen(int port, net_func_ptr accept_func);
func_public error_t net_unlisten(int port);
func_public int     net_poll_listeners(int sec, int usec);
func_public error_t net_on_read(netsocket i, net_func_ptr on_read_func);
func_public int     net_poll_readers(int sec, int usec);
func_public error_t net_on_close(netsocket i, net_func_ptr on_close_func);
func_public error_t net_close(netsocket i);
func_public error_t net_read(netsocket i, char *buf, size_t bufsiz);
func_public error_t net_write(netsocket i, char *buf, size_t bufsiz);
func_public error_t net_open_connection(char *host, int port, netsocket * handle);
func_public error_t net_local_connection_info(netsocket i, char *host, int *port);
func_public error_t net_remote_connection_info(netsocket i, char *host, int *port);
func_public error_t net_host_lookup(char *hostname, char *host);
func_public error_t net_service_lookup(char *service, int *port);
func_public error_t net_read_into_buffer(netsocket i, net_func_ptr bufferfunc, char *buf, size_t bytes);
func_public int     net_bytes_left_in_buffer(netsocket i);
func_public error_t net_put_assoc_data(netsocket i, long data);
func_public error_t net_get_assoc_data(netsocket i, long *data);
func_public FILE   *net_tofile(netsocket i);
func_public error_t net_fdtonetsocket(int fd, netsocket * rethandle);



#endif				/* DI_NET_H */


/*
 * $Log: dinet.h,v $
 * Revision 1.11.1.1  1998/03/30 18:19:15  abakun
 * this header file should be used with net.c 1.29.1
 * all the references to NETSOCKET* were changed to a new type def of netsocket
 *
 * Revision 1.11  1998/03/23 21:39:31  abakun
 * added net_bytes_left_in_buffer declaration
 *
 * Revision 1.10  1998/03/09 19:57:54  abakun
 * added error code NET_EUNSUPPORT
 *
 * Revision 1.9  1998/03/09 19:25:36  abakun
 * added header definitions for net_tofile and net_fdtonetsocket
 *
 * Revision 1.8  1998/03/06 22:49:25  abakun
 * changed HANDLETYPE_CONNECTED to HANDLETYPE_NETSTREAM, so it will be more obvious to support regular files
 * and UDP in the future
 *
 * Revision 1.7  1998/03/05 21:00:27  abakun
 * added net_get_assoc_data and net_put_assoc_data
 *
 * Revision 1.6  1998/03/05 19:40:18  abakun
 * net_end now takes no arguments specified via (void) argument list, rather than an empty arg list
 * this is how atexit() functions should be declared
 *
 * Revision 1.5  1998/03/03 20:13:41  abakun
 * added buffer and buffer_size elements to the netsocket structure, and the forward decl
 * for net_read_into_buffer was added
 *
 * Revision 1.4  1998/02/28 23:22:02  abakun
 * moved func_private/public defs to their own header file
 *
 * Revision 1.3  1998/02/27 21:18:15  abakun
 * added public declarations for net_host_lookup and net_service_lookup
 *
 * Revision 1.2  1998/02/27 17:07:33  abakun
 * added forward declaration for net_end
 *
 * Revision 1.1  1998/02/26 23:12:59  abakun
 * Initial revision
 *
 */
