summaryrefslogtreecommitdiffhomepage
path: root/connection.c
blob: 8fad3c5424232de8c7f8c1a9a5a10037a6dae51c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "connection.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <lauxlib.h>
#include <lua.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)
{
	(void) L; // TODO: Review if lua_State is still needed for connections and server handler.

	struct connection * c = malloc(sizeof(struct connection));

	if (NULL == c)
	{
		return NULL;
	}

	memset(c, 0, sizeof(struct connection));

	c->fd = client;
	c->ref = LUA_NOREF;
	c->L = NULL;

	return c;
}

/// 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)
{
	if (LUA_NOREF != c->ref)
	{
		luaL_unref(L, LUA_REGISTRYINDEX, c->ref);
	}

	close(c->fd); // TODO: Check for errors in close()?
	free(c);
}