Repository navigation
Expand file tree
/
Copy pathconnection.h
More file actions
128 lines (101 loc) · 3.25 KB
/
Copy pathconnection.h
File metadata and controls
128 lines (101 loc) · 3.25 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2023 enhua zhou
*
* Authors:
* enhua zhou <zhouenhua@bytedance.com>
*
* Design and some codes are taken from redis.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#define CONN_TYPE_SOCKET "tcp"
#define CONN_TYPE_TLS "tls"
#define CONN_TYPE_MAX 8
/*
* Overall wall-clock budget (milliseconds) for sending one response. The send
* path loops to deliver a full response across partial writes, but gives up
* once this deadline passes so a single slow client can never tie a worker
* thread down indefinitely.
*/
#define CONN_SEND_TIMEOUT_MS 10000
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>
#include <time.h>
typedef struct connection connection;
typedef struct connection_type {
const char *(*get_type)(struct connection *conn);
void (*init)(void);
void (*cleanup)(void);
int (*configure)(void *priv, int reconfiguration);
connection *(*conn_create)(int port, char *addr);
int (*listen)(connection *listener);
int (*accept)(struct connection *listener, struct connection *conn);
void (*close)(struct connection *conn);
void (*shutdown)(struct connection *conn);
int (*write)(struct connection *conn, const void *data, size_t data_len);
int (*writev)(struct connection *conn, const struct iovec *iov, int iovcnt);
int (*read)(struct connection *conn, void *buf, size_t buf_len);
} connection_type;
struct connection {
connection_type *type;
int fd;
int port;
int is_local;
char *bindaddr;
};
static inline int conn_configure(connection_type *ct, void *priv, int reconfigure)
{
return ct->configure(priv, reconfigure);
}
static inline connection *conn_create(connection_type *ct, int port, char *addr)
{
return ct->conn_create(port, addr);
}
static inline int conn_listen(connection *listener)
{
return listener->type->listen(listener);
}
static inline int conn_accept(connection *listener, connection *conn)
{
return conn->type->accept(listener, conn);
}
static inline void conn_close(connection *conn)
{
return conn->type->close(conn);
}
static inline void conn_shutdown(connection *conn)
{
return conn->type->shutdown(conn);
}
static inline int conn_write(connection *conn, const void *data, size_t data_len)
{
return conn->type->write(conn, data, data_len);
}
static inline int conn_writev(connection *conn, const struct iovec *iovs, int iovcnt)
{
return conn->type->writev(conn, iovs, iovcnt);
}
static inline int conn_read(connection *conn, void *buf, size_t buf_len)
{
return conn->type->read(conn, buf, buf_len);
}
void conntype_initialize(void);
int conntype_register(connection_type *ct);
int register_conntype_socket();
int register_conntype_tls();
int listen_to_port(int port, char *bindaddr, int af);
/* Monotonic clock in milliseconds, used to bound the send path. */
static inline long long conn_now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
connection_type *get_conntype_by_name(const char *typename);
int get_conntype_index_by_name(const char *typename);
#endif /* _CONNECTION_H_ */