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