static void wd_transfer_listen_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wi_socket_t *socket; wi_address_t *address; pool = wi_pool_init(wi_pool_alloc()); while(wd_running) { wi_pool_drain(pool); socket = wi_socket_accept_multiple(wd_transfer_sockets, 30.0, &address); if(!address) { wi_log_err(WI_STR("Could not accept a connection: %m")); continue; } if(!socket) { wi_log_err(WI_STR("Could not accept a connection for %@: %m"), wi_address_string(address)); continue; } if(!wi_thread_create_thread(wd_transfer_accept_thread, socket)) { wi_log_err(WI_STR("Could not create a transfer thread for %@: %m"), wi_address_string(address)); continue; } } wi_release(pool); }
void wd_trackers_register(wi_boolean_t update) { if(wi_array_count(wd_trackers) > 0) { wi_release(wd_trackers_guest_account); wd_trackers_guest_account = wi_retain(wd_accounts_read_user_and_group(WI_STR("guest"))); wi_log_info(WI_STR("Registering with trackers...")); if(!wi_thread_create_thread(wd_trackers_register_thread, wi_number_with_bool(update))) wi_log_err(WI_STR("Could not create a register thread: %m")); } }
void wi_test_fsevents(void) { #ifdef WI_PTHREADS wi_string_t *directory; wi_test_fsevents_fsevents = wi_fsevents_init(wi_fsevents_alloc()); if(!wi_test_fsevents_fsevents) { if(wi_error_domain() != WI_ERROR_DOMAIN_LIBWIRED && wi_error_code() != WI_ERROR_FSEVENTS_NOTSUPP) WI_TEST_ASSERT_NOT_NULL(wi_test_fsevents_fsevents, "%m"); return; } directory = wi_fs_temporary_path_with_template(WI_STR("/tmp/libwired-fsevents.XXXXXXXX")); wi_fs_create_directory(directory, 0700); wi_fsevents_add_path(wi_test_fsevents_fsevents, directory); wi_fsevents_set_callback(wi_test_fsevents_fsevents, wi_test_fsevents_callback); wi_test_fsevents_lock = wi_condition_lock_init_with_condition(wi_condition_lock_alloc(), 0); if(!wi_thread_create_thread(wi_test_fsevents_thread, NULL)) WI_TEST_FAIL("%m"); wi_thread_sleep(0.1); wi_string_write_to_file(WI_STR("foo"), wi_string_by_appending_path_component(directory, WI_STR("file"))); wi_condition_lock_lock_when_condition(wi_test_fsevents_lock, 1, 0.0); if(!wi_test_fsevents_path) WI_TEST_FAIL("No fsevents callback received"); else WI_TEST_ASSERT_EQUAL_INSTANCES(wi_test_fsevents_path, directory, ""); wi_condition_lock_unlock(wi_test_fsevents_lock); wi_fs_delete_path(directory); wi_release(wi_test_fsevents_lock); wi_release(wi_test_fsevents_fsevents); wi_release(wi_test_fsevents_path); #endif }
static void wd_control_listen_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wi_socket_t *socket; wi_address_t *address; wi_string_t *ip; wd_user_t *user; pool = wi_pool_init(wi_pool_alloc()); while(wd_running) { wi_pool_drain(pool); /* accept new user */ socket = wi_socket_accept_multiple(wd_control_sockets, wd_control_socket_context, 30.0, &address); if(!address) { wi_log_err(WI_STR("Could not accept a connection: %m")); continue; } ip = wi_address_string(address); if(!socket) { wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); continue; } wi_socket_set_direction(socket, WI_SOCKET_READ); wi_log_info(WI_STR("Connect from %@"), ip); /* spawn a user thread */ user = wd_user_with_socket(socket); if(!wi_thread_create_thread(wd_control_thread, user)) wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); } wi_release(pool); }
void wd_transfers_schedule(void) { if(!wi_thread_create_thread(wd_transfers_queue_thread, NULL)) wi_log_fatal(WI_STR("Could not create a transfers queue thread: %m")); }
void wd_server_create_threads(void) { if(!wi_thread_create_thread(wd_control_listen_thread, NULL) || !wi_thread_create_thread(wd_transfer_listen_thread, NULL)) wi_log_fatal(WI_STR("Could not create a listen thread: %m")); }
static void _wi_timer_create_thread(void) { if(!wi_thread_create_thread(_wi_timer_thread, NULL)) wi_log_fatal(WI_STR("Could not create a timer thread: %m")); }
static void wd_transfer_listen_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wi_socket_t *socket; wi_address_t *address; wi_array_t *arguments; wi_string_t *ip, *string, *command; wd_transfer_t *transfer; pool = wi_pool_init(wi_pool_alloc()); while(wd_running) { wi_pool_drain(pool); /* accept new connection */ socket = wi_socket_accept_multiple(wd_transfer_sockets, wd_transfer_socket_context, 30.0, &address); if(!address) { wi_log_err(WI_STR("Could not accept a connection: %m")); continue; } ip = wi_address_string(address); if(!socket) { wi_log_err(WI_STR("Could not accept a connection for %@: %m"), ip); continue; } string = wi_socket_read_to_string(socket, 5.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); if(!string || wi_string_length(string) == 0) { if(!string) wi_log_warn(WI_STR("Could not read from %@: %m"), ip); continue; } /* parse command */ wi_parse_wired_command(string, &command, &arguments); if(wi_is_equal(command, WI_STR("TRANSFER")) && wi_array_count(arguments) >= 1) { /* get transfer by identifier */ transfer = wd_transfers_transfer_with_hash(WI_ARRAY(arguments, 0)); if(!transfer) continue; if(!wi_is_equal(ip, wd_user_ip(transfer->user))) continue; transfer->socket = wi_retain(socket); /* spawn a transfer thread */ if(!wi_thread_create_thread(wd_transfer_thread, transfer)) wi_log_err(WI_STR("Could not create a thread for %@: %m"), ip); } } wi_release(pool); }