feat: membrane remaining wasm host surfaces
This commit is contained in:
+111
-12
@@ -21,6 +21,14 @@ int uce_host_task_spawn(const char* key, size_t key_len, uint64_t callback_id, d
|
||||
int uce_host_task_pid(const char* key, size_t key_len);
|
||||
int uce_host_task_kill(int pid, int sig);
|
||||
unsigned int uce_host_sleep_us(uint64_t usec);
|
||||
uint64_t uce_host_socket_connect(const char* host, size_t host_len, int port);
|
||||
void uce_host_socket_close(uint64_t sockfd);
|
||||
int uce_host_socket_write(uint64_t sockfd, const char* data, size_t data_len);
|
||||
size_t uce_host_socket_read(uint64_t sockfd, uint32_t max_length, uint32_t timeout, char* buf, size_t cap);
|
||||
int uce_host_server_start_http(const char* key, size_t key_len, const char* bind, size_t bind_len, const char* file, size_t file_len, const char* function, size_t function_len, const char* current, size_t current_len);
|
||||
int uce_host_server_stop(const char* key, size_t key_len);
|
||||
size_t uce_host_memcache_command(uint64_t sockfd, const char* command, size_t command_len, char* buf, size_t cap);
|
||||
size_t uce_host_mysql(const char* in, size_t in_len, char* out, size_t cap);
|
||||
}
|
||||
|
||||
static String wasm_current_unit_file()
|
||||
@@ -137,10 +145,25 @@ String time_format_relative(u64 timestamp, String format_very_recent, u64 medium
|
||||
return(wasm_time_expand_delta(format_not_recent, timestamp, now_timestamp));
|
||||
}
|
||||
u64 time_parse(String time_String) { char* end = 0; unsigned long long v = strtoull(time_String.c_str(), &end, 10); return(end && *end == 0 ? (u64)v : 0); }
|
||||
u64 socket_connect(String host, short port) { (void)host; (void)port; return(0); }
|
||||
void socket_close(u64 sockfd) { (void)sockfd; }
|
||||
bool socket_write(u64 sockfd, String data) { (void)sockfd; (void)data; return(false); }
|
||||
String socket_read(u64 sockfd, u32 max_length, u32 timeout) { (void)sockfd; (void)max_length; (void)timeout; return(""); }
|
||||
u64 socket_connect(String host, short port)
|
||||
{
|
||||
return(uce_host_socket_connect(host.data(), host.size(), port));
|
||||
}
|
||||
void socket_close(u64 sockfd) { uce_host_socket_close(sockfd); }
|
||||
bool socket_write(u64 sockfd, String data)
|
||||
{
|
||||
return(uce_host_socket_write(sockfd, data.data(), data.size()) != 0);
|
||||
}
|
||||
String socket_read(u64 sockfd, u32 max_length, u32 timeout)
|
||||
{
|
||||
size_t required = uce_host_socket_read(sockfd, max_length, timeout, 0, 0);
|
||||
if(required == 0)
|
||||
return("");
|
||||
String content(required, 0);
|
||||
size_t got = uce_host_socket_read(sockfd, max_length, timeout, &content[0], required);
|
||||
content.resize(got <= required ? got : 0);
|
||||
return(content);
|
||||
}
|
||||
String ws_message() { return(context ? context->in : ""); }
|
||||
String ws_connection_id() { return(context ? context->resources.websocket_connection_id : ""); }
|
||||
String ws_scope() { return(context ? context->resources.websocket_scope : ""); }
|
||||
@@ -156,12 +179,78 @@ String capture_backtrace_string(u32 max_frames, u32 skip_frames) { (void)max_fra
|
||||
String signal_name(int sig) { (void)sig; return(""); }
|
||||
String memcache_escape_key(String key) { return(key); }
|
||||
StringList memcache_escape_keys(StringList keys) { return(keys); }
|
||||
u64 memcache_connect(String host, short port) { (void)host; (void)port; return(0); }
|
||||
String memcache_command(u64 connection, String command) { (void)connection; (void)command; return(""); }
|
||||
bool memcache_set(u64 connection, String key, String value, u64 expires_in) { (void)connection; (void)key; (void)value; (void)expires_in; return(false); }
|
||||
bool memcache_delete(u64 connection, String key) { (void)connection; (void)key; return(false); }
|
||||
String memcache_get(u64 connection, String key, String default_value) { (void)connection; (void)key; return(default_value); }
|
||||
StringMap memcache_get_multiple(u64 connection, StringList keys) { (void)connection; (void)keys; return(StringMap()); }
|
||||
u64 memcache_connect(String host, short port)
|
||||
{
|
||||
if(host == "")
|
||||
host = "127.0.0.1";
|
||||
if(port == 0)
|
||||
port = 11211;
|
||||
return(socket_connect(host, port));
|
||||
}
|
||||
String memcache_command(u64 connection, String command)
|
||||
{
|
||||
size_t required = uce_host_memcache_command(connection, command.data(), command.size(), 0, 0);
|
||||
if(required == 0)
|
||||
return("");
|
||||
String content(required, 0);
|
||||
size_t got = uce_host_memcache_command(connection, command.data(), command.size(), &content[0], required);
|
||||
content.resize(got <= required ? got : 0);
|
||||
return(content);
|
||||
}
|
||||
bool memcache_set(u64 connection, String key, String value, u64 expires_in)
|
||||
{
|
||||
String response = memcache_command(
|
||||
connection,
|
||||
String("set ") + memcache_escape_key(key) + " 0 " + std::to_string(expires_in) + " " + std::to_string(value.length()) + "\r\n" + value
|
||||
);
|
||||
return("STORED" == trim(response));
|
||||
}
|
||||
bool memcache_delete(u64 connection, String key)
|
||||
{
|
||||
String response = memcache_command(connection, String("delete ") + memcache_escape_key(key));
|
||||
return("DELETED" == trim(response));
|
||||
}
|
||||
String memcache_get(u64 connection, String key, String default_value)
|
||||
{
|
||||
String res = memcache_command(connection, String("get ") + memcache_escape_key(key));
|
||||
String header_end = "\r\n";
|
||||
size_t header_pos = res.find(header_end);
|
||||
if(res.rfind("VALUE ", 0) != 0 || header_pos == String::npos)
|
||||
return(default_value);
|
||||
String header = res.substr(0, header_pos);
|
||||
StringList parts = split(header, " ");
|
||||
if(parts.size() < 4)
|
||||
return(default_value);
|
||||
u64 length = int_val(parts[3]);
|
||||
size_t data_pos = header_pos + header_end.size();
|
||||
if(res.size() < data_pos + length)
|
||||
return(default_value);
|
||||
return(res.substr(data_pos, length));
|
||||
}
|
||||
StringMap memcache_get_multiple(u64 connection, StringList keys)
|
||||
{
|
||||
StringMap result;
|
||||
String res = memcache_command(connection, String("get ") + join(memcache_escape_keys(keys), " "));
|
||||
while(res.rfind("VALUE ", 0) == 0)
|
||||
{
|
||||
size_t header_pos = res.find("\r\n");
|
||||
if(header_pos == String::npos)
|
||||
break;
|
||||
StringList parts = split(res.substr(0, header_pos), " ");
|
||||
if(parts.size() < 4)
|
||||
break;
|
||||
String key = parts[1];
|
||||
u64 length = int_val(parts[3]);
|
||||
size_t data_pos = header_pos + 2;
|
||||
if(res.size() < data_pos + length)
|
||||
break;
|
||||
result[key] = res.substr(data_pos, length);
|
||||
res = res.substr(data_pos + length);
|
||||
if(res.rfind("\r\n", 0) == 0)
|
||||
res = res.substr(2);
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
void on_segfault(int sig) { (void)sig; }
|
||||
|
||||
static u64 wasm_next_task_callback_id = 1;
|
||||
@@ -195,8 +284,18 @@ pid_t task_repeat(String key, f64 interval, std::function<void()> exec_after_spa
|
||||
pid_t task_pid(String key) { return((pid_t)uce_host_task_pid(key.data(), key.size())); }
|
||||
extern "C" unsigned int sleep(unsigned int seconds) { return(uce_host_sleep_us((uint64_t)seconds * 1000000ull)); }
|
||||
extern "C" int usleep(unsigned int usec) { uce_host_sleep_us(usec); return(0); }
|
||||
pid_t server_start_http(String key, String socket_fn_or_port, String call_uce_filename, String call_function) { (void)key; (void)socket_fn_or_port; (void)call_uce_filename; (void)call_function; return(0); }
|
||||
bool server_stop(String key) { (void)key; return(false); }
|
||||
pid_t server_start_http(String key, String socket_fn_or_port, String call_uce_filename, String call_function)
|
||||
{
|
||||
String current = wasm_current_unit_file();
|
||||
return((pid_t)uce_host_server_start_http(
|
||||
key.data(), key.size(),
|
||||
socket_fn_or_port.data(), socket_fn_or_port.size(),
|
||||
call_uce_filename.data(), call_uce_filename.size(),
|
||||
call_function.data(), call_function.size(),
|
||||
current.data(), current.size()
|
||||
));
|
||||
}
|
||||
bool server_stop(String key) { return(uce_host_server_stop(key.data(), key.size()) != 0); }
|
||||
StringMap default_config()
|
||||
{
|
||||
StringMap cfg;
|
||||
|
||||
Reference in New Issue
Block a user