131 lines
4.0 KiB
C
131 lines
4.0 KiB
C
|
|
#ifndef _SESSION_H
|
||
|
|
#define _SESSION_H
|
||
|
|
|
||
|
|
#ifdef __cpluscplus
|
||
|
|
extern "C"
|
||
|
|
{
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
enum session_state
|
||
|
|
{
|
||
|
|
SESSION_STATE_OPENING,
|
||
|
|
SESSION_STATE_ACTIVE,
|
||
|
|
SESSION_STATE_DISCARD,
|
||
|
|
SESSION_STATE_CLOSING,
|
||
|
|
SESSION_STATE_CLOSED,
|
||
|
|
SESSION_STATE_FREE,
|
||
|
|
};
|
||
|
|
|
||
|
|
enum session_type
|
||
|
|
{
|
||
|
|
SESSION_TYPE_TCP,
|
||
|
|
SESSION_TYPE_TCP_STREAM, // A TCP stream session is derived from the original TCP session
|
||
|
|
SESSION_TYPE_UDP,
|
||
|
|
};
|
||
|
|
|
||
|
|
enum session_event
|
||
|
|
{
|
||
|
|
SESSION_EVENT_NONE = 0,
|
||
|
|
SESSION_EVENT_OPEN,
|
||
|
|
SESSION_EVENT_DATA,
|
||
|
|
SESSION_EVENT_CLOSE,
|
||
|
|
SESSION_EVENT_TIMEOUT,
|
||
|
|
|
||
|
|
// Add new event before SESSION_EVENT_MAX
|
||
|
|
|
||
|
|
SESSION_EVENT_MAX,
|
||
|
|
};
|
||
|
|
|
||
|
|
struct metadata
|
||
|
|
{
|
||
|
|
char data[64]; // TODO
|
||
|
|
};
|
||
|
|
|
||
|
|
struct session;
|
||
|
|
|
||
|
|
/******************************************************************************
|
||
|
|
* session
|
||
|
|
******************************************************************************/
|
||
|
|
|
||
|
|
void session_init(struct session *sess);
|
||
|
|
|
||
|
|
// session id
|
||
|
|
void session_set_id(struct session *sess, uint64_t id);
|
||
|
|
uint64_t session_get_id(struct session *sess);
|
||
|
|
|
||
|
|
// session address
|
||
|
|
void session_set_address(struct session *sess, struct session_address *addr);
|
||
|
|
struct session_address *session_get0_address(struct session *sess);
|
||
|
|
|
||
|
|
// session state
|
||
|
|
void session_set_state(struct session *sess, enum session_state state);
|
||
|
|
enum session_state session_get_state(struct session *sess);
|
||
|
|
|
||
|
|
// session type
|
||
|
|
void session_set_type(struct session *sess, enum session_type type);
|
||
|
|
enum session_type session_get_type(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(struct session *sess);
|
||
|
|
uint64_t session_get_s2c_bytes(struct session *sess);
|
||
|
|
uint64_t session_get_c2s_packets(struct session *sess);
|
||
|
|
uint64_t session_get_s2c_packets(struct session *sess);
|
||
|
|
|
||
|
|
// session metadata
|
||
|
|
void session_set_c2s_1st_md(struct session *sess, struct metadata *md);
|
||
|
|
void session_set_s2c_1st_md(struct session *sess, struct metadata *md);
|
||
|
|
struct metadata *session_get0_c2s_1st_md(struct session *sess);
|
||
|
|
struct metadata *session_get0_s2c_1st_md(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);
|
||
|
|
void session_set_expire_time(struct session *sess, uint64_t timestamp);
|
||
|
|
uint64_t session_get_create_time(struct session *sess);
|
||
|
|
uint64_t session_get_last_time(struct session *sess);
|
||
|
|
uint64_t session_get_expire_time(struct session *sess);
|
||
|
|
|
||
|
|
// session event
|
||
|
|
bool session_push_event(struct session *sess, uint32_t event);
|
||
|
|
bool session_pop_event(struct session *sess, uint32_t *event);
|
||
|
|
|
||
|
|
/******************************************************************************
|
||
|
|
* 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(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);
|
||
|
|
|
||
|
|
#ifdef __cpluscplus
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#endif
|