summaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'server.c')
-rw-r--r--server.c65
1 files changed, 57 insertions, 8 deletions
diff --git a/server.c b/server.c
index 8d00dc7..5e35f3e 100644
--- a/server.c
+++ b/server.c
@@ -1,9 +1,16 @@
/* SPDX-License-Identifier: Unlicense */
+#include "debug.h"
#include "proto_io.h"
#include "proto.h"
#include "timespec/timespec.h"
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+#define OPTPARSE_IMPLEMENTATION
+#define OPTPARSE_API static
+#include "optparse/optparse.h"
+#pragma GCC diagnostic pop
#include <assert.h>
#include <errno.h>
@@ -55,7 +62,7 @@ static void ConnectionHandleFrame(
int fd)
{
if (frame->type == FT_SYNACK) {
- if (LOG_TRACE) {
+ if (g_log_level >= LOG_LEVEL_TRACE) {
fprintf(stderr,
"FT_SYNACK(tick=%u, lost=%d, rt_prev=%u, repeat=%u)\n",
frame->syn.tick, frame->syn.prev_is_lost, frame->syn.roundtrip_prev, frame->syn.repeat);
@@ -70,8 +77,10 @@ static void ConnectionHandleFrame(
long const srt_ms = timespec_to_ms(timespec_sub(now, self->last_syn));
assert(srt_ms >= 0);
self->server_roundtrip_prev = srt_ms;
- fprintf(stderr, "%5" PRIu32 " server_rt %5lu ms, client_rt %5" PRIu32 " ms\n",
- self->tick, (unsigned long)srt_ms, frame->syn.roundtrip_prev);
+ if (g_log_level >= LOG_LEVEL_DEBUG) {
+ fprintf(stderr, "%5" PRIu32 " server_rt %5lu ms, client_rt %5" PRIu32 " ms\n",
+ self->tick, (unsigned long)srt_ms, frame->syn.roundtrip_prev);
+ }
self->tick++;
}
}
@@ -99,7 +108,7 @@ static void HandleFrame(struct frame const *frame, struct timespec now, int fd)
return;
}
if (frame->type == FT_HANDSHAKE) {
- if (LOG_DEBUG) {
+ if (g_log_level >= LOG_LEVEL_DEBUG) {
fprintf(stderr, "FT_HANDSHAKE(tick_period=%" PRIu32 ")\n", frame->handshake.tick_period);
}
struct frame outgoing = *frame;
@@ -124,7 +133,7 @@ static void HandleFrame(struct frame const *frame, struct timespec now, int fd)
outgoing.type = FT_HANDSHAKE_RESP;
outgoing.connection_id = i;
SendFrame(fd, &outgoing);
- if (LOG_DEBUG) {
+ if (g_log_level >= LOG_LEVEL_DEBUG) {
fprintf(stderr, "New connection id=%" PRIu32 "\n", connection_id);
}
return;
@@ -139,14 +148,52 @@ static void HandleFrame(struct frame const *frame, struct timespec now, int fd)
}
if (frame->type == FT_RST) {
g_connections[connection_id].exist = false;
- if (LOG_DEBUG) {
+ if (g_log_level >= LOG_LEVEL_DEBUG) {
fprintf(stderr, "Connection closed id=%" PRIu32 "\n", connection_id);
}
}
}
-int main(void)
+static void PrintUsage(FILE *s, const char *argv0)
+{
+ // Please, keep all lines in 80 columns range when printed.
+ fprintf(s,
+ "Usage: %s [options]\n"
+ "Options:\n"
+ " -h, --help, Show this message.\n"
+ " -d, --debug-level=<level>,\n"
+ " Set id of debug log level: 0 means no logs, \n"
+ " 1 means info, 2 means some debug,\n"
+ " 3 is for maximum trace\n"
+ , argv0);
+}
+
+int main(int argc, char *argv[])
{
+ (void) argc;
+ struct optparse_long longopts[] = {
+ {"help", 'h', OPTPARSE_NONE},
+ {"debug-level", 'd', OPTPARSE_REQUIRED},
+ {0},
+ };
+ struct optparse options;
+ optparse_init(&options, argv);
+ // Parse opts
+ int option;
+ while ((option = optparse_long(&options, longopts, NULL)) != -1) {
+ switch (option) {
+ case 'h':
+ PrintUsage(stdout, argv[0]);
+ return EXIT_SUCCESS;
+ break;
+ case 'd':
+ g_log_level = atoi(options.optarg);
+ break;
+ case '?':
+ fprintf(stderr, "main: optparse_long: Error: \"%s\"\n", options.errmsg);
+ return EXIT_FAILURE;
+ }
+ }
int const fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket failed");
@@ -173,7 +220,9 @@ int main(void)
uint8_t const a1 = addr >> 16;
uint8_t const a2 = addr >> 8;
uint8_t const a3 = (uint8_t)addr;
- fprintf(stderr, "Server is listening on %u.%u.%u.%u:%u\n", a0, a1, a2, a3, port);
+ if (g_log_level >= LOG_LEVEL_INFO) {
+ fprintf(stderr, "Server is listening on %u.%u.%u.%u:%u\n", a0, a1, a2, a3, port);
+ }
}
struct pollfd fds[1] = { { .fd = fd, .events = POLLIN, }, };
int timeout_ms = 10000u;