summaryrefslogtreecommitdiffstats
path: root/src/buffio.h
blob: fff251499bbbe4735565b5284f64bc81e3f383ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef foobuffiohfoo
#define foobuffiohfoo

#include <inttypes.h>
#include <sys/types.h>

enum buffio_sched_cb {
    BUFFIO_SCHED_CB_IDLE = 0,
    BUFFIO_SCHED_CB_INPUT_READY = 1,
    BUFFIO_SCHED_CB_OUTPUT_REQUEST = 2,
    BUFFIO_SCHED_CB_OUTPUT_EMPTY = 32,
    BUFFIO_SCHED_CB_EOF = 4,
    BUFFIO_SCHED_CB_EPIPE = 8,
    BUFFIO_SCHED_CB_ERROR = 16
};

struct buffio {
    int ifd;
    int ofd;

    uint8_t *input_buf;
    size_t input_max_length, input_index, input_length, input_range;

    uint8_t *output_buf;
    size_t output_max_length, output_index, output_length, output_range;

    int b_read_cb, b_write_cb;

    void *user;

    int readable, writable;
    int prebuf;

    int (*input_ready_cb) (struct buffio *b, void *user);
    int (*output_request_cb) (struct buffio *b, void *user);
    int (*output_empty_cb) (struct buffio *b, void *user);
    int (*eof_cb) (struct buffio *b, void *user);
    int (*epipe_cb) (struct buffio *b, void *user);
    int (*error_cb) (struct buffio *b, void *user);

    enum buffio_sched_cb sched_cb;
    
    uint32_t output_delay_usec;
    uint32_t output_latency_usec;
    int delaying;
    struct timeval finish_tv, timeout_tv;
};

struct buffio* buffio_new(int ifd, int ofd);
void buffio_free(struct buffio *b);

void buffio_write(struct buffio *b, const uint8_t *d, size_t l);
void buffio_print(struct buffio *b, const char *s);

void buffio_flush_input(struct buffio *b);
void buffio_flush_output(struct buffio *b);

void buffio_command(struct buffio *b, const char *c);
int buffio_find_input(struct buffio *b, const char *c);
char *buffio_read_line(struct buffio *b, char *c, size_t l);

void buffio_dump(struct buffio *b);

void buffio_set_input_range(struct buffio *b, ssize_t w);
void buffio_set_output_range(struct buffio *b, ssize_t w);

const uint8_t* buffio_read_ptr(struct buffio *b, size_t *l);
void buffio_read_ptr_inc(struct buffio *b, size_t l);

uint8_t* buffio_write_ptr(struct buffio *b, size_t *l);
void buffio_write_ptr_inc(struct buffio *b, size_t l);

void buffio_close_input_fd(struct buffio *b);
void buffio_close_output_fd(struct buffio *b);

int buffio_output_is_empty(struct buffio *b);
int buffio_input_is_full(struct buffio *b);

void buffio_dump_lines(struct buffio *b);

void buffio_set_fds(struct buffio *b, int ifd, int ofd);

void buffio_set_delay_usec(struct buffio *b, unsigned long usec, unsigned long latency);

int buffio_can_write(struct buffio *b, size_t l);

void buffio_set_prebuf(struct buffio *b, int p);

#endif