/* ========================================================================== * connection.c - connection layer framework * -------------------------------------------------------------------------- * Copyright (C) 2022 zhenwei pi * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit * persons to whom the Software is furnished to do so, subject to the * following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * USE OR OTHER DEALINGS IN THE SOFTWARE. * ========================================================================== */ #include "server.h" #include "connection.h" static ConnectionType *connTypes[CONN_TYPE_MAX]; int connTypeRegister(ConnectionType *ct) { int type = ct->get_type(NULL); /* unknown connection type as a fatal error */ if (type >= CONN_TYPE_MAX) { serverPanic("Unsupported connection type %d", type); } if (connTypes[type] == ct) { serverLog(LL_WARNING, "Connection type %d already registered", type); return C_OK; } serverLog(LL_VERBOSE, "Connection type %d registered", type); connTypes[type] = ct; if (ct->init) { ct->init(); } return C_OK; } int connTypeInitialize() { /* currently socket connection type is necessary */ serverAssert(RedisRegisterConnectionTypeSocket() == C_OK); /* currently unix socket connection type is necessary */ serverAssert(RedisRegisterConnectionTypeUnix() == C_OK); /* may fail if without BUILD_TLS=yes */ RedisRegisterConnectionTypeTLS(); return C_OK; } ConnectionType *connectionByType(int type) { ConnectionType *ct; serverAssert(type < CONN_TYPE_MAX); ct = connTypes[type]; if (!ct) { serverLog(LL_WARNING, "Missing implement of connection type %d", type); } return ct; } void connTypeCleanup(int type) { ConnectionType *ct = connectionByType(type); if (ct && ct->cleanup) { ct->cleanup(); } } void connTypeCleanupAll() { int type; for (type = 0; type < CONN_TYPE_MAX; type++) { connTypeCleanup(type); } } int connTypeConfigure(int type, void *priv, int reconfigure) { ConnectionType *ct = connectionByType(type); if (ct && ct->configure) { return ct->configure(priv, reconfigure); } return C_ERR; } /* walk all the connection types until has pending data */ int connTypeHasPendingData(void) { ConnectionType *ct; int type; int ret = 0; for (type = 0; type < CONN_TYPE_MAX; type++) { ct = connTypes[type]; if (ct && ct->has_pending_data && (ret = ct->has_pending_data())) { return ret; } } return ret; } /* walk all the connection types and process pending data for each connection type */ int connTypeProcessPendingData(void) { ConnectionType *ct; int type; int ret = 0; for (type = 0; type < CONN_TYPE_MAX; type++) { ct = connTypes[type]; if (ct && ct->process_pending_data) { ret += ct->process_pending_data(); } } return ret; } void *connTypeGetCtx(int type) { ConnectionType *ct = connectionByType(type); if (ct && ct->get_ctx) { return ct->get_ctx(); } return NULL; } void *connTypeGetClientCtx(int type) { ConnectionType *ct = connectionByType(type); if (ct && ct->get_client_ctx) { return ct->get_client_ctx(); } return NULL; } connection *connCreate(int type) { ConnectionType *ct = connectionByType(type); serverAssert(ct && ct->conn_create); return ct->conn_create(); } connection *connCreateAccepted(int type, int fd, void *priv) { ConnectionType *ct = connectionByType(type); serverAssert(ct && ct->conn_create_accepted); return ct->conn_create_accepted(fd, priv); }