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

Code before:
    /* Postpone update state - with IO threads & TLS we don't want the IO threads to update the event loop events - let
     * the main-thread do it */
    void (*postpone_update_state)(struct connection *conn, int);
    /* Called by the main-thread */
    void (*update_state)(struct connection *conn);

    /* TLS specified methods */
    sds (*get_peer_cert)(struct connection *conn);

    /* Miscellaneous */
    int (*connIntegrityChecked)(void); // return 1 if connection type has built-in integrity checks

    /* Get peer username based on connection type */
    sds (*get_peer_username)(connection *conn);
} ConnectionType;

struct connection {
    ConnectionType *type;
    ConnectionState state;
    int last_errno;
    int fd;
    short int flags;
    short int refs;
    unsigned short int iovcnt;
    void *private_data;
    ConnectionCallbackFunc conn_handler;
    ConnectionCallbackFunc write_handler;
    ConnectionCallbackFunc read_handler;
};

#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.
 *
 * The fd supplied should therefore be associated with an already accept()ed
 * socket.
 *
 * connAccept() may directly call accept_handler(), or return and call it
 * at a later time. This behavior is a bit awkward but aims to reduce the need

Review comment (suggestion):
Let's put this next to `get_peer_cert` a few lines above

```c
    /* TLS specified methods */
    sds (*get_peer_cert)(struct connection *conn);
    sds (*get_peer_username)(connection *conn);
```

Code after:
    /* IO */
    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);
    int (*set_write_handler)(struct connection *conn, ConnectionCallbackFunc handler, int barrier);
    int (*set_read_handler)(struct connection *conn, ConnectionCallbackFunc handler);
    const char *(*get_last_error)(struct connection *conn);
    ssize_t (*sync_write)(struct connection *conn, char *ptr, ssize_t size, long long timeout);
    ssize_t (*sync_read)(struct connection *conn, char *ptr, ssize_t size, long long timeout);
    ssize_t (*sync_readline)(struct connection *conn, char *ptr, ssize_t size, long long timeout);

    /* pending data */
    int (*has_pending_data)(void);
    int (*process_pending_data)(void);

    /* Postpone update state - with IO threads & TLS we don't want the IO threads to update the event loop events - let
     * the main-thread do it */
    void (*postpone_update_state)(struct connection *conn, int);
    /* Called by the main-thread */
    void (*update_state)(struct connection *conn);

    /* TLS specified methods */
    sds (*get_peer_cert)(struct connection *conn);

    /* Get peer username based on connection type */
    sds (*get_peer_username)(connection *conn);

    /* Miscellaneous */
    int (*connIntegrityChecked)(void); // return 1 if connection type has built-in integrity checks
} ConnectionType;

struct connection {
    ConnectionType *type;
    ConnectionState state;
    int last_errno;
    int fd;
    short int flags;
    short int refs;
    unsigned short int iovcnt;
    void *private_data;
    ConnectionCallbackFunc conn_handler;
    ConnectionCallbackFunc write_handler;
    ConnectionCallbackFunc read_handler;
};

#define CONFIG_BINDADDR_MAX 16

/* Setup a listener by a connection type */
struct connListener {
    int fd[CONFIG_BINDADDR_MAX];
    int count;