summaryrefslogtreecommitdiff
path: root/src/connection.h
diff options
context:
space:
mode:
authorzhenwei pi <pizhenwei@bytedance.com>2022-07-27 12:18:28 +0800
committerzhenwei pi <pizhenwei@bytedance.com>2022-08-22 15:16:08 +0800
commit0b27cfe37d2d996cdab92629cfe6d6a39a15b464 (patch)
treef14eae4b629bc8a64bc747762b76cad430e4aed2 /src/connection.h
parent45617385e72eb814c6548064835cf19cdf466a5b (diff)
downloadredis-0b27cfe37d2d996cdab92629cfe6d6a39a15b464.tar.gz
Introduce .listen into connection type
Introduce listen method into connection type, this allows no hard code of listen logic. Originally, we initialize server during startup like this: if (server.port) listenToPort(server.port,&server.ipfd); if (server.tls_port) listenToPort(server.port,&server.tlsfd); if (server.unixsocket) anetUnixServer(...server.unixsocket...); ... if (createSocketAcceptHandler(&server.ipfd, acceptTcpHandler) != C_OK) if (createSocketAcceptHandler(&server.tlsfd, acceptTcpHandler) != C_OK) if (createSocketAcceptHandler(&server.sofd, acceptTcpHandler) != C_OK) ... If a new connection type gets supported, we have to add more hard code to setup listener. Introduce .listen and refactor listener, and Unix socket supports this. this allows to setup listener arguments and create listener in a loop. What's more, '.listen' is defined in connection.h, so we should include server.h to import 'struct socketFds', but server.h has already include 'connection.h'. To avoid including loop(also to make code reasonable), define 'struct connListener' in connection.h instead of 'struct socketFds' in server.h. This leads this commit to get more changes. There are more fields in 'struct connListener', hence it's possible to simplify changeBindAddr & applyTLSPort() & updatePort() into a single logic: update the listener config from the server.xxx, and re-create the listener. Because of the new field 'priv' in struct connListener, we expect to pass this to the accept handler(even it's not used currently), this may be used in the future. Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
Diffstat (limited to 'src/connection.h')
-rw-r--r--src/connection.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/connection.h b/src/connection.h
index c61b56451..ab68b6975 100644
--- a/src/connection.h
+++ b/src/connection.h
@@ -44,6 +44,7 @@
struct aeEventLoop;
typedef struct connection connection;
+typedef struct connListener connListener;
typedef enum {
CONN_STATE_NONE = 0,
@@ -77,6 +78,7 @@ typedef struct ConnectionType {
void (*ae_handler)(struct aeEventLoop *el, int fd, void *clientData, int mask);
aeFileProc *accept_handler;
int (*addr)(connection *conn, char *ip, size_t ip_len, int *port, int remote);
+ int (*listen)(connListener *listener);
/* create/close connection */
connection* (*conn_create)(void);
@@ -122,6 +124,19 @@ struct connection {
int fd;
};
+#define CONFIG_BINDADDR_MAX 16
+
+/* Setup a listener by a connection type */
+struct connListener {
+ int fd[CONFIG_BINDADDR_MAX];
+ int count;
+ char **bindaddr;
+ int bindaddr_count;
+ int port;
+ ConnectionType *ct;
+ void *priv; /* used by connection type specified data */
+};
+
/* The connection module does not deal with listening and accepting sockets,
* so we assume we have a socket when an incoming connection is created.
*
@@ -406,6 +421,11 @@ int connTypeHasPendingData(void);
/* walk all the connection types and process pending data for each connection type */
int connTypeProcessPendingData(void);
+/* Listen on an initialized listener */
+static inline int connListen(connListener *listener) {
+ return listener->ct->listen(listener);
+}
+
/* Get accept_handler of a connection type */
static inline aeFileProc *connAcceptHandler(ConnectionType *ct) {
if (ct)