Пример #1
0
void init(int_ choice){
	server_loop_code = new loop_t;
	reserve_ids();
	console_init();
	load_previous_server_state();
	server_loop_code->array.name = "server loop code";
	//signal(SIGINT, simple_signal_handler);
	switch(choice){
	case 1:
		server_info_init();
		if(check_for_parameter("--net-disable", argc_, argv_) == false) net_init();
		if(check_for_parameter("--physics-disable", argc_, argv_) == false) physics_init();
		if(check_for_parameter("--gametype-disable", argc_, argv_) == false) gametype_init();
		if(check_for_parameter("--input-disable", argc_, argv_) == false) input_init(); // signals should still work from the console
		break;
	case 2:
		test_logic_init();
		break;
	case 3:
		set_signal(SIGTERM, true);
		break;
	default:
		printf("WARNING: This was NOT one of the options, terminating\n");
		set_signal(SIGTERM, true);
		break;
	}
	server_time = new server_time_t;
}
void handle_uart_request(char * uart_req)
{
  char *ptr = NULL, edit = 0;
  
//  check_for_parameter(uart_req);
  
  ptr = strstr(uart_req,"=");
  if(ptr != NULL)
  {
    edit = 1;
    check_for_parameter(uart_req , edit);
  }
  else
  {
     ptr = strstr(uart_req,"?");
     if(ptr != NULL)
     {
        check_for_parameter(uart_req , edit);
     }
       else
        {
          ptr = strstr(uart_req,"RST");
          if(ptr != NULL)
          {
            change = default;
          }
        }
  }
Пример #3
0
void loop_run(loop_t *a){
	const bool print_this_time = CHECK_BIT(settings, LOOP_PRINT_THIS_TIME);
	if(print_this_time){
		FLIP_BIT(settings, LOOP_PRINT_THIS_TIME);
		printf_("settings: " + std::to_string(settings) + "\n", PRINTF_VITAL);
	}
	std::string summary = a->name + "\n";
	const uint_ code_size = a->code.size();
	const long double start_time = get_time();
	if(CHECK_BIT(settings, LOOP_CODE_PARTIAL_MT) == 1){
		std::vector<std::thread*> thread;
		for(uint_ i = 0;i < code_size;i++){
			if(loop_entry_will_run(a, &a->code[i])){
				thread.push_back(new std::thread(a->code[i].code));
			}
		}
		for(uint_ i = 0;i < code_size;i++){
			thread[i]->join();
			delete thread[i];
			thread[i] = nullptr;
		}
	}else if(CHECK_BIT(settings, LOOP_CODE_NEVEREND_MT) == 0){
		for(uint_ i = 0;i < code_size;i++){
			if(loop_entry_will_run(a, &a->code[i])){
				const long double start_time = get_time();
				a->code[i].code();
				if(likely(print_this_time)){
					summary += "\t" + a->code[i].name + "\t\t" + std::to_string(get_time()-start_time) + "\n";
				}
			} // perhaps print a little notice if it isn't running?
		}
		if(unlikely(a->neverend_threads.size() != 0)){
			for(uint_ i = 0;i < a->code.size();i++){
				a->code[i].term = true;
			}
			for(uint_ i = 0;i < a->neverend_threads.size();i++){
				a->neverend_threads[i]->join();
				delete a->neverend_threads[i];
			}
			a->neverend_threads.clear();
		}
	}else if(CHECK_BIT(settings, LOOP_CODE_NEVEREND_MT) == 1){
		loop_update_neverend_thread(a);
	}
	const long double end_time = get_time();
	const long double current_rate = 1/(end_time-start_time);
	if(unlikely(a->average_rate == 0)){
		a->average_rate = current_rate;
	}
	a->average_rate += current_rate;
	a->average_rate *= .5;
	if(print_this_time){
		summary += "current frame rate: " + std::to_string(current_rate) + "\naverage framerate: " + std::to_string(a->average_rate) + "\nloop_settings: " + std::to_string(settings) + "\n";
		if(a->neverend_threads.size() != 0){
			summary += "neverend_thread is in use with " + std::to_string(a->neverend_threads.size()) + " threads.\n";
		}else{
			summary += "neverend_thread is not in use\n";
		}// unify all of this into some sort of standard
		summary += "\tTitle\tIteration Skip\n";
		for(uint_ i = 0;i < a->code.size();i++){
			summary += "\t" + a->code[i].name + "\t" + std::to_string(a->code[i].iteration_skip) + "\n";
		}
		printf("%s", summary.c_str());
	}
	++a->tick;
	if(check_for_parameter("--debug", argc_, argv_)) ms_sleep(1000);
}
Пример #4
0
void loop_run(loop_t *a){
	if((a->settings & LOOP_PRINT_THIS_TIME) != 0){
		printf_("settings: " + std::to_string(a->settings) + "\n", PRINTF_VITAL);
	}
	if(check_for_parameter("--no-mt", argc_, argv_)){
		a->settings &= ~LOOP_CODE_NEVEREND_MT;
		a->settings &= ~LOOP_CODE_PARTIAL_MT;
	}
	std::string summary = a->array.name + "\n";
	const uint_ code_size = a->code.size();
	const long double start_time = get_time();
	for(uint_ i = 0;i < a->code.size();i++){
		try{
			loop_entry_t *tmp_entry = (loop_entry_t*)find_pointer(a->code[i]);
			throw_if_nullptr(tmp_entry);
			if(check_for_parameter("--no-mt", argc_, argv_)){
				tmp_entry->set_settings(0);
				tmp_entry->set_settings(0);
			}
			tmp_entry->array.data_lock.lock();
			if(tmp_entry->term == true){
				if(tmp_entry->thread != nullptr){
					stop_infinite_loop(tmp_entry);
				}else{
					a->code.erase(a->code.begin()+i);
				}
			}else if(tmp_entry->code != nullptr){ // Deleting its place in the array allows for a seg fault if both pieces run
				if(tmp_entry->get_settings(LOOP_CODE_PARTIAL_MT)){
					//printf_("DEBUG: loop_run: Running the following loop_entry_t in its own thread (LOOP_CODE_PARTIAL_MT):" + tmp_entry->array.print() + "\n", PRINTF_DEBUG);
					tmp_entry->thread = new std::thread(tmp_entry->code);
					tmp_entry->start_time = get_time();
				}else if(tmp_entry->get_settings(LOOP_CODE_NEVEREND_MT)){
					if(tmp_entry->thread == nullptr){
						//printf_("DEBUG: loop_run: Running the following loop_entry_t in its own thread (LOOP_CODE_NEVEREND_MT):" + tmp_entry->array.print() + "\n", PRINTF_DEBUG);
						tmp_entry->thread = new std::thread(infinite_loop_function, tmp_entry->code, &(tmp_entry->term));
						tmp_entry->start_time = get_time();
					}
				}else if(likely(tmp_entry->iteration_skip == 0 || a->tick%(tmp_entry->iteration_skip+1) == 0)){
					if(tmp_entry->thread != nullptr){
						stop_infinite_loop(tmp_entry);
					}
					tmp_entry->start_time = get_time();
					//printf_("DEBUG: loop_run: Running the following loop_entry_t in the current thread:" + tmp_entry->array.print() + "\n", PRINTF_DEBUG);
					tmp_entry->code();
				}
			}
			tmp_entry->array.data_lock.unlock();
			/*
			  WARNING: be careful of throwing something that misses a vital unlock
			 */
		}catch(std::logic_error &e){}
	}
	for(uint_ i = 0;i < a->code.size();i++){
		try{
			loop_entry_t *tmp_entry = (loop_entry_t*)find_pointer(a->code[i]);
			throw_if_nullptr(tmp_entry);
			tmp_entry->array.data_lock.lock();
			if(tmp_entry->get_settings(LOOP_CODE_PARTIAL_MT)){
				tmp_entry->thread->join();
				tmp_entry->end_time = get_time();
				delete tmp_entry->thread;
				tmp_entry->thread = nullptr;
			}
			tmp_entry->array.data_lock.unlock();
		}catch(std::logic_error &e){}
	}
	const long double end_time = get_time();
	const long double current_rate = 1/(end_time-start_time);
	if(unlikely(a->average_rate == 0)){
		a->average_rate = current_rate;
	}
	a->average_rate += current_rate;
	a->average_rate *= .5;
	if((a->settings & LOOP_PRINT_THIS_TIME) != 0){
		summary += "current frame rate: " + std::to_string(current_rate) + "\naverage framerate: " + std::to_string(a->average_rate) + "\nloop_settings: " + std::to_string(a->settings) + "\n";
		summary += "\tTitle\tIteration Skip\n";
		for(uint_ i = 0;i < a->code.size();i++){
			try{
				loop_entry_t *tmp = (loop_entry_t*)find_pointer(a->code[i]);
				summary += "\t" + tmp->name + "\t" + std::to_string(tmp->iteration_skip) + "\n";
			}catch(std::logic_error &e){}
		}
		printf("%s", summary.c_str());
		a->settings &= ~LOOP_PRINT_THIS_TIME;
	}
	++a->tick;
	if(check_for_parameter("--debug", argc_, argv_)) ms_sleep(1000);
}