aboutsummaryrefslogblamecommitdiff
path: root/src/main.c
blob: 07a3f2c96dca988e5098b9e8bcf8fafacc03a77b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
   
                                                        

                   
                
                   
                  


                      






                                                              

















                                                                                                       

                        
                                
            
                                         


               

                                                


                                 
                                           



                 

                 

           
/**
 * 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;
}