1#ifndef _NBBIO_H_INCLUDED_
2#define _NBBIO_H_INCLUDED_
3
4/*++
5/* NAME
6/*	nbbio 3h
7/* SUMMARY
8/*	non-blocking buffered I/O
9/* SYNOPSIS
10/*	#include "nbbio.h"
11/* DESCRIPTION
12/* .nf
13
14 /*
15  * Utility library.
16  */
17#include <events.h>			/* Needed for EVENT_READ etc. */
18
19 /*
20  * External interface. All structure members are private.
21  */
22typedef void (*NBBIO_ACTION) (int, char *);
23
24typedef struct {
25    int     fd;				/* socket file descriptor */
26    ssize_t bufsize;			/* read/write buffer size */
27    char   *label;			/* diagnostics */
28    NBBIO_ACTION action;		/* call-back routine */
29    char   *context;			/* call-back context */
30    int     flags;			/* buffer-pair status */
31
32    char   *read_buf;			/* start of buffer */
33    ssize_t read_pend;			/* nr of unread bytes */
34
35    char   *write_buf;			/* start of buffer */
36    ssize_t write_pend;			/* nr of unwritten bytes */
37} NBBIO;
38
39#define NBBIO_FLAG_READ		(1<<0)
40#define NBBIO_FLAG_WRITE	(1<<1)
41#define NBBIO_FLAG_EOF		(1<<2)
42#define NBBIO_FLAG_ERROR	(1<<3)
43#define NBBIO_FLAG_TIMEOUT	(1<<4)
44
45#define NBBIO_OP_NAME(np) \
46	(((np)->flags & NBBIO_FLAG_READ) ? "read" : \
47	 ((np)->flags & NBBIO_FLAG_WRITE) ? "write" : \
48	 "unknown")
49
50#define NBBIO_MASK_ACTIVE \
51	(NBBIO_FLAG_READ | NBBIO_FLAG_WRITE)
52
53#define NBBIO_MASK_ERROR \
54	(NBBIO_FLAG_EOF | NBBIO_FLAG_ERROR | NBBIO_FLAG_TIMEOUT)
55
56#define NBBIO_BUFSIZE(np)		(((np)->bufsize) + 0)	/* Read-only */
57
58#define NBBIO_READ_PEND(np)		((np)->read_pend)
59#define NBBIO_READ_BUF(np)		((np)->read_buf + 0)	/* Read-only */
60
61#define NBBIO_WRITE_PEND(np)		((np)->write_pend)
62#define NBBIO_WRITE_BUF(np)		((np)->write_buf + 0)	/* Read-only */
63
64#define NBBIO_ACTIVE_FLAGS(np)		((np)->flags & NBBIO_MASK_ACTIVE)
65#define NBBIO_ERROR_FLAGS(np)		((np)->flags & NBBIO_MASK_ERROR)
66
67extern NBBIO *nbbio_create(int, ssize_t, const char *, NBBIO_ACTION, char *);
68extern void nbbio_free(NBBIO *);
69extern void nbbio_enable_read(NBBIO *, int);
70extern void nbbio_enable_write(NBBIO *, int);
71extern void nbbio_disable_readwrite(NBBIO *);
72extern void nbbio_slumber(NBBIO *, int);
73
74/* LICENSE
75/* .ad
76/* .fi
77/*	The Secure Mailer license must be distributed with this software.
78/* AUTHOR(S)
79/*	Wietse Venema
80/*	IBM T.J. Watson Research
81/*	P.O. Box 704
82/*	Yorktown Heights, NY 10598, USA
83/*--*/
84
85#endif
86