session table support get oldest/newest session

This commit is contained in:
luwenpeng
2023-12-13 17:56:27 +08:00
parent fc5b5857b8
commit 92e5110503
7 changed files with 200 additions and 26 deletions

View File

@@ -10,12 +10,71 @@ struct session_table
session_free_cb free_cb;
void *arg;
uint64_t count;
struct session *oldest_ptr;
struct session *newest_ptr;
};
static void session_table_add_session_to_linklist(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
if (table->newest_ptr == NULL)
{
table->oldest_ptr = sess;
table->newest_ptr = sess;
sess->prev_ptr = NULL;
sess->next_ptr = NULL;
}
else
{
sess->next_ptr = table->newest_ptr;
table->newest_ptr->prev_ptr = sess;
sess->prev_ptr = NULL;
table->newest_ptr = sess;
}
}
static void session_table_del_session_from_linklist(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
if (sess->prev_ptr == NULL && sess->next_ptr == NULL)
{
table->oldest_ptr = NULL;
table->newest_ptr = NULL;
}
else if (sess->prev_ptr == NULL && sess->next_ptr != NULL)
{
table->newest_ptr = sess->next_ptr;
sess->next_ptr->prev_ptr = NULL;
}
else if (sess->prev_ptr != NULL && sess->next_ptr == NULL)
{
table->oldest_ptr = sess->prev_ptr;
sess->prev_ptr->next_ptr = NULL;
}
else
{
sess->prev_ptr->next_ptr = sess->next_ptr;
sess->next_ptr->prev_ptr = sess->prev_ptr;
}
sess->prev_ptr = NULL;
sess->next_ptr = NULL;
}
struct session_table *session_table_create()
{
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
table->count = 0;
table->oldest_ptr = NULL;
table->newest_ptr = NULL;
return table;
}
@@ -63,11 +122,16 @@ void session_table_set_freecb(struct session_table *table, session_free_cb free_
}
}
void session_table_add_session(struct session_table *table, struct session *sess)
int session_table_add_session(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
return -1;
}
if (session_table_find_session_by_id(table, sess->id) || session_table_find_session_by_addr(table, &sess->addr))
{
return -1;
}
if (session_address_selfcmp(&sess->addr) > 0)
@@ -78,7 +142,11 @@ void session_table_add_session(struct session_table *table, struct session *sess
HASH_ADD(hh1, table->root_id, id, sizeof(sess->id), sess);
HASH_ADD(hh2, table->root_addr, addr, sizeof(sess->addr), sess);
session_table_add_session_to_linklist(table, sess);
table->count++;
return 0;
}
void session_table_delete_session_by_id(struct session_table *table, uint64_t id)
@@ -102,6 +170,8 @@ void session_table_delete_session_by_id(struct session_table *table, uint64_t id
table->free_cb(sess, table->arg);
}
session_table_del_session_from_linklist(table, sess);
table->count--;
}
@@ -126,6 +196,8 @@ void session_table_delete_session_by_addr(struct session_table *table, struct se
table->free_cb(sess, table->arg);
}
session_table_del_session_from_linklist(table, sess);
table->count--;
}
@@ -159,3 +231,23 @@ struct session *session_table_find_session_by_addr(struct session_table *table,
return sess;
}
struct session *session_table_find_oldest_session(struct session_table *table)
{
if (table == NULL)
{
return NULL;
}
return table->oldest_ptr;
}
struct session *session_table_find_newest_session(struct session_table *table)
{
if (table == NULL)
{
return NULL;
}
return table->newest_ptr;
}