abstract_io.h 1.38 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
/* abstract_io.h
 *
 * This is the layer separating protocol processing from actual io.
 */

#ifndef LSH_ABSTRACT_IO_H_INCLUDED
#define LSH_ABSTRACT_IO_H_INCLUDED

#include "lsh_types.h"

struct abstract_read;
typedef int (*abstract_read_f)(struct abstract_read *closure,
			       UINT8 *buffer, UINT32 length);
14
15
16
17
18
19
20
21
22
23
/* A read-function returning n means:
 *
 * n > 0: n bytes were read successfully.
 * n = 0: No more data available, without blocking.
 * n = -1: Read failed.
 * n = -2: EOF.
 */
#define A_FAIL -1
#define A_EOF -2

Niels Möller's avatar
Niels Möller committed
24
25
26
27
28
29
30
struct abstract_read
{
  abstract_read_f read;
};

#define A_READ(f, buffer, length) (f)->read((f), (buffer), (length))

Niels Möller's avatar
Niels Möller committed
31
32
33
struct read_handler;
typedef struct read_handler * (*read_handler_f)(struct read_handler *closure,
						struct abstract_read *read);
Niels Möller's avatar
Niels Möller committed
34
35
struct read_handler
{
Niels Möller's avatar
Niels Möller committed
36
  read_handler_f handler;
Niels Möller's avatar
Niels Möller committed
37
38
};

Niels Möller's avatar
Niels Möller committed
39
#define READ_HANDLER(h, read) ((h)->handler((h), (read)))
40

41
42
43
/* FIXME: What should writers return? Perhaps a new writer,
 * analogous to read-handlers? */

Niels Möller's avatar
Niels Möller committed
44
45
46
47
48
49
50
51
52
struct abstract_write;
typedef int (*abstract_write_f)(struct abstract_write *closure,
				struct lsh_string *packet);

struct abstract_write
{
  abstract_write_f write;
};

Niels Möller's avatar
Niels Möller committed
53
54
55
56
57
58
59
60
61
#define A_WRITE(f, packet) ((f)->write((f), (packet)))

/* A processor that passes its result on to another processor */
struct abstract_write_pipe
{
  struct abstract_write super;
  struct abstract_write *next;
};

Niels Möller's avatar
Niels Möller committed
62
#endif /*LSH_ABSTRACT_IO_H_INCLUDED */