示例#1
0
int check_restrict(Account * ac) {
	stats_limit_cfg cfg_buf;
	stats_limit_cfg *sl = config_get_account_limit(ac->id, &cfg_buf);
	char restrict_bufer[_DBGOVERNOR_BUFFER_4096];
	int restrict_period = 0;
	GOVERNORS_FIELD_NAME _cur = is_stat_overlimit_current(&ac->current, sl);
	time_t now;
	time(&now);
    struct governor_config data_cfg;
    get_config_data( &data_cfg );
    
	if (_cur != NORESTRICT_PARAM2) {
		//Current restrict
		int old_restricted = ac->restricted;
		if ((ac->start_count + data_cfg.timeout) > now) {
			restrict_period = get_timeout(&ac->restricted, 1);
		} else {
			restrict_period = get_timeout(&ac->restricted, 0);
		}
		if( data_cfg.statistic_mode ) IncNumberOfRestricts( ac->id, get_cause_of_restrict(_cur) );
		ac->timeout = restrict_period;
		time(&ac->start_count);
		ac->info.field_restrict = CURRENT_PERIOD;
		ac->info.field_level_restrict = _cur;
		if (!old_restricted) account_restrict(ac, sl);
		if (data_cfg.restrict_log) {
			char tmp_buf[_DBGOVERNOR_BUFFER_8192];
			prepareRestrictDescription(tmp_buf, ac, sl);
			WRITE_LOG( &ac->current,
					1,
					tmp_buf,
					_DBGOVERNOR_BUFFER_8192,
					tmp_buf,
					data_cfg.log_mode);

		}
		return 1;

	} else {
		GOVERNORS_FIELD_NAME _short = is_stat_overlimit_short(
				&ac->short_average, sl);
		if (_short != NORESTRICT_PARAM2) {
			//Short restrict
			int old_restricted = ac->restricted;
			if ((ac->start_count + data_cfg.timeout) > now) {
				restrict_period = get_timeout(&ac->restricted, 1);
			} else {
				restrict_period = get_timeout(&ac->restricted, 0);
			}
			if( data_cfg.statistic_mode ) IncNumberOfRestricts( ac->id, get_cause_of_restrict(_cur) );
			ac->timeout = restrict_period;
			time(&ac->start_count);
			ac->info.field_restrict = SHORT_PERIOD;
			ac->info.field_level_restrict = _short;
			if (!old_restricted) account_restrict(ac, sl);
			if (data_cfg.restrict_log) {
				char tmp_buf[_DBGOVERNOR_BUFFER_8192];
				prepareRestrictDescription(tmp_buf, ac, sl);
				WRITE_LOG( &ac->short_average,
						1,
						tmp_buf,
						_DBGOVERNOR_BUFFER_8192,
						tmp_buf,
						data_cfg.log_mode);

			}
			return 1;
		} else {
			GOVERNORS_FIELD_NAME _mid = is_stat_overlimit_mid(&ac->mid_average,
					sl);
			if (_mid != NORESTRICT_PARAM2) {
				//Mid restrict
				int old_restricted = ac->restricted;
				if ((ac->start_count + data_cfg.timeout) > now) {
					restrict_period = get_timeout(&ac->restricted, 1);
				} else {
					restrict_period = get_timeout(&ac->restricted, 0);
				}
				if( data_cfg.statistic_mode ) IncNumberOfRestricts( ac->id, get_cause_of_restrict(_cur) );
				ac->timeout = restrict_period;
				time(&ac->start_count);
				ac->info.field_restrict = MID_PERIOD;
				ac->info.field_level_restrict = _mid;
				if (!old_restricted) account_restrict(ac, sl);
				if (data_cfg.restrict_log) {
					char tmp_buf[_DBGOVERNOR_BUFFER_8192];
					prepareRestrictDescription(tmp_buf, ac, sl);
					WRITE_LOG( &ac->mid_average,
							1,
							tmp_buf,
							_DBGOVERNOR_BUFFER_8192,
							tmp_buf,
							data_cfg.log_mode);
				}
				return 1;
			} else {
				GOVERNORS_FIELD_NAME _long = is_stat_overlimit_long(
						&ac->long_average, sl);
				if (_long != NORESTRICT_PARAM2) {
					//Long restrict
					int old_restricted = ac->restricted;
					if ((ac->start_count + data_cfg.timeout) > now) {
						restrict_period = get_timeout(&ac->restricted, 1);
					} else {
						restrict_period = get_timeout(&ac->restricted, 0);
					}
					if( data_cfg.statistic_mode ) IncNumberOfRestricts( ac->id, get_cause_of_restrict(_cur) );
					ac->timeout = restrict_period;
					time(&ac->start_count);
					ac->info.field_restrict = LONG_PERIOD;
					ac->info.field_level_restrict = _long;
					if (!old_restricted) account_restrict(ac, sl);
					if (data_cfg.restrict_log) {
						char tmp_buf[_DBGOVERNOR_BUFFER_8192];
						prepareRestrictDescription(tmp_buf, ac, sl);

						WRITE_LOG( &ac->long_average,
								1,
								tmp_buf,
								_DBGOVERNOR_BUFFER_8192,
								tmp_buf,
								data_cfg.log_mode);
					}
					return 1;
				}
			}
		}
	}
	return 0;
}
示例#2
0
文件: PROTOCOL.C 项目: jeske/GTalk
int recv_packet(char *buffer, int *length, int *packetno, char checksum_type)
{
#ifdef DEBUG_RECV
    char s[80];
#endif
    unsigned char temp_char;
    int ch;
    char cur_return = ACK_CHR;
    unsigned int checksum;
    unsigned int calc_checksum;

    ch = get_timeout(SMALL_TIMEOUT_TICKS);
#ifdef DEBUG
    sprintf(s,"Recieved first packet char %03d",ch);
    print_str_cr_to(s,0);
#endif
    switch (ch)
    {
      case (-1):    return (-1);
      case SOH_CHR: *length = 128;
                    break;
      case STX_CHR: *length = 1024;
                    break;
      case CAN_CHR: return (CAN_CHR);
      case EOT_CHR: return (EOT_CHR);
      default:      return (NAK_CHR);
    };

    *packetno = time_char();

#ifdef DEBUG_RECV
    sprintf(s,"Packet No %02X",*packetno);
    print_str_cr_to(s,0);
#endif

    if (*packetno == -1) return (-1);

    ch = time_char();

    if (ch == -1) return (-1);

    temp_char = (unsigned char)*packetno;

#ifdef DEBUG_RECV
    sprintf(s,"COmplement No %02X",ch);
    print_str_cr_to(s,0);
#endif

    if ((unsigned char)ch !=
        (((unsigned char)(~temp_char)))) return (NAK_CHR);

    if (time_buffer(buffer,*length) == -1) return (-1);
    switch (checksum_type)
    {
      case CRC_8: checksum = (int) time_char();
                  if ((int)checksum == -1) return (-1);
                  calc_checksum = (unsigned char) calc_8bit_checksum(buffer,*length);
                  if (((unsigned char)checksum) !=
                      ((unsigned char)calc_checksum))
                             cur_return = NAK_CHR;
                  break;
      case CRC_16: if (time_integer(&checksum) == -1) return (-1);
                   calc_checksum = calc_crc_checksum((unsigned char *)buffer,
                       *length);
#ifdef DEBUG
                   sprintf(s,"Checksum, %X Calc Checksum %X",
                       (unsigned char *)checksum,calc_checksum);
                   print_str_cr_to(s,0);
#endif
                   if (checksum != calc_checksum) cur_return = NAK_CHR;
                   break;
    }
    return (cur_return);
}
示例#3
0
文件: chansrv.c 项目: nrich/xrdp
THREAD_RV THREAD_CC
channel_thread_loop(void *in_val)
{
    tbus objs[32];
    tbus wobjs[32];
    int num_objs;
    int num_wobjs;
    int timeout;
    int error;
    THREAD_RV rv;

    LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread start"));
    rv = 0;
    setup_api_listen();
    error = setup_listen();

    if (error == 0)
    {
        timeout = -1;
        num_objs = 0;
        num_wobjs = 0;
        objs[num_objs] = g_term_event;
        num_objs++;
        trans_get_wait_objs(g_lis_trans, objs, &num_objs);
        trans_get_wait_objs(g_api_lis_trans, objs, &num_objs);

        while (g_obj_wait(objs, num_objs, wobjs, num_wobjs, timeout) == 0)
        {
            check_timeout();
            if (g_is_wait_obj_set(g_term_event))
            {
                LOGM((LOG_LEVEL_INFO, "channel_thread_loop: g_term_event set"));
                clipboard_deinit();
                sound_deinit();
                dev_redir_deinit();
                rail_deinit();
                break;
            }

            if (g_lis_trans != 0)
            {
                if (trans_check_wait_objs(g_lis_trans) != 0)
                {
                    LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
                          "trans_check_wait_objs error"));
                }
            }

            if (g_con_trans != 0)
            {
                if (trans_check_wait_objs(g_con_trans) != 0)
                {
                    LOGM((LOG_LEVEL_INFO, "channel_thread_loop: "
                          "trans_check_wait_objs error resetting"));
                    clipboard_deinit();
                    sound_deinit();
                    dev_redir_deinit();
                    rail_deinit();
                    /* delete g_con_trans */
                    trans_delete(g_con_trans);
                    g_con_trans = 0;
                    /* create new listener */
                    error = setup_listen();

                    if (error != 0)
                    {
                        break;
                    }
                }
            }

            if (g_api_lis_trans != 0)
            {
                if (trans_check_wait_objs(g_api_lis_trans) != 0)
                {
                    LOG(0, ("channel_thread_loop: trans_check_wait_objs failed"));
                }
            }

            LOG(10, ("0 %p", g_api_con_trans));

            if (g_api_con_trans != 0)
            {
                LOG(10, ("1 %p %d", g_api_con_trans, g_tcp_can_recv(g_api_con_trans->sck, 0)));

                if (trans_check_wait_objs(g_api_con_trans) != 0)
                {
                    LOG(10, ("channel_thread_loop: trans_check_wait_objs failed, "
                             "or disconnected"));
                    g_free(g_api_con_trans->callback_data);
                    trans_delete(g_api_con_trans);
                    g_api_con_trans = 0;
                }
            }

            xcommon_check_wait_objs();
            sound_check_wait_objs();
            dev_redir_check_wait_objs();
            xfuse_check_wait_objs();
            timeout = -1;
            num_objs = 0;
            num_wobjs = 0;
            objs[num_objs] = g_term_event;
            num_objs++;
            trans_get_wait_objs(g_lis_trans, objs, &num_objs);
            trans_get_wait_objs_rw(g_con_trans, objs, &num_objs,
                                   wobjs, &num_wobjs);
            trans_get_wait_objs(g_api_lis_trans, objs, &num_objs);
            trans_get_wait_objs(g_api_con_trans, objs, &num_objs);
            xcommon_get_wait_objs(objs, &num_objs, &timeout);
            sound_get_wait_objs(objs, &num_objs, &timeout);
            dev_redir_get_wait_objs(objs, &num_objs, &timeout);
            xfuse_get_wait_objs(objs, &num_objs, &timeout);
            get_timeout(&timeout);
        } /* end while (g_obj_wait(objs, num_objs, 0, 0, timeout) == 0) */
    }

    trans_delete(g_lis_trans);
    g_lis_trans = 0;
    trans_delete(g_con_trans);
    g_con_trans = 0;
    trans_delete(g_api_lis_trans);
    g_api_lis_trans = 0;
    trans_delete(g_api_con_trans);
    g_api_con_trans = 0;
    LOGM((LOG_LEVEL_INFO, "channel_thread_loop: thread stop"));
    g_set_wait_obj(g_thread_done_event);
    return rv;
}
示例#4
0
文件: api.c 项目: anatol/libsigrok
static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
{
	struct sr_dev_driver *di = sdi->driver;
	struct dev_context *devc;
	struct drv_context *drvc;
	struct sr_usb_dev_inst *usb;
	struct sr_trigger *trigger;
	struct libusb_transfer *transfer;
	unsigned int i, timeout, num_transfers;
	int ret;
	unsigned char *buf;
	size_t size, convsize;

	if (sdi->status != SR_ST_ACTIVE)
		return SR_ERR_DEV_CLOSED;

	drvc = di->priv;
	devc = sdi->priv;
	usb = sdi->conn;

	/* Configures devc->cur_channels. */
	if (configure_channels(sdi) != SR_OK) {
		sr_err("Failed to configure channels.");
		return SR_ERR;
	}

	devc->cb_data = cb_data;
	devc->sent_samples = 0;
	devc->empty_transfer_count = 0;
	devc->cur_channel = 0;
	memset(devc->channel_data, 0, sizeof(devc->channel_data));

	if ((trigger = sr_session_trigger_get(sdi->session))) {
		int pre_trigger_samples = 0;
		if (devc->limit_samples > 0)
			pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
		devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
		if (!devc->stl)
			return SR_ERR_MALLOC;
		devc->trigger_fired = FALSE;
	} else
		devc->trigger_fired = TRUE;

	timeout = get_timeout(devc);
	num_transfers = get_number_of_transfers(devc);
	size = get_buffer_size(devc);
	convsize = (size / devc->num_channels + 2) * 16;
	devc->submitted_transfers = 0;

	devc->convbuffer_size = convsize;
	if (!(devc->convbuffer = g_try_malloc(convsize))) {
		sr_err("Conversion buffer malloc failed.");
		return SR_ERR_MALLOC;
	}

	devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
	if (!devc->transfers) {
		sr_err("USB transfers malloc failed.");
		g_free(devc->convbuffer);
		return SR_ERR_MALLOC;
	}

	if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
					     devc->cur_channels)) != SR_OK) {
		g_free(devc->transfers);
		g_free(devc->convbuffer);
		return ret;
	}

	devc->num_transfers = num_transfers;
	for (i = 0; i < num_transfers; i++) {
		if (!(buf = g_try_malloc(size))) {
			sr_err("USB transfer buffer malloc failed.");
			if (devc->submitted_transfers)
				abort_acquisition(devc);
			else {
				g_free(devc->transfers);
				g_free(devc->convbuffer);
			}
			return SR_ERR_MALLOC;
		}
		transfer = libusb_alloc_transfer(0);
		libusb_fill_bulk_transfer(transfer, usb->devhdl,
				2 | LIBUSB_ENDPOINT_IN, buf, size,
				logic16_receive_transfer, (void *)sdi, timeout);
		if ((ret = libusb_submit_transfer(transfer)) != 0) {
			sr_err("Failed to submit transfer: %s.",
			       libusb_error_name(ret));
			libusb_free_transfer(transfer);
			g_free(buf);
			abort_acquisition(devc);
			return SR_ERR;
		}
		devc->transfers[i] = transfer;
		devc->submitted_transfers++;
	}

	devc->ctx = drvc->sr_ctx;

	usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);

	/* Send header packet to the session bus. */
	std_session_send_df_header(cb_data, LOG_PREFIX);

	if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
		abort_acquisition(devc);
		return ret;
	}

	return SR_OK;
}
示例#5
0
/* Search thread main function */
void *search_loop(void *arg) {
    uint8_t ply;
    move_t mv, mv_tmp;

    SET_BLANK_MOVE(mv);
    SET_BLANK_MOVE(mv_tmp);

    pthread_mutex_lock(&mutex);

    /* Initializations */
    precompute_moves();
    precompute_distances();
    init_zobrist_keys();
    init_history();
    init_transposition_table();
    config_alarm(config->max_seconds);
    max_depth = config->max_depth;

    /* Setup board */
    board = set_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
    if(board == NULL)
    	quit("Error: Could not setup new board!\n");

    /* ECO = encyclopedia of chess openings */

    if(atoi(config->name) >= 50)
        if(!load_eco(board))
            quit("Error: Could not load Encyclopedia of Chess Openings!\n");

    pthread_mutex_unlock(&mutex);
 
    /* Keep alive until the status changes to QUIT */
    while(status != QUIT) {
    	switch(status) {
    	case NOP:
    	    /* NOP: just wait for a change in status */
    	    pthread_mutex_lock(&mutex);
    	    while(status == NOP)
    	    	pthread_cond_wait(&cond, &mutex);
    	    pthread_mutex_unlock(&mutex);
    	    break;
    	case FORCE:
    	    /* FORCE: wait for a change in status but don't start searches */
    	    pthread_mutex_lock(&mutex);
    	    while(status == FORCE)
    	    	pthread_cond_wait(&cond, &mutex);
    	    pthread_mutex_unlock(&mutex);
    	    break;
    	case SEARCH:
    	    /* Iterative Deepening Search */
    	    /* Save the on-move color */
    	    onmove = board->onmove;
    	    /* Sets as blank the move to be played*/
    	    SET_BLANK_MOVE(mv);
    	    /* Starts counting the time */
    	    start_alarm();
    	    /* For each depth, search with alpha-beta minimax */
    	    for(ply = 2; ply <= max_depth; ply += 2) {
    	    	mv_tmp = alpha_beta(board, -MAX_HEU, MAX_HEU, ply);
    	    	/* Did we run out of time? If so, stops deepening iterations */
    	    	if(get_timeout())
    	    	    break;
    	    	mv = mv_tmp;
    	    }
    	    /* Stops counting the time, if it hasn't already reached limit */
    	    stop_alarm();
    	    /* If the move is still blank, use the partial move found */
    	    if(IS_BLANK_MOVE(mv))
    	    	mv = mv_tmp;
    	    /* Perform the move found in the search */
    	    move(board, mv);
    	    /* Returns to the NOP status */
    	    set_status(NOP);
    	    break;
    	case PONDER:
    	    /* Reserved for future use */
    	    set_status(NOP);
    	    break;
    	default:
    	    quit("Error: Invalid search status!\n");
    	}
    }
    
    /* Clean up memory */
    clear_eco();
    clear_transposition_table();
    clear_history();
    clear_board(board);

    /* Exit Search Thread */
    return NULL;
}
示例#6
0
/* Alpha Beta Pruning - Minimax Search Algorithm */
move_t alpha_beta(board_t *b, int32_t alpha, int32_t beta, uint32_t ply) {
    uint8_t i, type;
    move_list_t *list;
    move_t m, best;

    /* Query ECO tree */
    if(atoi(config->name) >= 50)
        if(query_eco(&m))	    
    	    return m;

    /* Query transposition table */
    type = query_transposition(b->hash, alpha, beta, ply, &m);
    switch(type) {
    case TYPE_ALPHA:
    	/* Chooses the best alpha between the old and the one from the table */
        alpha = MAX(alpha, m.eval);
    	/* If we have a Beta cutoff, returns */
    	if(alpha >= beta)
    	    return m;
    	break;
    case TYPE_BETA:
    	/* Chooses the best beta between the old and the one from the table */
    	beta = MIN(beta, m.eval);
    	/* If we have a Beta cutoff, returns */
    	if(alpha >= beta)
    	    return m;
    	break;
    case TYPE_EXACT:
    	/* If the table had an exact evaluation, returns it */
    	return m;
    case TYPE_INVALID:
    	/* If the transposition is invalid, just ignore and keep going*/
    	break;
    }

    /* If it's a leaf node, evaluate it properly */
    if(ply == 0) {
    	m.eval = heuristic(b, onmove);
    	return m;
    }

    /* Default type of the value to be inserted in the Transposition table */
    type = TYPE_ALPHA;

    /* Initialize the best possible move as blank */
    SET_BLANK_MOVE(best);

    /* Get the possible next moves */
    list = gen_move_list(b, FALSE);
    /* For each possible next move... */
    for(i = 0; i < list->size; i++) {
    	/* Let's see the board after that move... */
    	move(b, list->move[i]);

    	/* Did we reach any end game condition? */
    	switch(end(b)) {
    	case CHECK_MATE:
    	    m.eval = (onmove == b->onmove) ? -MAX_HEU : MAX_HEU;
    	    break;
    	case STALE_MATE:
    	case REPETITION:
    	case FIFTY_MOVES:
    	case TWO_KINGS:
    	    m.eval = -MAX_HEU;
    	    break;
    	case NO_MATE:
    	default:
    	    /* If not, keep searching down in the search tree */
    	    m = alpha_beta(b, -beta, -alpha, ply - 1);
    	    m.eval = -m.eval;
    	    break;
    	}

    	/* Restores the previous board (before the possible move) */
    	unmove(b);

    	/* Beta cutoff */
    	if(m.eval >= beta) {
    	    best = list->move[i];
    	    best.eval = m.eval;
    	    type = TYPE_BETA;
    	    break;
    	/* Alpha cutoff */
    	} else if(m.eval > alpha) {
    	    best = list->move[i];
    	    alpha = best.eval = m.eval;
    	    type = TYPE_EXACT;
    	/* Best possible move until now */
    	} else if(i == 0 || m.eval > best.eval) {
    	    best = list->move[i];
    	    best.eval = m.eval;
    	}

    	/* If our time's up, return immediately */
    	if(get_timeout()) {
    	    clear_move_list(list);
    	    return best; /*break;*/
    	}
    }

    /* Update the Transposition table */
    add_transposition(b->hash, type, ply, best);

    /* Clear temporary information and return */
    clear_move_list(list);
    return best;
}