This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/session/session_table.cpp

162 lines
3.2 KiB
C++
Raw Normal View History

2023-12-11 16:35:26 +08:00
#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++;
}
2023-12-13 14:07:24 +08:00
void session_table_delete_session_by_id(struct session_table *table, uint64_t id)
2023-12-11 16:35:26 +08:00
{
if (table == NULL)
{
return;
}
2023-12-13 14:07:24 +08:00
struct session *sess = session_table_find_session_by_id(table, id);
2023-12-11 16:35:26 +08:00
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--;
}
2023-12-13 14:07:24 +08:00
void session_table_delete_session_by_addr(struct session_table *table, struct session_address *addr)
2023-12-11 16:35:26 +08:00
{
if (table == NULL)
{
return;
}
2023-12-13 14:07:24 +08:00
struct session *sess = session_table_find_session_by_addr(table, addr);
2023-12-11 16:35:26 +08:00
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--;
}
2023-12-13 14:07:24 +08:00
struct session *session_table_find_session_by_id(struct session_table *table, uint64_t id)
2023-12-11 16:35:26 +08:00
{
if (table == NULL)
{
return NULL;
}
struct session *sess = NULL;
HASH_FIND(hh1, table->root_id, &id, sizeof(id), sess);
return sess;
}
2023-12-13 14:07:24 +08:00
struct session *session_table_find_session_by_addr(struct session_table *table, struct session_address *addr)
2023-12-11 16:35:26 +08:00
{
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;
}