Ejemplo n.º 1
0
/* Check if the time windows have expired, updating their counters and
 * expiration time if that's the case.
 *
 * @ta: the TimedAverage structure
 * @elapsed: if non-NULL, the elapsed time (in ns) within the current
 *           window will be stored here
 */
static void check_expirations(TimedAverage *ta, uint64_t *elapsed)
{
    int64_t now = qemu_clock_get_ns(ta->clock_type);
    int i;

    assert(ta->period != 0);

    /* Check if the windows have expired */
    for (i = 0; i < 2; i++) {
        TimedAverageWindow *w = &ta->windows[i];
        if (w->expiration <= now) {
            window_reset(w);
            update_expiration(w, now, ta->period);
        }
    }

    /* Make ta->current point to the oldest window */
    if (ta->windows[0].expiration < ta->windows[1].expiration) {
        ta->current = 0;
    } else {
        ta->current = 1;
    }

    /* Calculate the elapsed time within the current window */
    if (elapsed) {
        int64_t remaining = ta->windows[ta->current].expiration - now;
        *elapsed = ta->period - remaining;
    }
}
Ejemplo n.º 2
0
static void service_game_server(int connection)
{
	int rc;
	int i;
	struct sockaddr peer;
	struct sockaddr_in *ip4addr;
	unsigned int addrlen;
	struct ssgl_game_server gs;
	unsigned char *x;

	/* Get game server information */
	rc = ssgl_readsocket(connection, &gs, sizeof(gs));
	if (rc < 0)
		return;

	printf("1 gs.game_type = '%s', gs.port = %hu\n",gs.game_type, gs.port);
	if (sanitize_game_server_entry(&gs))
		return;
	printf("2 gs.game_type = '%s'\n",gs.game_type);

	/* Get the game server's ip addr (don't trust what we were told.) */
	memset(&gs.ipaddr, 0, sizeof(gs.ipaddr));
	addrlen = sizeof(peer);
	rc = getpeername(connection, &peer, &addrlen); 
	if (rc != 0) {
		printf("getpeername failed: %s\n", strerror(errno));
	}
	printf("addrlen = %d\n", addrlen);
	x = (unsigned char *) &peer;
	for (i = 0; i < addrlen; i++)
		printf("%02x ", x[i]);
	printf("\n");
	ip4addr = (struct sockaddr_in *) &peer;
	memcpy(&gs.ipaddr, &ip4addr->sin_addr, sizeof(gs.ipaddr));

	printf("3 gs.game_type = '%s'\n",gs.game_type);
	/* Update directory with new info. */

	ssgl_lock();

	/* replace with faster algorithm if need be. */
	for (i = 0; i < ngame_servers; i++) {
		/* already present? */
		if (memcmp(&game_server[i], &gs, sizeof(gs)) == 0) {
			update_expiration(i);
			goto out;
		}
	}
	printf("4 gs.game_type = '%s'\n",gs.game_type);

	if (ngame_servers >= MAX_GAME_SERVERS)
		goto out; /* no room at the inn. */

	printf("5 gs.game_type = '%s'\n",gs.game_type);
	/* add the new game server info into the directory... */
	game_server[ngame_servers] = gs;
	update_expiration(ngame_servers);
	printf("6 gs.game_type = '%s'\n",gs.game_type);
	ngame_servers++;
out:
	ssgl_unlock();
	return;
}
static void service_game_server(int connection)
{
	int rc;
	int i;
	struct ssgl_game_server gs;
#if 0
	struct sockaddr peer;
	struct sockaddr_in *ip4addr;
	unsigned int addrlen;
	unsigned char *x;
#endif

	/* Get game server information */
	rc = ssgl_readsocket(connection, &gs, sizeof(gs));
	if (rc < 0) {
		ssgl_log(SSGL_WARN, "Failed initial socket read from game server\n");
		return;
	}

	/* printf("1 gs.game_type = '%s', gs.port = %hu\n",gs.game_type, gs.port); */
	if (sanitize_game_server_entry(&gs)) { 
		ssgl_log(SSGL_WARN, "Failed to sanitize game server information\n");
		return;
	}
	/* printf("2 gs.game_type = '%s'\n",gs.game_type); */

#if 0

	/* This doesn't work... will return 127.0.0.1 if gameserver is on
	 * the same host as lobby server.
	 */

	/* Get the game server's ip addr (don't trust what we were told.) */
	memset(&gs.ipaddr, 0, sizeof(gs.ipaddr));
	addrlen = sizeof(peer);
	rc = getpeername(connection, &peer, &addrlen); 
	if (rc != 0) {
		printf("getpeername failed: %s\n", strerror(errno));
	}
	printf("addrlen = %d\n", addrlen);
	x = (unsigned char *) &peer;
	for (i = 0; i < addrlen; i++)
		printf("%02x ", x[i]);
	printf("\n");
	ip4addr = (struct sockaddr_in *) &peer;
	memcpy(&gs.ipaddr, &ip4addr->sin_addr, sizeof(gs.ipaddr));
#endif

	/* printf("3 gs.game_type = '%s'\n",gs.game_type); */
	/* Update directory with new info. */

	ssgl_lock();

	/* replace with faster algorithm if need be. */
	for (i = 0; i < ngame_servers; i++) {
		struct ssgl_game_server *ogs = &game_server[i];
		/* already present? */
		if (ogs->port == gs.port &&
			ogs->ipaddr == gs.ipaddr &&
			memcmp(ogs->game_type, gs.game_type, sizeof(gs.game_type)) == 0 &&
			memcmp(ogs->game_instance, gs.game_instance, sizeof(gs.game_instance)) == 0 &&
			memcmp(ogs->server_nickname, gs.server_nickname, sizeof(gs.server_nickname)) == 0 &&
			memcmp(ogs->location, gs.location, sizeof(gs.location)) == 0) {
			ogs->nconnections = gs.nconnections;
			update_expiration(i);
			goto out;
		}
	}
	/* printf("4 gs.game_type = '%s'\n",gs.game_type); */

	if (ngame_servers >= MAX_GAME_SERVERS) {
		ssgl_log(SSGL_WARN, "Too many game servers connected, rejecting game server.\n");
		goto out; /* no room at the inn. */
	}

	/* printf("5 gs.game_type = '%s'\n",gs.game_type); */
	/* add the new game server info into the directory... */
	game_server[ngame_servers] = gs;
	update_expiration(ngame_servers);
	/* printf("6 gs.game_type = '%s'\n",gs.game_type); */
	ngame_servers++;
out:
	ssgl_unlock();
	return;
}