Add session table & session
This commit is contained in:
161
src/session/session_table.cpp
Normal file
161
src/session/session_table.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <assert.h>
|
||||
|
||||
#include "session_table.h"
|
||||
#include "session_private.h"
|
||||
|
||||
struct session_table
|
||||
{
|
||||
struct session *root_id;
|
||||
struct session *root_addr;
|
||||
session_free_cb free_cb;
|
||||
void *arg;
|
||||
uint64_t count;
|
||||
};
|
||||
|
||||
struct session_table *session_table_create()
|
||||
{
|
||||
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
|
||||
table->count = 0;
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
void session_table_destroy(struct session_table *table)
|
||||
{
|
||||
if (table)
|
||||
{
|
||||
struct session *node = NULL;
|
||||
struct session *tmp = NULL;
|
||||
HASH_ITER(hh1, table->root_id, node, tmp)
|
||||
{
|
||||
HASH_DELETE(hh1, table->root_id, node);
|
||||
HASH_DELETE(hh2, table->root_addr, node);
|
||||
|
||||
if (table->free_cb && node)
|
||||
{
|
||||
table->free_cb(node, table->arg);
|
||||
}
|
||||
}
|
||||
|
||||
table->count--;
|
||||
|
||||
free(table);
|
||||
table = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t session_table_get_count(struct session_table *table)
|
||||
{
|
||||
if (table == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return table->count;
|
||||
}
|
||||
|
||||
void session_table_set_freecb(struct session_table *table, session_free_cb free_cb, void *arg)
|
||||
{
|
||||
if (table)
|
||||
{
|
||||
table->free_cb = free_cb;
|
||||
table->arg = arg;
|
||||
}
|
||||
}
|
||||
|
||||
void session_table_add_session(struct session_table *table, struct session *sess)
|
||||
{
|
||||
if (table == NULL || sess == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
table->count++;
|
||||
}
|
||||
|
||||
void session_table_delete_session_with_id(struct session_table *table, uint64_t id)
|
||||
{
|
||||
if (table == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct session *sess = session_table_search_session_with_id(table, id);
|
||||
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);
|
||||
}
|
||||
|
||||
table->count--;
|
||||
}
|
||||
|
||||
void session_table_delete_session_with_addr(struct session_table *table, struct session_address *addr)
|
||||
{
|
||||
if (table == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct session *sess = session_table_search_session_with_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);
|
||||
}
|
||||
|
||||
table->count--;
|
||||
}
|
||||
|
||||
struct session *session_table_search_session_with_id(struct session_table *table, uint64_t id)
|
||||
{
|
||||
if (table == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct session *sess = NULL;
|
||||
HASH_FIND(hh1, table->root_id, &id, sizeof(id), sess);
|
||||
|
||||
return sess;
|
||||
}
|
||||
|
||||
struct session *session_table_search_session_with_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);
|
||||
|
||||
return sess;
|
||||
}
|
||||
Reference in New Issue
Block a user