Repository: valkey-io/valkey
File: src/connection.h (C/C++)

Code before:

#define CONN_FLAG_CLOSE_SCHEDULED (1 << 0)      /* Closed scheduled by a handler */
#define CONN_FLAG_WRITE_BARRIER (1 << 1)        /* Write barrier requested */
#define CONN_FLAG_ALLOW_ACCEPT_OFFLOAD (1 << 2) /* Connection accept can be offloaded to IO threads. */

typedef enum {
    CONN_TYPE_ID_INVALID = 0,
    CONN_TYPE_ID_SOCKET,
    CONN_TYPE_ID_UNIX,
    CONN_TYPE_ID_TLS,
    CONN_TYPE_ID_RDMA,
} ConnectionTypeId;

#define CONN_TYPE_SOCKET "tcp"
#define CONN_TYPE_UNIX "unix"
#define CONN_TYPE_TLS "tls"
#define CONN_TYPE_RDMA "rdma"
#define CONN_TYPE_MAX 8 /* 8 is enough to be extendable */

static inline const char *getConnectionTypeName(int type_id) {
    switch (type_id) {
    case CONN_TYPE_ID_SOCKET:
        return CONN_TYPE_SOCKET;
    case CONN_TYPE_ID_UNIX:
        return CONN_TYPE_UNIX;
    case CONN_TYPE_ID_TLS:
        return CONN_TYPE_TLS;
    case CONN_TYPE_ID_RDMA:
        return CONN_TYPE_RDMA;
    default:
        return "invalid type";
    }
}

typedef void (*ConnectionCallbackFunc)(struct connection *conn);

typedef struct ConnectionType {
    /* connection type */
    int (*get_type_id)(struct connection *conn);
    const char *(*get_type)(struct connection *conn);

    /* connection type initialize & finalize & configure */
    void (*init)(void); /* auto-call during register */
    void (*cleanup)(void);
    int (*configure)(void *priv, int reconfigure);

    /* ae & accept & listen & error & address handler */
    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 (*is_local)(connection *conn);

Review comment (refactor):
let's simplify the code a bit further (and remove the \#defines)
```suggestion
static inline const char *getConnectionTypeName(int type_id) {
    switch (type_id) {
    case CONN_TYPE_ID_SOCKET:
        return "tcp";
    case CONN_TYPE_ID_UNIX:
        return "unix";
    case CONN_TYPE_ID_TLS:
        return "tls";
    case CONN_TYPE_ID_RDMA:
        return "rdma";
    default:
        return "invalid type";
```

Code after:

#define CONN_FLAG_CLOSE_SCHEDULED (1 << 0)      /* Closed scheduled by a handler */
#define CONN_FLAG_WRITE_BARRIER (1 << 1)        /* Write barrier requested */
#define CONN_FLAG_ALLOW_ACCEPT_OFFLOAD (1 << 2) /* Connection accept can be offloaded to IO threads. */

typedef enum {
    CONN_TYPE_INVALID = -1,
    CONN_TYPE_SOCKET,
    CONN_TYPE_UNIX,
    CONN_TYPE_TLS,
    CONN_TYPE_RDMA,
    CONN_TYPE_MAX,
} ConnectionTypeId;

static inline const char *getConnectionTypeName(int type) {
    switch (type) {
    case CONN_TYPE_SOCKET:
        return "tcp";
    case CONN_TYPE_UNIX:
        return "unix";
    case CONN_TYPE_TLS:
        return "tls";
    case CONN_TYPE_RDMA:
        return "rdma";
    default:
        return "invalid type";
    }
}

typedef void (*ConnectionCallbackFunc)(struct connection *conn);

typedef struct ConnectionType {
    /* connection type */
    int (*get_type)(void);

    /* connection type initialize & finalize & configure */
    void (*init)(void); /* auto-call during register */
    void (*cleanup)(void);
    int (*configure)(void *priv, int reconfigure);

    /* ae & accept & listen & error & address handler */
    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 (*is_local)(connection *conn);
    int (*listen)(connListener *listener);
    void (*closeListener)(connListener *listener);

    /* create/shutdown/close connection */
    connection *(*conn_create)(void);
    connection *(*conn_create_accepted)(int fd, void *priv);