Пример #1
0
int main( int argc, char** argv )
{
  auto args = check_program_arguments( argc, argv );
  std::string host      = std::move( std::get< HOST      >( args ) );
  std::string port      = std::move( std::get< PORT      >( args ) );
  std::string directory = std::move( std::get< DIRECTORY >( args ) );

  daemonization();

  std::ofstream daemon_log( "log.txt", std::ios::app );

  log( daemon_log, "host: "      + host );
  log( daemon_log, "port: "      + port );
  log( daemon_log, "directory: " + directory );

  int master = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  if ( master == -1 )
    log_and_exit( daemon_log, "socket");

  struct sockaddr_in addr;
  memset( &addr, 0, sizeof( addr ) );
  addr.sin_family = AF_INET;
  addr.sin_port = htons( ( uint16_t )std::stoul( port ) );
  //addr.sin_addr.s_addr = htonl( inet_addr( host.c_str() ) );
  addr.sin_addr.s_addr = htonl( INADDR_ANY );

  if ( bind( master, ( sockaddr* )&addr, sizeof( addr ) ) == -1 )
    return 0; // hack
    //log_and_exit( daemon_log, "bind");

  int optval = 1;
  if ( setsockopt( master, SOL_SOCKET, SO_REUSEADDR, &optval,
    sizeof( optval ) ) == -1 )
    log_and_exit( daemon_log, "setsockopt");

  if ( listen( master, SOMAXCONN ) == -1 )
    return 0; // hack
    //log_and_exit( daemon_log, "listen");

  int slave = 0;

  while ( slave = accept( master, nullptr, 0) )
  {
    if ( slave == -1 )
      log_and_exit( daemon_log, "accept");

    std::thread worker{ &processing_request, slave, std::ref( directory ),
      std::ref( daemon_log ) };

    worker.detach();
  }

  return EXIT_SUCCESS;
}
Пример #2
0
int visual_error_raise (int error)
{
	if (error_handler != NULL) {
		return error_handler (error, error_handler_priv);
	} else {
		return log_and_exit (error);
	}
}
Пример #3
0
/*
          .       .         
          \`-"'"-'/
           } 6 6 {       
          =.  Y  ,=   
        (""-'***`-"")    ------ Main ------
         `-/     \-'            
          (  )-(  )===' 
           ""   ""    
*/
int main(int argc, char **argv)
{
  /* Initialize the XPRP process */
  if (xpr_init(argc,argv) == FAILURE)
  {
    log_and_exit(XPRMOD,
           (char *)"Process %s, server %s: Unable to initalize, exiting ...",
           arb_get_process_id(), Server_id);

    if(ModLog)  {delete ModLog;  ModLog  = NULL;}
    if(ModConn) {delete ModConn; ModConn = NULL;}
    module_std_terminate(NULL);
    return 0;
  }
  
  proc_loop();
  
  ModLog->batchLog(XPRMOD, XPR_END_MSG, arb_get_current_state());
  
  proc_wrap_up();
  fflush(stdout);
  fflush(stderr);
  return 0;
}