-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
151 lines (129 loc) · 4.28 KB
/
server.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* server.h - Server-related subroutines
* Copyright (c) 2020 Nicholas West, Hantao Cui, CURENT, et. al.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided "as is" and the author disclaims all
* warranties with regard to this software including all implied warranties
* of merchantability and fitness. In no event shall the author be liable
* for any special, direct, indirect, or consequential damages or any
* damages whatsoever resulting from loss of use, data or profits, whether
* in an action of contract, negligence or other tortious action, arising
* out of or in connection with the use or performance of this software.
*/
/**
* @file server.h
* @brief Server-related subroutines
* @author Nicholas West
* @date 2020
*
* Contains data structures and subroutines intended to manage the
* server's state. The code creates a socket that listens for
* connections based on configuration variables set in the server
* struct, and creates a @link dime_client_t @endlink struct for each
* incoming connection. It then uses an event loop with @c poll to handle
* incoming reads and outgoing writes.
*
* @todo This could be global data, assuming we only run one server per process
*/
#include <stdint.h>
#ifdef DIME_USE_LIBEV
# include <ev.h>
#endif
#include <openssl/ssl.h>
#include "table.h"
#ifndef __DIME_server_H
#define __DIME_server_H
#ifdef __cplusplus
extern "C" {
#endif
enum dime_serialization {
DIME_NO_SERIALIZATION,
DIME_MATLAB,
DIME_PICKLE,
DIME_DIMEB,
DIME_JSON
};
typedef struct {
int fd;
int protocol;
#ifdef DIME_USE_LIBEV
ev_io watcher;
#endif
void *srv;
} dime_server_fd_t;
enum dime_protocol {
DIME_UNIX,
DIME_TCP,
DIME_WS
};
/**
* @brief Client's state
*
* Contains all data relevant to a single server's state. The configuration variables should be set before calling @link dime_server_init @endlink.
*/
typedef struct {
char err[81]; /** Error string */
unsigned int daemon : 1; /** Daemon flag */
unsigned int tls : 1; /** TLS flag */
unsigned int zlib : 1; /** zlib flag */
unsigned int ws : 1; /** WebSocket flag */
char : 0;
dime_server_fd_t *fds;
size_t fds_len;
size_t fds_cap;
char **pathnames;
size_t pathnames_len;
size_t pathnames_cap;
const char *certname; /** Certificate pathname (if using TLS) */
const char *privkeyname; /** Private key pathname (if using TLS) */
const char *socketname; /** Socket pathname (if using Unix socket) */
uint16_t port; /** Port (if using TCP socket) */
unsigned int verbosity; /** Verbosity level */
unsigned int threads; /** Number of worker threads */
int protocol; /** Protocol to use */
int serialization; /** Serialization method */
int fd; /** File descriptor */
dime_table_t fd2clnt; /** File descriptor-to-client translation table */
dime_table_t name2clnt; /** Name-to-client translation table */
SSL_CTX *tlsctx; /** OpenSSL context */
} dime_server_t;
/**
* @brief Initialize a new server
*
* Configuration variables in @em srv, such as @c pathname, @c port, etc. should be set before calling this function.
*
* @param srv Pointer to a @link dime_server_t @endlink struct
*
* @return A nonnegative value on success, or a negative value on
* failure
*
* @see dime_server_destroy
*/
int dime_server_init(dime_server_t *srv);
/**
* @brief Free resources used by a server
*
* @param srv Pointer to a @link dime_server_t @endlink struct
*
* @see dime_server_init
*/
void dime_server_destroy(dime_server_t *srv);
int dime_server_add(dime_server_t *srv, int protocol, ...);
/**
* @brief Run the event loop for the server
*
* This function does not return until either the process is sent a @c SIGINT or @c SIGTERM signal, or it encounters an irrecoverable error.
*
* @param srv Pointer to a @link dime_server_t @endlink struct
*
* @return A nonnegative value upon receiving a @c SIGINT or @c SIGTERM signal, or a negative value on failure
*/
int dime_server_loop(dime_server_t *srv);
#ifdef __cplusplus
}
#endif
#endif