aboutsummaryrefslogtreecommitdiffstats
path: root/server/tcp.h
blob: a39392202b48b199be3dd11dd4eb992265f1e368 (plain)
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
73
74
75
76
#ifndef KV_SERVER_TCP
#define KV_SERVER_TCP

#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <unistd.h>
#include <packet.h>
#include "channel.h"

#include <container.h>
#include <rstypes.h>

#define TCP_PORT	8085
#define LISTEN_AMOUNT	128
#define TCP_MAX_WAIT_MS	10
#define TCP_MAX_RETRIES	0
#define ADMIN_UID	0
#define POLL_RESTART	5000

struct channel_pool {
	u64 id;
	u64 owner;
	int pipefd;
	char *name;
	u16 port;
};

/* Size: 4 + 4 + 8 + 8 + 32 + 4 + 8 = 68 bytes */
/* I can't fucking count it's 64 bytes */
struct connection_state {
	// TODO: consider using clang extension to pack this enum into u8
	enum {
		PROG_NONE = 0,	// the socket has just been opened
			   	// in loop: read type and continue
		PROG_TYPE,	// command type has been read
				// in loop: start/continue reading command data
				// when finished reading, set (& ~POLLIN) | POLLOUT
		PROG_COMD,	// command data has been read
				// in loop: when revents & POLLOUT, write response
				// or error, shutdown socket
		PROG_ARRY,	// extra data needs to be written 
				// (like in get_channels command)
		PROG_ERRR,	// an error happened and it should be reported,
			   	// after that socket should be shut down and closed
	} progress;
	enum commd_type type;
	u64 bytes_ctr;
	u8 cmd_size;
	struct commd cmd;
	enum commd_error err;
	union {
		u64* response_array;
		u64 response;
	};
};

/* val: struct tcp_channel */
extern struct hash_set channels;
/* val: struct tcp_user */
extern struct hash_set users;
/* val: struct channel_pool*/
extern struct hash_set pools;

void print_state(int);
void exit_tcp(int);

void tcp_loop(void);
void new_tcp_loop(void);
u64 spawn_channel(struct thread_loop_arg *arg);
u64 spawn_channel_pool(void* arg); // TODO
bool sendto_channel(size_t chid, struct kv_system_packet* packet, int wait_ack_ms, int repeat);

#endif // KV_SERVER_TCP
/* vim: set ts=8 noet: */