aboutsummaryrefslogtreecommitdiff
path: root/server.c
blob: bab1edc9d23a8063886d2bcf306a67683fb4444e (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <strings.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <errno.h>

#include "eventloop.h"
#include "tsocket.h"
#include "util.h"

#define READ_BUFFER_SIZE 1024

void handleReadEvent(struct event *ev)
{
    char buf[READ_BUFFER_SIZE];
    ssize_t n_read_bytes;

    struct tsocket *sock = (struct tsocket *)ev->data;

    for (;;) {
        n_read_bytes = read(sock->fd, buf, sizeof(buf));
        if (n_read_bytes > 0) {
            printf("message from conn %d: %s\n", sock->fd, buf);
            write(sock->fd, buf, sizeof(buf));
        } else if (n_read_bytes == 0) {
            printf("conn %d disconnected\n", sock->fd);
            eventLoopDel(ev->el, ev);
            break;
        } else if (n_read_bytes == -1) {
            if (errno == EAGAIN || errno == EWOULDBLOCK)
                break;
        }
    }
}

void serverDeleteReadEvent(struct event *ev)
{
    tsocketDelete(ev->data);
    free(ev);
}

struct event *serverNewReadEvent(struct tsocket *sock)
{
    struct event *ev = malloc(sizeof(*ev));
    ev->data = sock;
    ev->handle = handleReadEvent;
    ev->fd = sock->fd;
    ev->delete = serverDeleteReadEvent;

    return ev;
}

void handleNewClientConnection(struct event *ev)
{
    struct tsocket *conn_sock = tsocketAccept(ev->data);
    if (conn_sock == NULL) {
        perror("socket accept");
        return;
    }

    if (setblocking(conn_sock->fd, false) == -1) {
        perror("setblocking");
        tsocketDelete(conn_sock);
        return;
    }

    struct event *read_ev = serverNewReadEvent(conn_sock);
    if (eventLoopAdd(ev->el, read_ev, EPOLLIN | EPOLLET) == -1) {
        perror("eventloop add fd: conn_sock");
        return;
    }

    printf("New client fd %d, ip: %s, port: %d\n", 
            conn_sock->fd, conn_sock->addr, conn_sock->port);
}

struct event *serverNewAcceptEvent(const char *addr, int port)
{
    struct tsocket *sock = tsocketNew();
    if (sock == NULL
            || tsocketBind(sock, addr, port) == -1
            || tsocketListen(sock) == -1)
        return NULL;

    struct event *ev = malloc(sizeof(*ev));
    ev->handle = handleNewClientConnection;
    ev->data = sock;
    ev->fd = sock->fd;

    return ev;
}

int main()
{
    struct eventLoop *el = eventLoopNew();
    if (el == NULL)
        panic("eventloop creation");

    struct event *acceptEvent = serverNewAcceptEvent("127.0.0.1", 8888);
    if (acceptEvent == NULL)
        panic("server client connection event");

    if (eventLoopAdd(el, acceptEvent, EPOLLIN) == -1)
        panic("eventloop add fd");

    eventLoopLoop(el);
}