aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/server.c
blob: 035adbe497fbfe8d106b723f6368ce43f68fec09 (plain) (blame)
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
#include "server.h"
#include "msg.h"
#include "net.h"

#include <stdio.h>
#include <unistd.h>

int server_create(struct server *server, const struct settings *settings)
{
	server->fd = bind_to_port(settings->port);
	if (server->fd < 0)
		return server->fd;

	return 0;
}

void server_destroy(const struct server *server)
{
	close(server->fd);
}

static int msg_handle(const struct msg *msg, void *)
{
	for (int i = 0; i < msg->argc; ++i)
		printf("msg[%d]: %s\n", i, msg->argv[i]);
	return 0;
}

static int server_handle(int fd, void *)
{
	return msg_recv_and_send_result(fd, msg_handle, NULL);
}

static int server_accept(const struct server *server)
{
	return accept_connection(server->fd, server_handle, NULL);
}

int server_main(const struct server *server)
{
	int ret = 0;

	while (1) {
		ret = server_accept(server);
		if (ret < 0)
			return ret;
	}
}