chiark / gitweb /
@@@ wip type definitions in manpage synopses
[mLib] / buf / pkbuf.3
CommitLineData
1e7e4330 1.\" -*-nroff-*-
fbf20b5b 2.TH pkbuf 3 "16 July 2000" "Straylight/Edgeware" "mLib utilities library"
1e7e4330 3.SH "NAME"
4pkbuf \- split packets out of asynchronously received blocks
5.\" @pkbuf_flush
6.\" @pkbuf_close
7.\" @pkbuf_free
8.\" @pkbuf_snarf
9.\" @pkbuf_want
10.\" @pkbuf_init
11.\" @pkbuf_destroy
12.SH "SYNOPSIS"
13.nf
14.B "#include <mLib/pkbuf.h>"
15
4729aa69
MW
16.B "enum {"
17.B "\h'4n'PKBUF_ENABLE = ..."
18.B "};"
19
20.B "typedef struct {"
21.B "\h'4n'unsigned f;"
22.B "\h'4n'..."
23.B "} pkbuf;"
24
25.ds mT \fBtypedef void pkbuf_func(
26.B "\*(mToctet *" b ", size_t " sz ", pkbuf *" p ,
27.BI "\h'\w'\*(mT'u'size_t *" keep ", void *" p );
28
1e7e4330 29.BI "void pkbuf_flush(pkbuf *" pk ", octet *" p ", size_t " len );
30.BI "void pkbuf_close(pkbuf *" pk );
31.BI "size_t pkbuf_free(pkbuf *" pk ", octet **" p );
32.BI "void pkbuf_snarf(pkbuf *" pk ", const void *" p ", size_t " sz );
33.BI "void pkbuf_want(pkbuf *" pk ", size_t " want );
0daaeb18 34.BI "void pkbuf_init(pkbuf *" pk ", pkbuf_func *" func ", void *" p );
1e7e4330 35.BI "void pkbuf_destroy(pkbuf *" pk );
36.fi
37.SH "DESCRIPTION"
38The declarations in
39.B <mLib/pkbuf.h>
40implement a
41.IR "packet buffer" .
42Given unpredictably-sized chunks of data, the packet buffer extracts
43completed packets of data, with sizes ascertained by a handler
d4efbcd9 44function.
1e7e4330 45.PP
46The state of a packet buffer is stored in an object of type
47.BR pkbuf .
48This is a structure which must be allocated by the caller. The
49structure should normally be considered opaque (see the section on
50.B Disablement
51for an exception to this).
52.SS "Initialization and finalization"
53The function
54.B pkbuf_init
55initializes a packet buffer ready for use. It is given three arguments:
56.TP
57.BI "pkbuf *" pk
58A pointer to the block of memory to use for the packet buffer. The packet
59buffer will allocate memory to store incoming data automatically: this
60structure just contains bookkeeping information.
61.TP
0daaeb18 62.BI "pkbuf_func *" func
1e7e4330 63The
64.I packet-handler
65function to which the packet buffer should pass packets of data when
0daaeb18 66they're received. See
67.B "Packet breaking and the handler function"
68below.
1e7e4330 69.TP
70.BI "void *" p
71A pointer argument to be passed to the function when a packet arrives.
72.PP
73By default, the buffer is
74allocated from the current arena,
75.BR arena_global (3);
76this may be changed by altering the buffer's
77.B a
78member to refer to a different arena at any time when the buffer is
79unallocated.
80.PP
81A packet buffer must be destroyed after use by calling
82.BR pkbuf_destroy ,
83passing it the address of the buffer block.
84.SS "Inserting data into the buffer"
85There are two interfaces for inserting data into the buffer. One's much
86simpler than the other, although it's less expressive.
87.PP
88The simple interface is
89.BR pkbuf_snarf .
90This function is given three arguments: a pointer
91.I pk
92to a packet buffer structure; a pointer
93.I p
528c8b4d 94to a chunk of data to read; and the size
1e7e4330 95.I sz
96of the chunk of data. The data is pushed through the packet buffer and
97any complete packets are passed on to the packet handler.
98.PP
99The complex interface is the pair of functions
100.I pkbuf_free
101and
102.IR pkbuf_flush .
103.PP
d4efbcd9 104The
1e7e4330 105.B pkbuf_free
106function returns the address and size of a free portion of the packet
107buffer's memory into which data may be written. The function is passed
d4efbcd9 108the address
1e7e4330 109.I pk
110of the packet buffer. Its result is the size of the free area, and it
111writes the base address of this free space to the location pointed to by
112the argument
113.IR p .
114The caller's data must be written to ascending memory locations starting
115at
116.BI * p
117and no data may be written beyond the end of the free space. However,
118it isn't necessary to completely fill the buffer.
119.PP
120Once the free area has had some data written to it,
121.B pkbuf_flush
122is called to examine the new data and break it into packets. This is
123given three arguments:
124.TP
125.BI "pkbuf *" pk
126The address of the packet buffer.
127.TP
128.BI "octet *" p
129The address at which the new data has been written. This must be the
130base address returned from
131.BR pkbuf_free .
132.TP
133.BI "size_t " len
134The number of bytes which have been written to the buffer.
135.PP
136The
137.B pkbuf_flush
138function breaks the new data into packets as described below, and passes
139each one in turn to the packet-handler function.
140.PP
141The
142.B pkbuf_snarf
143function is trivially implemented in terms of the more complex
528c8b4d 144.BR pkbuf_free / pkbuf_flush
1e7e4330 145interface.
0daaeb18 146.SS "Packet breaking and the handler function"
1e7e4330 147The function
148.B pkbuf_want
149is used to inform the packet buffer of the expected length of the next
150packet. It is given the pointer
151.I pk
152to the packet buffer and a size
153.I sz
154of the packet.
155.PP
4729aa69
MW
156When enough data has arrived, the packet-handler function is called. It
157is passed:
1e7e4330 158.TP
159.BI "octet *" b
160A pointer to the packet data in the buffer, or zero to signify
161end-of-file.
162.TP
163.BI "size_t " sz
164The size of the packet, as previously configured via
165.BR pkbuf_want .
166.TP
167.BI "pkbuf *" pk
168A pointer to the packet buffer.
169.TP
170.BI "size_t *" keep
171A location in which to store the number of bytes of the current packet
172to be retained. The bytes kept are from the end of the current packet:
173if you want to keep bytes from the beginning of the packet, you should
174move them into the right place.
175.TP
176.BI "void *" p
177The pointer which was set up in the call to
178.BR pkbuf_init .
179.PP
180.SS "Flushing the remaining data"
181When the client program knows that there's no more data arriving (for
182example, an end-of-file condition exists on its data source) it should
183call the function
184.BR pkbuf_close .
185This will call the handler one final time with a null pointer to inform
186it of the end-of-file.
187.SS "Disablement"
188The packet buffer is intended to be used in higher-level program
189objects, such as the packet selector described in
190.BR selpk (3).
191Unfortunately, a concept from this high level needs to exist at the
192packet buffer level, which complicates the description somewhat. The
193idea is that, when a packet-handler attached to some higher-level object
194decides that it's read enough, it can
195.I disable
196the object so that it doesn't see any more data.
197.PP
198Clearly, since an
199.B pkbuf_flush
528c8b4d 200call can emit more than one packet, it must be aware that the packet
1e7e4330 201handler isn't interested in any more packet. However, this fact must
202also be signalled to the higher-level object so that it can detach
203itself from its data source.
204.PP
205Rather than invent some complex interface for this, the packet buffer
3bc42912 206exports one of its structure members, a flags words called
207.BR f .
1e7e4330 208A higher-level object wishing to disable the packet buffer simply clears
209the bit
210.B PKBUF_ENABLE
3bc42912 211in this flags word.
1e7e4330 212.PP
213Disabling a buffer causes an immediate return from
214.BR pkbuf_flush .
215However, it is not permitted for the functions
216.B pkbuf_flush
217or
218.B pkbuf_close
219to be called on a disabled buffer. (This condition isn't checked for;
220it'll just do the wrong thing.) Furthermore, the
221.B pkbuf_snarf
222function does not handle disablement at all, because it would complicate
223the interface so much that it wouldn't have any advantage over the more
224general
225.BR pkbuf_free / pkbuf_flush .
226.SH "SEE ALSO"
227.BR lbuf (3),
228.BR selpk (3),
229.BR mLib (3).
230.SH "AUTHOR"
9b5ac6ff 231Mark Wooding, <mdw@distorted.org.uk>