task() API, documentation work

This commit is contained in:
Udo 2022-01-09 00:20:35 +00:00
parent 2c2225c76f
commit cbf57a9a35
79 changed files with 1094 additions and 136 deletions

Binary file not shown.

8
doc/areas/memcache.txt Normal file
View File

@ -0,0 +1,8 @@
Memcache Functions
memcache_command
memcache_connect
memcache_delete
memcache_get
memcache_get_multiple
memcache_set

8
doc/areas/mysql.txt Normal file
View File

@ -0,0 +1,8 @@
MySQL Functions
mysql_connect
mysql_disconnect
mysql_error
mysql_escape
mysql_insert_id
mysql_query

9
doc/areas/noise.txt Normal file
View File

@ -0,0 +1,9 @@
Noise Functions
draw_float
draw_int
generate_float
generate_int
noise01
noise32
noise64

6
doc/areas/ob.txt Normal file
View File

@ -0,0 +1,6 @@
Output Buffer Functions
ob_clear
ob_get
ob_get_clear
ob_start

6
doc/areas/socket.txt Normal file
View File

@ -0,0 +1,6 @@
Socket Functions
socket_close
socket_connect
socket_read
socket_write

8
doc/areas/string.txt Normal file
View File

@ -0,0 +1,8 @@
String Functions
filter
first
join
nibble
split
trim

16
doc/areas/sys.txt Normal file
View File

@ -0,0 +1,16 @@
System Functions
basename
dirname
expand_path
file_exists
file_get_contents
file_mtime
file_put_contents
get_cwd
ls
mkdir
set_cwd
shell_escape
shell_exec
unlink

7
doc/areas/time.txt Normal file
View File

@ -0,0 +1,7 @@
Time and Date Functions
microtime
time
date
gmdate
parse_time

7
doc/areas/uri.txt Normal file
View File

@ -0,0 +1,7 @@
URI Functions
encode_query
make_session_id
parse_query
uri_decode
uri_encode

View File

@ -1,5 +1,22 @@
void render_see_section(String name)
{
StringList lines = split(file_get_contents("areas/"+name+".txt"), "\n");
s32 idx = 0;
for(auto line : lines)
{
if(idx == 0)
{
<><h3><?= line ?></h3><ul></>
}
else if(line != "")
{
<><li><a href="index.uce?p=<?= line ?>"><?= line ?><span style="opacity:0.5">()</span></a></li></>
}
idx += 1;
}
<></ul></>
}
RENDER()
@ -20,13 +37,36 @@ RENDER()
if(page == "index")
{
?><div style="display:flex;"><?
?><div style="flex:1"><?
?><h3>All API Functions</h3><?
for(auto file_name : ls("pages/"))
{
String ft = nibble(file_name, ".");
?>
<div><a href="?p=<?= ft ?>"><?= ft ?></a></div>
<?
if(ft[1] == '_')
{
String fn = ft;
String pre = nibble(fn, "_");
?>
<div><a href="?p=<?= ft ?>"><?= fn ?></a><span style="opacity:0.5"> : struct</span></div>
<?
}
else
{
?>
<div><a href="?p=<?= ft ?>"><?= ft ?><span style="opacity:0.5">()</span></a></div>
<?
}
}
?></div><?
?><div style="flex:1"><?
for(auto file_name : ls("areas/"))
{
String ft = nibble(file_name, ".");
render_see_section(ft);
}
?></div><?
?></div><?
}
else
{
@ -51,10 +91,18 @@ RENDER()
{
?><h3>Signature</h3><?
}
else if(layout_class == "pre")
{
layout_class = "sig";
}
else if(layout_class == "desc")
{
?><h3>Description</h3><?
}
else if(layout_class == "see")
{
?><h3>Related</h3><?
}
else
{
?><h3><?= layout_class ?></h3><?
@ -72,6 +120,19 @@ RENDER()
{
?><div><b><?= trim(nibble(s, ":")) ?></b> : <?= trim(s) ?></div><?
}
else if(layout_class == "see")
{
?><div style="margin-left: 2em"><?
if(s[0] == '>')
{
render_see_section(s.substr(1));
}
else
{
?><div><a href="index.uce?p=<?= trim(s) ?>"><?= trim(s) ?><span style="opacity:0.5">()</span></a></div><?
}
?></div><?
}
else
{
?><div><?= (s) ?></div><?

63
doc/pages/0_context.txt Normal file
View File

@ -0,0 +1,63 @@
:sig
Request* context;
:ServerState* server
Contains the current server state
:StringMap params
All FastCGI server parameters
:StringMap get
The current request's GET variables
:StringMap post
The current request's POST variables
:StringMap cookies
Cookies that have been transmitted by the browser
:StringMap session
The current session
:String session_id
ID of the session cookie
String session_name
Name of the session cookie
:DTree var
Variable user-defined data
:std::vector<UploadedFile> uploaded_files
Files that have been uploaded in the current request
:StringMap header
Headers to be sent back to the browser
:StringList set_cookies;
Cookies that should be sent back to the browser
:u64 random_seed
The current request's "random" noise generator seed
:u64 random_index
The current request's "random" noise generator index position
:MemoryArena* mem
Contains the current request's memory arena
:bool flags.log_request
Whether the request should be logged
:Stats
u32 stats.bytes_written
f64 stats.time_init
f64 stats.time_start
f64 stats.time_end
:invoke(String file_name, [DTree& call_param])
Invokes the UCE file 'file_name'

12
doc/pages/basename.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String basename(String fn)
:params
fn : raw filename
return value : the file's name
:desc
Isolates the file name component from a path/file name.
:see
>sys

View File

@ -1,57 +0,0 @@
:sig
Request* context;
//
struct Request {
ServerState* server;
StringMap params;
StringMap get;
StringMap post;
StringMap cookies;
StringMap session;
DTree var;
String session_id = "";
String session_name = "";
std::vector<UploadedFile> uploaded_files;
StringMap header;
StringList set_cookies;
u64 random_seed;
u64 random_index;
MemoryArena* mem;
String in;
std::vector<std::ostringstream*> ob_stack;
std::ostringstream* ob;
String out;
String err;
bool is_finished = false;
struct Flags {
bool log_request = true;
} flags;
struct Stats {
u32 bytes_written;
f64 time_init;
f64 time_start;
f64 time_end;
} stats;
void invoke(String file_name);
void invoke(String file_name, DTree& call_param);
void ob_start();
};
:desc

125
doc/pages/date.txt Normal file
View File

@ -0,0 +1,125 @@
:sig
String date(String format = "", u64 timestamp = 0)
:params
format : formatting string specifying the date format
timestamp : optional timestamp value, defaults to current time
return value : a formatted date
:desc
Returns a formatted date. This is based on the Linux date() command. The formatting string supports the following sequences:
:pre
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; like %+4Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only
with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week
(00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week
(00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g.,
-04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
+ pad with zeros, and put '+' before future years with >4
digits
^ use upper case if possible
# use opposite case if possible
:see
>time

12
doc/pages/dirname.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String dirname(String fn)
:params
fn : raw filename
return value : the directory's name
:desc
Isolates the directory name component from a path/file name.
:see
>sys

View File

@ -9,3 +9,5 @@ return value : a noise value between 'from' and 'to'
:desc
This function works exactly like generate_float(), but context->random_index is used for the 'index' value and context->random_seed is used for the seed. After this function has been called, the context->random_index is increased by one. At the start of every request, context->random_seed is automatically populated with a new seed value.
:see
>noise

View File

@ -9,3 +9,5 @@ return value : a noise value between 'from' and 'to'
:desc
This function works exactly like generate_int(), but context->random_index is used for the 'index' value and context->random_seed is used for the seed. After this function has been called, the context->random_index is increased by one. At the start of every request, context->random_seed is automatically populated with a new seed value.
:see
>noise

View File

@ -0,0 +1,12 @@
:sig
String encode_query(StringMap map)
:params
q : StringMap containing URL parameters to be encoded
return value : a string with the encoded parameters
:desc
Encodes a StringMap containing URL parameters into a single String.
:see
>uri

12
doc/pages/expand_path.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String expand_path(String path)
:params
path : a relative path
return value : expanded version of the 'path'
:desc
Converts a relative path name into an absolute path, using the current working directory as a base.
:see
>sys

12
doc/pages/file_exists.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
bool file_exists(String path)
:params
path : the path name to be checked
return value : true if the file exists
:desc
Checks whether the file or path specified by 'path' exists.
:see
>sys

View File

@ -0,0 +1,12 @@
:sig
String file_get_contents(String file_name)
:params
file_name : file name of file that should be read
return value : String containing the file's contents
:desc
Reads the file identified by 'file_name' and returns it as a String. If the file cannot be read, this function will return an empty string.
:see
>sys

13
doc/pages/file_mtime.txt Normal file
View File

@ -0,0 +1,13 @@
:sig
time_t file_mtime(String file_name)
:params
file_name : name of the file
return value : Unix time stamp of the file's last modification
:desc
Retrieves the last modification date of 'file_name' as a Unix timestamp.
:see
>sys
>time

View File

@ -0,0 +1,13 @@
:sig
bool file_put_contents(String file_name, String content)
:params
file_name : file name of file that should be written
content : content that should be written
return value : true if write was successful
:desc
Writes the String 'content' into a file identified by 'file_name'. Any pre-existing content of the file will be overwritten.
:see
>sys

View File

@ -9,3 +9,6 @@ return value : a new list
:desc
Returns a list containing the members of 'items' for which 'f' returned boolean true.
:see
>string

View File

@ -8,3 +8,5 @@ return value : first of the 'args' that was not empty.
:desc
Given a variable number of String parameters, the first() function returns the first of these parameters that was not empty. Leading and trailing whitespace characters are not considered, resulting in a string that contains only whitespace characters being considered empty.
:see
>string

View File

@ -11,3 +11,6 @@ return value : noise value
:desc
Generates a noise value between 'from' and 'to', given the 'index' and 'seed' numbers.
:see
>noise

View File

@ -11,3 +11,6 @@ return value : noise value
:desc
Generates a noise value between 'from' and 'to', given the 'index' and 'seed' numbers.
:see
>noise

11
doc/pages/get_cwd.txt Normal file
View File

@ -0,0 +1,11 @@
:sig
String get_cwd()
:params
return value : the current working directory
:desc
Returns the current working directory.
:see
>sys

125
doc/pages/gmdate.txt Normal file
View File

@ -0,0 +1,125 @@
:sig
String gmdate(String format = "", u64 timestamp = 0)
:params
format : formatting string specifying the date format
timestamp : optional timestamp value, defaults to current time
return value : a formatted date
:desc
Returns a formatted date in the GMT/UTC timezone. This is based on the Linux date() command. The formatting string supports the following sequences:
:pre
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; like %+4Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only
with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%t a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week
(00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is Sunday
%W week number of year, with Monday as first day of week
(00..53)
%x locale's date representation (e.g., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%y last two digits of year (00..99)
%Y year
%z +hhmm numeric time zone (e.g., -0400)
%:z +hh:mm numeric time zone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g.,
-04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
+ pad with zeros, and put '+' before future years with >4
digits
^ use upper case if possible
# use opposite case if possible
:see
>time

View File

@ -9,3 +9,5 @@ return value : a string containing items joined by 'delim'
:desc
Joins the items contained in 'l' into a single String.
:see
>string

12
doc/pages/ls.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
StringList ls(String path)
:params
path : a filesystem path
return value : list of directory entries
:desc
Returns a list of files and subdirectories within the given 'path'.
:see
>sys

View File

@ -0,0 +1,11 @@
:sig
String make_session_id()
:params
return value : a new session ID
:desc
Creates a session ID
:see
>uri

View File

@ -0,0 +1,13 @@
:sig
String memcache_command(u64 connection, String command)
:params
connection : connection handle
command : string containing the Memcache command
return value : string containing the Memcache server's response
:desc
Executes a command on an open memcache connection.
:see
>memcache

View File

@ -0,0 +1,13 @@
:sig
u64 memcache_connect(String host = "127.0.0.1", short port = 11211)
:params
host : optional host name of the memcache server, defaults to local address 127.0.0.1
port : optional memcache server's port, defaults to 11211
return value : the connection handle (or -1 if an error occurred)
:desc
Connects to a memcache server instance.
:see
>memcache

View File

@ -0,0 +1,13 @@
:sig
bool memcache_delete(u64 connection, String key)
:params
connection : connection handle
key : key string
return value : true if the operation was successful
:desc
Deletes entry specified by the 'key'.
:see
>memcache

View File

@ -0,0 +1,14 @@
:sig
String memcache_get(u64 connection, String key, String default_value = "")
:params
connection : connection handle
key : key string
default_value : optional default value
return value : value that was returned by the Memcache server
:desc
Retrieves a value from an existing connection to a Memcache server.
:see
>memcache

View File

@ -0,0 +1,13 @@
:sig
StringMap memcache_get_multiple(u64 connection, StringList keys)
:params
connection : connection handle
keys : a list of strings containing the keys to be retrieved
return value : a StringMap with the retrieved entries
:desc
Retrieves a bunch of entries all at once.
:see
>memcache

View File

@ -0,0 +1,15 @@
:sig
bool memcache_set(u64 connection, String key, String value, u64 expires_in = 60*60)
:params
connection : connection handle
key : the entry's key
value : the value to be set
expires_in : optional expiration timeout, defaults to one hour
return value : true if the operation was successful
:desc
Stores a 'value' on the Memcache server.
:see
>memcache

11
doc/pages/microtime.txt Normal file
View File

@ -0,0 +1,11 @@
:sig
f64 microtime()
:params
return value : current Unix timestamp
:desc
Returns a 64 bit float containing the current Unix timestamp with millisecond accuracy or better.
:see
>time

12
doc/pages/mkdir.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
bool mkdir(String path)
:params
path : the path name to be created
return value : returns true if the directory was successfully created
:desc
Creates a directory stated by 'path'
:see
>sys

View File

@ -9,4 +9,5 @@ return value : string before first occurrence of 'delim'
:desc
Returns the part of 'haystack' before the first occurrence of 'delim', removing the corresponding part from 'haystack' (including 'delim'). If the substring 'delim' does not occurr in 'haystack', the entire string is returned and 'haystack' is set to an empty string.
:see
>string

View File

@ -8,3 +8,6 @@ return value : a noise value from 0 to 1
:desc
Generates a noise value in the range from 0 to 1 for the given 'index' and 'seed' values.
:see
>noise

View File

@ -8,3 +8,6 @@ return value : a noise value given the 'index' and 'seed' values.
:desc
Generates a noise value for the given 'index' and 'seed' values.
:see
>noise

View File

@ -8,3 +8,6 @@ return value : a noise value given the 'index' and 'seed' values.
:desc
Generates a noise value for the given 'index' and 'seed' values.
:see
>noise

View File

@ -2,7 +2,10 @@
void ob_clear()
:params
-
(none)
:desc
Discard the current output buffer.
:see
>ob

View File

@ -6,3 +6,6 @@ return value : content of the current output buffer
:desc
Returns the contents of the current output buffer.
:see
>ob

View File

@ -6,3 +6,6 @@ return value : content of the current output buffer
:desc
Returns the contents of the current output buffer and then discards the buffer.
:see
>ob

View File

@ -2,8 +2,10 @@
void ob_start()
:params
-
(none)
:desc
Starts a new output buffer. All subsequent output will be directed into this buffer.
:see
>ob

12
doc/pages/parse_query.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
StringMap parse_query(String q)
:params
q : string containing URL parameters
return value : a StringMap containing the parameters
:desc
Decodes a string of the format 'a=b&c=d' into a StringMap containing keyed entries.
:see
>uri

12
doc/pages/parse_time.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
u64 parse_time(String time_string)
:params
time_string : a string containing a date and/or time in text form
return value : the interpreted 'time_string' as a Unix timestamp
:desc
Attempts to parse the given 'time_string' into a Unix timestamp.
:see
>time

11
doc/pages/set_cwd.txt Normal file
View File

@ -0,0 +1,11 @@
:sig
void set_cwd(String path)
:params
path : the new working directory
:desc
Sets a new working directory.
:see
>sys

10
doc/pages/sha1.txt Normal file
View File

@ -0,0 +1,10 @@
:sig
String sha1(String s, bool as_binary = false)
:params
s : data to be hashed
as_binary : when set to false, returns hash in hexadecimal notation (defaults to false)
return value : the resulting hash value
:desc
Returns the sha1 hash of 's'.

View File

@ -0,0 +1,12 @@
:sig
String shell_escape(String raw)
:params
raw : string that should be escaped
return value : escaped version of 'raw'
:desc
Escapes a parameter for shell_exec
:see
>sys

12
doc/pages/shell_exec.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String shell_exec(String cmd)
:params
cmd : string that contains the shell command line to be executed
return value : output of the command execution
:desc
Executes a Linux shell command and returns the generated output
:see
>sys

View File

@ -0,0 +1,11 @@
:sig
void socket_close(u64 sockfd)
:params
sockfd : socket handle
:desc
Closes an existing socket connection.
:see
>socket

View File

@ -0,0 +1,13 @@
:sig
u64 socket_connect(String host, short port)
:params
host : host name
port : port number
return value : the socket handle
:desc
Opens a socket connection to the given 'host' and 'port'.
:see
>socket

14
doc/pages/socket_read.txt Normal file
View File

@ -0,0 +1,14 @@
:sig
String socket_read(u64 sockfd, u32 max_length = 1024*128, u32 timeout = 1);
:params
sockfd : socket handle
max_length : optional maximum data size, defaults to 128kBytes
timeout : optional operation timeout, defaults to one second
return value : string containing the data that was read
:desc
Reads data from a socket connection.
:see
>socket

View File

@ -0,0 +1,13 @@
:sig
bool socket_write(u64 sockfd, String data)
:params
sockfd : socket handle
data : a string containing the data to be written to the socket
return value : true if the write operation was successful
:desc
Writes a string of 'data' to the given socket.
:see
>socket

View File

@ -9,3 +9,5 @@ return value : a list of strings
:desc
Splits 'str' into multiple strings based on the given delimiter 'delim'.
:see
>string

11
doc/pages/time.txt Normal file
View File

@ -0,0 +1,11 @@
:sig
u64 time()
:params
return value : second-accurate current Unix timestamp
:desc
Returns a 64 bit integer containing the current Unix timestamp.
:see
>time

View File

@ -8,3 +8,5 @@ return value : string with leading and trailing whitespace characters removed
:desc
Returns a string where leading an trailing whitespace characters have been trimmed off.
:see
>string

11
doc/pages/unlink.txt Normal file
View File

@ -0,0 +1,11 @@
:sig
void unlink(String file_name)
:params
file_name : name of the file
:desc
Deletes the file identified by 'file_name'.
:see
>sys

12
doc/pages/uri_decode.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String uri_decode(String s)
:params
s : string containing URI encoded data
return value : a string that contains the decoded version of 's'
:desc
Decodes an URI-encoded string 's'.
:see
>uri

12
doc/pages/uri_encode.txt Normal file
View File

@ -0,0 +1,12 @@
:sig
String uri_encode(String s)
:params
s : string that should be encoded
return value : an URI-encoded version of 's'
:desc
URI-encodes a string.
:see
>uri

View File

@ -53,6 +53,7 @@ FastCGIServer::RequestInfo::RequestInfo() :
in_closed(false),
output_closed(false)
{
}
@ -71,6 +72,7 @@ FastCGIServer::FastCGIServer()
FastCGIServer::~FastCGIServer()
{
if(my_pid != parent_pid) // if we're a child process, we must not close the handles
return;
@ -214,6 +216,7 @@ FastCGIServer::process(int timeout_ms)
if (posix_con == -1)
throw std::runtime_error("accept() failed");
read_sockets[posix_con] = new Connection();
read_sockets[posix_con]->posix_con = posix_con;
}
for (std::map<int, Connection*>::iterator it = read_sockets.begin();
@ -377,6 +380,7 @@ FastCGIServer::process_connection_read(Connection& connection)
request_arena->clear();
switch_to_arena(request_arena);
RequestInfo* new_request = new RequestInfo();
new_request->resources.fcgi_socket = connection.posix_con;
new_request->mem = request_arena;
new_request->stats.time_init = microtime();
switch_to_system_alloc();

View File

@ -77,6 +77,7 @@ protected:
Connection();
RequestList requests;
u64 posix_con = 0;
std::string input_buffer;
std::string output_buffer;
bool close_responsibility;

View File

@ -386,68 +386,3 @@ String ob_get_close()
return(result);
}
#define BIT_NOISE1 0xB5297A4D
#define BIT_NOISE2 0x68E31DA4
#define BIT_NOISE3 0x1B56C4E9
// based on Squirrel3 https://www.youtube.com/watch?v=LWFzPP8ZbdU&t=2666s
u32 noise32(u32 index, u32 seed)
{
u32 r = index;
r *= BIT_NOISE1;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE2;
r ^= (r << 8);
r *= BIT_NOISE3;
r ^= (r >> 8);
return(r);
}
#define BIT_NOISE61 0x5134811636f8cc8a
#define BIT_NOISE62 0xb8E31DA41B56C4E9
#define BIT_NOISE63 0x18cd227aaa1168c1
u64 noise64(u64 index, u64 seed)
{
u64 r = index;
r *= BIT_NOISE61;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE62;
r ^= (r << 8);
r *= BIT_NOISE63;
r ^= (r >> 8);
return(r);
}
#define MAX_64 0xffffffffffffffff
f64 noise01(u64 index, u64 seed)
{
return((float)noise64(index, seed)/(float)MAX_64);
}
u64 generate_int(u64 from, u64 to, u64 index, u64 seed)
{
u64 b = 1 + to - from;
return(from + (noise64(index, seed) % b));
}
#include <tgmath.h>
f64 generate_float(f64 from, f64 to, u64 index, u64 seed, f64 decimal_precision)
{
f64 b = to - from;
return(from + fmod( decimal_precision*(f64)noise64(index, seed), b));
}
u64 draw_int(u64 from, u64 to)
{
return(generate_int(from, to, context->random_index++, context->random_seed));
}
f64 draw_float(f64 from, f64 to, f64 decimal_precision)
{
return(generate_float(from, to, context->random_index++, context->random_seed, decimal_precision));
}

View File

@ -46,10 +46,3 @@ void ob_clear();
String ob_get_clear();
String ob_get();
u32 noise32(u32 index, u32 seed = 0);
u64 noise64(u64 index, u64 seed = 0);
f64 noise01(u64 index, u64 seed = 0);
u64 generate_int(u64 from, u64 to, u64 index, u64 seed = 0);
f64 generate_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision = 0.000000000001);
u64 draw_int(u64 from, u64 to);
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);

View File

@ -273,3 +273,69 @@ sha1(String s, bool as_binary)
result += to_hex(v[i], 2);
return(result);
}
#define BIT_NOISE1 0xB5297A4D
#define BIT_NOISE2 0x68E31DA4
#define BIT_NOISE3 0x1B56C4E9
// based on Squirrel3 https://www.youtube.com/watch?v=LWFzPP8ZbdU&t=2666s
u32 noise32(u32 index, u32 seed)
{
u32 r = index;
r *= BIT_NOISE1;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE2;
r ^= (r << 8);
r *= BIT_NOISE3;
r ^= (r >> 8);
return(r);
}
#define BIT_NOISE61 0x5134811636f8cc8a
#define BIT_NOISE62 0xb8E31DA41B56C4E9
#define BIT_NOISE63 0x18cd227aaa1168c1
u64 noise64(u64 index, u64 seed)
{
u64 r = index;
r *= BIT_NOISE61;
r += seed;
r ^= (r >> 8);
r += BIT_NOISE62;
r ^= (r << 8);
r *= BIT_NOISE63;
r ^= (r >> 8);
return(r);
}
#define MAX_64 0xffffffffffffffff
f64 noise01(u64 index, u64 seed)
{
return((float)noise64(index, seed)/(float)MAX_64);
}
u64 generate_int(u64 from, u64 to, u64 index, u64 seed)
{
u64 b = 1 + to - from;
return(from + (noise64(index, seed) % b));
}
#include <tgmath.h>
f64 generate_float(f64 from, f64 to, u64 index, u64 seed, f64 decimal_precision)
{
f64 b = to - from;
return(from + fmod( decimal_precision*(f64)noise64(index, seed), b));
}
u64 draw_int(u64 from, u64 to)
{
return(generate_int(from, to, context->random_index++, context->random_seed));
}
f64 draw_float(f64 from, f64 to, f64 decimal_precision)
{
return(generate_float(from, to, context->random_index++, context->random_seed, decimal_precision));
}

View File

@ -7,3 +7,13 @@ By Steve Reid <steve@edmweb.com>
String sha1(String s, bool as_binary = false);
u32 noise32(u32 index, u32 seed = 0);
u64 noise64(u64 index, u64 seed = 0);
f64 noise01(u64 index, u64 seed = 0);
u64 generate_int(u64 from, u64 to, u64 index, u64 seed = 0);
f64 generate_float(f64 from, f64 to, u64 index, u64 seed = 0, f64 decimal_precision = 0.000000000001);
u64 draw_int(u64 from, u64 to);
f64 draw_float(f64 from, f64 to, f64 decimal_precision = 0.000000000001);

View File

@ -42,6 +42,11 @@ bool MySQL::connect(String host, String username, String password)
}
String MySQL::escape(String raw, char quote_char)
{
return(mysql_escape(raw, quote_char));
}
String mysql_escape(String raw, char quote_char)
{
String result;
result.append(1, quote_char);

View File

@ -31,3 +31,37 @@ struct MySQL {
DTree get_pending_result();
};
MySQL* mysql_connect(String host = "localhost", String username = "root", String password = "")
{
MySQL* m = new MySQL();
m->connect(host, username, password);
return(m);
}
void mysql_disconnect(MySQL* m)
{
m->disconnect();
}
String mysql_error(MySQL* m)
{
return(m->error());
}
String mysql_escape(String raw, char quote_char);
DTree mysql_query(MySQL* m, String q)
{
return(m->query(q));
}
DTree mysql_query(MySQL* m, String q, StringMap params)
{
return(m->query(q, params));
}
u64 mysql_insert_id(MySQL* m)
{
return(m->insert_id);
}

View File

@ -423,6 +423,44 @@ void spawn_subprocess(std::function<void()> exec_after_spawn)
}
}
pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout)
{
String status_file_name = context->server->config.BIN_DIRECTORY + "/task-" + key;
String status_file = file_get_contents(status_file_name);
pid_t p;
if(status_file != "")
{
p = int_val(status_file);
if(kill(p, 0) == 0) // process is still running
{
printf("(P) worker process '%s' already running: PID %i\n", key.c_str(), p);
return(p);
}
//printf("(P) worker process '%s' had crashed: PID %i\n", key.c_str(), p);
unlink(status_file_name);
}
p = fork();
if(p == 0)
{
my_pid = getpid();
file_put_contents(status_file_name, std::to_string(my_pid));
close(context->resources.fcgi_socket);
context->resources.fcgi_socket = 0;
//printf("(C) child procress started, PID:%i\n", my_pid);
//prctl(PR_SET_PDEATHSIG, SIGHUP);
exec_after_spawn();
unlink(status_file_name);
printf("(P) worker process '%s' terminated: PID %i\n", key.c_str(), my_pid);
exit(0);
}
else
{
printf("(P) worker process '%s' spawned: PID %i\n", key.c_str(), p);
return(p);
}
}
void on_child_exit(int sig)
{
pid_t pid;

View File

@ -39,3 +39,4 @@ pid_t parent_pid = 0;
pid_t my_pid = 0;
void on_segfault(int sig);
pid_t task(String key, std::function<void()> exec_after_spawn, u64 timeout = 60*10);

View File

@ -268,6 +268,7 @@ struct Request {
struct Resources {
std::vector<u64> sockets;
std::vector<void*> mysql_connections;
u64 fcgi_socket = 0;
} resources;
void invoke(String file_name);

View File

@ -26,6 +26,7 @@ RENDER()
<li><a href="mysql.uce">MySQL Connector</a></li>
<li><a href="fileio.uce">File I/O</a></li>
<li><a href="random.uce">RNG/Noise</a></li>
<li><a href="task.uce">Task API</a></li>
</ul>
<pre><?
print("Worker PID: ", my_pid, "\n");

33
test/task.uce Normal file
View File

@ -0,0 +1,33 @@
RENDER()
{
DTree t;
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Tasks
</h1>
<pre style="white-space: pre-wrap"><?
print("Task ID: ", task("example-task", []() {
sleep(10);
}), "\n");
print("FCGI SOCKET: ", context->resources.fcgi_socket);
?></pre>
Params
<pre><?= var_dump(context->params) ?></pre>
</>
}