Пример #1
0
/**
 *
 * @brief Starts sesman main loop
 *
 */
static void DEFAULT_CC
sesman_main_loop(void)
{
  int in_sck;
  int error;

  /*main program loop*/
  log_message(&(g_cfg->log), LOG_LEVEL_INFO, "listening...");
  g_sck = g_tcp_socket();
  g_tcp_set_non_blocking(g_sck);
  error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port);
  if (error == 0)
  {
    error = g_tcp_listen(g_sck);
    if (error == 0)
    {
      in_sck = g_tcp_accept(g_sck);
      while (in_sck == -1 && g_tcp_last_error_would_block(g_sck))
      {
        g_sleep(1000);
        in_sck = g_tcp_accept(g_sck);
      }
      while (in_sck > 0)
      {
        /* we've got a connection, so we pass it to scp code */
        LOG_DBG(&(g_cfg->log), "new connection");
        thread_sck=in_sck;
        //scp_process_start((void*)in_sck);
        thread_scp_start(in_sck);

        /* once we've processed the connection, we go back listening */
        in_sck = g_tcp_accept(g_sck);
        while (in_sck == -1 && g_tcp_last_error_would_block(g_sck))
        {
          g_sleep(1000);
          in_sck = g_tcp_accept(g_sck);
        }
      }
    }
    else
    {
      log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "listen error");
    }
  }
  else
  {
    log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "bind error");
  }
  g_tcp_close(g_sck);
}
Пример #2
0
/**
 *
 * @brief Starts sesman main loop
 *
 */
static void DEFAULT_CC
sesman_main_loop(void)
{
	int in_sck;
	int error;
	int robjs_count;
	int cont;
	tbus sck_obj;
	tbus robjs[8];

	/*main program loop*/
	log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: "
				"listening...");
	g_sck = g_tcp_socket();
	g_tcp_set_non_blocking(g_sck);
	error = scp_tcp_bind(g_sck, g_cfg->listen_address, g_cfg->listen_port);
	if (error == 0)
	{
		error = g_tcp_listen(g_sck);
		if (error == 0)
		{
			sck_obj = g_create_wait_obj_from_socket(g_sck, 0);
			cont = 1;
			while (cont)
			{
				/* build the wait obj list */
				robjs_count = 0;
				robjs[robjs_count++] = sck_obj;
				robjs[robjs_count++] = g_term_event;
				robjs[robjs_count++] = g_sync_event;
				/* wait */
				if (g_obj_wait(robjs, robjs_count, 0, 0, -1) != 0)
				{
					/* error, should not get here */
					g_sleep(100);
				}
				if (g_is_wait_obj_set(g_term_event)) /* term */
				{
					break;
				}
				if (g_is_wait_obj_set(g_sync_event)) /* sync */
				{
					g_reset_wait_obj(g_sync_event);
					session_sync_start();
				}
				if (g_is_wait_obj_set(sck_obj)) /* incomming connection */
				{
					in_sck = g_tcp_accept(g_sck);
					if ((in_sck == -1) && g_tcp_last_error_would_block(g_sck))
					{
						/* should not get here */
						g_sleep(100);
					}
					else if (in_sck == -1)
					{
						/* error, should not get here */
						break;
					}
					else
					{
						/* we've got a connection, so we pass it to scp code */
						log_message(&(g_cfg->log), LOG_LEVEL_INFO, "sesman[sesman_main_loop]: "
								"new connection");
						thread_scp_start(in_sck);
						/* todo, do we have to wait here ? */
					}
				}
			}
			g_delete_wait_obj_from_socket(sck_obj);
		}
		else
		{
			log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "sesman[sesman_main_loop]: "
					"listen error %d (%s)", g_get_errno(), g_get_strerror());
		}
	}
	else
	{
		log_message(&(g_cfg->log), LOG_LEVEL_ERROR, "bind error on "
				"port '%s': %d (%s)", g_cfg->listen_port, g_get_errno(), g_get_strerror());
	}
	g_tcp_close(g_sck);
}