summaryrefslogtreecommitdiffhomepage
path: root/main.c
blob: af36c5d2f83ed5b33a9b91a8b50b5c0818ccf17b (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
47
48
49
50
51
52
53
54
55
56
57
58
#include <poll.h>
#include <string.h>

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>

#include "plop.h"

/// Standard entry point for the program.
/// \param argc Argument count
/// \param argv Argument array
/// \return Error code
int main(int argc, char ** argv)
{
	lua_State * L = luaL_newstate();
	luaL_openlibs(L);

	if (3 != argc)
	{
		return 1; // TODO: Document error codes/print usage information
	}

	if (LUA_OK != luaL_dofile(L, argv[2]))
	{
		return 2; // TODO: Document error codes/print usage information
	}

	static const int max_clients = 200;

	int fdc = 0;
	struct request * requests[max_clients];
	struct pollfd fdv[max_clients];

	memset(requests, 0, sizeof(requests));
	memset(fdv, 0, sizeof(fdv));

	fdv[0].fd = make_server(NULL, argv[1]);
	fdv[0].events = POLLIN;
	fdc++;

	if (-1 == fdv[0].fd)
	{
		return 1; // TODO: Document error codes/print usage information
	}

	while (0 < fdc && -1 != poll(fdv, fdc, -1))
	{
		int shift_by = 0;

		for (int i = 1; i < fdc; ++i)
		{
			shift_by = handle_client(L, &fdv[i], &requests[i], shift_by);
		}

		fdc = handle_server(fdv, fdc - shift_by, max_clients);
	}
}