/***************************/
/*   j_net.h by IoDream    */
/*  j_net main interface   */
/* Not released yet        */
/*  UNDER THE Lesser GPL   */
/***************************/

/* IoDream (IoDream@ifrance.com) */

/* TODOs (to go to the next version)
 */

#ifndef j_dynarray_h
#define j_dynarray_h

/* C++ compilers friendly */
#ifdef __cplusplus
extern "C" {
#endif /*__cplusplus*/

#include <sys/types.h>

/* Constants */
/* TRUE and FALSE */
#ifndef FALSE
#define FALSE 0
#define TRUE  1
#endif /* FALSE */
/* Symbolic values for the second parm to shutdown */
#ifndef SHUT_RDWR
#define SHUT_RD   0
#define SHUT_WR   1
#define SHUT_RDWR 2
#endif /* SHUT_RDWR */

/***********************************/
/*** Socket creation/destruction ***/
/***********************************/

/* Opens a new socket bind to port number port_number.
   If port_number == 0, the post number is automatically given by the system.
   The choosen port number is returned in port_number. */
extern int j_net_create_new_socket(short *port_number);

/* Close the socket sock for the current process.
   Return 0 if successful, or -1 in case of failure (check errno). */
extern int j_net_close_socket(int sock);

/* Close the socket sock. The parameter how if set to close either in read, in write, or both
  (repectively SHUT_RD, SHUT_RD, SHUT_RDWR) for ALL the processus belonging to the same group.
   Return 0 if successful, or -1 in case of failure (check errno). */
extern int j_net_shutdown_socket(int sock, int how);

/*************************************/
/*** Server run & client connexion ***/
/*************************************/

/* Start a server using socket sock, with max_client maximum connections
   and p_server_function the server function. */
extern void j_net_run_server(int sock, int max_client, void (*p_server_function)(void *arg));

/* Connect to the server named remote_name at port port_number.
   Return the socket number if successfully connected, -1 otherwise. */
extern int j_net_connect(char *remote_name, short port_number);

/******************************/
/*** Socket reading/writing ***/
/******************************/

/* Improved read that handle interruption on reading.
   Read up to len characters, may be less.
   Return length of data read if successful, 0 if nothing to read, or -1 on failure (check errno). */
extern ssize_t j_net_read_max(int fd, const void *dest, size_t len);

/* Improved read that handle interruption on reading, and partial reads.
   Read len characters, no more, no less.
   Return length of data read if successful, 0 if nothing to read, or -1 on failure (check errno). */
extern ssize_t j_net_read_all(int fd, const void *dest, size_t len);

/* Improved write that handle interruption on writing, and partial writes.
   Return length of data written if successful, 0 if nothing to write, or -1 on failure (check errno). */
extern ssize_t j_net_write_all(int fd, const void *source, size_t len);

/*********************/
/*** Network stuff ***/
/*********************/

/* Return the name of the host, or "!Unknown!". */
extern const char *j_net_get_hostname(void);

/* Return the port number from the socket sock, or -1 if unknown. */
extern short j_net_get_sock_port_number(int sock);

/*******************/
/*** Misc. stuff ***/
/*******************/

/* Sweep all the zombie child processus around. Non blocking. */
extern void j_net_sweeper(void);

/* Generic error logging. Unfinished. Temporary use. */
extern void j_log_error(char *f, unsigned int l, int severity, char *text, ...);

/* C++ compilers friendly */
#ifdef __cplusplus
}
#endif /*__cplusplus*/

#endif /*j_dynarray_h*/
