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/packet_io/packet_io.cpp

121 lines
3.9 KiB
C++
Raw Normal View History

#include <stdlib.h>
#include <string.h>
#include "packet_io.h"
#include "packet_io_marsio.h"
#include "packet_io_dumpfile.h"
typedef void *new_cb(void *config);
typedef void free_cb(void *handle);
typedef void *stat_cb(void *handle);
typedef int init_cb(void *handle, uint16_t thread_id);
typedef int recv_cb(void *handle, uint16_t thread_id, struct packet **pkt);
typedef void send_cb(void *handle, uint16_t thread_id, struct packet *pkt);
struct packet_io
{
void *handle;
new_cb *on_new;
free_cb *on_free;
stat_cb *on_stat;
init_cb *on_init;
recv_cb *on_recv;
send_cb *on_send;
};
struct packet_io *packet_io_new(struct packet_io_config *config)
{
struct packet_io *handle = (struct packet_io *)calloc(1, sizeof(struct packet_io));
if (handle == NULL)
{
PACKET_IO_LOG_ERROR("unable to alloc packet io");
return NULL;
}
struct packet_io_marsio_confg marsio_config;
strncpy(marsio_config.app_symbol, config->app_symbol, sizeof(marsio_config.app_symbol));
strncpy(marsio_config.dev_symbol, config->dev_symbol, sizeof(marsio_config.dev_symbol));
memcpy(marsio_config.cpu_mask, config->cpu_mask, sizeof(marsio_config.cpu_mask));
marsio_config.nr_threads = config->nr_threads;
struct packet_io_dumpfile_confg dumpfile_config;
strncpy(dumpfile_config.dumpfile_dir, config->dumpfile_dir, sizeof(dumpfile_config.dumpfile_dir));
dumpfile_config.nr_threads = config->nr_threads;
void *_config = NULL;
if (config->mode == PACKET_IO_MARSIO)
{
_config = &marsio_config;
handle->on_new = (new_cb *)packet_io_marsio_new;
handle->on_free = (free_cb *)packet_io_marsio_free;
handle->on_stat = (stat_cb *)packet_io_marsio_stat;
handle->on_init = (init_cb *)packet_io_marsio_init;
handle->on_recv = (recv_cb *)packet_io_marsio_recv;
handle->on_send = (send_cb *)packet_io_marsio_send;
}
else
{
_config = &dumpfile_config;
handle->on_new = (new_cb *)packet_io_dumpfile_new;
handle->on_free = (free_cb *)packet_io_dumpfile_free;
handle->on_stat = (stat_cb *)packet_io_dumpfile_stat;
handle->on_init = (init_cb *)packet_io_dumpfile_init;
handle->on_recv = (recv_cb *)packet_io_dumpfile_recv;
handle->on_send = (send_cb *)packet_io_dumpfile_send;
}
handle->handle = handle->on_new(_config);
if (handle->handle == NULL)
{
goto error_out;
}
return handle;
error_out:
packet_io_free(handle);
return NULL;
}
void packet_io_free(struct packet_io *handle)
{
if (handle)
{
handle->on_free(handle->handle);
free(handle);
handle = NULL;
}
}
struct packet_io_stat *packet_io_get_stat(struct packet_io *handle)
{
return (struct packet_io_stat *)handle->on_stat(handle->handle);
}
void packet_io_print_stat(struct packet_io *handle)
{
struct packet_io_stat *stat = packet_io_get_stat(handle);
PACKET_IO_LOG_DEBUG("rx_pkts : %lu, rx_bytes : %lu", stat->rx_pkts, stat->rx_bytes);
PACKET_IO_LOG_DEBUG("tx_pkts : %lu, tx_bytes : %lu", stat->tx_pkts, stat->tx_bytes);
PACKET_IO_LOG_DEBUG("drop_pkts : %lu, drop_bytes : %lu", stat->drop_pkts, stat->drop_bytes);
PACKET_IO_LOG_DEBUG("inject_pkts : %lu, inject_bytes : %lu", stat->inject_pkts, stat->inject_bytes);
PACKET_IO_LOG_DEBUG("keepalive_pkts : %lu, keepalive_bytes : %lu", stat->keepalive_pkts, stat->keepalive_bytes);
}
int packet_io_init(struct packet_io *handle, uint16_t thread_id)
{
return handle->on_init(handle->handle, thread_id);
}
int packet_io_recv(struct packet_io *handle, uint16_t thread_id, struct packet **pkt)
{
return handle->on_recv(handle->handle, thread_id, pkt);
}
void packet_io_send(struct packet_io *handle, uint16_t thread_id, struct packet *pkt)
{
handle->on_send(handle->handle, thread_id, pkt);
}