-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaoo_server.hpp
More file actions
281 lines (233 loc) · 9.23 KB
/
aoo_server.hpp
File metadata and controls
281 lines (233 loc) · 9.23 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Copyright (c) 2021 Christof Ressi
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/** \file
* \brief C++ interface for AOO server
*
* 1. UDP: query -> get public IP + TCP server ID
* 2. TCP: connect to TCP server -> get user ID
* 3. TCP: join group -> get peer ID (start from 0) + relay server address
*/
#pragma once
#include "aoo_config.h"
#include "aoo_controls.h"
#include "aoo_defines.h"
#include "aoo_events.h"
#include "aoo_requests.h"
#include "aoo_types.h"
#if AOO_HAVE_CXX11
# include <memory>
#endif
/** \cond DO_NOT_DOCUMENT */
typedef struct AooServer AooServer;
/** \endcond */
/** \brief create a new AOO server instance
*
* \return new AooServer instance on success; `NULL` on failure
*/
AOO_API AooServer * AOO_CALL AooServer_new(void);
/** \brief destroy AOO server instance */
AOO_API void AOO_CALL AooServer_free(AooServer *server);
/*-----------------------------------------------------------*/
/** \brief AOO server interface */
struct AooServer {
public:
#if AOO_HAVE_CXX11
/** \brief custom deleter for AooServer */
class Deleter {
public:
void operator()(AooServer *obj){
AooServer_free(obj);
}
};
/** \brief smart pointer for AOO server instance */
using Ptr = std::unique_ptr<AooServer, Deleter>;
/** \brief create a new managed AOO server instance
*
* \return valid Ptr on success; empty Ptr failure
*/
static Ptr create() {
return Ptr(AooServer_new());
}
#endif
/*---------------------- methods ---------------------------*/
/** \brief setup the server object (before calling run())
*
* \param settings settings objects; it might be modified to reflect the actual values.
*/
virtual AooError AOO_CALL setup(AooServerSettings& settings) = 0;
/** \brief run the server
*
* \param timeout the timeout in seconds
* - A number >= 0: the method only blocks up to the given duration.
* It returns #kAooOk if it did something, #kAooErrorWouldBlock
* if timed out, or any other error code if an error occured.
* - #kAooInfinite: blocks until quit() is called or an error occured.
*/
virtual AooError AOO_CALL run(AooSeconds timeout) = 0;
/** \brief receive and handle UDP packets (from internal UDP socket)
*
* \param timeout the timeout in seconds
* - A number >= 0: the method only blocks up to the given duration.
* It returns #kAooOk if it did something, #kAooErrorWouldBlock
* if timed out, or any other error code if an error occured.
* - #kAooInfinite: blocks until quit() is called or an error occured.
*/
virtual AooError AOO_CALL receive(AooSeconds timeout) = 0;
/** \brief handle UDP packet from external UDP socket */
virtual AooError AOO_CALL handlePacket(
const AooByte *data, AooInt32 size,
const void *address, AooAddrSize addrlen) = 0;
/** \brief stop the AOO client from another thread */
virtual AooError AOO_CALL stop() = 0;
/* event handling */
/** \brief set event handler function and event handling mode
*
* \attention Not threadsafe - only call in the beginning!
*/
virtual AooError AOO_CALL setEventHandler(
AooEventHandler fn, void *user, AooEventMode mode) = 0;
/** \brief check for pending events
*
* \note Threadsafe and RT-safe
*/
virtual AooBool AOO_CALL eventsAvailable() = 0;
/** \brief poll events
*
* \note Threadsafe and RT-safe, but not reentrant.
*
* This function will call the registered event handler one or more times.
* \attention The event handler must have been registered with #kAooEventModePoll.
*/
virtual AooError AOO_CALL pollEvents() = 0;
/* request handling */
/** \brief set request handler (to intercept client requests) */
virtual AooError AOO_CALL setRequestHandler(
AooRequestHandler cb, void *user, AooFlag flags) = 0;
/** \brief handle request
*
* If `result` is `kAooErrorNone`, the request has been handled successfully
* and `response` points to the corresponding response structure.
*
* Otherwise the request has failed or been denied; in this case `response`
* is either `NULL` or points to an `AooResponseError` structure for more detailed
* information about the error. For example, in the case of `kAooErrorSystem`,
* the response may contain an OS-specific error code and error message.
*
* \attention The response must be properly initialized with `AOO_RESPONSE_INIT`.
*/
virtual AooError AOO_CALL handleRequest(
AooId client, AooId token, const AooRequest *request,
AooError result, AooResponse *response) = 0;
/* push notifications */
/** \brief send custom push notification to client;
*
* if `client` is #kAooIdAll, all clients are notified.
*/
virtual AooError AOO_CALL notifyClient(
AooId client, const AooData &data) = 0;
/** \brief send custom push notification to group member(s);
*
* if `user` is #kAooIdAll, all group members are notified.
*/
virtual AooError AOO_CALL notifyGroup(
AooId group, AooId user, const AooData &data) = 0;
/* group management */
/** \brief find a group by name */
virtual AooError AOO_CALL findGroup(
const AooChar *name, AooId *id) = 0;
/** \brief add a group
* By default, the metadata is passed to clients
* via AooResponseGroupJoin::groupMetadata. */
virtual AooError AOO_CALL addGroup(
const AooChar *name, const AooChar *password,
const AooData *metadata, const AooIpEndpoint *relayAddress,
AooFlag flags, AooId *groupId) = 0;
/** \brief remove a group */
virtual AooError AOO_CALL removeGroup(
AooId group) = 0;
/** \brief find a user in a group by name */
virtual AooError AOO_CALL findUserInGroup(
AooId group, const AooChar *userName, AooId *userId) = 0;
/** \brief add user to group
* By default, the metadata is passed to peers via AooEventPeer::metadata. */
virtual AooError AOO_CALL addUserToGroup(
AooId group, const AooChar *userName, const AooChar *userPwd,
const AooData *metadata, AooFlag flags, AooId *userId) = 0;
/** \brief remove user from group */
virtual AooError AOO_CALL removeUserFromGroup(
AooId group, AooId user) = 0;
/** \brief group control interface
*
* Not to be used directly.
*/
virtual AooError AOO_CALL groupControl(
AooId group, AooCtl ctl, AooIntPtr index,
void *data, AooSize size) = 0;
/** \brief control interface
*
* Not to be used directly.
*/
virtual AooError AOO_CALL control(
AooCtl ctl, AooIntPtr index, void *data, AooSize size) = 0;
/*--------------------------------------------*/
/* type-safe control functions */
/*--------------------------------------------*/
/** \brief set the server password */
AooError setPassword(const AooChar *pwd)
{
return control(kAooCtlSetPassword, (AooIntPtr)pwd, NULL, 0);
}
/** \brief set external (global) relay host
*
* If `hostName` is `NULL`, it means that the relay has
* the same IP address(es) as the AOO server.
* If `ep` is `NULL`, the relay host is unset. */
AooError setRelayHost(const AooIpEndpoint *ep)
{
return control(kAooCtlSetRelayHost, (AooIntPtr)ep, NULL, 0);
}
/** \brief enabled/disable internal relay */
AooError setUseInternalRelay(AooBool b)
{
return control(kAooCtlSetUseInternalRelay, 0, AOO_ARG(b));
}
/** \brief check if internal relay is enabled */
AooError getUseInternalRelay(AooBool& b)
{
return control(kAooCtlGetUseInternalRelay, 0, AOO_ARG(b));
}
/** \brief enable/disable automatic group creation */
AooError setGroupAutoCreate(AooBool b)
{
return control(kAooCtlSetGroupAutoCreate, 0, AOO_ARG(b));
}
/** \brief check if automatic group creation is enabled */
AooError getGroupAutoCreate(AooBool& b)
{
return control(kAooCtlGetGroupAutoCreate, 0, AOO_ARG(b));
}
/** \brief Set client ping settings */
AooError setPingSettings(const AooPingSettings& settings) {
return control(kAooCtlSetPingSettings, 0, AOO_ARG(settings));
}
/** \brief Get client ping settings */
AooError getPingSettings(AooPingSettings& settings) {
return control(kAooCtlGetPingSettings, 0, AOO_ARG(settings));
}
/*--------------------------------------------------*/
/* type-safe group control functions */
/*--------------------------------------------------*/
/** \brief update group metadata */
AooError updateGroup(AooId group, const AooData *metadata)
{
return groupControl(group, kAooCtlUpdateGroup, 0, AOO_ARG(metadata));
}
/** \brief update user metadata */
AooError updateUser(AooId group, AooId user, const AooData *metadata)
{
return groupControl(group, kAooCtlUpdateUser, user, AOO_ARG(metadata));
}
protected:
~AooServer(){} // non-virtual!
};