summaryrefslogtreecommitdiff
path: root/infra/packet_io
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2024-09-20 18:41:07 +0800
committerluwenpeng <[email protected]>2024-09-20 18:41:32 +0800
commitb3ddebf7704a624ccfb41607f35f7a38732ff38a (patch)
tree22b20aee6510271726ced13eb42af7854ac1d1f0 /infra/packet_io
parent94f1913e3e4cabee1d93d6de446f489dcf45ca62 (diff)
refactor(main loop): compiled
Diffstat (limited to 'infra/packet_io')
-rw-r--r--infra/packet_io/packet_io.c54
-rw-r--r--infra/packet_io/packet_io.h6
2 files changed, 31 insertions, 29 deletions
diff --git a/infra/packet_io/packet_io.c b/infra/packet_io/packet_io.c
index 673b1ce..70d0599 100644
--- a/infra/packet_io/packet_io.c
+++ b/infra/packet_io/packet_io.c
@@ -12,6 +12,7 @@
struct packet_io
{
+ struct packet_io_config *cfg;
void *handle;
void *(*new_func)(const struct packet_io_config *cfg);
@@ -27,7 +28,7 @@ struct packet_io
struct packet_io_stat *(*stat_func)(void *handle, uint16_t thr_idx);
};
-int packet_io_config_load(struct packet_io_config *cfg, const char *toml_file)
+static struct packet_io_config *packet_io_config_new(const char *toml_file)
{
int ret = -1;
const char *ptr;
@@ -41,6 +42,12 @@ int packet_io_config_load(struct packet_io_config *cfg, const char *toml_file)
toml_table_t *table = NULL;
toml_array_t *mask;
+ struct packet_io_config *cfg = (struct packet_io_config *)calloc(1, sizeof(struct packet_io_config));
+ if (cfg == NULL)
+ {
+ return NULL;
+ }
+
fp = fopen(toml_file, "r");
if (fp == NULL)
{
@@ -185,32 +192,18 @@ error_out:
fclose(fp);
}
- return ret;
-}
-
-struct packet_io_config *packet_io_config_new(const char *toml_file)
-{
- if (toml_file == NULL)
- {
- return NULL;
- }
-
- struct packet_io_config *cfg = (struct packet_io_config *)calloc(1, sizeof(struct packet_io_config));
- if (cfg == NULL)
+ if (ret == -1)
{
+ free(cfg);
return NULL;
}
-
- if (packet_io_config_load(cfg, toml_file) == -1)
+ else
{
- packet_io_config_free(cfg);
- return NULL;
+ return cfg;
}
-
- return cfg;
}
-void packet_io_config_free(struct packet_io_config *cfg)
+static void packet_io_config_free(struct packet_io_config *cfg)
{
if (cfg)
{
@@ -219,7 +212,7 @@ void packet_io_config_free(struct packet_io_config *cfg)
}
}
-void packet_io_config_print(const struct packet_io_config *cfg)
+static void packet_io_config_print(const struct packet_io_config *cfg)
{
if (cfg)
{
@@ -242,7 +235,7 @@ void packet_io_config_print(const struct packet_io_config *cfg)
}
}
-struct packet_io *packet_io_new(const struct packet_io_config *cfg)
+struct packet_io *packet_io_new(const char *toml_file)
{
struct packet_io *pkt_io = (struct packet_io *)calloc(1, sizeof(struct packet_io));
if (pkt_io == NULL)
@@ -250,7 +243,16 @@ struct packet_io *packet_io_new(const struct packet_io_config *cfg)
return NULL;
}
- if (cfg->mode == PACKET_IO_MARSIO)
+ pkt_io->cfg = packet_io_config_new(toml_file);
+ if (pkt_io->cfg == NULL)
+ {
+ free(pkt_io);
+ return NULL;
+ }
+
+ packet_io_config_print(pkt_io->cfg);
+
+ if (pkt_io->cfg->mode == PACKET_IO_MARSIO)
{
pkt_io->new_func = marsio_io_new;
pkt_io->free_func = marsio_io_free;
@@ -277,7 +279,7 @@ struct packet_io *packet_io_new(const struct packet_io_config *cfg)
pkt_io->stat_func = pcap_io_stat;
}
- pkt_io->handle = pkt_io->new_func(cfg);
+ pkt_io->handle = pkt_io->new_func(pkt_io->cfg);
if (pkt_io->handle == NULL)
{
packet_io_free(pkt_io);
@@ -295,6 +297,10 @@ void packet_io_free(struct packet_io *pkt_io)
{
pkt_io->free_func(pkt_io->handle);
}
+ if (pkt_io->cfg)
+ {
+ packet_io_config_free(pkt_io->cfg);
+ }
free(pkt_io);
pkt_io = NULL;
}
diff --git a/infra/packet_io/packet_io.h b/infra/packet_io/packet_io.h
index 5b677e8..d5b089b 100644
--- a/infra/packet_io/packet_io.h
+++ b/infra/packet_io/packet_io.h
@@ -64,13 +64,9 @@ struct packet_io_config
uint64_t idle_yield_interval_ms; // range: [0, 6000] (ms)
};
-struct packet_io_config *packet_io_config_new(const char *toml_file);
-void packet_io_config_free(struct packet_io_config *cfg);
-void packet_io_config_print(const struct packet_io_config *cfg);
-
struct packet;
struct packet_io;
-struct packet_io *packet_io_new(const struct packet_io_config *cfg);
+struct packet_io *packet_io_new(const char *toml_file);
void packet_io_free(struct packet_io *pkt_io);
int packet_io_isbreak(struct packet_io *pkt_io);