aboutsummaryrefslogtreecommitdiff
path: root/socket_server.h
blob: 222de1190ade25971927e7e60164aa0ea104c913 (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
#ifndef socket_server_h
#define socket_server_h
#include <unistd.h>
#include <pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <poll.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <stdatomic.h>

struct AcceptedSocket{
    int socketFD;
    struct sockaddr_in address;
    int error;
    bool succ;
    pthread_t thread_id;
    atomic_bool shutdown;

    struct AcceptedSocket *next;
};

struct Server{
    int domain;
    int service;
    int protocol;
    u_long interface;
    int port;
    int backlog;
    atomic_bool running;

    pthread_t server_thread_id;
    pthread_t cleanup_thread_id;

    struct sockaddr_in address;

    int socket;

    pthread_rwlock_t list_lock;
    struct AcceptedSocket *accepted_socket;

    void (*on_message)(struct Server *server, struct AcceptedSocket* cur_socket, char message[], ssize_t amountRecv);
    void (*on_disconnect)(struct Server *server, struct AcceptedSocket* cur_socket);
};

struct Server server_constructor(int domain, int service, int protocol, u_long interface, int port, int backlog);

int server_close_socket(struct Server *server);

int init_socket_server(struct Server *server);

void soc_send(struct Server* server, struct AcceptedSocket* cur_socket, char* message);

void soc_broadcast(struct Server *server, struct AcceptedSocket* cur_socket, char* message);
#endif