/* *函数名称: base_stop_program *函数功能: signal handler *输入参数: signum Signal number *输出参数: none *返回值 : none */ static void base_stop_program(int signum) { // set stop flag global_shared.continue_run = CONTINUE_EXIT; // run destroy func packet_get_destroy(); net_destroy(); data_transit_destroy(); plugin_manager_destroy(); feature_manager_destroy(); storage_unit_destroy(); base_destroy(); public_destroy(); }
int main(int argc, char *argv[]) { Net network = NULL; int flags = 0; output out = NULL; char * file_name = NULL; flags = check_options(argc, argv); network = read_data(file_name); out = out_new(); dinic(network, &out, flags); out_print(out,flags); out_destroy(out); net_destroy(network); return 0; }
/* :<acct> NET <name> <address> [<nick> [<user> [:<gecos>]]] */ static int db_net(int parc, const char *parv[], void *priv) { struct a_network *net; int line = (int)priv; if (parc < 3 || parc > 6) { a_log(LERROR, "Bad NET line on %d", line); return 0; } net = net_create(parv[1]); if (net == NULL) { a_log(LERROR, "Failed to allocate network!"); return 0; } mowgli_strput(&net->nick, parv[0]); parse_server(net, parv[2]); if (parc > 3) mowgli_strput(&net->nick, parv[3]); if (parc > 4) mowgli_strput(&net->ident, parv[4]); if (parc > 5) mowgli_strput(&net->gecos, parv[5]); if (net_add_to_account(net, parv[0]) < 0) { a_log(LERROR, "Failed to add network %s to account %s", net->name, parv[0]); goto fail; } net_start_connect(net); return 0; fail: net_destroy(net); return 0; }
int main(int argc, char **argv) { int return_status = 1; #define MAIN_SDL_CHECK(expression, error_prefix) { \ if (!(expression)) { \ log_error(error_prefix, SDL_GetError()); \ goto exit; \ } \ } MAIN_SDL_CHECK(SDL_Init(SDL_INIT_VIDEO) == 0, "SDL_Init"); if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) { log_error("IMG_Init", IMG_GetError()); goto exit; } MAIN_SDL_CHECK(SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"), "SDL_SetHint SDL_HINT_RENDER_SCALE_QUALITY"); startup_info_t info = startup(argc, argv); if (!info.success) goto exit; net_mode_t net_mode = info.net_mode; network = info.network; char wtitle[256]; if (net_mode == NET_SERVER) { snprintf(wtitle, sizeof(wtitle), "NetCheckers - server (%s)", info.port); } else { snprintf(wtitle, sizeof(wtitle), "NetCheckers - client (%s:%s)", info.host, info.port); } window = SDL_CreateWindow( wtitle, #if 1 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, #else (net_mode == NET_SERVER ? 10 : window_width + 20), SDL_WINDOWPOS_CENTERED, #endif window_width, window_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI ); MAIN_SDL_CHECK(window, "SDL_CreateWindow"); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); MAIN_SDL_CHECK(renderer, "SDL_CreateRenderer"); MAIN_SDL_CHECK(SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) == 0, "SDL_SetRenderDrawBlendMode SDL_BLENDMODE_BLEND"); char *error = 0; char path[1024]; #define LOAD_TEXTURE_CHECKED(var, file) { \ sprintf(path, "%s/" file, info.assets_path); \ var = load_png_texture(path, &error); \ if (error) { \ log_error("load_png_texture " file, error); \ goto exit; \ } \ } LOAD_TEXTURE_CHECKED(tex.textures.board, "board.png"); LOAD_TEXTURE_CHECKED(tex.textures.red_piece, "piece_red.png"); LOAD_TEXTURE_CHECKED(tex.textures.red_piece_king, "piece_red_king.png"); LOAD_TEXTURE_CHECKED(tex.textures.white_piece, "piece_white.png"); LOAD_TEXTURE_CHECKED(tex.textures.white_piece_king, "piece_white_king.png"); LOAD_TEXTURE_CHECKED(tex.textures.highlight, "highlight.png"); LOAD_TEXTURE_CHECKED(tex.textures.player_turn, "player_turn.png"); LOAD_TEXTURE_CHECKED(tex.textures.opponent_turn, "opponent_turn.png"); LOAD_TEXTURE_CHECKED(tex.textures.victory, "victory.png"); LOAD_TEXTURE_CHECKED(tex.textures.defeat, "defeat.png"); window_resized(window_width, window_height); // Put the pieces on the board #if 0 // Game over testing for (int i = 0; i < 24; i++) { piece_t *piece = pieces + i; piece->color = (i >= 12) ? PIECE_BLACK : PIECE_WHITE; piece->captured = true; } pieces[0].captured = false; pieces[0].king = true; pieces[0].pos = cell_pos(1, 3); board[1][3] = &pieces[0]; pieces[12].captured = false; pieces[12].king = true; pieces[12].pos = cell_pos(5, 3); board[5][3] = &pieces[12]; #else int fill_row = 0; int fill_col = 0; for (int i = 0; i < 12; i++) { piece_t *piece = pieces + i; piece->color = PIECE_WHITE; piece->pos = cell_pos(fill_row, fill_col); board[fill_row][fill_col] = piece; advance_board_row_col(&fill_row, &fill_col); } fill_row = 5; fill_col = 1; for (int i = 12; i < 24; i++) { piece_t *piece = pieces + i; piece->color = PIECE_BLACK; piece->pos = cell_pos(fill_row, fill_col); board[fill_row][fill_col] = piece; advance_board_row_col(&fill_row, &fill_col); } #endif game_over = false; current_turn = PIECE_BLACK; local_color = (net_mode == NET_SERVER) ? PIECE_BLACK : PIECE_WHITE; bool running = true; int last_time = SDL_GetTicks(); while (running) { int current_time = SDL_GetTicks(); int ellapsed_ms = current_time - last_time; last_time = current_time; float delta_time = (float)ellapsed_ms / 1000.0f; SDL_Event event = {0}; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: { running = false; } break; case SDL_WINDOWEVENT: { if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { window_resized(event.window.data1, event.window.data2); } } break; case SDL_MOUSEBUTTONDOWN: { #if 1 if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_RIGHT) { message_t net_msg = {0}; net_msg.move_piece = cell_pos(5, 1); net_msg.move_target = cell_pos(4, 0); net_send_message(network, &net_msg); } #endif if (event.button.state == SDL_PRESSED && event.button.button == SDL_BUTTON_LEFT) { if (!game_over && !animating_piece && current_turn == local_color) { int click_x = event.button.x * dpi_rate; int click_y = event.button.y * dpi_rate; if (rect_includes(&board_rect, click_x, click_y)) { cell_pos_t clicked_cell = point_to_cell(click_x, click_y); piece_t *clicked_piece = board[clicked_cell.row][clicked_cell.col]; if (clicked_piece && clicked_piece->color == current_turn) { piece_moves_t moves = find_valid_moves(clicked_piece); if (moves.count) { selected_piece = clicked_piece; available_moves = moves; } } else if (selected_piece) { cell_pos_t from_cell = selected_piece->pos; move_result_t res = perform_move(selected_piece, clicked_cell); if (res != MOVE_INVALID) { if (res == MOVE_END_TURN) { selected_piece = 0; } else { available_moves = find_valid_moves(selected_piece); } message_t net_msg = {0}; net_msg.move_piece = from_cell; net_msg.move_target = clicked_cell; if (!net_send_message(network, &net_msg)) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Falha de comunicação", "Falha ao enviar movimento para o adversário.", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } } } } } } } break; } } if (net_get_state(network) != NET_RUNNING) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Conexão Interrompida", "A conexão foi interrompida pelo adversário", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } message_t net_msg; if (net_poll_message(network, &net_msg)) { bool valid_move = false; piece_t *piece = board[net_msg.move_piece.row][net_msg.move_piece.col]; if (piece && current_turn != local_color && piece->color != local_color) { move_result_t res = perform_move(piece, net_msg.move_target); if (res != MOVE_INVALID) valid_move = true; } if (!valid_move) { int err = SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR, "Erro - Movimento inválido", "Seu adversário enviou um movimento inválido.", window ); if (err) { log_error("SDL_ShowSimpleMessageBox invalid movement", SDL_GetError()); } goto exit; } } render(delta_time); } return_status = 0; exit: if (network) net_destroy(network); for (int i = 0; i < ARRAY_SIZE(tex.array); i++) { if (tex.array[i]) SDL_DestroyTexture(tex.array[i]); } if (renderer) SDL_DestroyRenderer(renderer); if (window) SDL_DestroyWindow(window); IMG_Quit(); SDL_Quit(); return return_status; }
int main_loop() { struct hub_config configuration; struct acl_handle acl; struct hub_info* hub = 0; if (net_initialize() == -1) return -1; do { if (hub) { LOG_INFO("Reloading configuration files..."); LOG_DEBUG("Hub status: %d", (int) hub->status); /* Reinitialize logs */ hub_log_shutdown(); hub_log_initialize(arg_log, arg_log_syslog); hub_set_log_verbosity(arg_verbose); } if (read_config(arg_config, &configuration, !arg_have_config) == -1) return -1; if (acl_initialize(&configuration, &acl) == -1) return -1; /* * Don't restart networking when re-reading configuration. * This might not be possible either, since we might have * dropped our privileges to do so. */ if (!hub) { hub = hub_start_service(&configuration); if (!hub) { acl_shutdown(&acl); free_config(&configuration); net_destroy(); hub_log_shutdown(); return -1; } #if !defined(WIN32) setup_signal_handlers(hub); #ifdef SYSTEMD /* Notify the service manager that this daemon has * been successfully initalized and shall enter the * main loop. */ sd_notifyf(0, "READY=1\n" "MAINPID=%lu", (unsigned long) getpid()); #endif /* SYSTEMD */ #endif /* ! WIN32 */ } hub_set_variables(hub, &acl); hub_event_loop(hub); hub_free_variables(hub); acl_shutdown(&acl); free_config(&configuration); } while (hub->status == hub_status_restart); #if !defined(WIN32) shutdown_signal_handlers(hub); #endif if (hub) { hub_shutdown_service(hub); } net_destroy(); hub_log_shutdown(); return 0; }