From 51acce51a60d9e32dee4b27ecddc907ab4d2d8c4 Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 31 Jan 2024 11:55:58 -0600 Subject: docs stuff --- docs/net.md | 26 +++++++++++++++---------- readme.md | 8 ++++++++ src/lua.h | 36 ++++++++++++++++++++++++++++++++--- src/net.c | 61 ++++++++++++++++++++++++----------------------------------- tests/net.lua | 7 ++++++- 5 files changed, 88 insertions(+), 50 deletions(-) diff --git a/docs/net.md b/docs/net.md index 192048e..b75ae46 100644 --- a/docs/net.md +++ b/docs/net.md @@ -6,6 +6,12 @@ the function will be ran on initilization, the argument has info on the server and functions to set it up +** +right now everything within a server:GET function is completley local, cannot access the global context at all, +i am planning on copying the global state to each thread (optionally ofc) to make the global state read only, and maybe +and option to sync the global state (push and pull seperately) +** + ```lua llib.net.listen(function(server) ... @@ -61,6 +67,16 @@ end) ... ``` +#### res:deny ** + +denies request as if there was no server + +```lua +... +res:deny() --make the client timeout, lol +... +``` + #### res:send 'takes a string @@ -73,16 +89,6 @@ res:send("

hello world

") ... ``` -#### res:set ** - -'takes an even number of strings, key and value pairs - -set the key to value in the response header, certain keys will affect other values or have other side effects on res:send, listed below - -|key|side effect| -|--|--| -|Code|Changes response note, ie: (200: OK)| - #### res:close ** closes connection diff --git a/readme.md b/readme.md index f7f4255..2153cc9 100644 --- a/readme.md +++ b/readme.md @@ -8,3 +8,11 @@ todo: - (working on seperatley) gui for graphs - fix -O3 breaking some hashes (not sure if i care) + +--- + +credits: + + * [luaproc](https://github.com/askyrme/luaproc) helped with multithreading + + diff --git a/src/lua.h b/src/lua.h index f853408..c31e31f 100644 --- a/src/lua.h +++ b/src/lua.h @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #if LUA_VERSION_NUM == 504 @@ -9,6 +9,10 @@ luaL_register(L, NULL, FN);\ lua_settable(L, -3); + + #define requiref( L, modname, f, glob ) \ + { luaL_requiref( L, modname, f, glob ); lua_pop( L, 1 ); } + #define lua_objlen(L,i) lua_rawlen(L,(i)) #define luaL_register(L, M, F) luaL_newlib(L, F); #else @@ -16,6 +20,32 @@ lua_newtable(L);\ luaL_register(L, NULL, FN);\ lua_setfield(L, 2, N); + + //taken straight from luaproc + #define requiref(L, modname, f, glob){\ + lua_pushcfunction(L, f);\ + lua_pushstring(L, modname); \ + lua_call(L, 1, 1);\ + lua_getfield(L, LUA_GLOBALSINDEX, LUA_LOADLIBNAME);\ + if(lua_type(L, -1) == LUA_TTABLE){\ + lua_getfield(L, -1, "loaded");\ + if(lua_type(L, -1) == LUA_TTABLE){\ + lua_getfield(L, -1, modname);\ + if(lua_type(L, -1) == LUA_TNIL) {\ + lua_pushvalue(L, 1);\ + lua_setfield(L, -3, modname);\ + }\ + lua_pop(L, 1);\ + }\ + lua_pop(L, 1);\ + }\ + lua_pop(L, 1);\ + if(glob){\ + lua_setglobal(L, modname);\ + }else{\ + lua_pop(L, 1);\ + }\ + } #endif diff --git a/src/net.c b/src/net.c index 786ce4f..8bb0cd3 100644 --- a/src/net.c +++ b/src/net.c @@ -35,6 +35,7 @@ struct lchar { }; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER; size_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ char* buffer = malloc(BUFFER_SIZE * sizeof * buffer); @@ -233,15 +234,14 @@ int l_send(lua_State* L){ str* header_vs = str_init(""); lua_pushnil(L); for(;lua_next(L, header) != 0;){ - char* key = (char)luaL_tolstring(L, -2, NULL); + char* key = (char*)luaL_checklstring(L, -2, NULL); if(strcmp(key, "Code") != 0){ str_push(header_vs, key); str_push(header_vs, ": "); - str_push(header_vs, (char)luaL_tolstring(L, -2, NULL)); + str_push(header_vs, (char*)luaL_checklstring(L, -2, NULL)); str_push(header_vs, "\r\n"); - lua_pop(L, 1); } - lua_pop(L, 2); + lua_pop(L, 1); } lua_pushvalue(L, header); @@ -414,7 +414,7 @@ int start_serv(lua_State* L, int port){ printf("failed to accept\n"); abort(); } - printf("%i\n",threads); + //printf("%i\n",threads); //open a state to call shit, should be somewhat thread safe @@ -435,9 +435,6 @@ int start_serv(lua_State* L, int port){ } -#define requiref( L, modname, f, glob ) \ - { luaL_requiref( L, modname, f, glob ); lua_pop( L, 1 ); } - int l_GET(lua_State* L){ lua_pushstring(L, "port"); lua_gettable(L, 1); @@ -454,7 +451,7 @@ int l_GET(lua_State* L){ lua_call(L, 1, 1); size_t len; - char* a = (char*)luaL_tolstring(L, -1, &len); + char* a = (char*)luaL_checklstring(L, -1, &len); struct lchar* awa = malloc(len + 1); awa->c = a; awa->len = len; @@ -462,34 +459,18 @@ int l_GET(lua_State* L){ if(paths == NULL) paths = parray_init(); parray_set(paths, portc, (void*)awa); - /* - int tab_idx = ports[port]; - int ot; - if(tab_idx == 0){ - lua_newtable(L); - ports[port] = tab_idx = luaL_ref(L, LUA_REGISTRYINDEX); - lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); - - } else { - lua_rawgeti(L,LUA_REGISTRYINDEX,tab_idx); - } - int o = lua_gettop(L); - - lua_newtable(L); - lua_pushstring(L, "fn"); - lua_pushvalue(L, 3); - lua_settable(L, -3); - - lua_pushvalue(L, o); - lua_pushvalue(L, 2); - lua_pushvalue(L, -3); - lua_settable(L, -3); - */ - //printf("%i%s",port, lua_tostring(L, 2)); - return 1; } +int l_lock(lua_State* L){ + pthread_mutex_lock(&lua_mutex); + return 0; +} + +int l_unlock(lua_State* L){ + pthread_mutex_unlock(&lua_mutex); + return 0; +} int l_listen(lua_State* L){ if(lua_gettop(L) != 2) { @@ -507,6 +488,14 @@ int l_listen(lua_State* L){ lua_pushcfunction(L, l_GET); lua_settable(L, -3); + lua_pushstring(L, "lock"); + lua_pushcfunction(L, l_lock); + lua_settable(L, -3); + + lua_pushstring(L, "unlock"); + lua_pushcfunction(L, l_unlock); + lua_settable(L, -3); + lua_pushstring(L, "port"); lua_pushvalue(L, 2); lua_settable(L, -3); @@ -536,7 +525,7 @@ int l_spawn(lua_State* L){ lua_call(L, 1, 1); size_t len; - char* a = (char*)luaL_tolstring(L, -1, &len); + char* a = (char*)luaL_checklstring(L, -1, &len); //luaL_loadbuffer(L, a, len, a); //lua_call(L,0,0); @@ -546,7 +535,7 @@ int l_spawn(lua_State* L){ requiref(sL, "package", luaopen_package, 1); lua_pushlstring(sL, a, len); - char* b = (char*)luaL_tolstring(sL, -1, &len); + char* b = (char*)luaL_checklstring(sL, -1, &len); luaL_loadbuffer(sL, b, len, b); //l_pprint(L); diff --git a/tests/net.lua b/tests/net.lua index e9f041c..e7a03a1 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -17,6 +17,11 @@ llib.io.pprint(llib.net.listen( --res:send("hi"); --res.Code = 201 --sleep(1) + require "llib" + llib.io.pprint(_G) + local sec = tonumber(os.clock() + 5); + while (os.clock() < sec) do + end res:send("

hello world

") end)) @@ -28,4 +33,4 @@ llib.io.pprint(llib.net.listen( end, 8080 -)) \ No newline at end of file +)) -- cgit v1.2.3