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

155 lines
3.7 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <string.h>
2024-04-10 17:50:51 +08:00
#include "marsio_io.h"
#include "dumpfile_io.h"
struct packet_io
{
2024-04-10 17:50:51 +08:00
enum packet_io_mode mode;
struct marsio_io *marsio;
struct dumpfile_io *dumpfile;
};
2024-03-08 14:51:21 +08:00
struct packet_io *packet_io_new(struct packet_io_options *opts)
{
2024-04-10 17:50:51 +08:00
struct packet_io *packet_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
if (packet_io == NULL)
{
return NULL;
}
2024-04-10 17:50:51 +08:00
packet_io->mode = opts->mode;
2024-03-08 14:51:21 +08:00
if (opts->mode == PACKET_IO_MARSIO)
{
packet_io->marsio = marsio_io_new(opts->app_symbol, opts->dev_symbol, opts->cpu_mask, opts->nr_worker_thread);
}
else
{
packet_io->dumpfile = dumpfile_io_new(opts->dumpfile_path, packet_io->mode, opts->nr_worker_thread);
}
2024-04-10 17:50:51 +08:00
if (packet_io->marsio == NULL && packet_io->dumpfile == NULL)
{
goto error_out;
}
2024-04-10 17:50:51 +08:00
return packet_io;
error_out:
2024-04-10 17:50:51 +08:00
packet_io_free(packet_io);
return NULL;
}
2024-04-10 17:50:51 +08:00
void packet_io_free(struct packet_io *packet_io)
{
2024-04-10 17:50:51 +08:00
if (packet_io)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
2024-04-10 17:50:51 +08:00
{
marsio_io_free(packet_io->marsio);
}
else
{
dumpfile_io_free(packet_io->dumpfile);
}
free(packet_io);
packet_io = NULL;
}
}
int packet_io_isbreak(struct packet_io *packet_io) // used for dumpfile mode
2024-05-15 11:40:00 +08:00
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
{
return 0;
}
else
{
return dumpfile_io_isbreak(packet_io->dumpfile);
2024-05-15 11:40:00 +08:00
}
}
2024-04-10 17:50:51 +08:00
int packet_io_init(struct packet_io *packet_io, uint16_t thr_idx)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
2024-04-10 17:50:51 +08:00
{
return marsio_io_init(packet_io->marsio, thr_idx);
}
else
{
return dumpfile_io_init(packet_io->dumpfile, thr_idx);
}
}
uint16_t packet_io_input(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
2024-04-10 17:50:51 +08:00
{
return marsio_io_input(packet_io->marsio, thr_idx, pkts, nr_pkts);
2024-04-10 17:50:51 +08:00
}
else
{
return dumpfile_io_input(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
2024-04-10 17:50:51 +08:00
}
}
void packet_io_output(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
2024-04-10 17:50:51 +08:00
{
marsio_io_output(packet_io->marsio, thr_idx, pkts, nr_pkts);
2024-04-10 17:50:51 +08:00
}
else
{
dumpfile_io_output(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
2024-04-10 17:50:51 +08:00
}
}
void packet_io_drop(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
2024-04-10 17:50:51 +08:00
{
marsio_io_drop(packet_io->marsio, thr_idx, pkts, nr_pkts);
}
else
{
dumpfile_io_drop(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
}
}
uint16_t packet_io_inject(struct packet_io *packet_io, uint16_t thr_idx, struct packet *pkts, uint16_t nr_pkts)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
{
return marsio_io_inject(packet_io->marsio, thr_idx, pkts, nr_pkts);
}
else
{
return dumpfile_io_inject(packet_io->dumpfile, thr_idx, pkts, nr_pkts);
}
}
void packet_io_yield(struct packet_io *packet_io, uint16_t thr_idx, uint64_t timeout_ms)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
{
marsio_io_yield(packet_io->marsio, thr_idx, timeout_ms);
}
else
{
dumpfile_io_yield(packet_io->dumpfile, thr_idx, timeout_ms);
}
}
2024-06-25 14:08:33 +08:00
struct packet_io_stat *packet_io_stat(struct packet_io *packet_io, uint16_t thr_idx)
{
if (likely(packet_io->mode == PACKET_IO_MARSIO))
{
return marsio_io_stat(packet_io->marsio, thr_idx);
}
else
{
return dumpfile_io_stat(packet_io->dumpfile, thr_idx);
}
}