~kennylevinsen/seatd

ref: b3438f446e19f9cbe37724a1efe070d03baa292b seatd/include/connection.h -rw-r--r-- 1.1 KiB
b3438f44Kenny Levinsen connection: Shrink buffer sizes 9 months ago
                                                                                
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
#ifndef _SEATD_CONNECTION_H
#define _SEATD_CONNECTION_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#define MAX_FDS			  4
#define CONNECTION_BUFFER_SIZE	  256
#define CONNECTION_FD_BUFFER_SIZE (MAX_FDS * sizeof(int))

struct connection_buffer {
	uint16_t capacity, head, tail;
	char *data;
};

struct connection {
	struct connection_buffer in, out;
	struct connection_buffer fds_in, fds_out;
	char connection_data[CONNECTION_BUFFER_SIZE * 2 + CONNECTION_FD_BUFFER_SIZE * 2];

	int fd;
	bool want_flush;
};

void connection_init(struct connection *connection);

int connection_read(struct connection *connection);
int connection_flush(struct connection *connection);

int connection_put(struct connection *connection, const void *data, size_t count);
int connection_put_fd(struct connection *connection, int fd);

size_t connection_pending(struct connection *connection);
int connection_get(struct connection *connection, void *dst, size_t count);
int connection_get_fd(struct connection *connection);
void connection_restore(struct connection *connection, size_t count);

void connection_close_fds(struct connection *connection);

#endif