-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_configuration.c
More file actions
48 lines (41 loc) · 1.42 KB
/
Copy pathsocket_configuration.c
File metadata and controls
48 lines (41 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "socket_configuration.h"
#include <libconfig.h>
static char config_file[256] = "socket_configuration.conf";
static int tcp_no_delay = 1; // enable TCP_NODELAY by default
const char *socket_get_config_file(void) {
return config_file;
}
int socket_get_tcp_no_delay(void) {
return tcp_no_delay;
}
void load_socket_from_file(void) {
config_t config;
config_init(&config);
if (config_read_file(&config, config_file)) {
int tmp_tcp_no_delay;
if (config_lookup_bool(&config, "tcp_no_delay", &tmp_tcp_no_delay)) {
tcp_no_delay = tmp_tcp_no_delay;
}
printf("Loaded config file: %s\n", config_file);
} else {
fprintf(stderr, "Failed to load file %s: %s:%d - %s\n",
config_file,
config_error_file(&config),
config_error_line(&config),
config_error_text(&config));
}
config_destroy(&config);
}
void save_socket_to_file(void) {
config_t config;
config_init(&config);
config_setting_t *root = config_root_setting(&config);
config_setting_t *setting = config_setting_add(root, "tcp_no_delay", CONFIG_TYPE_BOOL);
config_setting_set_bool(setting, tcp_no_delay);
if (!config_write_file(&config, config_file)) {
fprintf(stderr, "Failed to write file %s\n", config_file);
} else {
printf("Saved configuration: %s\n", config_file);
}
config_destroy(&config);
}