diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/net.c | 51 | ||||
| -rw-r--r-- | src/net.h | 64 |
2 files changed, 48 insertions, 67 deletions
@@ -29,6 +29,8 @@ static int ports[65535] = { 0 }; static parray_t* paths = NULL; +pthread_mutex_t mutex; + size_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ char* buffer = malloc(BUFFER_SIZE * sizeof * buffer); memset(buffer, 0, BUFFER_SIZE); @@ -238,6 +240,8 @@ int l_send(lua_State* L){ } void* handle_client(void *_arg){ + //pthread_mutex_lock(&mutex); + printf("start\n"); //int client_fd = *((int*)_arg); thread_arg_struct* args = (thread_arg_struct*)_arg; int client_fd = args->fd; @@ -248,7 +252,7 @@ void* handle_client(void *_arg){ int header_eof; //read full request size_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof); - + printf("buffer made\n"); //printf("%i\n",recv(client_fd, dummy, 1, 0 )); //if the buffer, yknow exists @@ -257,6 +261,7 @@ void* handle_client(void *_arg){ int len = 0; //checks for a valid header if(parse_header(buffer, header_eof, &table, &len) != -1){ + printf("parsed\n"); //printf("%s\n",buffer); //str* resp; @@ -279,13 +284,15 @@ void* handle_client(void *_arg){ send(client_fd, resp->c, resp->len, 0); str_free(resp); } else { + printf("starting\n"); lua_rawgeti(L, LUA_REGISTRYINDEX, *(int*)v); + printf("read table\n"); int func = lua_gettop(L); lua_newtable(L); lua_newtable(L); - + printf("after tables\n"); //printf("%s\n",buffer); for(int i = 0; i != len * 2; i+=2){ //printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c); @@ -322,8 +329,10 @@ void* handle_client(void *_arg){ lua_pushvalue(L, func); lua_pushvalue(L, res_idx); lua_pushvalue(L, req_idx); - + printf("calling\n"); + pthread_mutex_lock(&mutex); lua_call(L, 2, 0); + pthread_mutex_unlock(&mutex); //*/ } @@ -338,9 +347,12 @@ void* handle_client(void *_arg){ } free(table); } + printf("close\n"); closesocket(client_fd); free(args); free(buffer); + printf("end\n"); + //pthread_mutex_unlock(&mutex); return NULL; } @@ -417,6 +429,9 @@ int start_serv(lua_State* L, int port){ lua_pushstring(L, "fn"); lua_gettable(L, -2); int aa = lua_gettop(L);*/ + if (pthread_mutex_init(&mutex, NULL) != 0) + printf("mutex init failed\n"); + for(;;){ struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(client_addr); @@ -441,7 +456,7 @@ int start_serv(lua_State* L, int port){ //printf("%i\n",lua_gettop(L)); //lua_pop(oL, 1); //printf("%i %i\n",ports[port], port); - + lua_remove(L, -1); thread_arg_struct* args = malloc(sizeof * args); args->fd = *client_fd; args->port = port; @@ -450,7 +465,8 @@ int start_serv(lua_State* L, int port){ pthread_t thread_id; pthread_create(&thread_id, NULL, handle_client, (void*)args); pthread_detach(thread_id); - lua_remove(L, -1); + //pthread_join(thread_id, NULL); + //handle_client((void*)args); @@ -528,3 +544,28 @@ int l_listen(lua_State* L){ start_serv(L, port); return 0; } + +void* hh(void* args){ + pthread_mutex_lock(&mutex); + lua_State* L = ((thread_arg_struct*)args)->L; + lua_pcall(L, 0, 0, 0); + pthread_mutex_unlock(&mutex); + return NULL; +} + +int l_spawn(lua_State* L){ + if(pthread_mutex_init(&mutex, NULL) != 0)abort(); + + lua_State* sL = luaL_newstate(); + luaL_openlibs(sL); + lua_xmove(L, sL, 1); + thread_arg_struct* args = malloc(sizeof * args); + args->L = sL; + //send request to handle_client() + pthread_t thread_id; + pthread_create(&thread_id, NULL, hh, (void*)args); + pthread_detach(thread_id); + + + return 0; +} @@ -1,74 +1,14 @@ #include "lua.h"
int l_listen(lua_State*);
+int l_spawn(lua_State*);
static char* http_codes[600] = {0};
-#define define_http_codes()\
- http_codes[100] = "Continue ";\
- http_codes[101] = "Switching Protocols ";\
- http_codes[102] = "Processing ";\
- http_codes[103] = "Early Hints ";\
- http_codes[200] = "OK ";\
- http_codes[201] = "Created ";\
- http_codes[202] = "Accepted ";\
- http_codes[203] = "Non-Authoritative Information ";\
- http_codes[204] = "No Content ";\
- http_codes[205] = "Reset Content ";\
- http_codes[206] = "Partial Content ";\
- http_codes[207] = "Multi-Status ";\
- http_codes[208] = "Already Reported ";\
- http_codes[226] = "IM Used ";\
- http_codes[300] = "Multiple Choices ";\
- http_codes[301] = "Moved Permanently ";\
- http_codes[302] = "Found ";\
- http_codes[303] = "See Other ";\
- http_codes[304] = "Not Modified ";\
- http_codes[307] = "Temporary Redirect ";\
- http_codes[308] = "Permanent Redirect ";\
- http_codes[400] = "Bad Request ";\
- http_codes[401] = "Unauthorized ";\
- http_codes[402] = "Payment Required ";\
- http_codes[403] = "Forbidden ";\
- http_codes[404] = "Not Found ";\
- http_codes[405] = "Method Not Allowed ";\
- http_codes[406] = "Not Acceptable ";\
- http_codes[407] = "Proxy Authentication Required ";\
- http_codes[408] = "Request Timeout ";\
- http_codes[409] = "Conflict ";\
- http_codes[410] = "Gone ";\
- http_codes[411] = "Length Required ";\
- http_codes[412] = "Precondition Failed ";\
- http_codes[413] = "Content Too Large ";\
- http_codes[414] = "URI Too Long ";\
- http_codes[415] = "Unsupported Media Type ";\
- http_codes[416] = "Range Not Satisfiable ";\
- http_codes[417] = "Expectation Failed ";\
- http_codes[418] = "I'm a teapot ";\
- http_codes[421] = "Misdirected Request ";\
- http_codes[422] = "Unprocessable Content ";\
- http_codes[423] = "Locked ";\
- http_codes[424] = "Failed Dependency ";\
- http_codes[425] = "Too Early ";\
- http_codes[426] = "Upgrade Required ";\
- http_codes[428] = "Precondition Required ";\
- http_codes[429] = "Too Many Requests ";\
- http_codes[431] = "Request Header Fields Too Large ";\
- http_codes[451] = "Unavailable For Legal Reasons ";\
- http_codes[500] = "Internal Server Error ";\
- http_codes[501] = "Not Implemented ";\
- http_codes[502] = "Bad Gateway ";\
- http_codes[503] = "Service Unavailable ";\
- http_codes[504] = "Gateway Timeout ";\
- http_codes[505] = "HTTP Version Not Supported ";\
- http_codes[506] = "Variant Also Negotiates ";\
- http_codes[507] = "Insufficient Storage ";\
- http_codes[508] = "Loop Detected ";\
- http_codes[510] = "Not Extended ";\
- http_codes[511] = "Network Authentication Required ";
static const luaL_Reg net_function_list [] = {
{"listen",l_listen},
+ {"spawn",l_spawn},
{NULL,NULL}
};
|
