void handle_player(int sock) { int z, prevx, prevy; struct plyr *p = malloc(sizeof(struct plyr)); p->fd = sock; p->x = p->y = p->lastdump = 0; p->worldx = p->worldy = 0; p->location = NULL; p->location = (struct loc *) get_world(p, 0,0); if(p->location == NULL) { printf("FAILED TO GET WORLD!\n"); close(p->fd); return; } startmsg(p->fd); while( read_byte(p->fd) == 0); dump_world(p); while(1){ prevx = p->x; prevy = p->y; //handle input if(handle_input(p)){ printf("disconnecting client\n"); break; } //display if(p->x != prevx || p->y != prevy) { //printf("trying to display @ %d, %d: %d\n", prevx, prevy, CHARAT(p,prevx, prevy)); write_byte(p->fd, CHARAT(p,prevx, prevy)); move_cursor(p->fd, p->x, p->y); //printf("now at %d,%d: %d\n",p->x,p->y, CHARAT(p,p->x,p->y)); } check_updates(p); } send(p->fd, "Goodbye\n", 8, 0); close(p->fd); }
static void* check_updates_worker(void* arg){ timing_info timing; reset_timing(&timing); while(!stop){ start_timing(&timing); if(!updated && push_commit){ if(push_commit){ pthread_mutex_lock(&commit_mutex); if(push_result_shell(push_commit, NULL)){ puts("Failed to push coin, reseting."); pthread_mutex_lock(&update_mutex); fetch_updates(); updated = 1; pthread_mutex_unlock(&update_mutex); } else { puts("Earned it!"); } push_commit = NULL; pthread_mutex_unlock(&commit_mutex); } else { if(check_updates()){ pthread_mutex_lock(&update_mutex); updated = 1; pthread_mutex_unlock(&update_mutex); } } time_point(&timing); print_timing(&timing); } else { skip_point(&timing); usleep(10); } } puts("Update thread ending"); pthread_exit(NULL); }
/* * N-way merge "len" trees. Returns 0 on success, -1 on failure to manipulate the * resulting index, -2 on failure to reflect the changes to the work tree. * * CE_ADDED, CE_UNPACKED and CE_NEW_SKIP_WORKTREE are used internally */ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o) { int i, ret; static struct cache_entry *dfc; struct exclude_list el; if (len > MAX_UNPACK_TREES) die("unpack_trees takes at most %d trees", MAX_UNPACK_TREES); memset(&state, 0, sizeof(state)); state.base_dir = ""; state.force = 1; state.quiet = 1; state.refresh_cache = 1; state.istate = &o->result; memset(&el, 0, sizeof(el)); if (!core_apply_sparse_checkout || !o->update) o->skip_sparse_checkout = 1; if (!o->skip_sparse_checkout) { char *sparse = git_pathdup("info/sparse-checkout"); if (add_excludes_from_file_to_list(sparse, "", 0, &el, 0) < 0) o->skip_sparse_checkout = 1; else o->el = ⪙ free(sparse); } memset(&o->result, 0, sizeof(o->result)); o->result.initialized = 1; o->result.timestamp.sec = o->src_index->timestamp.sec; o->result.timestamp.nsec = o->src_index->timestamp.nsec; o->result.version = o->src_index->version; o->result.split_index = o->src_index->split_index; if (o->result.split_index) o->result.split_index->refcount++; hashcpy(o->result.sha1, o->src_index->sha1); o->merge_size = len; mark_all_ce_unused(o->src_index); /* * Sparse checkout loop #1: set NEW_SKIP_WORKTREE on existing entries */ if (!o->skip_sparse_checkout) mark_new_skip_worktree(o->el, o->src_index, 0, CE_NEW_SKIP_WORKTREE); if (!dfc) dfc = xcalloc(1, cache_entry_size(0)); o->df_conflict_entry = dfc; if (len) { const char *prefix = o->prefix ? o->prefix : ""; struct traverse_info info; setup_traverse_info(&info, prefix); info.fn = unpack_callback; info.data = o; info.show_all_errors = o->show_all_errors; info.pathspec = o->pathspec; if (o->prefix) { /* * Unpack existing index entries that sort before the * prefix the tree is spliced into. Note that o->merge * is always true in this case. */ while (1) { struct cache_entry *ce = next_cache_entry(o); if (!ce) break; if (ce_in_traverse_path(ce, &info)) break; if (unpack_index_entry(ce, o) < 0) goto return_failed; } } if (traverse_trees(len, t, &info) < 0) goto return_failed; } /* Any left-over entries in the index? */ if (o->merge) { while (1) { struct cache_entry *ce = next_cache_entry(o); if (!ce) break; if (unpack_index_entry(ce, o) < 0) goto return_failed; } } mark_all_ce_unused(o->src_index); if (o->trivial_merges_only && o->nontrivial_merge) { ret = unpack_failed(o, "Merge requires file-level merging"); goto done; } if (!o->skip_sparse_checkout) { int empty_worktree = 1; /* * Sparse checkout loop #2: set NEW_SKIP_WORKTREE on entries not in loop #1 * If the will have NEW_SKIP_WORKTREE, also set CE_SKIP_WORKTREE * so apply_sparse_checkout() won't attempt to remove it from worktree */ mark_new_skip_worktree(o->el, &o->result, CE_ADDED, CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE); ret = 0; for (i = 0; i < o->result.cache_nr; i++) { struct cache_entry *ce = o->result.cache[i]; /* * Entries marked with CE_ADDED in merged_entry() do not have * verify_absent() check (the check is effectively disabled * because CE_NEW_SKIP_WORKTREE is set unconditionally). * * Do the real check now because we have had * correct CE_NEW_SKIP_WORKTREE */ if (ce->ce_flags & CE_ADDED && verify_absent(ce, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) { if (!o->show_all_errors) goto return_failed; ret = -1; } if (apply_sparse_checkout(&o->result, ce, o)) { if (!o->show_all_errors) goto return_failed; ret = -1; } if (!ce_skip_worktree(ce)) empty_worktree = 0; } if (ret < 0) goto return_failed; /* * Sparse checkout is meant to narrow down checkout area * but it does not make sense to narrow down to empty working * tree. This is usually a mistake in sparse checkout rules. * Do not allow users to do that. */ if (o->result.cache_nr && empty_worktree) { ret = unpack_failed(o, "Sparse checkout leaves no entry on working directory"); goto done; } } o->src_index = NULL; ret = check_updates(o) ? (-2) : 0; if (o->dst_index) { if (!ret) { if (!o->result.cache_tree) o->result.cache_tree = cache_tree(); if (!cache_tree_fully_valid(o->result.cache_tree)) cache_tree_update(&o->result, WRITE_TREE_SILENT | WRITE_TREE_REPAIR); } discard_index(o->dst_index); *o->dst_index = o->result; } else { discard_index(&o->result); } done: clear_exclude_list(&el); return ret; return_failed: if (o->show_all_errors) display_error_msgs(o); mark_all_ce_unused(o->src_index); ret = unpack_failed(o, NULL); if (o->exiting_early) ret = 0; goto done; }
int main (int argc, char **argv) { git_index *index = NULL; pthread_t updateThread, hashThread; int rc, difficulty; void *status; hash_args args; timing_info timing; git_oid curr_commit; reset_timing(&timing); pthread_mutex_init(&commit_mutex, NULL); pthread_mutex_init(&update_mutex, NULL); push_commit = NULL; difficulty = init_args(&args); init_git(&index); init_hasher(difficulty); check_updates(); reset_hard(); puts("Starting update thread"); rc = pthread_create(&updateThread, NULL, check_updates_worker, NULL); if (rc){ printf("ERROR creating update thread %d\n", rc); exit(-1); } signal (SIGINT, int_handler); while(!stop){ start_timing(&timing); args.found = 0; time_point(&timing); pthread_mutex_lock(&commit_mutex); pthread_mutex_lock(&update_mutex); if(updated){ reset_hard(); updated = 0; push_commit = NULL; } pthread_mutex_unlock(&update_mutex); pthread_mutex_unlock(&commit_mutex); time_point(&timing); puts("Preparing index"); prepare_index(index, args.msg); time_point(&timing); puts("Starting brute force thread"); rc = pthread_create(&hashThread, NULL, force_hash, &args); time_point(&timing); if (rc){ printf("ERROR creating hash thread %d\n", rc); stop = 1; } else { pthread_join(hashThread, &status); } time_point(&timing); if(!stop && !updated && args.found){ puts("Found one!"); while(push_commit){ usleep(10); } time_point(&timing); if(!stop && !updated){ pthread_mutex_lock(&commit_mutex); commit_result(args.msg, &curr_commit); push_commit = &curr_commit; pthread_mutex_unlock(&commit_mutex); } } else { puts("Reset while looking for a hash"); time_point(&timing); } time_point(&timing); print_timing(&timing); } pthread_join(updateThread, &status); free_hasher(); free(args.msg); git_index_free(index); git_repository_free(repo); git_threads_shutdown(); return 0; }
/*-----------------------------------------------------------------------------------*/ unsigned short ctk_term_send(struct ctk_term_state* ts, unsigned char* buf, unsigned short maxlen) { unsigned char x, y, x0; unsigned char col, c; unsigned short tmp; unsigned short totlen; check_updates(ts); if (ts->updates_current == NULL) return 0; x0 = ts->x1; col = ts->c1; totlen = 0; /* Loop across the update region starting at (x1,y1) */ for(y = ts->y1; y < ts->y + ts->h; ++y) { for(x = x0; x < ts->x + ts->w; ++x) { /* New line ? */ if (x == ts->x) { /* Move cursor to start of line */ tmp = move_to(x,y,buf,maxlen); if (tmp == 0) goto loopend; buf += tmp; totlen += tmp; maxlen -= tmp; } /* Check color */ c = colorscreen[x + y * CHARS_WIDTH]; if (c != col) { PRINTF(("colorchange at (%d, %d) to %d\n", x,y,c)); /* Send new color information */ tmp = set_color(c, buf, maxlen); if (tmp == 0) goto loopend; col = c; buf += tmp; totlen += tmp; maxlen -= tmp; } /* Check remaining space */ if (maxlen < 1) goto loopend; /* Add character */ *buf = screen[x + y * CHARS_WIDTH]; buf++; maxlen--; totlen++; } x0 = ts->x; } loopend: /* Always save current color state */ ts->c2 = col; PRINTF(("ending loop at (%d, %d)\n", x,y)); /* Check if done */ if (x == ts->x+ts->w && y == ts->y+ts->h) { /* Signal done with this update */ ts->x2 = ts->y2 = 0; } else { /* Not done. Save state */ ts->x2 = x; ts->y2 = y; } return totlen; }
int main(int argc, char *argv[]) { char query[100]; const char *name, *sub; int i, id, ts, inputid, maxid = 0; double value; input_t *input; sqlite3_stmt *stmt; if (argc > 1) read_config(argv[1]); else read_config(NULL); if (!settings.sqlitefile) { fprintf(stderr, "No sqlite database defined in %s", argc>1?argv[1]:CONFIG_FILE); return EXIT_FAILURE; } if (sqlite3_open(settings.sqlitefile, &settings.sqlitehandle) != SQLITE_OK) { fprintf(stderr, "Failed to open sqlite database '%s': %s\n", settings.sqlitefile, sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } sqlite3_prepare_v2(settings.sqlitehandle, "SELECT `id`, `name`, `sub` FROM inputs", 40, &stmt, NULL); if (!stmt) { fprintf(stderr, "Failed to prepare query for inputs on SQLite db: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } while ((i = sqlite3_step(stmt)) == SQLITE_ROW) { if (sqlite3_column_count(stmt) != 3) break; id = sqlite3_column_int(stmt, 0); name = sqlite3_column_text(stmt, 1); sub = sqlite3_column_text(stmt, 2); for (input = inputs; input; input = input->next) { if (!strcmp(input->name, name)) { if (sub) input = add_input((char *)sub, input); input->sqlid = id; } } } if (i != SQLITE_DONE) { fprintf(stderr, "Error while reading inputs from SQLite db: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } snprintf(query, 100, "SELECT rowid, ts, value FROM data WHERE input = ? ORDER BY ts DESC LIMIT %d", block_width()-9); signal(SIGWINCH, do_winch); ioctl(0, TIOCGWINSZ, &settings.ws); go_ncurses(); for (input = inputs; input; input = input->next) { if (!input->sqlid) continue; sqlite3_prepare_v2(settings.sqlitehandle, query, -1, &stmt, NULL); if (!stmt) { fprintf(stderr, "Failed to prepare query for latest data: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } if (sqlite3_bind_int(stmt, 1, input->sqlid) != SQLITE_OK) { fprintf(stderr, "Failed to bind param 1 on initial data query: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } while ((i = sqlite3_step(stmt)) == SQLITE_ROW) { if (sqlite3_column_count(stmt) != 3) break; id = sqlite3_column_int(stmt, 0); ts = sqlite3_column_int(stmt, 1); value = sqlite3_column_double(stmt, 2); if (id > maxid) maxid = id; if (input->vallast-input->valhist == VALUE_HIST_SIZE-1) input->vallast = input->valhist; else input->vallast++; input->valcnt++; if (input->update < ts) input->update = ts; *input->vallast = value; } sqlite3_finalize(stmt); if (i != SQLITE_DONE) fprintf(stderr, "Error while reading initial data from SQLite db: %s\n", sqlite3_errmsg(settings.sqlitehandle)); update_block(input); } while (1) { if (settings.winch) { ioctl(0, TIOCGWINSZ, &settings.ws); refresh(); resizeterm(settings.ws.ws_row, settings.ws.ws_col); arrange_blocks(); settings.winch = 0; } sqlite3_prepare_v2(settings.sqlitehandle, "SELECT rowid, input, ts, value FROM data WHERE rowid > ? ORDER BY ts", 69, &stmt, NULL); if (!stmt) { fprintf(stderr, "Failed to prepare query for latest data on SQLite db: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } if (sqlite3_bind_int(stmt, 1, maxid) != SQLITE_OK) { fprintf(stderr, "Error binding param 1 for latest data query: %s\n", sqlite3_errmsg(settings.sqlitehandle)); return EXIT_FAILURE; } while ((i = sqlite3_step(stmt)) == SQLITE_ROW) { if (sqlite3_column_count(stmt) != 4) break; id = sqlite3_column_int(stmt, 0); inputid = sqlite3_column_int(stmt, 1); ts = sqlite3_column_int(stmt, 2); value = sqlite3_column_double(stmt, 3); if (id > maxid) maxid = id; for (input = inputs; input; input = input->next) { if (inputid == input->sqlid) { if (input->vallast-input->valhist == VALUE_HIST_SIZE-1) input->vallast = input->valhist; else input->vallast++; input->valcnt++; input->update = ts; *input->vallast = value; update_block(input); } } } sqlite3_finalize(stmt); if (i != SQLITE_DONE) fprintf(stderr, "Error while reading data from SQLite db: %s\n", sqlite3_errmsg(settings.sqlitehandle)); check_updates(); sleep(60); } }
error_code ContentDirectory::Browse(const client_info_ptr& client, const http::http_request& /*http_request*/, /* IN */ const std::string& ObjectID, /* IN */ A_ARG_TYPE_BrowseFlag BrowseFlag, /* IN */ const std::string& Filter, /* IN */ ui4 StartingIndex, /* IN */ ui4 RequestedCount, /* IN */ const std::string& /*SortCriteria*/, /* OUT */ std::string& Result, /* OUT */ ui4& NumberReturned, /* OUT */ ui4& TotalMatches, /* OUT */ ui4& UpdateID) { if (BrowseFlag == A_ARG_TYPE_BrowseFlag_UNKNOWN) return error::invalid_action; std::ostringstream value; NumberReturned = 0; TotalMatches = 0; UpdateID = m_device->system_update_id(); { value << R"(<DIDL-Lite xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/">)" "\n"; auto item = m_device->get_item(ObjectID); if (item) log::debug() << type_info<A_ARG_TYPE_BrowseFlag>::to_string(BrowseFlag) << " [" << ObjectID << "] \"" << item->get_title() << "\""; else log::warning() << type_info<A_ARG_TYPE_BrowseFlag>::to_string(BrowseFlag) << " [" << ObjectID << "] failed"; if (item) { auto client_ptr = std::static_pointer_cast<client_interface>(client); auto filter = parse_filter(Filter); if (BrowseFlag == VALUE_BrowseDirectChildren) { auto children = item->list(StartingIndex, RequestedCount); for (auto && child : children) { log::debug() << " [" << child->get_objectId_attr() << "] \"" << child->get_title() << "\""; child->output(value, filter, client_ptr, m_device->config()); } NumberReturned = children.size(); TotalMatches = item->predict_count(StartingIndex + NumberReturned); } else { item->check_updates(); item->output(value, filter, client_ptr, m_device->config()); NumberReturned = 1; TotalMatches = 1; } // could change during the item->list UpdateID = item->update_id(); } value << "</DIDL-Lite>"; } Result = std::move(value.str()); return error::no_error; }