int start_main_cicle(t_server *server) { while (1) { if (wait_for_select(server) == -1) return (-1); if (check_fd(server) == -1) return (-1); if (server->minerals_consumed >= server->generation_interval) { if (generate_minerals(server) == -1) return (-1); } if (check_requests(server) == -1) return (-1); if (check_life_players(server) == -1) return (-1); if (check_end_game(server, MAX_LVL) == -1) { printf("Fin de partie\n"); return (-1); } } return (0); }
static void run(Options *options) { int socket = open_udp_socket(options->listen_port); if (options->verbose) fprintf(stderr, "Opened socket on port %d\n", options->listen_port); if (signal(SIGQUIT, handle_sigquit) == SIG_IGN) signal(SIGQUIT, SIG_IGN); reload_config = 1; while (1) { int sleep_time; if (reload_config) { if (options->config != NULL) destroy_config(options->config); options->config = load_config(options->config_filename); if (!options->config) { fprintf(stderr, "Warning: Config file cannot be read: %s\n", options->config_filename); options->config = create_config(); } initialise_config(options->config); if (options->verbose) { fprintf(stderr, "Loaded config from %s\n", options->config_filename); print_config(options->config, stderr); } reload_config = 0; } //TODO how long until the next request goes out? sleep_time = 1; input_timeout(socket, sleep_time); check_for_responses(options, socket); check_requests(options, socket); } close(socket); }
int main(int argc, char *argv[]) { #if !defined(NOALARM) struct itimerval new_timer, old_timer; #endif /* !NOALARM */ struct rlimit rlp; #if defined(USE_SIGACTION) struct sigaction sigact; #endif struct sockaddr_in sadd; getrlimit(RLIMIT_NOFILE, &rlp); rlp.rlim_cur = rlp.rlim_max; setrlimit(RLIMIT_NOFILE, &rlp); /* First things first, let the client know we're alive */ write(1, SERVER_CONNECT_MSG, strlen(SERVER_CONNECT_MSG)); /* Need this to keep in sync with the client side * * <start><ident_id><local_port><sin_family><s_addr><sin_port> */ req_size = sizeof(char) + sizeof(ident_identifier) + sizeof(short int) + sizeof(sadd.sin_family) + sizeof(sadd.sin_addr.s_addr) + sizeof(sadd.sin_port); /* Set up signal handling */ #if defined( USE_SIGACTION ) #if defined( USE_SIGEMPTYSET ) sigemptyset(&(sigact.sa_mask)); #if !defined( FREEBSD ) && !defined( GLIBC ) sigact.sa_sigaction = 0; #endif #else sigact.sa_mask = 0; #if defined ( LINUX ) sigact.sa_restorer = (void *) 0; #endif #endif /* USE_SIGEMPTYSET */ sigact.sa_handler = catch_sigterm; sigaction(SIGTERM, &sigact, (struct sigaction *) 0); #if !defined ( NOALARM ) sigact.sa_handler = catch_sigalrm; sigaction(SIGALRM, &sigact, (struct sigaction *) 0); #endif /* !NOALARM */ sigact.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sigact, (struct sigaction *) 0); #else /* !USE_SIGACTION */ signal(SIGTERM, catch_sigterm); #if !defined(NOALARM) signal(SIGALRM, catch_sigalrm); #endif /* !NOALARM */ signal(SIGPIPE, SIG_IGN); #endif /* USE_SIGACTION */ #if !defined(NOALARM) beats_per_second = 5; /* Set up a timer to wake us up now and again */ new_timer.it_interval.tv_sec = 0; new_timer.it_interval.tv_usec = 1000000 / beats_per_second; new_timer.it_value.tv_sec = 0; new_timer.it_value.tv_usec = new_timer.it_interval.tv_usec; if (0 > setitimer(ITIMER_REAL, &new_timer, &old_timer)) { perror("ident"); } #endif /* !NOALARM */ #if defined(HAVE_BZERO) #ifdef SUNOS bzero((char *)&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request)); #elif OSF bzero((void *)&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request)); #else bzero(&idents_in_progress[0], MAX_IDENTS_IN_PROGRESS * sizeof(ident_request)); #endif #else /* !HAVE_BZERO */ memset(&idents_in_progress[0], 0, MAX_IDENTS_IN_PROGRESS * sizeof(ident_request)); #endif /* HAVE_BZERO */ /* Now enter the main loop */ status = STATUS_RUNNING; while (status != STATUS_SHUTDOWN) { if (1 == getppid()) { /* If our parent is now PID 1 (init) the talker must have died without * killing us, so we have no business still being here * * time to die... */ exit(0); } check_requests(); check_connections(); #if !defined(NOALARM) sigpause(0); #endif /* !NOALARM */ } return 0; }
/** used in client thread */ void sdl_user::init() { main_config = new config("Lineage.ini"); if (!main_config->config_ok()) { delete main_config; main_config = 0; throw "ERROR Loading configuration file.\n"; } char *test; test = (char*)getfiles->load_file("Sprite00.idx", 0, FILE_REGULAR1, 0); if (test == 0) { throw "Lineage Data not found"; } delete [] test; lineage_font.init("Font/eng.fnt", this); //TODO : make a client specific version of the font unsigned char *sprite_data; int sprite_dlength; sprite_data = (unsigned char*)getfiles->load_file("sprite_table.txt", &sprite_dlength, FILE_REGULAR2, 0); if (sprite_data == 0) { printf("sprite_table.txt not found\n"); } else { int offset = 1; int sprnum = 0; printf("The first number is %d\n", atoi((char*)&sprite_data[offset])); while (isdigit(sprite_data[offset])) { offset++; } if (sprite_data[offset] == '=') { offset += 1; int alias = atoi((char*)&sprite_data[offset]); printf("Alias of %d found\n", alias); while (isdigit(sprite_data[offset])) { offset++; } while (!isdigit(sprite_data[offset])) { offset++; } } char value = atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } int temp; switch (value) { case 100: //switches //read a value, then read 4 times that many values temp = atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } for (int i = 0; i < temp; i++) { atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } atoi((char*)&sprite_data[offset]); while (isdigit(sprite_data[offset])) { offset++; } } break; case 101: //shadow //read one value break; case 102: //objType //read one value break; case 103: //altAttack //read one value break; case 104: //attr //read one value break; case 105: //clothing //read a value //read 0 or that value of numbers, whichever is higher break; case 106: //weapons //? break; case 107: //size //read 2 numbers break; case 108: //flyingType //read one number break; case 109: //immaterial //read two numbers break; case 110: //read a number and modify the stack with it break; case 111: //stride //read one number break; case 112: //furniture //read one number break; case 113: //morphEffect //read one number break; case 114: //furnitureSize //read one number break; default: printf("?(%d)\n", value); break; } while (offset < sprite_dlength) { offset++; } } DesKeyInit("~!@#%^$<"); //TODO : move this code to a class and use an object init_packs(); init_tiles(); change_drawmode(DRAWMODE_LOADING); while (!are_you_ready()) { check_requests(); } draw_loading *load; int what_server; load = (draw_loading*)get_drawmode(false); //wait for the user to pick a server do { what_server = load->get_server_pick(); check_requests(); } while (what_server == -1); server_name = new char[strlen(main_config->get_name(what_server)) + 1]; strcpy(server_name, main_config->get_name(what_server)); server = new connection(main_config, what_server); proc = new packet(server, this); if (get_updates(server, load) > 0) { } //check for custom opcodes unsigned char *copcodes; copcodes = (unsigned char*)getfiles->load_file("opcodes.txt", 0, FILE_REGULAR3, 0); if (copcodes != 0) { //there are custom opcodes for this server printf("This server has custom opcodes\n"); int offset; char *data = (char*)copcodes; for (int i = 0; i < 256; i++) { char *p; p = strtok(data, "\n"); if (data != NULL) data = NULL; int temp; sscanf(p, "%d", &temp); convert_client_packets[i] = (unsigned char)temp; } for (int i = 0; i < 256; i++) { char *p; p = strtok(data, "\n"); if (data != NULL) data = NULL; int temp; sscanf(p, "%d", &temp); convert_server_packets[i] = (unsigned char)temp; } delete [] copcodes; copcodes = 0; } //begin game portion of client // if (server->connection_ok() == 1) { if (server->change() != 1) { throw "Failed to connect to game server\n"; } } init_codepage(0); init_math_tables(); // printf("STUB Load player config\n"); // printf("STUB Initialize emblem cache\n"); init_strings(); load->load_done(); }
void * manage_list(void *vlibrary) { int exit_status = 0, old_count; char *ent_pnt = "manage_list"; ushort_t delayed; time_t now, short_delay, auto_check; robo_event_t *current, *next; library_t *library = (library_t *)vlibrary; mutex_lock(&library->mutex); /* wait for initialization */ mutex_unlock(&library->mutex); short_delay = 0; old_count = 0; delayed = 0; auto_check = (time(&now) + 5); for (;;) { mutex_lock(&library->list_mutex); /* * See if there in anything to do. We will wait if the * active count is 0 or its equal to the same value it had * when we last woke up and there is a delayed request. */ if (library->active_count == 0 || ((old_count == library->active_count) && delayed)) { timestruc_t wait_time; wait_time.tv_sec = time(&now) + library->un->delay; wait_time.tv_nsec = 0; if ((auto_check >= now) && (auto_check < wait_time.tv_sec)) wait_time.tv_sec = auto_check; if (delayed && (short_delay < wait_time.tv_sec)) wait_time.tv_sec = short_delay; if (wait_time.tv_sec > now) { cond_timedwait(&library->list_condit, &library->list_mutex, &wait_time); if (library->chk_req) { library->chk_req = FALSE; if (library->un->state == DEV_ON) /* * Force a check */ auto_check = 0; } } } /* * Get the current time */ time(&now); if (auto_check <= now) { mutex_unlock(&library->list_mutex); (void) check_requests(library); auto_check = now + library->un->delay; continue; } /* * If there is something on the list . . . */ if ((old_count = library->active_count) == 0) { mutex_unlock(&library->list_mutex); continue; } short_delay = 0; delayed = FALSE; current = library->first; mutex_unlock(&library->list_mutex); do { mutex_lock(&library->list_mutex); /* * If delayed and the time has not expired, * go on tothe next */ next = current->next; if ((current->status.b.delayed) && (current->timeout > now)) { if (short_delay == 0) short_delay = current->timeout; else if (current->timeout < short_delay) short_delay = current->timeout; current = next; /* * Need to know there are delayed requests */ delayed = TRUE; mutex_unlock(&library->list_mutex); continue; } if (current == library->first) library->first = unlink_list(current); else (void) unlink_list(current); current->next = NULL; ETRACE((LOG_NOTICE, "LbEv c %#x n %#x (%d)\n", current, library->first, library->active_count)); library->active_count--; library->un->active = library->active_count; mutex_unlock(&library->list_mutex); /* * Entry is off the list and ready to process */ switch (current->type) { case EVENT_TYPE_INTERNAL: switch (current->request.internal.command) { case ROBOT_INTRL_AUDIT_SLOT: if (start_audit(library, current, current->request.internal.slot)) { /* * Unable to find resources, * delay the request and try * later */ current->status.b.delayed = TRUE; current->timeout = now + 10; delayed = TRUE; add_to_end(library, current); } current = next; break; default: sam_syslog(LOG_ERR, "%s:Bad internal event: %s:%d\n", ent_pnt, __FILE__, __LINE__); break; } break; case EVENT_TYPE_MESS: if (current->request.message.magic != MESSAGE_MAGIC) { sam_syslog(LOG_ERR, "%s: Bad magic %#x.", ent_pnt, current->request.message.magic); current->completion = EAGAIN; disp_of_event(library, current, EBADF); current = next; continue; } if (library->un->state >= DEV_OFF && (current->request.message.command > ACCEPT_DOWN)) { current->completion = EAGAIN; disp_of_event(library, current, EAGAIN); current = next; continue; } switch (current->request.message.command) { case MESS_CMD_SHUTDOWN: if (DBG_LVL(SAM_DBG_DEBUG)) sam_syslog(LOG_DEBUG, "received" " shutdown:%s:%d.\n", __FILE__, __LINE__); post_shutdown(library); threads[SONY_WORK_THREAD] = (thread_t)-1; thr_exit(&exit_status); break; case MESS_CMD_STATE: /* * state_request will put the event * back on the free list when * the command is done. */ state_request(library, current); current = next; break; case MESS_CMD_TAPEALERT: /* * tapealert_request will put the * event back on the * free list when the command is done. */ tapealert_solicit(library, current); current = next; break; case MESS_CMD_SEF: /* * sef_request will put the event * back on the free list when the * command is done. */ sef_solicit(library, current); current = next; break; case MESS_CMD_LABEL: if (label_request(library, current)) { /* * Unable to find resources, * delay the request, try later. */ current->status.b.delayed = TRUE; current->timeout = now + 10; delayed = TRUE; add_to_end(library, current); } current = next; break; case MESS_CMD_MOUNT: /* * mount_request will take care of * putting the event back on free list */ if (mount_request(library, current)) { /* * Unable to find resources, * delay request and try later. */ current->status.b.delayed = TRUE; current->timeout = now + 10; delayed = TRUE; add_to_end(library, current); } current = next; break; case MESS_CMD_LOAD_UNAVAIL: load_unavail_request(library, current); current = next; break; case MESS_CMD_AUDIT: if (start_audit(library, current, current-> request.message.param.audit_request.slot)) { current->status.b.delayed = TRUE; current->timeout = now + 10; delayed = TRUE; add_to_end(library, current); } current = next; break; case MESS_CMD_PREVIEW: (void) check_requests(library); time(&now); auto_check = now + library->un->delay; disp_of_event(library, current, 0); current = next; break; case MESS_CMD_UNLOAD: /* * unload_request will put the event * back on the free list when * the command is done. * unload_request will add the request * to the drive's worklist. */ unload_request(library, current); current = next; break; case MESS_CMD_TODO: todo_request(library, current); current = next; break; case MESS_CMD_ADD: add_to_cat_req(library, current); current = next; break; case MESS_CMD_EXPORT: /* * export_request will add the request * to the * mailbox worklist. */ export_media(library, current); current = next; break; case MESS_CMD_ACK: /* * A no-op. Dispose of event. */ disp_of_event(library, current, 0); current = next; break; default: sam_syslog(LOG_ERR, "%s: Unknown robot command %d.", ent_pnt, current->request.message.command); disp_of_event(library, current, 0); current = next; break; } break; default: sam_syslog(LOG_ERR, "%s: Unknown event type %d.\n", ent_pnt, current->type); disp_of_event(library, current, EBADF); current = next; break; } break; } while (current != NULL); } }