-
Notifications
You must be signed in to change notification settings - Fork 41
/
buffer_stream.h
54 lines (42 loc) · 1.56 KB
/
buffer_stream.h
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
#pragma once
#include "buffer_pool.h"
#include "shiftbuffer.h"
#include "tunnel.h"
#include <stddef.h>
#include <stdint.h>
/*
This implements a simple container that holds buffers, some opmizations are also applied.
you can for example check byte index 1 or 5 of the buffers without concating them, then
you'll be able to read only when your protocol is satisfied, the size you want
*/
#define i_TYPE queue, shift_buffer_t * // NOLINT
#include "stc/deq.h"
struct buffer_stream_s
{
buffer_pool_t *pool;
queue q;
size_t size;
};
typedef struct buffer_stream_s buffer_stream_t;
buffer_stream_t *newBufferStream(buffer_pool_t *pool);
void emptyBufferStream(buffer_stream_t *self);
void destroyBufferStream(buffer_stream_t *self);
void bufferStreamPush(buffer_stream_t *self, shift_buffer_t *buf);
shift_buffer_t *bufferStreamRead(buffer_stream_t *self, size_t bytes);
shift_buffer_t *bufferStreamIdealRead(buffer_stream_t *self);
uint8_t bufferStreamViewByteAt(buffer_stream_t *self, size_t at);
void bufferStreamViewBytesAt(buffer_stream_t *self, size_t at, uint8_t *buf, size_t len);
static inline size_t bufferStreamLen(buffer_stream_t *self)
{
return self->size;
}
static inline shift_buffer_t *bufferStreamFullRead(buffer_stream_t *self)
{
return bufferStreamRead(self, bufferStreamLen(self));
}
static inline void bufferStreamPushContextPayload(buffer_stream_t *self,context_t *c)
{
assert(c->payload);
bufferStreamPush(self,c->payload);
dropContexPayload(c);
}