-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52e91f6
commit 33e3b48
Showing
8 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[package] | ||
name = "wasm-benchmark" | ||
version = "0.1.0" | ||
authors = [ | ||
"natrixaeria", | ||
"TrueDoctor" | ||
] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
js-sys = "0.3" | ||
wasm-bindgen = "0.2" | ||
log = "0.4" | ||
fern = "0.5" | ||
|
||
[dependencies.web-sys] | ||
version = "0.3.22" | ||
features = [ | ||
'HtmlElement', | ||
'Node', | ||
'Document', | ||
'Event', | ||
'MessageEvent', | ||
'Element', | ||
'HtmlCanvasElement', | ||
'WorkerGlobalScope', | ||
'DedicatedWorkerGlobalScope', | ||
'OffscreenCanvas', | ||
'WebGl2RenderingContext', | ||
'WebGlShader', | ||
'WebGlProgram', | ||
'WebGlBuffer', | ||
'Window', | ||
'Worker' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from threading import Thread | ||
from socket import socket, SOL_SOCKET, SO_REUSEADDR | ||
|
||
def enable_csp(): | ||
global ADD_HEADERS | ||
ALL = ["'self'", "developer.mozilla.org"] | ||
CSP = { | ||
'script-src': ["'unsafe-inline'", *ALL], | ||
'worker-src': [*ALL], | ||
'style-src': ["'unsafe-inline'", *ALL], | ||
'default-src': [*ALL], | ||
} | ||
|
||
ADD_HEADERS = 'Content-Security-Policy: ' + '; '.join( | ||
k + ' ' + ' '.join(v) for k, v in CSP.items()) | ||
# ADD_HEADERS += '\r\n' + ADD_HEADERS.replace('cy: ', 'cy-Report-Only: ') | ||
print(ADD_HEADERS) | ||
ADD_HEADERS = '\r\n' + ADD_HEADERS | ||
|
||
ADD_HEADERS = '' | ||
|
||
class Client: | ||
def __init__(self, sock, addr): | ||
self.sock, self.addr = sock, addr | ||
self.thread = None | ||
|
||
def rec(self): | ||
b = b'' | ||
while not b.endswith(b'\r\n\r\n'): | ||
b += self.sock.recv(1) | ||
lines = b.replace(b'\r\n', b'\n').decode('utf-8').strip('\n').split('\n') | ||
method, loc, ver = lines[0].split(' ') | ||
print(f'request from \'{self.addr}\': "{loc}"') | ||
attrs = {key: value for key, value in (i.split(': ') for i in lines[1:])} | ||
return method, loc, attrs, ver | ||
|
||
def sen(self, loc, ver): | ||
print(f'request {loc}') | ||
closing = False | ||
if loc.startswith('/'): | ||
loc = loc[1:] | ||
if not loc: | ||
loc = 'index.html' | ||
try: | ||
if loc == 'favicon.ico': | ||
raise FileNotFoundError | ||
f = open(loc, 'rb') | ||
c = f.read() | ||
f.close() | ||
print(f'successfully requested {loc}') | ||
if loc.endswith('.js'): | ||
mime = 'application/javascript' | ||
elif loc.endswith('.html'): | ||
mime = 'text/html' | ||
elif loc.endswith('.wasm'): | ||
mime = 'application/wasm' | ||
else: | ||
mime = 'text/plain' | ||
packet = f'HTTP/1.1 200 Success\r\nContent-Length: {len(c)}\r\nContent-Type: {mime}{ADD_HEADERS}\r\n\r\n'.encode('utf-8') + c | ||
pl = len(packet) | ||
if pl != self.sock.send(packet): | ||
self.sock.close() | ||
closing = True | ||
except FileNotFoundError: | ||
print(f'error request {loc}') | ||
self.sock.send(f'HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nContent-Type: text/plain\r\n\r\n'.encode('utf-8')) | ||
finally: | ||
try: | ||
f.close() | ||
except: | ||
... | ||
if closing: | ||
return True | ||
|
||
def run(self): | ||
while True: | ||
method, loc, attrs, ver = self.rec() | ||
if self.sen(loc, ver): break | ||
|
||
def run_threaded(self): | ||
self.thread = Thread(target=self.run) | ||
self.thread.start() | ||
|
||
|
||
def run_server(addr, port): | ||
ws = socket() | ||
ws.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) | ||
ws.bind((addr, port)) | ||
ws.listen() | ||
|
||
clients = [] | ||
|
||
while True: | ||
c, a = ws.accept() | ||
print(f'{a[0]}:{a[1]} connected') | ||
client = Client(c, a) | ||
clients.append(clients) | ||
client.run_threaded() | ||
print('lnuertdenuvtdevn') | ||
|
||
|
||
if __name__ == '__main__': | ||
from sys import argv | ||
daemon = False | ||
addr = 'localhost' | ||
flag = None | ||
for arg in argv[1:]: | ||
if flag is not None: | ||
if flag == 'addr': | ||
addr = arg | ||
elif flag == 'port': | ||
port = int(arg) | ||
flag = None | ||
elif arg in ('-d', '--daemon'): | ||
daemon = True | ||
elif arg in ('-a', '--addr', '--address'): | ||
flag = 'addr' | ||
elif arg in ('-p', '--port'): | ||
flag = 'port' | ||
elif arg in ('--csp',): | ||
enable_csp() | ||
else: | ||
print(f'warn: ignore invalid argument "{arg}"') | ||
daemon = len(argv) > 1 and argv[1] in ('-d', '--daemon') | ||
if ':' in addr: | ||
addr, port = addr.split(':') | ||
port = int(port) | ||
if daemon: | ||
import sys | ||
from os import getcwd | ||
from daemon import DaemonContext | ||
with DaemonContext(working_directory=getcwd(), stderr=sys.stderr): | ||
run_server(addr, port) | ||
else: | ||
run_server(addr, port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>webhogg</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
background: black; | ||
} | ||
#canvas { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
img { | ||
background: violet; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<canvas id='canvas'></canvas> | ||
</body> | ||
<script src='loader.js'></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
killall python3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
wasm-pack build --release --target web | ||
python update.py | ||
sh killpy | ||
python3 deploy.py -d -a 0.0.0.0:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(js_namespace=console, js_name=debug)] | ||
fn __console_debug_colored2(f: &str, c1: &str, c2: &str); | ||
#[wasm_bindgen(js_namespace=console, js_name=info)] | ||
fn __console_info_colored2(f: &str, c1: &str, c2: &str); | ||
#[wasm_bindgen(js_namespace=console, js_name=warn)] | ||
fn __console_warn_colored2(f: &str, c1: &str, c2: &str); | ||
#[wasm_bindgen(js_namespace=console, js_name=error)] | ||
fn __console_error_colored2(f: &str, c1: &str, c2: &str); | ||
} | ||
|
||
fn log(rec: &log::Record) { | ||
let log_fn = match rec.level() { | ||
log::Level::Trace | log::Level::Debug => __console_debug_colored2, | ||
log::Level::Info => __console_info_colored2, | ||
log::Level::Warn => __console_warn_colored2, | ||
log::Level::Error => __console_error_colored2, | ||
}; | ||
log_fn(&format!("{}", rec.args()), | ||
&format!("color: {}", match rec.level() { | ||
log::Level::Trace => "violet", | ||
log::Level::Debug => "blue", | ||
log::Level::Info => "green", | ||
log::Level::Warn => "orange", | ||
log::Level::Error => "red" | ||
}), ""); | ||
} | ||
|
||
pub fn init_logger() { | ||
fern::Dispatch::new().format(|out, message, record|{ | ||
out.finish(format_args!( | ||
"%c{}%c |{}| {} > {}", | ||
record.level(), | ||
String::from(js_sys::Date::new_0().to_iso_string()), | ||
record.target(), | ||
message | ||
) | ||
) | ||
}) | ||
.level(log::LevelFilter::Debug) | ||
.chain(fern::Output::call(log)) | ||
.apply().unwrap(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mod client_logger; | ||
|
||
use wasm_bindgen::prelude::*; | ||
|
||
#[macro_use] | ||
extern crate log; | ||
|
||
#[wasm_bindgen] | ||
pub fn game_logic_entry(worker: web_sys::Worker) { | ||
client_logger::init_logger(); | ||
|
||
info!("hello from game logic wasm"); | ||
worker.post_message(&wasm_bindgen::JsValue::from_str("premsg frm wasm_gLe")) | ||
.unwrap(); | ||
info!("game logic terminated"); | ||
} |