aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 07a3f2c96dca988e5098b9e8bcf8fafacc03a77b (plain) (blame)
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
49
50
51
52
53
54
55
56
57
/**
 * Initializes main game loop and hands off to listeners
 * */
#include "server.h"
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <libconfig.h>

config_t cfg;

/**
 * Server entrypoint
 *
 * sets up everything needed to handle sockets and connections
 * */
int main() {

  debug("Initializing config...");
  config_init(&cfg);
  if (config_read_file(&cfg, "config.cfg") != CONFIG_TRUE)  {
    error("Could not read config:");
    printf("%s (%d): %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));

    exit(-1);
  }
  debug("Done");

  const char* name;
  if (!config_lookup_string(&cfg, "name", &name)) {
    error("Invalid config");
    exit(-1);
  }

  info("Starting up %s game server", name);
  int err = 0;
  server_t server = {0};
  err = server_listen(&server); 
  if (err) {
    error("Failed to listen on address");
    return err;
  }

  info("Listening and waiting for connections");

  for (;;) {
    err = server_accept(&server);
    if (err) {
      error("Failed accepting connection");
      return err;
    }
  }

  closeServer(0);

  return 0;
}