int hashify_check_local_consistency(string_const_t string, hash_t hash_value, const hashify_string_t* local_hashes) { size_t ilocal, localsize; for (ilocal = 0, localsize = array_size(local_hashes); ilocal < localsize; ++ilocal) { if (local_hashes[ilocal].hash == hash_value) { if (!string_equal(local_hashes[ilocal].string.str, local_hashes[ilocal].string.length, string.str, string.length)) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash string mismatch, \"%.*s\" with hash 0x%" PRIx64 " stored in output file, read \"%.*s\" from input file"), STRING_FORMAT(local_hashes[ilocal].string), local_hashes[ilocal].hash, STRING_FORMAT(string)); return HASHIFY_RESULT_HASH_STRING_MISMATCH; } break; } else if (string_equal(local_hashes[ilocal].string.str, local_hashes[ilocal].string.length, string.str, string.length)) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash mismatch, \"%.*s\" with hash 0x%" PRIx64 " stored in output file, read \"%.*s\" with hash 0x%" PRIx64 " from input file"), STRING_FORMAT(local_hashes[ilocal].string), local_hashes[ilocal].hash, STRING_FORMAT(string), hash_value); return HASHIFY_RESULT_HASH_MISMATCH; } } if (ilocal == localsize) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash missing in output file, \"%.*s\" with hash 0x%" PRIx64), STRING_FORMAT(string), hash_value); return HASHIFY_RESULT_HASH_MISSING; } return HASHIFY_RESULT_OK; }
int hashify_write_file(stream_t* generated_file, string_t output_filename) { bool need_update = false; stream_t* output_file = 0; int result = HASHIFY_RESULT_OK; output_file = stream_open(STRING_ARGS(output_filename), STREAM_OUT | STREAM_IN); if (!output_file) { need_update = true; output_file = stream_open(STRING_ARGS(output_filename), STREAM_OUT); if (!output_file) { log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open output file: %.*s"), STRING_FORMAT(output_filename)); return HASHIFY_RESULT_MISSING_OUTPUT_FILE; } } if (!need_update) need_update = !uint128_equal(stream_md5(generated_file), stream_md5(output_file)); if (need_update) { char local_buffer[1024]; size_t read = 0; size_t written = 0; uint64_t total_written = 0; stream_seek(generated_file, 0, STREAM_SEEK_BEGIN); stream_seek(output_file, 0, STREAM_SEEK_BEGIN); while (!stream_eos(generated_file)) { read = stream_read(generated_file, local_buffer, 1024); if (!read) break; written = stream_write(output_file, local_buffer, read); total_written += written; if (written != read) { log_errorf(0, ERROR_SYSTEM_CALL_FAIL, STRING_CONST("Unable to write to output file '%.*s': %" PRIsize " of %" PRIsize " bytes written"), STRING_FORMAT(output_filename), written, read); result = HASHIFY_RESULT_OUTPUT_FILE_WRITE_FAIL; break; } } if (result == HASHIFY_RESULT_OK) { stream_truncate(output_file, (size_t)total_written); log_infof(0, STRING_CONST(" wrote %.*s : %" PRIu64 " bytes"), STRING_FORMAT(output_filename), total_written); } } else { log_info(0, STRING_CONST(" hash file already up to date")); } stream_deallocate(output_file); return result; }
void UIOS::OnReadOSMaxMin() { vector<ULONG> ipList; GetIPList(ipList); float max, min; UCHAR monitorFlag; for(UINT i=0; i<ipList.size(); ++i) { bool rt = GetOSMaxMinPMSyn( 2, monitorFlag, max, min, ipList[i] ); if( rt==true ) { if( monitorFlag==1 ) _grid21.SetCellString( i+1, 2, "1" ); else _grid21.SetCellString( i+1, 2, "0" ); string str; STRING_FORMAT( str, "%0.2f", max ); _grid21.SetCellString( i+1, 3, str ); STRING_FORMAT( str, "%0.2f", min ); _grid21.SetCellString( i+1, 4, str ); } else { _grid21.SetCellString( i+1, 2,"超时" ); } } }
void mutex_signal(mutex_t* mutex) { #if !BUILD_DEPLOY profile_signal(mutex->name.str, mutex->name.length); #endif #if FOUNDATION_PLATFORM_WINDOWS SetEvent(mutex->event); #elif FOUNDATION_PLATFORM_POSIX || FOUNDATION_PLATFORM_PNACL mutex_lock(mutex); mutex->pending = true; int ret = pthread_cond_broadcast(&mutex->cond); if (ret != 0) { string_const_t errmsg = system_error_message(ret); log_errorf(0, ERROR_SYSTEM_CALL_FAIL, STRING_CONST("Unable to signal mutex '%.*s': %.*s (%d)"), STRING_FORMAT(mutex->name), STRING_FORMAT(errmsg), ret); } mutex_unlock(mutex); #else # error mutex_signal not implemented #endif }
void UIOS::ShowKB( float k, float b ) { string str; STRING_FORMAT( str, "%f", k ); _grid2.SetCellString( 1, 1, str ); STRING_FORMAT( str, "%f", b ); _grid2.SetCellString( 2, 1, str ); }
bool UIOS::ShowResistance( ULONG IBMON, ULONG IMPDMON, ULONG resistance ) { string str; bool rt = true; float IBMONLimit, IMPDMONLimit, resistanceLimit; // _grid3.GetCellString( 1, 3, str ); IBMONLimit = ( float )atof( str.c_str() ); // _grid3.GetCellString( 2, 3, str ); IMPDMONLimit = ( float )atof( str.c_str() ); // _grid3.GetCellString( 3, 3, str ); resistanceLimit = ( float )atof( str.c_str() ); float k1, k2; // _grid3.GetCellString( 1, 1, str ); k1 = ( float )atof( str.c_str() ); // _grid3.GetCellString( 2, 1, str ); k2 = ( float )atof( str.c_str() ); float IBMONTemp, IMPDMONTemp, resistanceTemp; IBMONTemp = IBMON*k1; IMPDMONTemp = IMPDMON*k2; resistanceTemp = 25+( resistance+1 )/1024.f*25000; STRING_FORMAT( str, "%0.2f", IBMONTemp ); _grid3.SetCellString( 1, 2, str ); if( IBMONTemp>IBMONLimit ) { rt = false; _grid3.SetCellColor( 1, 2, _RED_ ); } STRING_FORMAT( str, "%0.2f", IMPDMONTemp ); _grid3.SetCellString( 2, 2, str ); if( IMPDMONTemp>IMPDMONLimit ) { rt = false; _grid3.SetCellColor( 2, 2, _RED_ ); } STRING_FORMAT( str, "%0.2f", resistanceTemp ); _grid3.SetCellString( 3, 2, str ); if( resistanceTemp<resistanceLimit ) { rt = false; _grid3.SetCellColor( 3, 2, _RED_ ); } return rt; }
int assert_report(hash_t context, const char* condition, size_t cond_length, const char* file, size_t file_length, unsigned int line, const char* msg, size_t msg_length) { static const char nocondition[] = "<Static fail>"; static const char nofile[] = "<No file>"; static const char nomsg[] = "<No message>"; static const char assert_format[] = "****** ASSERT FAILED ******\nCondition: %.*s\nFile/line: %.*s : %d\n%.*s%.*s\n%.*s\n"; #if BUILD_ENABLE_ASSERT string_t tracestr = { _assert_stacktrace_buffer, sizeof(_assert_stacktrace_buffer) }; string_t contextstr = { _assert_context_buffer, sizeof(_assert_context_buffer) }; string_t messagestr = { _assert_message_buffer, sizeof(_assert_message_buffer) }; #endif if (!condition || !cond_length) { condition = nocondition; cond_length = sizeof(nocondition); } if (!file || !file_length) { file = nofile; file_length = sizeof(nofile); } if (!msg || !msg_length) { msg = nomsg; msg_length = sizeof(nomsg); } if (_assert_handler && (_assert_handler != assert_report)) return (*_assert_handler)(context, condition, cond_length, file, file_length, line, msg, msg_length); #if BUILD_ENABLE_ASSERT contextstr = error_context_buffer(STRING_ARGS(contextstr)); if (foundation_is_initialized()) { size_t num_frames = stacktrace_capture(_assert_stacktrace, ASSERT_STACKTRACE_MAX_DEPTH, ASSERT_STACKTRACE_SKIP_FRAMES); if (num_frames) tracestr = stacktrace_resolve(STRING_ARGS(tracestr), _assert_stacktrace, num_frames, 0U); else tracestr = string_copy(STRING_ARGS(tracestr), STRING_CONST("<no stacktrace>")); } else { tracestr = string_copy(STRING_ARGS(tracestr), STRING_CONST("<no stacktrace - not initialized>")); } messagestr = string_format(STRING_ARGS(messagestr), assert_format, sizeof(assert_format) - 1, (int)cond_length, condition, (int)file_length, file, line, STRING_FORMAT(contextstr), (int)msg_length, msg, STRING_FORMAT(tracestr)); log_errorf(context, ERROR_ASSERT, STRING_CONST("%.*s"), STRING_FORMAT(messagestr)); system_message_box(STRING_CONST("Assert Failure"), STRING_ARGS(messagestr), false); #else log_errorf(context, ERROR_ASSERT, assert_format, sizeof(assert_format) - 1, (int)cond_length, condition, (int)file_length, file, line, 0, "", (int)msg_length, msg, 0, ""); #endif return 1; }
int main_run(void* main_arg) { int result = RENDERCOMPILE_RESULT_OK; rendercompile_input_t input = rendercompile_parse_command_line(environment_command_line()); FOUNDATION_UNUSED(main_arg); if (input.display_help) { rendercompile_print_usage(); goto exit; } resource_source_set_path(STRING_ARGS(input.source_path)); resource_compile_register(render_compile); size_t ifile, fsize; for (ifile = 0, fsize = array_size(input.input_files); ifile < fsize; ++ifile) { uuid_t uuid = string_to_uuid(STRING_ARGS(input.input_files[ifile])); if (uuid_is_null(uuid)) { char buffer[BUILD_MAX_PATHLEN]; string_t pathstr = string_copy(buffer, sizeof(buffer), STRING_ARGS(input.input_files[ifile])); pathstr = path_clean(STRING_ARGS(pathstr), sizeof(buffer)); pathstr = path_absolute(STRING_ARGS(pathstr), sizeof(buffer)); uuid = resource_import_map_lookup(STRING_ARGS(pathstr)); } if (uuid_is_null(uuid)) { log_warnf(HASH_RESOURCE, WARNING_INVALID_VALUE, STRING_CONST("Failed to lookup: %.*s"), STRING_FORMAT(input.input_files[ifile])); result = RENDERCOMPILE_RESULT_INVALID_INPUT; break; } if (resource_compile(uuid, RESOURCE_PLATFORM_ALL)) { string_const_t uuidstr = string_from_uuid_static(uuid); log_infof(HASH_RESOURCE, STRING_CONST("Successfully compiled: %.*s (%.*s)"), STRING_FORMAT(uuidstr), STRING_FORMAT(input.input_files[ifile])); } else { string_const_t uuidstr = string_from_uuid_static(uuid); log_warnf(HASH_RESOURCE, WARNING_UNSUPPORTED, STRING_CONST("Failed to compile: %.*s (%.*s)"), STRING_FORMAT(uuidstr), STRING_FORMAT(input.input_files[ifile])); } } exit: array_deallocate(input.input_files); return result; }
void UIOS::ShowOSOutPutPMList( UCHAR portNum, float* pmList ) { string str; STRING_FORMAT( str, "%d", portNum ); _grid1.SetCellString( 1, 2, str, false ); for( int i=0; i<portNum; ++i ) { STRING_FORMAT( str, "%0.2f", pmList[i] ); _grid1.SetCellString( 2+i, 1, str, false ); } UIRefresh(); }
void UIOS::ShowVerifyKB( float* readPM, float* referPM ) { string str; for( int i=0; i<3; ++i ) { STRING_FORMAT( str, "%0.2f", readPM[i] ); _grid4.SetCellString( i+1, 1, str ); STRING_FORMAT( str, "%0.2f", referPM[i] ); _grid4.SetCellString( i+1, 2, str ); STRING_FORMAT( str, "%0.2f", readPM[i]-referPM[i] ); _grid4.SetCellString( i+1, 3, str ); } }
static void blast_client_read_ack(blast_client_t* client) { const network_address_t* address = 0; char databuf[PACKET_DATABUF_SIZE]; size_t size = udp_socket_recvfrom(client->sock, databuf, sizeof(databuf), &address); while (size > 0) { packet_t* packet = (packet_t*)databuf; if (network_address_equal(address, socket_address_remote(client->sock))) { if (packet->type == PACKET_ACK) { packet_ack_t* ack = (packet_ack_t*)packet; blast_client_process_ack(client, ack->ack, packet->timestamp); } else if (packet->type == PACKET_TERMINATE) { log_info(HASH_BLAST, STRING_CONST("Client terminating due to TERMINATE packet from server")); client->state = BLAST_STATE_FINISHED; break; } } else { char buffer[NETWORK_ADDRESS_NUMERIC_MAX_LENGTH]; string_t addr = network_address_to_string(buffer, sizeof(buffer), address, true); log_warnf(HASH_BLAST, WARNING_SUSPICIOUS, STRING_CONST("Ignoring datagram from unknown host %.*s"), STRING_FORMAT(addr)); } size = udp_socket_recvfrom(client->sock, databuf, sizeof(databuf), &address); } }
int hashify_process_strings(string_t* strings) { size_t istr, strings_size; for (istr = 0, strings_size = array_size(strings); istr < strings_size; ++istr) { uint64_t hash_value = hash(STRING_ARGS(strings[istr])); log_infof(0, STRING_CONST("String '%.*s' hash: 0x%" PRIx64), STRING_FORMAT(strings[istr]), hash_value); } return 0; }
static FOUNDATION_NOINLINE int lua_panic(lua_State* state) { string_const_t errmsg = {0, 0}; errmsg.str = lua_tolstring(state, -1, &errmsg.length); //FOUNDATION_ASSERT_FAILFORMAT("unprotected error in call to Lua API: %.*s", errmsg.length, // errmsg.str); log_errorf(HASH_LUA, ERROR_EXCEPTION, STRING_CONST("unprotected error in call to Lua API: %.*s"), STRING_FORMAT(errmsg)); return 0; }
void UIOS::ShowSelDev() { DeviceInfo devInfo; if( ReadProductInfoSyn( SELDEV_IP, devInfo ) ) { string str; STRING_FORMAT( str, "0x%08X", devInfo._singleDiscSN ); _edit1.SetString( str ); } }
void UIOS::ShowOSWaveLenList( UCHAR portNum, string* waveLenList ) { string str; STRING_FORMAT( str, "%d", portNum ); _grid1.SetCellString( 1, 2, str, false ); for( int i=0; i<portNum; ++i ) _grid1.SetCellString( 2+i, 2, waveLenList[i], false ); UIRefresh(); }
void UIOS::OnLoadFromOSDB() { string str; _edit1.GetString( str ); float pm[16]; float k=0, b=0, IBMON=0, IMPDMON=0, resistance=0; if( !SelectDBOS( strtol( str.c_str(), 0, 16 ), pm, k, b, IBMON, IMPDMON, resistance ) ) { View0( "读取数据失败.." ); return; } for(int i=0; i<16; ++i) { STRING_FORMAT( str, "%0.2f", pm[i] ); _grid1.SetCellString( i+2, 1, str ); } // STRING_FORMAT( str, "%f", k ); _grid2.SetCellString( 1, 1, str ); STRING_FORMAT( str, "%f", b ); _grid2.SetCellString( 2, 1, str ); // STRING_FORMAT( str, "%0.2f", IBMON ); _grid3.SetCellString( 1, 2, str ); STRING_FORMAT( str, "%0.2f", IMPDMON ); _grid3.SetCellString( 2, 2, str ); STRING_FORMAT( str, "%0.2f", resistance ); _grid3.SetCellString( 3, 2, str ); View0( "读取数据成功.." ); }
int hashify_check_collisions(string_const_t string, hash_t hash_value, const hashify_string_t* history) { size_t ihist, history_size; for (ihist = 0, history_size = array_size(history); ihist < history_size; ++ihist) { if (history[ihist].hash == hash_value) { if (string_equal(history[ihist].string.str, history[ihist].string.length, string.str, string.length)) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" global string duplication, \"%.*s\""), STRING_FORMAT(string)); return HASHIFY_RESULT_STRING_COLLISION; } else { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" global hash collision, 0x%" PRIx64 " between: \"%.*s\" and \"%.*s\" "), hash_value, STRING_FORMAT(string), STRING_FORMAT(history[ihist].string)); return HASHIFY_RESULT_HASH_COLLISION; } } } return 0; }
int hashify_check_match(const hashify_string_t* hashes, const hashify_string_t* generated) { //From hashify_check_local_consistency we know that generated array already is a subset of hashes //This test checks that hashes is a subset of generated, i.e sets are equal size_t ihash, hashes_size, igen, generated_size; for (ihash = 0, hashes_size = array_size(hashes); ihash < hashes_size; ++ihash) { for (igen = 0, generated_size = array_size(generated); igen < generated_size; ++igen) { if (hashes[ihash].hash == generated[igen].hash) { if (!string_equal(hashes[ihash].string.str, hashes[ihash].string.length, generated[igen].string.str, generated[igen].string.length)) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash string mismatch, \"%.*s\" with hash 0x%" PRIx64 " stored in output file, generated by \"%.*s\" from input file"), STRING_FORMAT(hashes[ihash].string), hashes[ihash].hash, STRING_FORMAT(generated[igen].string)); return HASHIFY_RESULT_HASH_STRING_MISMATCH; } break; } else if (string_equal(hashes[ihash].string.str, hashes[ihash].string.length, generated[igen].string.str, generated[igen].string.length)) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash mismatch, \"%.*s\" with hash 0x%" PRIx64 " stored in output file, \"%.*s\" generated hash 0x%" PRIx64 " from input file"), STRING_FORMAT(hashes[ihash].string), hashes[ihash].hash, STRING_FORMAT(generated[igen].string), generated[igen].hash); return HASHIFY_RESULT_HASH_MISMATCH; } } if (igen == generated_size) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" extra hash \"%.*s\" with hash 0x%" PRIx64 " not found in input file"), STRING_FORMAT(hashes[ihash].string), hashes[ihash].hash); return HASHIFY_RESULT_EXTRA_STRING; } } return HASHIFY_RESULT_OK; }
static int blast_client_send_handshake(blast_client_t* client, blast_reader_t* reader) { int iaddr, addrsize = 0; socket_t** socks; const network_address_t** addrarr; packet_handshake_t packet; packet.type = PACKET_HANDSHAKE; packet.token = 0; packet.timestamp = blast_timestamp(client->begin_send); packet.datasize = reader->size; packet.seq = blast_seq(client->seq++); packet.namesize = (uint32_t)string_copy(packet.name, sizeof(packet.name), STRING_ARGS(reader->name)).length; if (client->target) { addrarr = &client->target; socks = &client->sock; addrsize = 1; } else { addrarr = (const network_address_t**)client->address; socks = client->socks; addrsize = array_size(addrarr); } for (iaddr = 0; iaddr < addrsize; ++iaddr) { udp_socket_sendto(socks[iaddr], &packet, sizeof(packet_handshake_t) - (PACKET_NAME_MAXSIZE - packet.namesize) + 1, addrarr[iaddr]); #if BUILD_ENABLE_LOG { char buffer[NETWORK_ADDRESS_NUMERIC_MAX_LENGTH]; string_t addr = network_address_to_string(buffer, sizeof(buffer), addrarr[iaddr], true); log_infof(HASH_BLAST, STRING_CONST("Sent handshake to %.*s (seq %lld, timestamp %lld)"), STRING_FORMAT(addr), packet.seq, (tick_t)packet.timestamp); } #endif } client->last_send = time_current(); return 0; }
static void blast_client_report_progress(blast_client_t* client, bool force) { int progress = (int)((real)((float64_t)((client->seq - array_size(client->pending)) * PACKET_CHUNK_SIZE) / (float64_t)client->readers[client->current]->size) * REAL_C(100.0)); if (force || (progress > (client->last_progress_percent + 5)) || (time_elapsed(client->last_progress) > 1.0f)) { if (client->packets_sent > 0) { real resend_rate = (real)((float64_t)client->packets_resent / (float64_t)client->packets_sent) * REAL_C(100.0); log_infof(HASH_BLAST, STRING_CONST("Progress: %.*s %d%% (resend rate %.2" PRIreal "%% %lld/%lld))"), STRING_FORMAT(client->readers[client->current]->name), progress, resend_rate, client->packets_resent, client->packets_sent); } client->last_progress = time_current(); client->last_progress_percent = progress; } }
void UIOS::OnAddOS() { int rowNum, columnNum; _grid21.GetRowColumn( rowNum, columnNum ); string str; STRING_FORMAT( str, "%d", rowNum ); string ipStr; _edit21.GetString( ipStr ); VECTOR_STRING row; row.push_back( str ); row.push_back( ipStr ); row.push_back( "" ); row.push_back( "" ); row.push_back( "" ); _grid21.AddRow( row ); }
int hashify_read_hashes(stream_t* file, hashify_string_t** hashes) { //Read in hashes in file char line_buffer[HASHIFY_LINEBUFFER_LENGTH]; string_const_t tokens[32]; memset(line_buffer, 0, sizeof(line_buffer)); do { string_t line = stream_read_line_buffer(file, line_buffer, sizeof(line_buffer), '\n'); string_const_t stripped_line = string_strip(STRING_ARGS(line), STRING_CONST("\n\r")); if ((string_find_string(STRING_ARGS(stripped_line), STRING_CONST("define"), 0) != STRING_NPOS) && (string_find_string(STRING_ARGS(stripped_line), STRING_CONST("static_hash"), 0) != STRING_NPOS)) { //Format is: #define HASH_<hashstring> static_hash_string( "<string>", 0x<hashvalue>ULL ) size_t num_tokens = string_explode(STRING_ARGS(stripped_line), STRING_CONST(" \t"), tokens, 32, false); if (num_tokens >= 6) { hashify_string_t hash_string; string_const_t stripped = string_strip(STRING_ARGS(tokens[3]), STRING_CONST(",")); stripped = string_strip(STRING_ARGS(stripped), STRING_CONST("\"")); hash_string.string = string_copy(hash_string.buffer, HASHIFY_STRING_LENGTH, STRING_ARGS(stripped)); hash_string.hash = string_to_uint64(STRING_ARGS(tokens[4]), true); if (hash(STRING_ARGS(hash_string.string)) != hash_string.hash) { log_errorf(0, ERROR_INVALID_VALUE, STRING_CONST(" hash output file is out of date, %.*s is set to 0x%" PRIx64 " but should be 0x%" PRIx64), STRING_FORMAT(hash_string.string), hash_string.hash, hash(STRING_ARGS(hash_string.string))); return HASHIFY_RESULT_OUTPUT_FILE_OUT_OF_DATE; } array_push_memcpy(*hashes, &hash_string); } } } while (!stream_eos(file)); return 0; }
static lua_result_t lua_do_call_custom(lua_t* env, const char* method, size_t length, lua_arg_t* arg) { lua_State* state; lua_result_t result; int numargs, i; int stacksize; size_t start, next; string_const_t part; state = env->state; stacksize = lua_gettop(state); result = LUA_OK; ++env->calldepth; next = string_find(method, length, '.', 0); if (next != STRING_NPOS) { part = string_const(method, next); lua_getlglobal(state, part.str, part.length); if (lua_isnil(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script call, '%.*s' is not set (%.*s)"), STRING_FORMAT(part), (int)length, method); --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } else if (!lua_istable(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script call, existing data '%.*s' in '%.*s' is not a table"), STRING_FORMAT(part), (int)length, method); --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } //Top of stack is now table FOUNDATION_ASSERT(lua_istable(state, -1)); ++next; start = next; next = string_find(method, length, '.', next); while (next != STRING_NPOS) { part = string_const(method + start, next - start); lua_pushlstring(state, part.str, part.length); lua_gettable(state, -2); if (lua_isnil(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script call, '%.*s' is not set (%.*s)"), STRING_FORMAT(part), (int)next, method); --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } else if (!lua_istable(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script call, existing data '%.*s' in '%.*s' is not a table"), STRING_FORMAT(part), (int)next, method); --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } //Top of stack is now table FOUNDATION_ASSERT(lua_istable(state, -1)); ++next; start = next; next = string_find(method, length, '.', next); } part = string_const(method + start, length - start); lua_pushlstring(state, part.str, part.length); lua_gettable(state, -2); } else { lua_getlglobal(state, method, length); } if (lua_isnil(state, -1)) { --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); //Method does not exist in Lua context log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script call, '%.*s' is not a function"), (int)length, method); return LUA_ERROR; } numargs = 0; if (arg) { numargs = (arg->num < LUA_MAX_ARGS) ? arg->num : LUA_MAX_ARGS; for (i = 0; i < numargs; ++i) { switch (arg->type[i]) { case LUADATA_PTR: lua_pushlightuserdata(state, arg->value[i].ptr); break; case LUADATA_OBJ: lua_pushobject(state, arg->value[i].obj); break; case LUADATA_INT: lua_pushinteger(state, arg->value[i].ival); break; case LUADATA_REAL: lua_pushnumber(state, arg->value[i].val); break; case LUADATA_STR: lua_pushlstring(state, arg->value[i].str, arg->size[i]); break; case LUADATA_BOOL: lua_pushboolean(state, arg->value[i].flag); break; case LUADATA_INTARR: { const int* values = arg->value[i].ptr; lua_newtable(state); for (uint16_t ia = 0; ia < arg->size[i]; ++ia) { lua_pushinteger(state, ia + 1); lua_pushinteger(state, values[ia]); lua_settable(state, -3); } break; } case LUADATA_REALARR: { const real* values = arg->value[i].ptr; lua_newtable(state); for (uint16_t ia = 0; ia < arg->size[i]; ++ia) { lua_pushinteger(state, ia + 1); lua_pushnumber(state, values[ia]); lua_settable(state, -3); } break; } default: --numargs; break; } } } //TODO: Parse return value from call if (lua_pcall(state, numargs, 0, 0) != 0) { string_const_t errmsg = {0, 0}; errmsg.str = lua_tolstring(state, -1, &errmsg.length); log_errorf(HASH_LUA, ERROR_INTERNAL_FAILURE, STRING_CONST("Calling %.*s : %.*s"), (int)length, method, STRING_FORMAT(errmsg)); result = LUA_ERROR; } --env->calldepth; lua_pop(state, lua_gettop(state) - stacksize); return result; }
void UIOS::OnCreate() { // SetCB_View_ShowMsg( View0 ); SetCB_View_ShowOSWaveLenList( View5 ); SetCB_View_ShowOSOutPutPMList( View6 ); PCB_View_ShowMsg = View0; PCB_View_ShowKB = View101; PCB_View_ShowResistance = View102; PCB_View_ShowVerifyKB = View103; // string str; _tabX1.Create( 0, this, _clientRC, UILayoutCalc::SIZE_X|UILayoutCalc::SIZE_Y ); _tabX1.HideTab(); _tabX1.SetCellNum( 2 ); _tabX1.SetCell( 0, "定标", &_canvas1 ); _tabX1.SetCell( 1, "监视", &_canvas2 ); _tabX1.SetCurCell( 0 ); _canvas1.CreateDirect( &_tabX1 ); _but1.Create( 101, &_canvas1 ); _but1.SetString( "获取采样值" ); _but2.Create( 102, &_canvas1 ); _but2.SetString( "获取功率值" ); _but3.Create( 103, &_canvas1 ); _but3.SetString( "打开光源" ); _but4.Create( 104, &_canvas1 ); _but4.SetString( "关闭光源" ); _but5.Create( 105, &_canvas1 ); _but5.SetString( "读取波长" ); _but6.Create( 106, &_canvas1 ); _but6.SetString( "设置波长" ); _but7.Create( 107, &_canvas1 ); _but7.SetString( "读取出光功率" ); _but8.Create( 108, &_canvas1 ); _but8.SetString( "获取电流、电阻值" ); _but9.Create( 109, &_canvas1 ); _but9.SetString( "计算KB" ); _but10.Create( 110, &_canvas1 ); _but10.SetString( "设置定标数据" ); _but11.Create( 111, &_canvas1 ); _but11.SetString( "验证定标数据" ); _but12.Create( 112, &_canvas1 ); _but12.SetString( "写数据库" ); _but13.Create( 113, &_canvas1 ); _but13.SetString( "读数据库" ); _grid1.Create( 0, &_canvas1, GenRect()( 280, 20, 480, 400 ) ); _grid1.SetRowColumn( 18, 3 ); _grid1.SetRowFix(); _grid1.SetColumnFix(); _grid1.SetColumnWidth( 0, 50 ); _grid1.SetColumnWidth( 1, 70 ); _grid1.SetCellString( 0, 0, "序号" ); _grid1.SetCellString( 0, 1, "出光功率" ); _grid1.SetCellString( 0, 2, "波长" ); _grid1.SetCellString( 1, 0, "端口数" ); for( int i=0; i<16; ++i ) { STRING_FORMAT( str, "%d", i+1 ); _grid1.SetCellString( i+2, 0, str ); } _grid2.Create( 0, &_canvas1 ); _grid2.SetRowColumn( 3, 2 ); _grid2.SetRowFix(); _grid2.SetColumnFix(); _grid2.SetColumnWidth( 1, 80 ); _grid2.SetColumnWidth( 2, 80 ); _grid2.SetCellString( 0, 0, "序号" ); _grid2.SetCellString( 1, 0, "K" ); _grid2.SetCellString( 2, 0, "B" ); _grid3.Create( 0, &_canvas1 ); _grid3.SetRowColumn( 4, 4 ); _grid3.SetRowFix(); _grid3.SetColumnFix(); _grid3.SetColumnWidth( 0, 90 ); _grid3.SetColumnWidth( 1, 90 ); _grid3.SetCellString( 0, 0, "项" ); _grid3.SetCellString( 0, 1, "k值" ); _grid3.SetCellString( 0, 2, "当前值" ); _grid3.SetCellString( 0, 3, "告警阈值" ); _grid3.SetCellString( 1, 0, "驱动电流(mA)" ); _grid3.SetCellString( 2, 0, "背光电流(uA)" ); _grid3.SetCellString( 3, 0, "电阻值(欧姆)" ); _grid3.SetCellString( 1, 3, "100" ); _grid3.SetCellString( 2, 3, "1000" ); _grid3.SetCellString( 3, 3, "1000" ); _grid3.SetCellString( 1, 1, OSLogic::GetSingleton()->k1[0] ); _grid3.SetCellString( 2, 1, OSLogic::GetSingleton()->k1[1] ); _grid4.Create( 0, &_canvas1 ); _grid4.SetRowColumn( 3, 4 ); _grid4.SetRowFix(); _grid4.SetColumnFix(); _grid4.SetCellString( 0, 0, "点" ); _grid4.SetCellString( 0, 1, "读取值" ); _grid4.SetCellString( 0, 2, "参考值" ); _grid4.SetCellString( 0, 3, "差值" ); _grid4.SetCellString( 1, 0, "1" ); _grid4.SetCellString( 2, 0, "2" ); _check1.Create( 121, &_canvas1 ); _check1.SetString( "cyoptics" ); _check1.SetCheck( true ); _check2.Create( 122, &_canvas1 ); _check2.SetString( "accelink" ); UISetCheckButtonMutex( &_check1, &_check2 ); _lable1.Create( 0, &_canvas1 ); _lable1.SetString( "Step 1:" ); _lable1.SetColor( _BLUE_ ); _lable2.Create( 0, &_canvas1 ); _lable2.SetString( "Step 2:" ); _lable2.SetColor( _BLUE_ ); _lable3.Create( 0, &_canvas1 ); _lable3.SetString( "Step 3:" ); _lable3.SetColor( _BLUE_ ); _lable4.Create( 0, &_canvas1 ); _lable4.SetString( "Step 4:" ); _lable4.SetColor( _BLUE_ ); _lable5.Create( 0, &_canvas1 ); _lable5.SetString( "( 端口值=端口X功率-端口1功率 )自动计算差值,只填写功率值." ); _lable6.Create( 0, &_canvas1 ); _lable6.SetString( "设备SN号:" ); _edit1.Create( 0, &_canvas1 ); _canvas2.CreateDirect( &_tabX1 ); // // _but21.Create( 21, &_canvas2 ); _but21.SetString( "开始监视" ); _but22.Create( 22, &_canvas2 ); _but22.SetString( "停止监视" ); _but23.Create( 23, &_canvas2 ); _but23.SetString( "读取最大最小值" ); _but24.Create( 24, &_canvas2 ); _but24.SetString( "添加设备" ); _but25.Create( 25, &_canvas2 ); _but25.SetString( "删除选中设备" ); _but26.Create( 26, &_canvas2 ); _but26.SetString( "清除所有设备" ); // _lable21.Create( 0, &_canvas2 ); _lable21.SetString( "注意:设备IP地址需与主机位于同一网段内.." ); _edit21.Create( 0, &_canvas2 ); _grid21.Create( 0, &_canvas2 ); _grid21.SetRowColumn( 1, 5 ); _grid21.SetRowFix(); _grid21.SetColumnFix(); _grid21.SetCellString( 0, 0, "序号" ); _grid21.SetColumnWidth( 0, 100 ); _grid21.SetCellString( 0, 1, "IP地址" ); _grid21.SetColumnWidth( 1, 150 ); _grid21.SetCellString( 0, 2, "监测标志" ); _grid21.SetColumnWidth( 2, 120 ); _grid21.SetCellString( 0, 3, "MAX" ); _grid21.SetColumnWidth( 3, 120 ); _grid21.SetCellString( 0, 4, "MIN" ); _grid21.SetColumnWidth( 4, 120 ); _layoutGrid1.InitPoint( GenPoint()( 20, 20 ) ); _layoutGrid1.SetRowColumn( 10, 2 ); for( int r=0; r<10; ++r ) _layoutGrid1.SetRowHeightInterval( r, 50 ); // _layoutGrid1.SetCell( 0, 0, &_but1 ); _layoutGrid1.SetCell( 0, 1, &_but2 ); _layoutGrid1.SetCell( 1, 0, &_but3 ); _layoutGrid1.SetCell( 1, 1, &_but4 ); _layoutGrid1.SetCell( 3, 0, &_but5 ); _layoutGrid1.SetCell( 3, 1, &_but6 ); _layoutGrid1.SetCell( 4, 0, 4, 1, &_but7 ); _layoutGrid1.SetCell( 7, 0, 7, 1, &_but8 ); // _layoutGrid1.CalcGridPos(); _layoutGrid2.InitPoint( GenPoint()( 280, 450 ) ); _layoutGrid2.SetRowColumn( 4, 3 ); // _layoutGrid2.SetCell( 0, 0, &_check1 ); _layoutGrid2.SetCell( 0, 1, &_check2 ); _layoutGrid2.SetCell( 1, 0, 3, 2, &_grid3 ); // _layoutGrid2.CalcGridPos(); _layoutGrid3.InitPoint( GenPoint()( 500, 20 ) ); _layoutGrid3.SetRowColumn( 10, 4 ); _layoutGrid3.SetColumnWidthInterval( 0, 60 ); _layoutGrid3.SetColumnWidthInterval( 1, 120 ); _layoutGrid3.SetColumnWidthInterval( 2, 120 ); _layoutGrid3.SetColumnWidthInterval( 3, 120 ); // _layoutGrid3.SetCell( 0, 0, &_lable1 ); _layoutGrid3.SetCell( 0, 1, &_but9 ); _layoutGrid3.SetCell( 0, 2, 1, 3, &_grid2 ); _layoutGrid3.SetCell( 2, 0, 2, 3, &_lable5 ); _layoutGrid3.SetCell( 3, 0, &_lable2 ); _layoutGrid3.SetCell( 3, 1, &_but10 ); _layoutGrid3.SetCell( 4, 0, &_lable3 ); _layoutGrid3.SetCell( 4, 1, &_but11 ); _layoutGrid3.SetCell( 4, 2, 5, 3, &_grid4 ); _layoutGrid3.SetCell( 6, 0, &_lable4 ); _layoutGrid3.SetCell( 6, 1, &_but12 ); _layoutGrid3.SetCell( 6, 2, &_but13 ); _layoutGrid3.SetCell( 7, 1, &_lable6 ); _layoutGrid3.SetCell( 7, 2, &_edit1 ); // _layoutGrid3.CalcGridPos(); _layoutGrid4.InitPoint( GenPoint()( 20, 20 ) ); _layoutGrid4.SetRowColumn( 12, 8 ); // _layoutGrid4.SetCell( 0, 0, 0, 5, &_lable21 ); _layoutGrid4.SetCell( 1, 0, 8, 5, &_grid21 ); _layoutGrid4.SetCell( 9, 0, 10, 1, &_but21 ); _layoutGrid4.SetCell( 9, 2, 10, 3, &_but22 ); _layoutGrid4.SetCell( 9, 4, 10, 5, &_but23 ); _layoutGrid4.SetCell( 1, 6, 2, 7, &_but24 ); _layoutGrid4.SetCell( 3, 6, 3, 7, &_edit21 ); //_layoutGrid4.SetCell( 5, 6, 6, 7, &_but25 ); _layoutGrid4.SetCell( 7, 6, 8, 7, &_but26 ); // _layoutGrid4.CalcGridPos(); }
lua_result_t lua_do_bind(lua_t* env, const char* property, size_t length, lua_command_t cmd, lua_value_t val) { lua_State* state; int stacksize; size_t start, next; string_const_t part; if (!env || !length) return LUA_ERROR; state = env->state; stacksize = lua_gettop(state); next = string_find(property, length, '.', 0); if (next != STRING_NPOS) { int tables; unsigned int numtables = 0; part = string_const(property, next); lua_getlglobal(state, part.str, part.length); if (lua_isnil(state, -1)) { //Create global table lua_pop(state, 1); lua_newtable(state); lua_pushvalue(state, -1); lua_setlglobal(state, part.str, part.length); log_debugf(HASH_LUA, STRING_CONST("Created global table: %.*s"), STRING_FORMAT(part)); } else if (!lua_istable(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script bind call, existing data '%.*s' in '%.*s' is not a table"), STRING_FORMAT(part), (int)length, property); lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } //Top of stack is now table FOUNDATION_ASSERT(lua_istable(state, -1)); ++next; start = next; ++numtables; next = string_find(property, length, '.', next); while (next != STRING_NPOS) { part = string_const(property + start, next - start); lua_pushlstring(state, part.str, part.length); lua_gettable(state, -2); if (lua_isnil(state, -1)) { //Create sub-table lua_pop(state, 1); lua_newtable(state); lua_pushlstring(state, part.str, part.length); lua_pushvalue(state, -2); lua_settable(state, -4); log_debugf(HASH_LUA, STRING_CONST("Created table: %.*s"), next, property); } else if (!lua_istable(state, -1)) { log_errorf(HASH_LUA, ERROR_INVALID_VALUE, STRING_CONST("Invalid script bind call, existing data '%.*s' in '%.*s' is not a table"), STRING_FORMAT(part), (int)next, property); lua_pop(state, lua_gettop(state) - stacksize); return LUA_ERROR; } //Top of stack is now table FOUNDATION_ASSERT(lua_istable(state, -1)); ++next; start = next; next = string_find(property, length, '.', next); ++numtables; } part = string_const(property + start, length - start); switch (cmd) { case LUACMD_BIND: lua_push_method(state, STRING_ARGS(part), val.fn); break; case LUACMD_BIND_INT: lua_push_integer(state, STRING_ARGS(part), val.ival); break; case LUACMD_BIND_VAL: lua_push_number(state, STRING_ARGS(part), val.val); break; default: break; } tables = lua_gettop(state) - stacksize; lua_pop(state, tables); FOUNDATION_ASSERT(tables == (int)numtables); } else { part = string_const(property, length); switch (cmd) { case LUACMD_BIND: lua_push_method_global(state, STRING_ARGS(part), val.fn); break; case LUACMD_BIND_INT: lua_push_integer_global(state, STRING_ARGS(part), val.ival); break; case LUACMD_BIND_VAL: lua_push_number_global(state, STRING_ARGS(part), val.val); break; default: break; } } return LUA_OK; }
size_t network_poll(network_poll_t* pollobj, network_poll_event_t* events, size_t capacity, unsigned int timeoutms) { int avail = 0; size_t num_events = 0; #if FOUNDATION_PLATFORM_WINDOWS //TODO: Refactor to keep fd_set across loop and rebuild on change (add/remove) int num_fd = 0; int ret = 0; size_t islot; fd_set fdread, fdwrite, fderr; #endif if (!pollobj->num_sockets) return num_events; #if FOUNDATION_PLATFORM_APPLE int ret = poll(pollobj->pollfds, pollobj->num_sockets, timeoutms); #elif FOUNDATION_PLATFORM_LINUX || FOUNDATION_PLATFORM_ANDROID int ret = epoll_wait(pollobj->fd_poll, pollobj->events, pollobj->num_sockets + 1, timeoutms); int num_polled = ret; #elif FOUNDATION_PLATFORM_WINDOWS FD_ZERO(&fdread); FD_ZERO(&fdwrite); FD_ZERO(&fderr); for (islot = 0; islot < pollobj->num_sockets; ++islot) { int fd = pollobj->slots[islot].fd; socket_base_t* sockbase = _socket_base + pollobj->slots[islot].base; FD_SET(fd, &fdread); if (sockbase->state == SOCKETSTATE_CONNECTING) FD_SET(fd, &fdwrite); FD_SET(fd, &fderr); if (fd >= num_fd) num_fd = fd + 1; } if (!num_fd) { return num_events; } else { struct timeval tv; tv.tv_sec = timeoutms / 1000; tv.tv_usec = (timeoutms % 1000) * 1000; ret = select(num_fd, &fdread, &fdwrite, &fderr, &tv); } #else # error Not implemented #endif if (ret < 0) { int err = NETWORK_SOCKET_ERROR; string_const_t errmsg = system_error_message(err); log_warnf(HASH_NETWORK, WARNING_SUSPICIOUS, STRING_CONST("Error in socket poll: %.*s (%d)"), STRING_FORMAT(errmsg), err); if (!avail) return num_events; ret = avail; } if (!avail && !ret) return num_events; #if FOUNDATION_PLATFORM_APPLE struct pollfd* pfd = pollobj->pollfds; network_poll_slot_t* slot = pollobj->slots; for (size_t i = 0; i < pollobj->num_sockets; ++i, ++pfd, ++slot) { socket_t* sock = slot->sock; socket_base_t* sockbase = _socket_base + slot->base; int fd = slot->fd; if (pfd->revents & POLLIN) { if (sockbase->state == SOCKETSTATE_LISTENING) { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTION, sock); } else { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_DATAIN, sock); } } if ((sockbase->state == SOCKETSTATE_CONNECTING) && (pfd->revents & POLLOUT)) { sockbase->state = SOCKETSTATE_CONNECTED; pfd->events = POLLIN | POLLERR | POLLHUP; network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTED, sock); } if (pfd->revents & POLLERR) { pfd->events = POLLOUT | POLLERR | POLLHUP; network_poll_push_event(events, capacity, num_events, NETWORKEVENT_ERROR, sock); socket_close(sock); } if (pfd->revents & POLLHUP) { pfd->events = POLLOUT | POLLERR | POLLHUP; network_poll_push_event(events, capacity, num_events, NETWORKEVENT_HANGUP, sock); socket_close(sock); } } #elif FOUNDATION_PLATFORM_LINUX || FOUNDATION_PLATFORM_ANDROID struct epoll_event* event = pollobj->events; for (int i = 0; i < num_polled; ++i, ++event) { FOUNDATION_ASSERT(pollobj->slots[ event->data.fd ].base >= 0); socket_t* sock = pollobj->slots[ event->data.fd ].sock; socket_base_t* sockbase = _socket_base + pollobj->slots[ event->data.fd ].base; int fd = pollobj->slots[ event->data.fd ].fd; if (event->events & EPOLLIN) { if (sockbase->state == SOCKETSTATE_LISTENING) { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTION, sock); } else { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_DATAIN, sock); } } if ((sockbase->state == SOCKETSTATE_CONNECTING) && (event->events & EPOLLOUT)) { sockbase->state = SOCKETSTATE_CONNECTED; struct epoll_event mod_event; mod_event.events = EPOLLIN | EPOLLERR | EPOLLHUP; mod_event.data.fd = event->data.fd; epoll_ctl(pollobj->fd_poll, EPOLL_CTL_MOD, fd, &mod_event); network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTED, sock); } if (event->events & EPOLLERR) { struct epoll_event del_event; epoll_ctl(pollobj->fd_poll, EPOLL_CTL_DEL, fd, &del_event); network_poll_push_event(events, capacity, num_events, NETWORKEVENT_ERROR, sock); socket_close(sock); } if (event->events & EPOLLHUP) { struct epoll_event del_event; epoll_ctl(pollobj->fd_poll, EPOLL_CTL_DEL, fd, &del_event); network_poll_push_event(events, capacity, num_events, NETWORKEVENT_HANGUP, sock); socket_close(sock); } } #elif FOUNDATION_PLATFORM_WINDOWS for (islot = 0; islot < pollobj->num_sockets; ++islot) { int fd = pollobj->slots[islot].fd; socket_t* sock = pollobj->slots[islot].sock; socket_base_t* sockbase = _socket_base + pollobj->slots[islot].base; if (sockbase->fd != fd) continue; if (FD_ISSET(fd, &fdread)) { if (sockbase->state == SOCKETSTATE_LISTENING) { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTION, sock); } else { //SOCKETSTATE_CONNECTED network_poll_push_event(events, capacity, num_events, NETWORKEVENT_DATAIN, sock); } } if ((sockbase->state == SOCKETSTATE_CONNECTING) && FD_ISSET(fd, &fdwrite)) { sockbase->state = SOCKETSTATE_CONNECTED; network_poll_push_event(events, capacity, num_events, NETWORKEVENT_CONNECTED, sock); } if (FD_ISSET(fd, &fderr)) { network_poll_push_event(events, capacity, num_events, NETWORKEVENT_HANGUP, sock); socket_close(sock); } } #else # error Not implemented #endif return num_events; }
int main_run(void* main_arg) { #if !BUILD_MONOLITHIC string_const_t pattern; string_t* exe_paths = 0; size_t iexe, exesize; process_t* process = 0; string_t process_path = { 0, 0 }; unsigned int* exe_flags = 0; #else void* test_result; #endif #if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL int remain_counter = 0; #endif #if BUILD_DEBUG const string_const_t build_name = string_const(STRING_CONST("debug")); #elif BUILD_RELEASE const string_const_t build_name = string_const(STRING_CONST("release")); #elif BUILD_PROFILE const string_const_t build_name = string_const(STRING_CONST("profile")); #elif BUILD_DEPLOY const string_const_t build_name = string_const(STRING_CONST("deploy")); #endif #if BUILD_MONOLITHIC const string_const_t build_type = string_const(STRING_CONST(" monolithic")); #else const string_const_t build_type = string_empty(); #endif char* pathbuf; int process_result = 0; thread_t event_thread; FOUNDATION_UNUSED(main_arg); FOUNDATION_UNUSED(build_name); log_set_suppress(HASH_TEST, ERRORLEVEL_DEBUG); log_infof(HASH_TEST, STRING_CONST("Task library v%s built for %s using %s (%.*s%.*s)"), string_from_version_static(task_module_version()).str, FOUNDATION_PLATFORM_DESCRIPTION, FOUNDATION_COMPILER_DESCRIPTION, STRING_FORMAT(build_name), STRING_FORMAT(build_type)); thread_initialize(&event_thread, event_loop, 0, STRING_CONST("event_thread"), THREAD_PRIORITY_NORMAL, 0); thread_start(&event_thread); pathbuf = memory_allocate(HASH_STRING, BUILD_MAX_PATHLEN, 0, MEMORY_PERSISTENT); while (!thread_is_running(&event_thread)) thread_sleep(10); #if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL while (!_test_should_start) { #if FOUNDATION_PLATFORM_ANDROID system_process_events(); #endif thread_sleep(100); } #endif fs_remove_directory(STRING_ARGS(environment_temporary_directory())); #if BUILD_MONOLITHIC test_run_fn tests[] = { test_task_run, 0 }; #if FOUNDATION_PLATFORM_ANDROID thread_t test_thread; thread_initialize(&test_thread, test_runner, tests, STRING_CONST("test_runner"), THREAD_PRIORITY_NORMAL, 0); thread_start(&test_thread); log_debug(HASH_TEST, STRING_CONST("Starting test runner thread")); while (!thread_is_running(&test_thread)) { system_process_events(); thread_sleep(10); } while (thread_is_running(&test_thread)) { system_process_events(); thread_sleep(10); } test_result = thread_join(&test_thread); process_result = (int)(intptr_t)test_result; thread_finalize(&test_thread); #else test_result = test_runner(tests); process_result = (int)(intptr_t)test_result; #endif if (process_result != 0) log_warnf(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Tests failed with exit code %d"), process_result); #if FOUNDATION_PLATFORM_IOS || FOUNDATION_PLATFORM_ANDROID || FOUNDATION_PLATFORM_PNACL while (!_test_should_terminate && _test_have_focus && (remain_counter < 50)) { system_process_events(); thread_sleep(100); ++remain_counter; } #endif log_debug(HASH_TEST, STRING_CONST("Exiting main loop")); #else // !BUILD_MONOLITHIC //Find all test executables in the current executable directory #if FOUNDATION_PLATFORM_WINDOWS pattern = string_const(STRING_CONST("^test-.*\\.exe$")); #elif FOUNDATION_PLATFORM_MACOSX pattern = string_const(STRING_CONST("^test-.*$")); #elif FOUNDATION_PLATFORM_POSIX pattern = string_const(STRING_CONST("^test-.*$")); #else # error Not implemented #endif exe_paths = fs_matching_files(STRING_ARGS(environment_executable_directory()), STRING_ARGS(pattern), false); array_resize(exe_flags, array_size(exe_paths)); memset(exe_flags, 0, sizeof(unsigned int) * array_size(exe_flags)); #if FOUNDATION_PLATFORM_MACOSX //Also search for test applications string_const_t app_pattern = string_const(STRING_CONST("^test-.*\\.app$")); regex_t* app_regex = regex_compile(app_pattern.str, app_pattern.length); string_t* subdirs = fs_subdirs(STRING_ARGS(environment_executable_directory())); for (size_t idir = 0, dirsize = array_size(subdirs); idir < dirsize; ++idir) { if (regex_match(app_regex, subdirs[idir].str, subdirs[idir].length, 0, 0)) { string_t exe_path = { subdirs[idir].str, subdirs[idir].length - 4 }; array_push(exe_paths, exe_path); array_push(exe_flags, PROCESS_MACOSX_USE_OPENAPPLICATION); } } string_array_deallocate(subdirs); regex_deallocate(app_regex); #endif for (iexe = 0, exesize = array_size(exe_paths); iexe < exesize; ++iexe) { string_const_t* process_args = 0; string_const_t exe_file_name = path_base_file_name(STRING_ARGS(exe_paths[iexe])); if (string_equal(STRING_ARGS(exe_file_name), STRING_ARGS(environment_executable_name()))) continue; //Don't run self process_path = path_concat(pathbuf, BUILD_MAX_PATHLEN, STRING_ARGS(environment_executable_directory()), STRING_ARGS(exe_paths[iexe])); process = process_allocate(); process_set_executable_path(process, STRING_ARGS(process_path)); process_set_working_directory(process, STRING_ARGS(environment_executable_directory())); process_set_flags(process, PROCESS_ATTACHED | exe_flags[iexe]); if (!_test_memory_tracker) array_push(process_args, string_const(STRING_CONST("--no-memory-tracker"))); process_set_arguments(process, process_args, array_size(process_args)); log_infof(HASH_TEST, STRING_CONST("Running test executable: %.*s"), STRING_FORMAT(exe_paths[iexe])); process_result = process_spawn(process); while (process_result == PROCESS_WAIT_INTERRUPTED) { thread_sleep(10); process_result = process_wait(process); } process_deallocate(process); array_deallocate(process_args); if (process_result != 0) { if (process_result >= PROCESS_INVALID_ARGS) log_warnf(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Tests failed, process terminated with error %x"), process_result); else log_warnf(HASH_TEST, WARNING_SUSPICIOUS, STRING_CONST("Tests failed with exit code %d"), process_result); process_set_exit_code(-1); goto exit; } log_infof(HASH_TEST, STRING_CONST("All tests from %.*s passed (%d)"), STRING_FORMAT(exe_paths[iexe]), process_result); } log_info(HASH_TEST, STRING_CONST("All tests passed")); exit: if (exe_paths) string_array_deallocate(exe_paths); array_deallocate(exe_flags); #endif _test_should_terminate = true; thread_signal(&event_thread); thread_finalize(&event_thread); memory_deallocate(pathbuf); log_infof(HASH_TEST, STRING_CONST("Tests exiting: %s (%d)"), process_result ? "FAILED" : "PASSED", process_result); if (process_result) memory_set_tracker(memory_tracker_none()); return process_result; }
static int render_import_shader(stream_t* stream, const uuid_t uuid) { char buffer[1024]; char pathbuf[BUILD_MAX_PATHLEN]; resource_source_t source; resource_platform_t platformdecl = {-1, -1, -1, -1, -1, -1}; uint64_t platform; tick_t timestamp; int ret = 0; resource_source_initialize(&source); resource_source_read(&source, uuid); platform = resource_platform(platformdecl); timestamp = time_system(); while (!stream_eos(stream)) { uuid_t shaderuuid; string_const_t target, ref, fullpath; string_t line = stream_read_line_buffer(stream, buffer, sizeof(buffer), '\n'); string_split(STRING_ARGS(line), STRING_CONST(" \t"), &target, &ref, false); ref = string_strip(STRING_ARGS(ref), STRING_CONST(STRING_WHITESPACE)); shaderuuid = string_to_uuid(STRING_ARGS(ref)); if (uuid_is_null(shaderuuid)) { if (path_is_absolute(STRING_ARGS(ref))) { fullpath = ref; } else { string_t full; string_const_t path = stream_path(stream); path = path_directory_name(STRING_ARGS(path)); full = path_concat(pathbuf, sizeof(pathbuf), STRING_ARGS(path), STRING_ARGS(ref)); full = path_absolute(STRING_ARGS(full), sizeof(pathbuf)); fullpath = string_const(STRING_ARGS(full)); } resource_signature_t sig = resource_import_lookup(STRING_ARGS(fullpath)); if (uuid_is_null(sig.uuid)) { if (!resource_import(STRING_ARGS(fullpath), uuid_null())) { log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Unable to import linked shader: %.*s"), STRING_FORMAT(fullpath)); ret = -1; goto finalize; } sig = resource_import_lookup(STRING_ARGS(fullpath)); if (uuid_is_null(sig.uuid)) { log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Import linked shader gave no UUID: %.*s"), STRING_FORMAT(fullpath)); ret = -1; goto finalize; } } shaderuuid = sig.uuid; } if (!uuid_is_null(shaderuuid)) { resource_platform_t targetplatformdecl = render_import_parse_target(STRING_ARGS(target), platformdecl); uint64_t targetplatform = resource_platform(targetplatformdecl); if (resource_autoimport_need_update(shaderuuid, targetplatform)) resource_autoimport(shaderuuid); const string_const_t uuidstr = string_from_uuid_static(shaderuuid); resource_source_set(&source, timestamp, HASH_SHADER, targetplatform, STRING_ARGS(uuidstr)); resource_source_set_dependencies(uuid, targetplatform, &shaderuuid, 1); } } resource_source_set(&source, timestamp, HASH_RESOURCE_TYPE, 0, STRING_CONST("shader")); if (!resource_source_write(&source, uuid, false)) { string_const_t uuidstr = string_from_uuid_static(uuid); log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Failed writing imported shader: %.*s"), STRING_FORMAT(uuidstr)); ret = -1; goto finalize; } else { string_const_t uuidstr = string_from_uuid_static(uuid); log_infof(HASH_RESOURCE, STRING_CONST("Wrote imported shader: %.*s"), STRING_FORMAT(uuidstr)); } finalize: resource_source_finalize(&source); return 0; }
static int render_import_program(stream_t* stream, const uuid_t uuid) { char buffer[1024]; char pathbuf[BUILD_MAX_PATHLEN]; resource_source_t source; resource_platform_t platformdecl = {-1, -1, -1, -1, -1, -1}; uint64_t platform; tick_t timestamp; int ret = 0; resource_source_initialize(&source); resource_source_read(&source, uuid); platform = resource_platform(platformdecl); timestamp = time_system(); while (!stream_eos(stream)) { string_const_t type, ref; string_const_t fullpath; string_const_t uuidstr; uuid_t shaderuuid; hash_t typehash; string_t line = stream_read_line_buffer(stream, buffer, sizeof(buffer), '\n'); string_split(STRING_ARGS(line), STRING_CONST(" \t"), &type, &ref, false); type = string_strip(STRING_ARGS(type), STRING_CONST(STRING_WHITESPACE)); ref = string_strip(STRING_ARGS(ref), STRING_CONST(STRING_WHITESPACE)); if (!type.length || !ref.length) continue; typehash = hash(STRING_ARGS(type)); if ((typehash != HASH_VERTEXSHADER) && (typehash != HASH_PIXELSHADER)) { log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Ignore invalid line: %.*s"), STRING_FORMAT(line)); continue; } shaderuuid = string_to_uuid(STRING_ARGS(ref)); if (uuid_is_null(shaderuuid)) { if (path_is_absolute(STRING_ARGS(ref))) { fullpath = ref; } else { string_t full; string_const_t path = stream_path(stream); path = path_directory_name(STRING_ARGS(path)); full = path_concat(pathbuf, sizeof(pathbuf), STRING_ARGS(path), STRING_ARGS(ref)); full = path_absolute(STRING_ARGS(full), sizeof(pathbuf)); fullpath = string_const(STRING_ARGS(full)); } resource_signature_t sig = resource_import_lookup(STRING_ARGS(fullpath)); if (uuid_is_null(sig.uuid)) { if (!resource_import(STRING_ARGS(fullpath), uuid_null())) { log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Unable to import linked shader: %.*s"), STRING_FORMAT(fullpath)); ret = -1; goto finalize; } sig = resource_import_lookup(STRING_ARGS(fullpath)); if (uuid_is_null(sig.uuid)) { log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Import linked shader gave no UUID: %.*s"), STRING_FORMAT(fullpath)); ret = -1; goto finalize; } } shaderuuid = sig.uuid; } if (!uuid_is_null(shaderuuid)) { uuidstr = string_from_uuid_static(shaderuuid); resource_source_set(&source, timestamp, typehash, platform, STRING_ARGS(uuidstr)); } } resource_source_set(&source, timestamp, HASH_RESOURCE_TYPE, 0, STRING_CONST("program")); if (!resource_source_write(&source, uuid, false)) { string_const_t uuidstr = string_from_uuid_static(uuid); log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Failed writing imported program: %.*s"), STRING_FORMAT(uuidstr)); ret = -1; goto finalize; } else { string_const_t uuidstr = string_from_uuid_static(uuid); log_infof(HASH_RESOURCE, STRING_CONST("Wrote imported program: %.*s"), STRING_FORMAT(uuidstr)); } finalize: resource_source_finalize(&source); return ret; }
static int render_import_glsl_shader(stream_t* stream, const uuid_t uuid, const char* type, size_t type_length) { resource_source_t source; void* blob = 0; size_t size; size_t read; size_t begin; size_t next; hash_t checksum; tick_t timestamp; uint64_t platform; size_t maxtokens; size_t parameter; string_const_t* token = 0; string_const_t valstr; char buffer[128]; int ret = 0; resource_platform_t platformdecl = { -1, -1, RENDERAPIGROUP_OPENGL, -1, -1, -1}; resource_source_initialize(&source); resource_source_read(&source, uuid); read = 0; size = stream_size(stream); blob = memory_allocate(HASH_RESOURCE, size, 0, MEMORY_PERSISTENT); size = stream_read(stream, blob, size); platform = resource_platform(platformdecl); timestamp = time_system(); checksum = hash(blob, size); if (resource_source_write_blob(uuid, timestamp, HASH_SOURCE, platform, checksum, blob, size)) { resource_source_set_blob(&source, timestamp, HASH_SOURCE, platform, checksum, size); } else { ret = -1; goto finalize; } //Parse source and set parameters maxtokens = 256; token = memory_allocate(HASH_RESOURCE, sizeof(string_const_t) * maxtokens, 0, MEMORY_PERSISTENT); begin = 0; parameter = 0; do { string_const_t tokens[64], line; size_t itok, ntokens; next = string_find_first_of(blob, size, STRING_CONST("\n\r"), begin); line = string_substr(blob, size, begin, next - begin); ntokens = string_explode(STRING_ARGS(line), STRING_CONST(GLSL_TOKEN_DELIM), tokens, maxtokens, false); for (itok = 0; itok < ntokens; ++itok) { if ((string_equal(STRING_ARGS(tokens[itok]), STRING_CONST("attribute")) || string_equal(STRING_ARGS(tokens[itok]), STRING_CONST("uniform"))) && (itok + 2 < ntokens)) { char typebuf[16], dimbuf[16]; string_const_t typestr = tokens[itok + 1]; string_const_t namestr = tokens[itok + 2]; string_const_t dimstr; int parameter_type = glsl_type_to_parameter_type(typestr); if (parameter_type < 0) continue; int parameter_dim = glsl_dim_from_token(typestr); if (parameter_dim == 1) parameter_dim = glsl_dim_from_token(namestr); namestr = glsl_name_from_token(namestr); typestr = string_to_const(string_from_uint(typebuf, sizeof(typebuf), (unsigned int)parameter_type, false, 0, 0)); dimstr = string_to_const(string_from_uint(dimbuf, sizeof(dimbuf), (unsigned int)parameter_dim, false, 0, 0)); log_debugf(HASH_RESOURCE, STRING_CONST("parameter: %.*s type %.*s dim %.*s"), STRING_FORMAT(namestr), STRING_FORMAT(typestr), STRING_FORMAT(dimstr)); string_t param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_type_%" PRIsize), parameter); resource_source_set(&source, timestamp, hash(STRING_ARGS(param)), platform, STRING_ARGS(typestr)); param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_name_%" PRIsize), parameter); resource_source_set(&source, timestamp, hash(STRING_ARGS(param)), platform, STRING_ARGS(namestr)); param = string_format(buffer, sizeof(buffer), STRING_CONST("parameter_dim_%" PRIsize), parameter); resource_source_set(&source, timestamp, hash(STRING_ARGS(param)), platform, STRING_ARGS(dimstr)); ++parameter; } } begin = string_find_first_not_of(blob, size, STRING_CONST(STRING_WHITESPACE), next); } while (next != STRING_NPOS); valstr = string_from_uint_static(parameter, false, 0, 0); resource_source_set(&source, timestamp, HASH_PARAMETER_COUNT, platform, STRING_ARGS(valstr)); resource_source_set(&source, timestamp, HASH_RESOURCE_TYPE, 0, type, type_length); if (!resource_source_write(&source, uuid, false)) { string_const_t uuidstr = string_from_uuid_static(uuid); log_warnf(HASH_RESOURCE, WARNING_SUSPICIOUS, STRING_CONST("Failed writing imported GLSL shader: %.*s"), STRING_FORMAT(uuidstr)); ret = -1; goto finalize; } else { string_const_t uuidstr = string_from_uuid_static(uuid); log_infof(HASH_RESOURCE, STRING_CONST("Wrote imported GLSL shader: %.*s"), STRING_FORMAT(uuidstr)); } finalize: memory_deallocate(blob); memory_deallocate(token); resource_source_finalize(&source); return ret; }