Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8a63b3a1eaf8fc2af6495aab94f22af75de8571 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     Michael Sills-Lavoie(École Polytechnique de Montréal)  - ZeroCopy support
 *              *                         *                       - splice_block
 *******************************************************************************/

/*
 * Abstract byte stream. Bytes in the stream can be divided into groups - messages.
 */

#ifndef D_streams
#define D_streams

#include <tcf/config.h>

/*
 * MARKER_EOM - end of message
 * MARKER_EOS - end of stream
 */
#define MARKER_EOM  (-1)
#define MARKER_EOS  (-2)

#define ESC 3

typedef struct OutputStream OutputStream;

struct OutputStream {
    int supports_zero_copy; /* Stream supports block (zero copy) write */
    unsigned char * cur;
    unsigned char * end;
    void (*write)(OutputStream * stream, int byte);
    void (*write_block)(OutputStream * stream, const char * bytes, size_t size);
    ssize_t (*splice_block)(OutputStream * stream, int fd, size_t size, int64_t * offset);
};

typedef struct InputStream InputStream;

struct InputStream {
    unsigned char * cur;
    unsigned char * end;
    int (*read)(InputStream * stream);
    int (*peek)(InputStream * stream);
};

#if ENABLE_STREAM_MACROS

#define read_stream(inp) (((inp)->cur < (inp)->end) ? *(inp)->cur++ : (inp)->read((inp)))
#define peek_stream(inp) (((inp)->cur < (inp)->end) ? *(inp)->cur : (inp)->peek((inp)))

#define write_stream(out, b) { OutputStream * _s_ = (out); int _x_ = (b); \
    if (_x_ > ESC && _s_->cur < _s_->end) *_s_->cur++ = (unsigned char)_x_; else _s_->write(_s_, _x_); }
#define write_block_stream(out, b, size) (out)->write_block((out), (b), (size))
#define splice_block_stream(out, fd, size, offset) (out)->splice_block((out), (fd), (size), (offset))

#endif

extern int (read_stream)(InputStream * inp);
extern int (peek_stream)(InputStream * inp);
extern void (write_stream)(OutputStream * out, int b);
extern void (write_block_stream)(OutputStream * out, const char * bytes, size_t size);
extern ssize_t (splice_block_stream)(OutputStream * out, int fd, size_t size, int64_t * offset);

extern void write_string(OutputStream * out, const char * str);
extern void write_stringz(OutputStream * out, const char * str);

/*
 * Implementation of an output stream in which the data is written into a byte array.
 * The buffer automatically grows as data is written to it.
 * The data can be retrieved using get_byte_array_output_stream_data().
 * Clients should dispose the data using loc_free().
 */
typedef struct ByteArrayOutputStream {
    OutputStream out;
    char buf[256];
    char * mem;
    size_t pos;
    size_t max;
} ByteArrayOutputStream;

extern OutputStream * create_byte_array_output_stream(ByteArrayOutputStream * buf);
extern void get_byte_array_output_stream_data(ByteArrayOutputStream * buf, char ** data, size_t * size);

/*
 * Implementation of an input stream that reads the data from a byte array.
 */
typedef struct ByteArrayInputStream {
    InputStream inp;
    char * buf;
    size_t pos;
    size_t max;
} ByteArrayInputStream;

extern InputStream * create_byte_array_input_stream(ByteArrayInputStream * buf, char * data, size_t size);

/*
 * Implementation of an input stream that forwards all data being read
 * into an output stream.
 */
typedef struct ForwardingInputStream {
    InputStream fwd;
    InputStream * inp;
    OutputStream * out;
} ForwardingInputStream;

extern InputStream * create_forwarding_input_stream(ForwardingInputStream * buf,
                                                    InputStream * inp, OutputStream * out);

#endif /* D_streams */

Back to the top