#ifndef _SESSION_H #define _SESSION_H #ifdef __cpluscplus extern "C" { #endif #include #include "tuple.h" #include "packet.h" enum session_state { SESSION_STATE_INIT = 0, SESSION_STATE_OPENING, SESSION_STATE_ACTIVE, SESSION_STATE_CLOSING, SESSION_STATE_CLOSED, }; enum session_type { SESSION_TYPE_TCP = 0x1, SESSION_TYPE_UDP = 0x2, }; enum session_dir { SESSION_DIR_NONE = 0, SESSION_DIR_C2S = 1, SESSION_DIR_S2C = 2, }; enum closing_reasion { CLOSING_BY_TIMEOUT = 1, CLOSING_BY_EVICTED = 2, CLOSING_BY_CLIENT_FIN = 3, CLOSING_BY_CLIENT_RST = 4, CLOSING_BY_SERVER_FIN = 5, CLOSING_BY_SERVER_RST = 6, }; struct session; /****************************************************************************** * session base info ******************************************************************************/ void session_init(struct session *sess); // session id void session_set_id(struct session *sess, uint64_t id); uint64_t session_get_id(const struct session *sess); // session key void session_set_tuple6(struct session *sess, struct tuple6 *tuple); const struct tuple6 *session_get0_tuple6(const struct session *sess); void session_set_tuple6_dir(struct session *sess, enum session_dir dir); enum session_dir session_get_tuple6_dir(const struct session *sess); // session state void session_set_state(struct session *sess, enum session_state state); enum session_state session_get_state(const struct session *sess); // session type void session_set_type(struct session *sess, enum session_type type); enum session_type session_get_type(const struct session *sess); // closing reasion void session_set_closing_reasion(struct session *sess, enum closing_reasion reasion); enum closing_reasion session_get_closing_reasion(const struct session *sess); // session metrics void session_inc_c2s_metrics(struct session *sess, uint64_t packets, uint64_t bytes); void session_inc_s2c_metrics(struct session *sess, uint64_t packets, uint64_t bytes); uint64_t session_get_c2s_bytes(const struct session *sess); uint64_t session_get_s2c_bytes(const struct session *sess); uint64_t session_get_c2s_packets(const struct session *sess); uint64_t session_get_s2c_packets(const struct session *sess); // session timestamp void session_set_create_time(struct session *sess, uint64_t timestamp); void session_set_last_time(struct session *sess, uint64_t timestamp); uint64_t session_get_create_time(const struct session *sess); uint64_t session_get_last_time(const struct session *sess); /****************************************************************************** * session packet ******************************************************************************/ const struct packet *session_get0_c2s_1st_pkt(const struct session *sess); const struct packet *session_get0_s2c_1st_pkt(const struct session *sess); const struct packet *session_get0_1st_pkt(const struct session *sess); // session current packet void session_set0_cur_pkt(struct session *sess, const struct packet *pkt); const struct packet *session_get0_cur_pkt(const struct session *sess); // session current dir void session_set_cur_dir(struct session *sess, enum session_dir dir); enum session_dir session_get_cur_dir(const struct session *sess); /****************************************************************************** * session ex data ******************************************************************************/ typedef void session_ex_free_cb(struct session *sess, uint8_t idx, void *ex_ptr, void *arg); /* * the exdata prodoced by user, and comsumed by same user. * so, the exdata is not shared by different user. * otherwise, the exdata need dup by refer count, and free by refer count. * * if key exist, not allow update, return original index. */ uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args); /* * Support update ex_data. * * if key exist: run free_cb free old value, then set new value. * if not run free_cb, old value will be memory leak. * if not allow update, new value will be memory leak. * if key not exist: set new value. */ void session_set_ex_data(struct session *sess, uint8_t idx, void *val); void *session_get0_ex_data(const struct session *sess, uint8_t idx); /* * after set ex_data, the owner of ex_data is session, so user should not free it directly. * if user want to free ex_data, should use session_free_ex_data. */ void session_free_ex_data(struct session *sess, uint8_t idx); /****************************************************************************** * session expire ******************************************************************************/ typedef void (*session_expire_cb)(struct session *sess, void *arg); // session timer void session_set_expirecb(struct session *sess, session_expire_cb expire_cb, void *expire_arg, uint64_t expire_abs_ts); void session_del_expirecb(struct session *sess); void session_run_expirecb(struct session *sess); /****************************************************************************** * session dump ******************************************************************************/ const char *session_closing_reasion_tostring(enum closing_reasion reasion); const char *session_state_tostring(enum session_state state); const char *session_type_tostring(enum session_type type); const char *session_dir_tostring(enum session_dir dir); void session_dump(struct session *sess); #ifdef __cpluscplus } #endif #endif