Implemented server
This commit is contained in:
45
main.c
45
main.c
@@ -8,12 +8,14 @@
|
||||
#include <sys/signalfd.h>
|
||||
|
||||
#include "event_loop.h"
|
||||
#include "server.h"
|
||||
|
||||
/*
|
||||
* Data
|
||||
*/
|
||||
static bool _to_work = true;
|
||||
static int _sig_fd = -1;
|
||||
static int _server_fd = -1;
|
||||
|
||||
/*
|
||||
* Private API
|
||||
@@ -58,10 +60,40 @@ static bool signals_init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Finalize signals.
|
||||
static void signals_deinit() {
|
||||
close(_sig_fd);
|
||||
}
|
||||
|
||||
// Initialize the server.
|
||||
// Returns:
|
||||
// - true on success
|
||||
static bool server_init() {
|
||||
_server_fd = server_open("0.0.0.0", 20070);
|
||||
if (_server_fd == -1) {
|
||||
return false;
|
||||
}
|
||||
struct epoll_event ev;
|
||||
ev.data.fd = _server_fd;
|
||||
ev.events = EPOLLIN;
|
||||
bool loop_res = loop_ctl(
|
||||
EPOLL_CTL_ADD,
|
||||
_server_fd,
|
||||
&ev
|
||||
);
|
||||
if (!loop_res) {
|
||||
server_close();
|
||||
printf("[!] loop_ctl failed\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Finalize the server.
|
||||
static void server_deinit() {
|
||||
server_close();
|
||||
}
|
||||
|
||||
/*
|
||||
* Callbacks and main
|
||||
*/
|
||||
@@ -71,6 +103,13 @@ static void on_fd_event(int fd, uint32_t events) {
|
||||
if (fd == _sig_fd) {
|
||||
_to_work = false;
|
||||
}
|
||||
// Server event
|
||||
else if (fd == _server_fd) {
|
||||
if (!server_event(_server_fd)) {
|
||||
printf("[!] server_event failed\n");
|
||||
_to_work = false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("[!] Event has happened on an unknown file descriptor\n");
|
||||
}
|
||||
@@ -84,6 +123,11 @@ int main(int argc, char **argv) {
|
||||
loop_deinit();
|
||||
return 1;
|
||||
}
|
||||
if (!server_init()) {
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (_to_work) {
|
||||
if (!loop_wait()) {
|
||||
@@ -93,6 +137,7 @@ int main(int argc, char **argv) {
|
||||
printf("--- loop ---\n");
|
||||
}
|
||||
|
||||
server_close();
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user