-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutgoing.c
52 lines (39 loc) · 961 Bytes
/
outgoing.c
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
#include <assert.h>
#include <string.h>
#include "outgoing.h"
struct frame64wr *frame64wr_init(
struct frame64wr *s,
void *opaque,
frame64wr_write_cb write_data
) {
assert(s);
assert(write_data);
memset(s, 0, sizeof(struct frame64wr));
s->opaque = opaque;
s->write_data = write_data;
s->p = &s->buf[1];
return s;
}
void frame64wr_reset(struct frame64wr *s) {
assert(s);
memset(s->buf, 0, 64);
s->p = &s->buf[1];
}
bool frame64wr_send(struct frame64wr *s, uint8_t chn, bool fin) {
assert(s);
assert(chn <= 1);
// compute length:
size_t len = frame64wr_len(s);
assert(len <= 63);
// compute header byte:
s->buf[0] = ((fin & 1) << 7) | ((chn & 1) << 6) | (len & 63);
// send frame out:
assert(s->write_data);
long n;
if (!s->write_data(s, s->buf, len + 1, &n)) {
return false;
}
// reset pointer:
s->p = s->buf + 1;
return true;
}