From c47d4b2314b06f28cae47d4dd14ef4c9c5c1af60 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 24 Aug 2020 19:42:00 +0200 Subject: Added connection stub --- connection.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 connection.c (limited to 'connection.c') diff --git a/connection.c b/connection.c new file mode 100644 index 0000000..c7dd151 --- /dev/null +++ b/connection.c @@ -0,0 +1,52 @@ +#include "connection.h" + +#include +#include + +#include +#include + +#include "request.h" + +/// Creates new connection. +/// \param L Server's Lua state +/// \param client File descriptor of client's socket +/// \return Pointer to connection or NULL if an error occured +struct connection * connection_new(lua_State * L, const int client) +{ + struct connection * c = malloc(sizeof(struct connection)); + + if (NULL == c) + { + return NULL; + } + + memset(c, 0, sizeof(struct connection)); + + c->fd = client; + c->L = lua_newthread(L); + c->lua_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + return c; +} + +/// Clears the state of connection readying it for new request. +/// \param c Connection to clear +void connection_clear(struct connection * c) +{ + if (NULL != c->request) + { + free_request(c->request); + } +} + +/// Frees all resources associated with the connection. +/// \param L Server's Lua state +/// \param c Connection to free +void connection_free(lua_State * L, struct connection * c) +{ + connection_clear(c); + luaL_unref(L, LUA_REGISTRYINDEX, c->lua_ref); + close(c->fd); // TODO: Check for errors in close()? + free(c); +} -- cgit v1.1