diff options
| author | ame <[email protected]> | 2024-01-31 11:55:58 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2024-01-31 11:55:58 -0600 |
| commit | 51acce51a60d9e32dee4b27ecddc907ab4d2d8c4 (patch) | |
| tree | 4e19be9a8655a9f1a628594e272c16849a9d836e | |
| parent | c7d68dc85604f2bfc73a6e4d10a1de8e85b80be2 (diff) | |
docs stuff
| -rw-r--r-- | docs/net.md | 26 | ||||
| -rw-r--r-- | readme.md | 8 | ||||
| -rw-r--r-- | src/lua.h | 36 | ||||
| -rw-r--r-- | src/net.c | 61 | ||||
| -rw-r--r-- | 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("<h1>hello world</h1>") ...
```
-#### 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
@@ -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 + + @@ -1,6 +1,6 @@ -#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
+#include <lua5.1/lua.h>
+#include <lua5.1/lualib.h>
+#include <lua5.1/lauxlib.h>
#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
@@ -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("<h2>hello world</h2>")
end))
@@ -28,4 +33,4 @@ llib.io.pprint(llib.net.listen( end,
8080
-))
\ No newline at end of file +))
|
