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/infra/packet_io/packet_io.c

121 lines
3.4 KiB
C
Raw Normal View History

#include "pcap_io.h"
#include "mars_io.h"
#include "utils_internal.h"
struct packet_io
{
void *handle;
void *(*new_func)(const char *toml_file);
void (*free_func)(void *handle);
int (*isbreak_func)(void *handle);
int (*init_func)(void *handle, uint16_t thr_idx);
int (*recv_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
void (*send_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
void (*drop_func)(void *handle, uint16_t thr_idx, struct packet *pkts[], int nr_pkts);
void (*yield_func)(void *handle, uint16_t thr_idx);
void (*polling_func)(void *handle, uint16_t thr_idx);
struct packet_io_stat *(*stat_func)(void *handle, uint16_t thr_idx);
};
2024-09-20 18:41:07 +08:00
struct packet_io *packet_io_new(const char *toml_file)
{
char mode[64] = {0};
struct packet_io *pkt_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
if (pkt_io == NULL)
{
return NULL;
}
load_toml_str_config(toml_file, "packet_io.mode", mode);
if (strcmp(mode, "marsio") == 0)
2024-09-20 18:41:07 +08:00
{
pkt_io->new_func = mars_io_new;
pkt_io->free_func = mars_io_free;
pkt_io->isbreak_func = mars_io_isbreak;
pkt_io->init_func = mars_io_init;
pkt_io->recv_func = mars_io_recv;
pkt_io->send_func = mars_io_send;
pkt_io->drop_func = mars_io_drop;
pkt_io->yield_func = mars_io_yield;
pkt_io->polling_func = mars_io_polling;
pkt_io->stat_func = mars_io_stat;
}
else
{
pkt_io->new_func = pcap_io_new;
pkt_io->free_func = pcap_io_free;
pkt_io->isbreak_func = pcap_io_isbreak;
pkt_io->init_func = pcap_io_init;
pkt_io->recv_func = pcap_io_recv;
pkt_io->send_func = pcap_io_send;
pkt_io->drop_func = pcap_io_drop;
pkt_io->yield_func = pcap_io_yield;
pkt_io->polling_func = pcap_io_polling;
pkt_io->stat_func = pcap_io_stat;
}
pkt_io->handle = pkt_io->new_func(toml_file);
if (pkt_io->handle == NULL)
{
packet_io_free(pkt_io);
return NULL;
}
return pkt_io;
}
void packet_io_free(struct packet_io *pkt_io)
{
if (pkt_io)
{
if (pkt_io->handle)
{
pkt_io->free_func(pkt_io->handle);
}
free(pkt_io);
pkt_io = NULL;
}
}
int packet_io_isbreak(struct packet_io *pkt_io)
{
return pkt_io->isbreak_func(pkt_io->handle);
}
int packet_io_init(struct packet_io *pkt_io, uint16_t thr_idx)
{
return pkt_io->init_func(pkt_io->handle, thr_idx);
}
int packet_io_recv(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
{
return pkt_io->recv_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
}
void packet_io_send(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
{
pkt_io->send_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
}
void packet_io_drop(struct packet_io *pkt_io, uint16_t thr_idx, struct packet *pkts[], int nr_pkts)
{
pkt_io->drop_func(pkt_io->handle, thr_idx, pkts, nr_pkts);
}
void packet_io_yield(struct packet_io *pkt_io, uint16_t thr_idx)
{
pkt_io->yield_func(pkt_io->handle, thr_idx);
}
void packet_io_polling(struct packet_io *pkt_io, uint16_t thr_idx)
{
pkt_io->polling_func(pkt_io->handle, thr_idx);
}
struct packet_io_stat *packet_io_stat(struct packet_io *pkt_io, uint16_t thr_idx)
{
return pkt_io->stat_func(pkt_io->handle, thr_idx);
}