Repository navigation
Expand file tree
/
Copy pathnetsvc.h
More file actions
72 lines (61 loc) · 1.91 KB
/
Copy pathnetsvc.h
File metadata and controls
72 lines (61 loc) · 1.91 KB
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
#ifndef NETSVC_H
#define NETSVC_H
/* Connection fan-out for the emulators.
*
* One accept path, all I/O on the emulator thread, N clients per stream.
* The accept thread does nothing but accept() and queue the fd; the
* emulator thread adopts it, registers it with pollfd.c, and does all
* reading, dispatching and writing. Nothing here may block the emulator:
* sockets are non-blocking and a short write keeps the remainder in the
* connection's out buffer.
*/
typedef struct NetConn NetConn;
typedef struct NetSvc NetSvc;
enum {
SVC_LINE = 0, /* callbacks get whole \n-terminated lines */
SVC_RAW, /* callbacks get bytes as they arrive */
NETLINE = 1024, /* longest line we accept, see DEBUG_PROTOCOL_SPEC §1 */
NETOUT = 64*1024, /* default output buffer */
};
struct NetConn
{
FD fd;
NetSvc *svc;
NetConn *next;
void *aux; /* the service's per-connection state */
/* line assembly, SVC_LINE only */
char in[NETLINE+1];
int nin;
int toolong; /* discarding the rest of an over-long line */
char *out;
int nout, outsz;
int dead;
};
struct NetSvc
{
int port;
int maxconn;
int mode; /* SVC_LINE or SVC_RAW */
int outsz; /* per-connection output buffer, 0 = NETOUT */
/* all called on the emulator thread */
void (*accepted)(NetConn *c);
void (*line)(NetConn *c, char *line); /* nil = the line was too long */
void (*data)(NetConn *c, u8 *buf, int n);
void (*closed)(NetConn *c);
/* if set, the service takes the raw fd and no NetConn is made */
void (*adopt)(NetSvc *svc, int fd);
/* private */
NetConn *conns;
int nconn;
NetSvc *nextsvc;
};
void netsvc_add(NetSvc *svc);
void netsvc_start(void);
void netsvc_poll(void);
void netsvc_write(NetConn *c, const void *buf, int n);
void netsvc_print(NetConn *c, const char *fmt, ...);
void netsvc_broadcast(NetSvc *svc, const void *buf, int n);
void netsvc_close(NetConn *c);
NetConn *netsvc_adopt(NetSvc *svc, int fd);
int netsvc_nconn(NetSvc *svc);
#endif