Beispiel #1
0
void checkHttpServer( void )
{
  fd_set rfds;
  struct timeval tv;
  int ret = -1;
  int connfd;
  socklen_t clilen;
  struct sockaddr_in cliaddr;

  /* We're listening only on our server fd (listenfd) */
  FD_ZERO( &rfds );
  FD_SET( listenfd, &rfds );

  /* Set up a one second timeout */
  tv.tv_sec = 1;
  tv.tv_usec = 0;

  /* Call select.  This function will await until a read event is available
   * on the defined sockets (one in this case) or our timeout period has
   * expired.
   */
  ret = select( listenfd+1, &rfds, NULL, NULL, &tv );

  if (ret > 0) {

    /* Check to see if our read event was found.  Note that in this case
     * we're using the read event as an indication of a new socket on
     * our listening socket (no new data is available).
     */
    if (FD_ISSET(listenfd, &rfds)) {

      /* Accept the connection and respond to it with handleConnection. */
      clilen = sizeof(cliaddr);
      connfd = accept( listenfd, (struct sockaddr *)&cliaddr, &clilen );
      if (connfd > 0) {
        handleConnection( connfd );
        close( connfd );
      }

    } else {

      /* Some kind of error, reinitialize... */
      initHttpServer();

    }

  } else if (ret < 0) {

      /* Some kind of error, reinitialize... */
      initHttpServer();

  } else {

    // timeout -- no action.

  }

  return;
}
Beispiel #2
0
int			main(int argc, char** argv)
{
	// Make current directory=path to execuable
	char *p = strrchr(argv[0], '/');
	if (p != NULL)
	{
		*p = '\0';
		chdir(argv[0]);
	}

	const char	*config_file = "config/config.yml";
	const char	*name = NULL;

	int c;
	while ((c = getopt(argc, argv, ":c:n:hv")) != -1)
	{
		switch (c)
		{
			case 'c':
				config_file = optarg;
				break;
			case 'n':
				name = optarg;
				break;
			case 'h':
				showHelp();
				return 0;
			case 'v':
				printf( "SAYAN server - ultra-fast, modular and super-lightweight web server\n"
						"version      : sayan/" VERSION "\n"
						"build-date   : " __DATE__ " " __TIME__ "\n"
						"project page : https://jacob-zak/projets/sayan-server/\n"
					  );
				return 0;
			case '?':
				fprintf(stderr, "unkown option: -%c\n\n", optopt);
				showHelp();
				return EXIT_FAILURE;
		}
	}

	if ((argc - optind) > 0)
	{
		fprintf(stderr, "too many arguments\n\n");
		showHelp();
		return EXIT_FAILURE;
	}
	/* if (name != NULL) */
	/* 	createNewProject(name); */

	PRINT("name, %s", name);

	// Load config file
	loadConfig("config/config.yml");
	/* const Config *config = getConfig(); */
	/* PRINT("Config %d", config->port); */

	// Init cache
	initHttpCache(false);

	// openLog(getConfig()->config.log_file, WARNING);

	// Run server
	/* int procs=get_nprocs(); */
	int procs=2;
	HttpServer *server=initHttpServer(procs, 1024, procs);
	if(getConfig()->isDaemon)
		runDaemonServer(server);
	else
		runNormalServer(server);

	// Cleanup
	destroyHttpServer(server);
	destroyHttpCache();

	return (EXIT_SUCCESS);
}