summaryrefslogtreecommitdiffstats
path: root/src/protocol-simple.c
blob: 3335bc1499c50d8ff4c1d32193736a8029f1aa44 (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
#include <assert.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "inputstream.h"
#include "outputstream.h"
#include "protocol-simple.h"
#include "client.h"

struct connection {
    struct protocol_simple *protocol;
    struct iochannel *io;
    struct input_stream *istream;
    struct output_stream *ostream;
    struct client *client;
};

struct protocol_simple {
    struct core *core;
    struct socket_server*server;
    struct idxset *connections;
    enum protocol_simple_mode mode;
};

#define BUFSIZE PIPE_BUF

static void free_connection(void *data, void *userdata) {
    struct connection *c = data;
    assert(data);
    
    if (c->istream)
        input_stream_free(c->istream);
    if (c->ostream)
        output_stream_free(c->ostream);

    client_free(c->client);

    iochannel_free(c->io);
    free(c);
}

static void io_callback(struct iochannel*io, void *userdata) {
    struct connection *c = userdata;
    assert(io && c);

    if (c->istream && iochannel_is_readable(io)) {
        struct memchunk chunk;
        ssize_t r;

        chunk.memblock = memblock_new(BUFSIZE);
        assert(chunk.memblock);

        if ((r = iochannel_read(io, chunk.memblock->data, BUFSIZE)) <= 0) {
            fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
            memblock_unref(chunk.memblock);
            goto fail;
        }
        
        chunk.memblock->length = r;
        chunk.length = r;
        chunk.index = 0;
        
        memblockq_push(c->istream->memblockq, &chunk, 0);
        input_stream_notify(c->istream);
        memblock_unref(chunk.memblock);
    }

    if (c->ostream && iochannel_is_writable(io)) {
        struct memchunk chunk;
        ssize_t r;

        memblockq_peek(c->ostream->memblockq, &chunk);
        assert(chunk.memblock && chunk.length);

        if ((r = iochannel_write(io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
            fprintf(stderr, "write(): %s\n", strerror(errno));
            memblock_unref(chunk.memblock);
            goto fail;
        }
        
        memblockq_drop(c->ostream->memblockq, r);
        memblock_unref(chunk.memblock);
    }

    return;
    
fail:
    idxset_remove_by_data(c->protocol->connections, c, NULL);
    free_connection(c, NULL);
}

static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
    struct protocol_simple *p = userdata;
    struct connection *c = NULL;
    assert(s && io && p);

    c = malloc(sizeof(struct connection));
    assert(c);
    c->io = io;
    c->istream = NULL;
    c->ostream = NULL;
    c->protocol = p;
    
    if (p->mode & PROTOCOL_SIMPLE_RECORD) {
        struct source *source;

        if (!(source = core_get_default_source(p->core))) {
            fprintf(stderr, "Failed to get default source.\n");
            goto fail;
        }

        c->ostream = output_stream_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
        assert(c->ostream);
    }

    if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
        struct sink *sink;

        if (!(sink = core_get_default_sink(p->core))) {
            fprintf(stderr, "Failed to get default sink.\n");
            goto fail;
        }

        c->istream = input_stream_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
        assert(c->istream);
    }

    c->client = client_new(p->core, "SIMPLE", "Client");
    assert(c->client);

    iochannel_set_callback(c->io, io_callback, c);
    idxset_put(p->connections, c, NULL);
    return;
    
fail:
    if (c) {
        if (c->istream)
            input_stream_free(c->istream);
        if (c->ostream)
            output_stream_free(c->ostream);

        iochannel_free(c->io);
        free(c);
    }
}

struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
    struct protocol_simple* p;
    assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);

    p = malloc(sizeof(struct protocol_simple));
    assert(p);
    p->core = core;
    p->server = server;
    p->connections = idxset_new(NULL, NULL);
    p->mode = mode;

    socket_server_set_callback(p->server, on_connection, p);
    
    return p;
}


void protocol_simple_free(struct protocol_simple *p) {
    assert(p);

    idxset_free(p->connections, free_connection, NULL);
    socket_server_free(p->server);
    free(p);
}