void wt_control_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wt_client_t *client = argument; wi_string_t *string; wi_socket_state_t state; wi_uinteger_t i = 0; pool = wi_pool_init(wi_pool_alloc()); wt_client_set(client); while(client->state <= WT_CLIENT_STATE_SAID_HELLO) { do { state = wi_socket_wait(client->socket, 0.1); } while(state == WI_SOCKET_TIMEOUT && client->state <= WT_CLIENT_STATE_SAID_HELLO); if(client->state > WT_CLIENT_STATE_SAID_HELLO) { /* invalid state */ break; } if(state == WI_SOCKET_ERROR) { if(wi_error_code() == EINTR) { /* got a signal */ continue; } else { /* error in TCP communication */ wi_log_err(WI_STR("Could not read from %@: %m"), client->ip); break; } } string = wi_socket_read_to_string(client->socket, 0.0, WI_STR(WT_MESSAGE_SEPARATOR_STR)); if(!string || wi_string_length(string) == 0) { if(!string) wi_log_info(WI_STR("Could not read from %@: %m"), client->ip); break; } wt_parse_command(string); if(++i % 10 == 0) wi_pool_drain(pool); } wi_log_info(WI_STR("Disconnect from %@ after %.2fs"), client->ip, wi_time_interval() - client->connect_time); wi_release(pool); }
void wi_test_error(void) { wi_string_t *string; wi_boolean_t result; result = wi_filesystem_file_exists_at_path(WI_STR("/non/existing/path"), NULL); WI_TEST_ASSERT_FALSE(result, ""); WI_TEST_ASSERT_EQUALS(wi_error_domain(), WI_ERROR_DOMAIN_ERRNO, ""); WI_TEST_ASSERT_EQUALS(wi_error_code(), ENOENT, ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_error_string(), WI_STR("No such file or directory"), ""); string = wi_string_with_format(WI_STR("%m")); WI_TEST_ASSERT_EQUAL_INSTANCES(string, WI_STR("No such file or directory"), ""); }
void wd_reply_error(void) { switch(wi_error_code()) { case ENOENT: wd_reply(520, WI_STR("File or Directory Not Found")); break; case EEXIST: wd_reply(521, WI_STR("File or Directory Exists")); break; default: wd_reply(500, WI_STR("Command Failed")); break; } }
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 }
void wd_control_thread(wi_runtime_instance_t *argument) { wi_pool_t *pool; wd_client_t *client = argument; wi_string_t *string; unsigned int i = 0; int state; pool = wi_pool_init(wi_pool_alloc()); wd_clients_add_client(client); wd_client_set(client); while(client->state <= WD_CLIENT_STATE_LOGGED_IN) { if(!pool) pool = wi_pool_init(wi_pool_alloc()); if(client->buffer_offset == 0) { do { state = wi_socket_wait(client->socket, 0.1); } while(state == 0 && client->state <= WD_CLIENT_STATE_LOGGED_IN); if(client->state > WD_CLIENT_STATE_LOGGED_IN) { /* invalid state */ break; } if(state < 0) { if(wi_error_code() == EINTR) { /* got a signal */ continue; } else { /* error in TCP communication */ wi_log_err(WI_STR("Could not read from %@: %m"), client->ip); break; } } } wd_client_lock_socket(client); string = wi_socket_read_to_string(client->socket, 0.0, WI_STR(WD_MESSAGE_SEPARATOR_STR)); wd_client_unlock_socket(client); if(!string || wi_string_length(string) == 0) { if(!string) wi_log_info(WI_STR("Could not read from %@: %m"), client->ip); break; } wd_parse_command(string); if(++i % 10 == 0) { wi_release(pool); pool = NULL; } } /* announce parting if client disconnected by itself */ if(client->state == WD_CLIENT_STATE_LOGGED_IN) { client->state = WD_CLIENT_STATE_DISCONNECTED; wd_broadcast_lock(); wd_client_broadcast_leave(client, WD_PUBLIC_CID); wd_broadcast_unlock(); } /* update status for clients logged in and above */ if(client->state >= WD_CLIENT_STATE_LOGGED_IN) { wi_lock_lock(wd_status_lock); wd_current_users--; wd_write_status(true); wi_lock_unlock(wd_status_lock); } wi_log_info(WI_STR("Disconnect from %@"), client->ip); wi_socket_close(client->socket); wd_clients_remove_client(client); wi_release(pool); }
void wd_reply_error(void) { if(wi_error_domain() == WI_ERROR_DOMAIN_ERRNO) wd_reply_errno(wi_error_code()); else wd_reply(500, WI_STR("Command Failed")); }