summaryrefslogtreecommitdiffstats
path: root/src/iochannel.c
blob: 25d6b05ec6559a755faeb0e878ec0b35d79c3c44 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdlib.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

#include "iochannel.h"
#include "util.h"

struct iochannel {
    int ifd, ofd;
    struct pa_mainloop_api* mainloop;

    void (*callback)(struct iochannel*io, void *userdata);
    void*userdata;
    
    int readable;
    int writable;

    int no_close;

    void* input_source, *output_source;
};

static void enable_mainloop_sources(struct iochannel *io) {
    assert(io);

    if (io->input_source == io->output_source) {
        enum pa_mainloop_api_io_events e = PA_MAINLOOP_API_IO_EVENT_NULL;
        assert(io->input_source);
        
        if (!io->readable)
            e |= PA_MAINLOOP_API_IO_EVENT_INPUT;
        if (!io->writable)
            e |= PA_MAINLOOP_API_IO_EVENT_OUTPUT;

        io->mainloop->enable_io(io->mainloop, io->input_source, e);
    } else {
        if (io->input_source)
            io->mainloop->enable_io(io->mainloop, io->input_source, io->readable ? PA_MAINLOOP_API_IO_EVENT_NULL : PA_MAINLOOP_API_IO_EVENT_INPUT);
        if (io->output_source)
            io->mainloop->enable_io(io->mainloop, io->output_source, io->writable ? PA_MAINLOOP_API_IO_EVENT_NULL : PA_MAINLOOP_API_IO_EVENT_OUTPUT);
    }
}

static void callback(struct pa_mainloop_api* m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
    struct iochannel *io = userdata;
    int changed = 0;
    assert(m && fd >= 0 && events && userdata);

    if ((events & PA_MAINLOOP_API_IO_EVENT_INPUT) && !io->readable) {
        io->readable = 1;
        changed = 1;
        assert(id == io->input_source);
    }
    
    if ((events & PA_MAINLOOP_API_IO_EVENT_OUTPUT) && !io->writable) {
        io->writable = 1;
        changed = 1;
        assert(id == io->output_source);
    }

    if (changed) {
        enable_mainloop_sources(io);
        
        if (io->callback)
            io->callback(io, io->userdata);
    }
}

struct iochannel* iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd) {
    struct iochannel *io;
    assert(m && (ifd >= 0 || ofd >= 0));

    io = malloc(sizeof(struct iochannel));
    io->ifd = ifd;
    io->ofd = ofd;
    io->mainloop = m;

    io->userdata = NULL;
    io->callback = NULL;
    io->readable = 0;
    io->writable = 0;
    io->no_close = 0;

    if (ifd == ofd) {
        assert(ifd >= 0);
        make_nonblock_fd(io->ifd);
        io->input_source = io->output_source = m->source_io(m, ifd, PA_MAINLOOP_API_IO_EVENT_BOTH, callback, io);
    } else {

        if (ifd >= 0) {
            make_nonblock_fd(io->ifd);
            io->input_source = m->source_io(m, ifd, PA_MAINLOOP_API_IO_EVENT_INPUT, callback, io);
        } else
            io->input_source = NULL;

        if (ofd >= 0) {
            make_nonblock_fd(io->ofd);
            io->output_source = m->source_io(m, ofd, PA_MAINLOOP_API_IO_EVENT_OUTPUT, callback, io);
        } else
            io->output_source = NULL;
    }

    return io;
}

void iochannel_free(struct iochannel*io) {
    assert(io);

    if (!io->no_close) {
        if (io->ifd >= 0)
            close(io->ifd);
        if (io->ofd >= 0 && io->ofd != io->ifd)
            close(io->ofd);
    }

    if (io->input_source)
        io->mainloop->cancel_io(io->mainloop, io->input_source);
    if (io->output_source && (io->output_source != io->input_source))
        io->mainloop->cancel_io(io->mainloop, io->output_source);
    
    free(io);
}

int iochannel_is_readable(struct iochannel*io) {
    assert(io);
    return io->readable;
}

int iochannel_is_writable(struct iochannel*io) {
    assert(io);
    return io->writable;
}

ssize_t iochannel_write(struct iochannel*io, const void*data, size_t l) {
    ssize_t r;
    assert(io && data && l && io->ofd >= 0);

    if ((r = write(io->ofd, data, l)) >= 0) {
        io->writable = 0;
        enable_mainloop_sources(io);
    }

    return r;
}

ssize_t iochannel_read(struct iochannel*io, void*data, size_t l) {
    ssize_t r;
    
    assert(io && data && l && io->ifd >= 0);

    if ((r = read(io->ifd, data, l)) >= 0) {
        io->readable = 0;
        enable_mainloop_sources(io);
    }

    return r;
}

void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochannel*io, void *userdata), void *userdata) {
    assert(io);
    io->callback = callback;
    io->userdata = userdata;
}

void iochannel_set_noclose(struct iochannel*io, int b) {
    assert(io);
    io->no_close = b;
}

void iochannel_peer_to_string(struct iochannel*io, char*s, size_t l) {
    assert(io && s && l);
    peer_to_string(s, l, io->ifd);
}