noise func
This commit is contained in:
parent
af29d51536
commit
3bc6fe7f90
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -5,18 +5,18 @@
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the names of the copyright holders nor the names of contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
@ -41,110 +41,110 @@
|
||||
|
||||
class FastCGIServer {
|
||||
public:
|
||||
FastCGIServer();
|
||||
~FastCGIServer();
|
||||
FastCGIServer();
|
||||
~FastCGIServer();
|
||||
|
||||
// called when the parameters and standard input have been receieved
|
||||
void request_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void request_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_request, new Handler<C>(object, function));
|
||||
}
|
||||
// called when the parameters and standard input have been receieved
|
||||
void request_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void request_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_request, new Handler<C>(object, function));
|
||||
}
|
||||
|
||||
// called when new data appears on stdin
|
||||
void data_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void data_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_data, new Handler<C>(object, function));
|
||||
}
|
||||
// called when new data appears on stdin
|
||||
void data_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void data_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_data, new Handler<C>(object, function));
|
||||
}
|
||||
|
||||
// called when the complete request has been received
|
||||
void complete_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void complete_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_complete, new Handler<C>(object, function));
|
||||
}
|
||||
// called when the complete request has been received
|
||||
void complete_handler(int (* function)(FastCGIRequest&));
|
||||
template<class C>
|
||||
void complete_handler(C& object, int (C::* function)(FastCGIRequest&)) {
|
||||
set_handler(handle_complete, new Handler<C>(object, function));
|
||||
}
|
||||
|
||||
void listen(unsigned tcp_port);
|
||||
void listen(const std::string& local_path);
|
||||
void abandon_files();
|
||||
void listen(unsigned tcp_port);
|
||||
void listen(const std::string& local_path);
|
||||
void abandon_files();
|
||||
|
||||
void process(int timeout_ms = -1); // timeout_ms<0 blocks forever
|
||||
void process_forever();
|
||||
void process(int timeout_ms = -1); // timeout_ms<0 blocks forever
|
||||
void process_forever();
|
||||
|
||||
protected:
|
||||
struct RequestInfo : FastCGIRequest {
|
||||
RequestInfo();
|
||||
struct RequestInfo : FastCGIRequest {
|
||||
RequestInfo();
|
||||
|
||||
std::string params_buffer;
|
||||
bool params_closed;
|
||||
bool in_closed;
|
||||
int status;
|
||||
bool output_closed;
|
||||
std::string params_buffer;
|
||||
bool params_closed;
|
||||
bool in_closed;
|
||||
int status;
|
||||
bool output_closed;
|
||||
|
||||
friend class FastCGIServer;
|
||||
};
|
||||
friend class FastCGIServer;
|
||||
};
|
||||
|
||||
typedef unsigned RequestID;
|
||||
typedef std::map<RequestID, RequestInfo*> RequestList;
|
||||
struct Connection {
|
||||
Connection();
|
||||
typedef unsigned RequestID;
|
||||
typedef std::map<RequestID, RequestInfo*> RequestList;
|
||||
struct Connection {
|
||||
Connection();
|
||||
|
||||
RequestList requests;
|
||||
std::string input_buffer;
|
||||
std::string output_buffer;
|
||||
bool close_responsibility;
|
||||
bool close_socket;
|
||||
};
|
||||
RequestList requests;
|
||||
std::string input_buffer;
|
||||
std::string output_buffer;
|
||||
bool close_responsibility;
|
||||
bool close_socket;
|
||||
};
|
||||
|
||||
typedef StringMap Pairs;
|
||||
typedef StringMap Pairs;
|
||||
|
||||
std::vector<int> listen_sockets;
|
||||
std::vector<std::string> listen_unlink;
|
||||
std::vector<int> listen_sockets;
|
||||
std::vector<std::string> listen_unlink;
|
||||
|
||||
std::map<int, Connection*> read_sockets;
|
||||
std::map<int, Connection*> read_sockets;
|
||||
|
||||
void process_connection_read(Connection&);
|
||||
static void process_write_request(Connection&, RequestID, RequestInfo&);
|
||||
static void process_connection_write(Connection&);
|
||||
static Pairs parse_pairs(const char*, std::string::size_type);
|
||||
static void write_pair(std::string& buffer,
|
||||
const std::string& key, const std::string&);
|
||||
static void write_data(std::string& buffer, RequestID id,
|
||||
const std::string& input, unsigned char type);
|
||||
void process_connection_read(Connection&);
|
||||
static void process_write_request(Connection&, RequestID, RequestInfo&);
|
||||
static void process_connection_write(Connection&);
|
||||
static Pairs parse_pairs(const char*, std::string::size_type);
|
||||
static void write_pair(std::string& buffer,
|
||||
const std::string& key, const std::string&);
|
||||
static void write_data(std::string& buffer, RequestID id,
|
||||
const std::string& input, unsigned char type);
|
||||
|
||||
|
||||
struct HandlerBase {
|
||||
virtual int operator()(FastCGIRequest&);
|
||||
};
|
||||
struct HandlerBase {
|
||||
virtual int operator()(FastCGIRequest&);
|
||||
};
|
||||
|
||||
struct StaticHandler : public HandlerBase {
|
||||
StaticHandler(int (* p_function)(FastCGIRequest&)) :
|
||||
function(p_function) {}
|
||||
int operator()(FastCGIRequest& request) {
|
||||
return function(request);
|
||||
}
|
||||
struct StaticHandler : public HandlerBase {
|
||||
StaticHandler(int (* p_function)(FastCGIRequest&)) :
|
||||
function(p_function) {}
|
||||
int operator()(FastCGIRequest& request) {
|
||||
return function(request);
|
||||
}
|
||||
|
||||
int (* function)(FastCGIRequest&);
|
||||
};
|
||||
int (* function)(FastCGIRequest&);
|
||||
};
|
||||
|
||||
template<class C>
|
||||
struct Handler : public HandlerBase {
|
||||
Handler(C& p_object, int (C::* p_function)(FastCGIRequest&)) :
|
||||
object(p_object), function(p_function) {}
|
||||
int operator()(FastCGIRequest& request) {
|
||||
return (object.*function)(request);
|
||||
}
|
||||
template<class C>
|
||||
struct Handler : public HandlerBase {
|
||||
Handler(C& p_object, int (C::* p_function)(FastCGIRequest&)) :
|
||||
object(p_object), function(p_function) {}
|
||||
int operator()(FastCGIRequest& request) {
|
||||
return (object.*function)(request);
|
||||
}
|
||||
|
||||
C& object;
|
||||
int (C::* function)(FastCGIRequest&);
|
||||
};
|
||||
C& object;
|
||||
int (C::* function)(FastCGIRequest&);
|
||||
};
|
||||
|
||||
void set_handler(HandlerBase*&, HandlerBase*);
|
||||
void set_handler(HandlerBase*&, HandlerBase*);
|
||||
|
||||
HandlerBase* handle_request;
|
||||
HandlerBase* handle_data;
|
||||
HandlerBase* handle_complete;
|
||||
HandlerBase* handle_request;
|
||||
HandlerBase* handle_data;
|
||||
HandlerBase* handle_complete;
|
||||
};
|
||||
|
||||
#endif // !FCGICC_H
|
||||
|
||||
@ -397,3 +397,68 @@ 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));
|
||||
}
|
||||
|
||||
|
||||
@ -29,3 +29,11 @@ void ob_start();
|
||||
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);
|
||||
|
||||
@ -153,7 +153,7 @@ struct ServerSettings {
|
||||
String COMPILER_SYS_PATH = ".";
|
||||
u32 LISTEN_PORT = 9993;
|
||||
u64 SESSION_TIME = 60*60*24*30;
|
||||
u32 WORKER_COUNT = 4;
|
||||
u32 WORKER_COUNT = 1;
|
||||
u32 MAX_MEMORY = 1024*1024*16;
|
||||
|
||||
};
|
||||
@ -236,6 +236,9 @@ struct Request {
|
||||
StringMap header;
|
||||
StringList set_cookies;
|
||||
|
||||
u64 random_seed;
|
||||
u64 random_index;
|
||||
|
||||
MemoryArena* request_arena;
|
||||
|
||||
String in;
|
||||
@ -246,12 +249,11 @@ struct Request {
|
||||
|
||||
bool is_finished = false;
|
||||
|
||||
f64 time_init;
|
||||
f64 time_start;
|
||||
f64 time_end;
|
||||
|
||||
struct Stats {
|
||||
u32 bytes_written;
|
||||
f64 time_init;
|
||||
f64 time_start;
|
||||
f64 time_end;
|
||||
} stats;
|
||||
|
||||
struct Resources {
|
||||
|
||||
@ -11,7 +11,7 @@ int handle_request(FastCGIRequest& request) {
|
||||
// server receives all parameters. There may be more data coming on the
|
||||
// standard input stream.
|
||||
request.request_arena = request_arena;
|
||||
request.time_init = microtime();
|
||||
request.stats.time_init = microtime();
|
||||
request.ob_start(); // this one is for the headers
|
||||
request.ob_start(); // this one is for the content
|
||||
if (request.params.count("REQUEST_URI"))
|
||||
@ -45,9 +45,11 @@ int handle_complete(FastCGIRequest& request) {
|
||||
|
||||
context = &request;
|
||||
request.server = &server_state;
|
||||
request.time_start = microtime();
|
||||
request.stats.time_start = microtime();
|
||||
request.header["Content-Type"] = context->server->config.CONTENT_TYPE;
|
||||
request.get = parse_query(request.params["QUERY_String"]);
|
||||
request.random_index = 0;
|
||||
request.random_seed = noise64(*reinterpret_cast<u64*>(&request.stats.time_start));
|
||||
|
||||
if(request.params["HTTP_COOKIE"].length() > 0)
|
||||
request.cookies = parse_cookies(request.params["HTTP_COOKIE"]);
|
||||
|
||||
@ -24,6 +24,7 @@ RENDER()
|
||||
<li><a href="memcached.uce">Memcached</a></li>
|
||||
<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>
|
||||
</ul>
|
||||
<pre><?
|
||||
print("Worker PID: ", my_pid, "\n");
|
||||
|
||||
46
test/random.uce
Normal file
46
test/random.uce
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
|
||||
RENDER()
|
||||
{
|
||||
|
||||
<>
|
||||
<link rel="stylesheet" href='style.css'></link>
|
||||
<h1>
|
||||
<a href="index.uce">UCE Test</a>:
|
||||
Random
|
||||
</h1>
|
||||
</>
|
||||
|
||||
|
||||
|
||||
<>
|
||||
Generate Some Numbers
|
||||
<pre><?
|
||||
|
||||
u64 max_64 = 0xffffffffffffffff;
|
||||
u64 max_32 = 0xffffffff;
|
||||
|
||||
for(auto i = 0; i < 100; i++)
|
||||
{
|
||||
print(i, ": ", (float)noise32(i)/(float)max_32, " / ", (float)noise64(i)/(float)max_64, " / ", noise01(i), " / ",
|
||||
generate_int(0, 4, i), " / ", generate_float(0.0, 4.0, i), "\n");
|
||||
}
|
||||
|
||||
?></pre>
|
||||
Draw Some Numbers
|
||||
<div>
|
||||
Seed <?= context->random_seed ?>
|
||||
</div>
|
||||
<pre><?
|
||||
|
||||
for(auto i = 0; i < 100; i++)
|
||||
{
|
||||
print(i, ": ", draw_int(0, 4), " / ", draw_float(0.0, 4.0), "\n");
|
||||
}
|
||||
|
||||
?></pre>
|
||||
Params
|
||||
<pre><?= var_dump(context->params) ?></pre>
|
||||
</>
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user