Exemplo n.º 1
0
int main(int argc, char* argv[])
{
    Logger* stderr_logger = stderr_logger_create();
    logger_set_next(stderr_logger, stdout_logger_create());

    logger_message(stderr_logger, "information", DEBUG);
    logger_message(stderr_logger, "Notice", NOTICE);
    logger_message(stderr_logger, "Error", ERR);

    logger_destroy(stderr_logger);
    return 0;
}
Exemplo n.º 2
0
bool client_manager_init(int port)
{
  logger_message("[CLIENT] Manager starting ...");
  if (!(g_client_manager = malloc(sizeof(t_client_manager))))
    return false;
  list_init(&g_client_manager->clients);
  list_init(&g_client_manager->commands);
  if (!network_init()
      || !socket_init(&g_client_manager->listen)
      || !network_listen_to(&g_client_manager->listen, port, &client_connect))
    return false;
  logger_message("[CLIENT] Manager started");
  return true;
}
Exemplo n.º 3
0
t_bool	init_map(t_kernel *kernel, int width, int height)
{
    int	i;
    int	j;
    int	it;

    if (!(kernel->game.map = malloc(sizeof(t_map))))
        return (FALSE);
    kernel->game.map->width = width;
    kernel->game.map->height = height;
    if (!(kernel->game.map->map
            = (t_case **)malloc(sizeof(t_case *) * (height) + 1)))
        return (FALSE);
    for (i = 0; i < height; i++)
    {
        if (!(kernel->game.map->map[i]
                = (t_case *)malloc(sizeof(t_case) * (width + 1))))
            return (FALSE);
        for (j = 0; j < width; j++)
        {
            for (it = 0; it < ITEM_TYPE; ++it)
                kernel->game.map->map[i][j].inventory.items[it] = random_res();
            kernel->game.map->map[i][j].players = NULL;
        }
    }
    logger_message("{MAP} Initialisation");
    return (TRUE);
}
Exemplo n.º 4
0
void client_food_dead(t_client *client)
{
  logger_message("[CLIENT] Client %i starved ... dead!", CLP_ID(client));
  player_free(client->player);
  client->player = NULL;
  client_send(client, "mort");
  client_unconnect(client);
}
Exemplo n.º 5
0
/* Connection management thread */
static void *server_conn_thread( void *p )
{
	server_conn_desc_t *conn_desc = (server_conn_desc_t *)p;

	for ( ;; )
	{
		/* Wait for some activity */
		int retv = rd_with_notify_wait(conn_desc->m_rdwn);
		if (!retv)
		{
			logger_error(player_log, 0,
					_("Connection select failed: %s"),
					strerror(errno));
			break;
		}

		/* Notification */
		if (retv & RDWN_NOTIFY_READY)
		{
			char nv;
			if (read(RDWN_NOTIFY_READ_FD(conn_desc->m_rdwn), &nv, 1) <= 0)
			{
				/* Some error */
				break;
			}

			/* Exit */
			if (nv == SERVER_NOTIFY_EXIT)
				break;

			/* Handle notification */
			server_conn_client_notify(conn_desc, nv);
		}

		/* Input from client */
		if (retv & RDWN_READ_READY)
		{
			ssize_t sz = recv(conn_desc->m_socket, 
					&conn_desc->m_buf, sizeof(conn_desc->m_buf) - 1, 0);
			if (sz == -1)
				continue;
			else if (sz == 0)
				break;
			conn_desc->m_buf[sz] = 0;

			if (!server_conn_parse_input(conn_desc))
				break;
		}
	}

	logger_message(player_log, 0, _("Closing connection"));

	/* Destroy the connection */
	server_conn_desc_free(conn_desc);

	return NULL;
} /* End of 'server_conn_thread' function */
Exemplo n.º 6
0
void client_manager_destroy()
{
  if (!g_client_manager)
    return;
  logger_message("[CLIENT] Manager destroy");
  list_foreach(&g_client_manager->clients, (feach) &client_destroy);
  list_clear(&g_client_manager->clients);
  list_free(&g_client_manager->commands);
  network_destroy();
  free(g_client_manager);
  g_client_manager = NULL;
}
Exemplo n.º 7
0
int		session_destroy(void)
{
  t_session	*session;

  logger_message("[SESSION] Stop session service");
  session = session_get_session();
  list_foreach(&(session->in), &delete_instruction_in);
  list_clear(&(session->in));
  list_foreach(&(session->out), &delete_instruction_out);
  list_clear(&(session->out));
  return (0);
}
Exemplo n.º 8
0
/** Write an entry to the log (va_list variant).
 *
 * @param ctx Log to use (use LOG_DEFAULT if you have no idea what it means).
 * @param level Severity level of the message.
 * @param fmt Format string in printf-like format (without trailing newline).
 * @param args Arguments.
 */
void log_msgv(log_t ctx, log_level_t level, const char *fmt, va_list args)
{
	assert(level < LVL_LIMIT);

	char *message_buffer = malloc(MESSAGE_BUFFER_SIZE);
	if (message_buffer == NULL)
		return;

	vsnprintf(message_buffer, MESSAGE_BUFFER_SIZE, fmt, args);
	logger_message(logger_session, ctx, level, message_buffer);
	free(message_buffer);
}
Exemplo n.º 9
0
void
logger_printf(const TCHAR *format, ...)
{
	va_list ap;
	TCHAR *tmp;

	va_start(ap, format);
	tmp = tcsdup_vprintf(format, ap);
	va_end(ap);

	logger_message(tmp);
	free(tmp);
}
Exemplo n.º 10
0
static t_bool	add_team(t_kernel *kernel, char *teamname)
{
    t_team	*team;

    if (!(team = malloc(sizeof(t_team))))
        return (FALSE);
    strcpy(team->name, teamname);
    if (!(team->players = list_create()))
        return (FALSE);
    team->place_left = kernel->options.max_clients;
    if (FALSE == list_push_back(&(kernel->game.teams), team))
        return (FALSE);
    logger_message("{TEAM} Create %s", teamname);
    return (TRUE);
}
Exemplo n.º 11
0
void __cdecl
_tmain(int argc, _TCHAR *argv[])
{
	SERVICE_TABLE_ENTRY DispatchTable[] = 
	{
		{ TEXT(""), ServiceMain },
		{ NULL, NULL },
	};

	if (argc == 2 && lstrcmpi(argv[1], TEXT("install")) == 0)
	{
		do_install();
		return;
	}

	if (argc == 2 && lstrcmpi(argv[1], TEXT("uninstall")) == 0)
	{
		do_uninstall();
		return;
	}

	if (argc == 2 && lstrcmpi(argv[1], TEXT("test")) == 0)
	{
		logger_message("hello world!");
		return;
	}

	if (!StartServiceCtrlDispatcher(DispatchTable))
	{
		if (GetLastError() != ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
		{
			fatal_win32_error(TEXT("StartServiceCtrlDispatcher"));
		}
	
		fprintf(stderr, "Error: no option specified\n");
		fprintf(stderr, "Usage: %s install\n", argv[0]);
		exit(1);
	}
}
Exemplo n.º 12
0
/* The main server thread function
 * It is responsible for accepting connections and launching 
 * per-connection threads */
static void *server_thread( void *p )
{
	for ( ;; )
	{
		/* Wait for input */
		int retv = rd_with_notify_wait(server_rdwn);
		if (!retv)
		{
			logger_error(player_log, 0,
					_("Server select failed: %s"),
					strerror(errno));
			return NULL;
		}

		/* Exit */
		if (retv & RDWN_NOTIFY_READY)
			break;

		/* Try to accept connection */
		if (retv & RDWN_READ_READY)
		{
			server_conn_desc_t *conn_desc = NULL;
			int conn_socket = -1;
			int err;

			conn_socket = accept(server_socket, NULL, NULL);
			if (conn_socket == -1)
			{
				logger_error(player_log, 0,
						_("Server socket accept failed: %s"),
						strerror(errno));
				goto failure;
			}

			logger_message(player_log, 0, _("Received a connection"));

			/* Start the connection thread */
			conn_desc = server_conn_desc_new(conn_socket);
			if (!conn_desc)
			{
				close(conn_socket);
				goto failure;
			}
			err = pthread_create(&conn_desc->m_tid, NULL,
					server_conn_thread, conn_desc);
			if (err)
			{
				logger_error(player_log, 0,
						_("Server connection thread create failed: %s"),
						strerror(err));
				goto failure;
			}

			continue;
		failure:
			if (conn_desc)
				server_conn_desc_free(conn_desc);
			continue;
		}
	}

	return NULL;
} /* End of 'server_thread' function */
Exemplo n.º 13
0
/* Start the server */
bool_t server_start( void )
{
	struct sockaddr_in addr;
	int err, i;

	int server_port = cfg_get_var_int(cfg_list, "server-port");
	if (!server_port)
		server_port = 0x4D50; /* 'MP' / 19792 */

	int server_port_pool_size = cfg_get_var_int(cfg_list, "server-port-pool-size");
	if (!server_port_pool_size)
		server_port_pool_size = 10;

	logger_message(player_log, 0, _("Starting the server at port %d"), server_port);

	/* Create socket */
	server_socket = socket(AF_INET, SOCK_STREAM, 0);
	if (server_socket == -1)
	{
		logger_error(player_log, 0,
				_("Server socket creation failed: %s"),
				strerror(errno));
		goto failed;
	}

	/* Bind */
	for ( int i = 0; i < server_port_pool_size; i++, server_port++ )
	{
		addr.sin_addr.s_addr = INADDR_ANY;
		addr.sin_port = htons(server_port);
		addr.sin_family = AF_INET;
		if (bind(server_socket, (struct sockaddr*)&addr, sizeof(addr)) != -1)
			goto bind_succeeded;
		else
			logger_error(player_log, 0,
					_("Server socket bind at port %d failed: %s"),
					server_port, strerror(errno));
	}

bind_succeeded:

	logger_message(player_log, 0, _("Server listening at port %d"), server_port);

	/* Listen */
	if (listen(server_socket, 5) == -1)
	{
		logger_error(player_log, 0,
				_("Server socket listen failed: %s"),
				strerror(errno));
		goto failed;
	}

	/* Create rdwn */
	server_rdwn = rd_with_notify_new(server_socket);
	if (!server_rdwn)
	{
		logger_error(player_log, 0,
				_("Server notification pipe create failed: %s"),
				strerror(errno));
		goto failed;
	}

	/* Start the main thread */
	err = pthread_create(&server_tid, NULL, server_thread, NULL);
	if (err)
	{
		logger_error(player_log, 0,
				_("Server thread create failed: %s"),
				strerror(err));
		goto failed;
	}

	/* Install hook handler */
	server_hook_id = pmng_add_hook_handler(player_pmng, server_hook_handler);

	return TRUE;

failed:
	if (server_socket != -1)
	{
		close(server_socket);
		server_socket = -1;
	}
	if (server_rdwn)
	{
		rd_with_notify_free(server_rdwn);
		server_rdwn = NULL;
	}
	return FALSE;
} /* End of 'server_start' function */
Exemplo n.º 14
0
static void
SvcInit(DWORD dwArgc, TCHAR *lpszArgv[])
{
	MyParams params = {0};

	/* create an event; this event will be raised when we're told
	 * to shut down */
	stopEvent = CreateEvent(
			NULL, /* default security attributes */
			TRUE, /* manual reset event */
			FALSE, /* not signaled */
			NULL); /* no name */
	if (stopEvent == NULL)
	{
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	/* look in registry for guidance */
	get_registry_params(lpszArgv[0], &params);
	logger_message(TEXT("startup: version 2011.03.21"));

	report_status(SERVICE_RUNNING, NO_ERROR, 0);

	while(1)
	{

	if (params.delay != 0)
	{
		HANDLE hTimer;
		LARGE_INTEGER li = {0};
		const __int64 nTimerUnitsPerSecond = 10000000;
		int nsecs_delay;

		if (params.hour != 101)
		{
			struct tm *tm;
			time_t t;
			int nhours_delay;

			time(&t);
			tm = localtime(&t);

			nhours_delay = (params.hour + 24 - tm->tm_hour) % 24;
			nsecs_delay = params.delay + 3600 * nhours_delay;
		}
		else
		{
			nsecs_delay = params.delay;
		}
		logger_printf(TEXT("waiting for %d seconds"), nsecs_delay);
		li.QuadPart = -(nsecs_delay * nTimerUnitsPerSecond);

		hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
		SetWaitableTimer(hTimer, &li, 0, NULL, NULL, TRUE);

		{
			HANDLE handles[2];
			DWORD dwEvent;

			handles[0] = stopEvent;
			handles[1] = hTimer;

			dwEvent = WaitForMultipleObjects(2, handles, FALSE, INFINITE);

			if (dwEvent == WAIT_OBJECT_0 + 0)
			{
				/* stop the service */
				logger_message(TEXT("received stop signal"));
				report_status(SERVICE_STOPPED, NO_ERROR, 0);
				CloseHandle(hTimer);
				return;
			}
			else if (dwEvent != WAIT_OBJECT_0 + 1)
			{
				/* FIXME- error returned by WaitForMultipleObjects */
			}
		}
		CloseHandle(hTimer);
	}

	if (check_for_active_sessions())
	{
		logger_printf(TEXT("station appears to be in use"));
		continue;
	}

		break;
	}

	if (params.script != NULL)
	{
		logger_printf(TEXT("resetting the idle timer"));
		SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_CONTINUOUS);

		if (params.status_window_cmd != NULL)
		{
			logger_printf(TEXT("launching status window process"));
			launch_status_window(params.status_window_cmd);
		}

		logger_printf(TEXT("launching script %s"), params.script);
		create_process(params.script);
	}
	else
	{
		logger_message(TEXT("nothing to run"));
	}

	{
		WaitForSingleObject(stopEvent, INFINITE);
		logger_message(TEXT("received stop signal"));
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}
}