refactor: change session key from address list to six tuple

This commit is contained in:
luwenpeng
2023-12-14 15:23:13 +08:00
parent 5620ac211b
commit a232045764
14 changed files with 773 additions and 563 deletions

View File

@@ -1,12 +1,13 @@
#include <assert.h>
#define HASH_FUNCTION(keyptr, keylen, hashv) HASH_FUNCTION_OVERWRITE(keyptr, keylen, &hashv)
#define HASH_KEYCMP(a, b, len) HASH_KEYCMP_OVERWRITE(a, b, len)
#include "session_table.h"
#include "session_private.h"
struct session_table
{
struct session *root_id;
struct session *root_addr;
struct session *root;
session_free_cb free_cb;
void *arg;
uint64_t count;
@@ -15,6 +16,35 @@ struct session_table
struct session *newest_ptr;
};
/******************************************************************************
* Private API
******************************************************************************/
static void HASH_FUNCTION_OVERWRITE(const struct session_key *key, unsigned int keylen, uint32_t *hashv)
{
*hashv = session_key_hash(key);
}
static int HASH_KEYCMP_OVERWRITE(const void *key1, const void *key2, size_t len)
{
struct session_key *sess_key1 = (struct session_key *)key1;
struct session_key *sess_key2 = (struct session_key *)key2;
if (session_key_cmp(sess_key1, sess_key2) == 0)
{
return 0;
}
struct session_key reverse_key;
session_key_reverse(sess_key2, &reverse_key);
if (session_key_cmp(sess_key1, &reverse_key) == 0)
{
return 0;
}
return -1;
}
static void session_table_add_session_to_linklist(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
@@ -69,6 +99,10 @@ static void session_table_del_session_from_linklist(struct session_table *table,
sess->next_ptr = NULL;
}
/******************************************************************************
* Public API
******************************************************************************/
struct session_table *session_table_create()
{
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
@@ -85,17 +119,14 @@ void session_table_destroy(struct session_table *table)
{
struct session *node = NULL;
struct session *tmp = NULL;
HASH_ITER(hh1, table->root_id, node, tmp)
HASH_ITER(hh, table->root, node, tmp)
{
HASH_DELETE(hh1, table->root_id, node);
HASH_DELETE(hh2, table->root_addr, node);
HASH_DELETE(hh, table->root, node);
if (table->free_cb && node)
{
table->free_cb(node, table->arg);
}
}
table->count--;
free(table);
@@ -122,86 +153,48 @@ void session_table_set_freecb(struct session_table *table, session_free_cb free_
}
}
int session_table_add_session(struct session_table *table, struct session *sess)
int session_table_add_session(struct session_table *table, const struct session_key *key, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return -1;
}
if (session_table_find_session_by_id(table, sess->id) || session_table_find_session_by_addr(table, &sess->addr))
if (session_table_find_session(table, key))
{
return -1;
}
if (session_address_selfcmp(&sess->addr) > 0)
{
session_address_reverse(&sess->addr);
}
HASH_ADD(hh1, table->root_id, id, sizeof(sess->id), sess);
HASH_ADD(hh2, table->root_addr, addr, sizeof(sess->addr), sess);
HASH_ADD(hh, table->root, key, sizeof(sess->key), 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)
void session_table_delete_session(struct session_table *table, const struct session_key *key)
{
if (table == NULL)
{
return;
}
struct session *sess = session_table_find_session_by_id(table, id);
struct session *sess = session_table_find_session(table, key);
if (sess == NULL)
{
return;
}
HASH_DELETE(hh1, table->root_id, sess);
HASH_DELETE(hh2, table->root_addr, sess);
HASH_DELETE(hh, table->root, sess);
if (table->free_cb && sess)
{
table->free_cb(sess, table->arg);
}
session_table_del_session_from_linklist(table, sess);
table->count--;
}
void session_table_delete_session_by_addr(struct session_table *table, struct session_address *addr)
{
if (table == NULL)
{
return;
}
struct session *sess = session_table_find_session_by_addr(table, addr);
if (sess == NULL)
{
return;
}
HASH_DELETE(hh1, table->root_id, sess);
HASH_DELETE(hh2, table->root_addr, sess);
if (table->free_cb && sess)
{
table->free_cb(sess, table->arg);
}
session_table_del_session_from_linklist(table, sess);
table->count--;
}
struct session *session_table_find_session_by_id(struct session_table *table, uint64_t id)
struct session *session_table_find_session(struct session_table *table, const struct session_key *key)
{
if (table == NULL)
{
@@ -209,25 +202,7 @@ struct session *session_table_find_session_by_id(struct session_table *table, ui
}
struct session *sess = NULL;
HASH_FIND(hh1, table->root_id, &id, sizeof(id), sess);
return sess;
}
struct session *session_table_find_session_by_addr(struct session_table *table, struct session_address *addr)
{
if (table == NULL)
{
return NULL;
}
if (session_address_selfcmp(addr) > 0)
{
session_address_reverse(addr);
}
struct session *sess = NULL;
HASH_FIND(hh2, table->root_addr, addr, sizeof(struct session_address), sess);
HASH_FIND(hh, table->root, key, sizeof(struct session_key), sess);
return sess;
}