summaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_EventBuffer.h
blob: 8e82a5e6ff3f0e013dff24c1c7ef183990bfb92a (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_EventBuffer.h
 * @ingroup events
 * @brief Buffered event management
 *
 * Provides a facade implementing many regularly available event functions.
 * This is an interface that software implementors may use to substitute
 * different kinds of underlying implementations of these event management functions.
 * Notable examples are libevent and libev.
 *
 */
#ifndef libparc_parc_EventBuffer_h
#define libparc_parc_EventBuffer_h

#include <parc/algol/parc_EventQueue.h>

#ifdef PARCLibrary_DISABLE_VALIDATION
#  define parcEventBuffer_OptionalAssertValid(_instance_)
#else
#  define parcEventBuffer_OptionalAssertValid(_instance_) parcEventBuffer_AssertValid(_instance_)
#endif

/**
 * @typedef PARCEventBuffer
 * @brief A structure containing private libevent state data variables
 */
typedef struct PARCEventBuffer PARCEventBuffer;

/**
 * Create an event buffer instance.
 *
 * @returns A pointer to a new PARCEventBuffer instance.
 *
 * Example:
 * @code
 * {
 *     PARCEventBuffer *parcEventBuffer = parcEventBuffer_Create();
 * }
 * @endcode
 *
 */
PARCEventBuffer *parcEventBuffer_Create(void);

/**
 * Destroy a network parcEventBuffer instance.
 *
 * The address of the event instance is passed in.
 *
 * @param [in] parcEventBuffer - The address of the instance to destroy.
 *
 * Example:
 * @code
 * {
 *     parcEventBuffer_Destroy(&parcEventBuffer)
 * }
 * @endcode
 *
 */
void parcEventBuffer_Destroy(PARCEventBuffer **parcEventBuffer);

/**
 * Determine if an instance of `PARCEventBuffer` is valid.
 *
 * Valid means the internal state of the type is consistent with its required current or future behaviour.
 * This may include the validation of internal instances of types.
 *
 * @param [in] eventBuffer A pointer to a `PARCEventBuffer` instance.
 *
 * @return true The instance is valid.
 * @return false The instance is not valid.
 *
 * Example:
 * @code
 * {
 *     PARCEventBuffer *instance = parcEventBuffer_Create();
 *
 *     if (parcEventBuffer_IsValid(instance)) {
 *         printf("Instance is valid.\n");
 *     }
 * }
 * @endcode
 */
bool parcEventBuffer_IsValid(const PARCEventBuffer *eventBuffer);

/**
 * Assert that the given `PARCEventBuffer` instance is valid.
 *
 * @param [in] eventBuffer A pointer to a valid PARCEventBuffer instance.
 *
 * Example:
 * @code
 * {
 *     PARCEventBuffer *a = parcEventBuffer_Create();
 *
 *     parcEventBuffer_AssertValid(a);
 *
 *     printf("Instance is valid.\n");
 *
 *     parcEventBuffer_Release(&b);
 * }
 * @endcode
 */
void parcEventBuffer_AssertValid(const PARCEventBuffer *eventBuffer);

/**
 * Get the input parcEventBuffer instance from the Queue
 *
 * @param [in] parcEventQueue - The queue to obtain the input parcEventBuffer from
 * @returns A pointer to the a PARCEventBuffer instance.
 *
 * Example:
 * @code
 * {
 *     PARCEventBuffer *parcEventBuffer = parcEventBuffer_GetQueueBufferInput(eventQueue);
 * }
 * @endcode
 *
 */
PARCEventBuffer *parcEventBuffer_GetQueueBufferInput(PARCEventQueue *parcEventQueue);

/**
 * Get the output parcEventBuffer instance from the Queue
 *
 * @param [in] parcEventQueue - The queue to obtain the output parcEventBuffer from
 * @returns A pointer to the a PARCEventBuffer instance.
 *
 * Example:
 * @code
 * {
 *     PARCEventBuffer *parcEventBuffer = parcEventBuffer_GetQueueBufferOutput(parcEventQueue);
 * }
 * @endcode
 *
 */
PARCEventBuffer *parcEventBuffer_GetQueueBufferOutput(PARCEventQueue *parcEventQueue);

/**
 * Get the data length of the associated buffer
 *
 * @param [in] parcEventBuffer - The buffer to obtain the length from
 * @returns The length of data within the buffer, 0 if the internal buffer has been freed
 *
 * Example:
 * @code
 * {
 *     int length = parcEventBuffer_GetLength(parcEventBuffer);
 * }
 * @endcode
 *
 */
size_t parcEventBuffer_GetLength(PARCEventBuffer *parcEventBuffer);

/**
 * Consolidate data in the associated parcEventBuffer
 *
 * @param [in] parcEventBuffer - The buffer to consolidate
 * @param [in] size - The length of data to consolidate, -1 linearizes the entire buffer
 * @returns A pointer to the first byte in the buffer.
 *
 * Example:
 * @code
 * {
 *     unsigned char *ptr = parcEventBuffer_Pullup(parcEventBuffer, size);
 * }
 * @endcode
 *
 */
uint8_t *parcEventBuffer_Pullup(PARCEventBuffer *parcEventBuffer, ssize_t size);

/**
 * Read data from the associated parcEventBuffer
 *
 * @param [in] parcEventBuffer - The parcEventBuffer to read from
 * @param [in] data - The memory to read into, NULL to discard data
 * @param [in] length - The number of bytes of data to be read
 * @returns Number of bytes read, -1 on failure.
 *
 * Example:
 * @code
 * {
 *     int bytesRead = parcEventBuffer_Read(parcEventBuffer, destination, bytesToRead);
 * }
 * @endcode
 *
 */
int parcEventBuffer_Read(PARCEventBuffer *parcEventBuffer, void *data, size_t length);

/**
 * Read data from the associated parcEventBuffer without delete them from the buffer.
 * Data can not be NULL
 *
 * @param [in] parcEventBuffer - The parcEventBuffer to read from
 * @param [in] data - The memory to read into
 * @param [in] length - The number of bytes of data to be read
 * @returns Number of bytes read, -1 on failure.
 *
 */
int parcEventBuffer_copyOut(PARCEventBuffer *readBuffer, void *data_out, size_t length);


/**
 * Read from a file descriptor into the end of a buffer
 *
 * @param [in] parcEventBuffer - The buffer to read from
 * @param [in] fd - The file descriptor to read from
 * @param [in] length - The number of bytes of data to be read
 * @returns The number of bytes read, 0 on EOF and -1 on error.
 *
 * Example:
 * @code
 * {
 *     int bytesTransfered = parcEventBuffer_ReadFromFileDescriptor(parcEventBuffer, fd, length);
 * }
 * @endcode
 *
 */
int parcEventBuffer_ReadFromFileDescriptor(PARCEventBuffer *parcEventBuffer, int fd, size_t length);

/**
 * Write to a file descriptor from a buffer
 *
 * @param [in] parcEventBuffer - The buffer to read from
 * @param [in] fd - The file descriptor to write to
 * @param [in] length - The number of bytes of data to be read (-1 for all).
 * @returns The number of bytes read, 0 on EOF and -1 on error.
 *
 * Example:
 * @code
 * {
 *     int bytesTransfered = parcEventBuffer_WriteToFileDescriptor(parcEventBuffer, fd, length);
 * }
 * @endcode
 *
 */
int parcEventBuffer_WriteToFileDescriptor(PARCEventBuffer *parcEventBuffer, int fd, ssize_t length);

/**
 * Append one PARCEventBuffer to another
 *
 * @param [in] source - The buffer to append
 * @param [in] destination - The buffer to append to
 * @returns 0 on success, -1 on failure
 *
 * Example:
 * @code
 * {
 *     int result = parcEventBuffer_AppendBuffer(source, destination);
 * }
 * @endcode
 *
 */
int parcEventBuffer_AppendBuffer(PARCEventBuffer *source, PARCEventBuffer *destination);

/**
 * Append bytes to a parcEventBuffer
 *
 * @param [in] parcEventBuffer - The buffer to write into
 * @param [in] sourceData - The data to append
 * @param [in] length - The number of bytes of data to be added
 * @returns 0 on success, -1 on failure
 *
 * Example:
 * @code
 * {
 *     int result = parcEventBuffer_Append(parcEventBuffer, sourceData, length);
 * }
 * @endcode
 *
 */
int parcEventBuffer_Append(PARCEventBuffer *parcEventBuffer, void *sourceData, size_t length);

/**
 * Prepend data to the associated parcEventBuffer
 *
 * @param [in] parcEventBuffer - The parcEventBuffer to prepend to
 * @param [in] sourceData - The data to prepend
 * @param [in] length - The number of bytes of data to be added
 * @returns 0 on success, -1 on failure
 *
 * Example:
 * @code
 * {
 *     int result = parcEventBuffer_Read(parcEventBuffer, sourceData, length);
 * }
 * @endcode
 *
 */
int parcEventBuffer_Prepend(PARCEventBuffer *parcEventBuffer, void *sourceData, size_t length);

/**
 * Move data from one buffer to another
 *
 * @param [in] sourceEventBuffer - The buffer to read from
 * @param [in] destinationEventBuffer - The buffer to move into
 * @param [in] length - The number of bytes of data to be moved
 * @returns The number of bytes moved on success, -1 on failure
 *
 * Example:
 * @code
 * {
 *     int bytesMoved = parcEventBuffer_Create(sourceEventBuffer, destinationEventBuffer, length);
 * }
 * @endcode
 *
 */
int parcEventBuffer_ReadIntoBuffer(PARCEventBuffer *sourceEventBuffer, PARCEventBuffer *destinationEventBuffer, size_t length);

/**
 * Read a text line terminated by an optional carriage return, followed by a single linefeed
 *
 * @param [in] parcEventBuffer - The buffer to read from
 * @param [in] length - The number of bytes of data to be moved
 * @returns A newly allocated null terminated string, which must be freed by the caller
 *
 * Example:
 * @code
 * {
 *     char *text = parcEventBuffer_ReadLine(parcEventBuffer, length);
 *     ...
 *     parcMemory_Deallocate((void *)&text);
 * }
 * @endcode
 *
 */
char *parcEventBuffer_ReadLine(PARCEventBuffer *parcEventBuffer, size_t *length);

/**
 * Free a text line returned from parcEventBuffer_ReadLine
 *
 * @param [in] parcEventBuffer - The buffer to read from
 * @param [in] line address of returned value from previous call to parcEventBuffer_ReadLine
 *
 * Example:
 * @code
 * {
 *     char *text = parcEventBuffer_ReadLine(parcEventBuffer, length);
 *     ...
 *     parcEventBuffer_FreeLine(parcEventBuffer, &text);
 * }
 * @endcode
 *
 */
void parcEventBuffer_FreeLine(PARCEventBuffer *parcEventBuffer, char **line);

/**
 * Turn on debugging flags and messages
 *
 * @param [in] logger - the log to write debug messages to
 *
 * Example:
 * @code
 * {
 *     parcEventBuffer_EnableDebug(logger);
 * }
 * @endcode
 *
 */
void parcEventBuffer_EnableDebug(PARCLog *logger);

/**
 * Turn off debugging flags and messages
 *
 * Example:
 * @code
 * {
 *     parcEventBuffer_DisableDebug();
 * }
 * @endcode
 *
 */
void parcEventBuffer_DisableDebug(void);
#endif // libparc_parc_EventBuffer_h