-
Notifications
You must be signed in to change notification settings - Fork 31
/
ext_sock.h
61 lines (47 loc) · 1.97 KB
/
ext_sock.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
55
56
57
58
59
60
61
#ifndef ext_sock_h
#define ext_sock_h
/*
#include "ext.h"
#define SOCKC_NEW 0
#define SOCKC_OPEN 1
#define SOCKC_CLOSED 2
typedef struct sockc_struct
{
uint8_t state;
uint32_t readable, writing;
uint8_t *readbuf, *writebuf, *zwrite;
lob_t opts;
chan_t c;
int fd; // convenience for app usage, initialized to -1
} *sockc_t;
// process channel for new incoming sock channel, any returned sockc must have a read or close function called
// state is SOCKC_OPEN or SOCKC_NEW (until sockc_accept())
// it's set to SOCKC_CLOSED after all the data is read on an ended channel or sockc_close() is called
sockc_t ext_sock(chan_t c);
// changes state from SOCKC_NEW to SOCKC_OPEN
void sockc_accept(sockc_t sc);
// create a sock channel to this hn, optional opts (ip, port), sets state=SOCKC_OPEN
sockc_t sockc_connect(switch_t s, char *hn, lob_t opts);
// tries to flush and end, cleans up, sets SOCKC_CLOSED
sockc_t sockc_close(sockc_t sock);
// must be called to free up sc resources (internally calls sockc_close to be sure)
sockc_t sockc_free(sockc_t sc);
// -1 on err, returns bytes read into buf up to len, sets SOCKC_CLOSED when channel ended
int sockc_read(sockc_t sock, uint8_t *buf, int len);
// -1 on err, returns len and will always buffer up to len
int sockc_write(sockc_t sock, uint8_t *buf, int len);
// general flush outgoing buffer into a packet
void sockc_flush(chan_t c);
// advances readbuf by this len, for use when doing zero-copy reading directly from ->readbuf
// sets SOCKC_CLOSED when channel ended
void sockc_zread(sockc_t sc, int len);
// creates zero-copy buffer space of requested len at sc->zwrite or returns 0
int sockc_zwrite(sockc_t sc, int len);
// must use after writing anything to ->zwrite, adds len to outgoing buffer and resets sc->zwrite, returns len
int sockc_zwritten(sockc_t sc, int len);
// serial-style single character interface
int sockc_available(sockc_t sc);
uint8_t sockc_sread(sockc_t sc);
uint8_t sockc_swrite(sockc_t sc, uint8_t byte);
*/
#endif