-
Notifications
You must be signed in to change notification settings - Fork 5
/
OrtcClient.cpp
executable file
·431 lines (337 loc) · 14.9 KB
/
OrtcClient.cpp
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// OrtcClient.cpp
// cocos2d_libs
//
// Created by João Caixinha on 19/10/15.
//
//
#include "OrtcClient.h"
//------- ortc standart delegate ---------
void onMessage(ortc_context *ortc, char *channel, char *message){
std::string *msg = new std::string(message);
std::string *ch = new std::string(channel);
ortcClient->delegate->onMessage(ortcClient, msg, ch);
delete msg;
delete ch;
}
void onConnect(ortc_context* ortc){
ortcClient->delegate->onConnect(ortcClient);
}
void onDisconnect(ortc_context* ortc){
ortcClient->delegate->onDisconnect(ortcClient);
}
void onSubscribe(ortc_context* ortc, char* channel){
std::string *ch = new std::string(channel);
ortcClient->delegate->onSubscribe(ortcClient, ch);
delete ch;
}
void onUnsubscribe(ortc_context* ortc, char* channel){
std::string *ch = new std::string(channel);
ortcClient->delegate->onUnsubscribe(ortcClient, ch);
delete ch;
}
void onException(ortc_context* ortc, char* error){
std::string *err = new std::string(error);
ortcClient->delegate->onException(ortcClient, err);
delete err;
}
void onReconnect(ortc_context* ortc){
ortcClient->delegate->onReconnect(ortcClient);
}
void onReconnecting(ortc_context* ortc){
ortcClient->delegate->onReconnecting(ortcClient);
}
//------- presence delegate ---------
void onEnable_presence(ortc_context *context, char* channel, char* error, char* result){
if (ortcClient->presenceDelegate) {
std::string* ch = new std::string(channel);
std::string* err = new std::string(error);
std::string* res = new std::string(result);
ortcClient->presenceDelegate->onEnable_presence(ortcClient, ch, err, res);
delete ch;
delete err;
delete res;
}
}
void onDisable_presence(ortc_context *context, char* channel, char* error, char* result){
if (ortcClient->presenceDelegate) {
std::string* ch = new std::string(channel);
std::string* err = new std::string(error);
std::string* res = new std::string(result);
ortcClient->presenceDelegate->onDisable_presence(ortcClient, new std::string(channel), new std::string(error), new std::string(result));
delete ch;
delete err;
delete res;
}
}
void onPresence(ortc_context *context, char* channel, char* error, ortc_presenceData* result){
if (ortcClient->presenceDelegate) {
std::string* ch = new std::string(channel);
std::string* err = new std::string(error);
ortcClient->presenceDelegate->onPresence(ortcClient, new std::string(channel), new std::string(error), result);
delete ch;
delete err;
}
}
//------- authentication delegate ---------
void onSave_authentication(ortc_context *context, char* error, char* result){
if (ortcClient->authenticationDelegate) {
std::string* err = new std::string(error);
std::string* res = new std::string(result);
ortcClient->authenticationDelegate->onSave_authentication(ortcClient, new std::string(error), new std::string(result));
delete err;
delete res;
}
}
OrtcClient::OrtcClient(OrtcClientDelegate* pDelegate){
this->delegate = pDelegate;
context = ortc_create_context();
ortc_set_onConnected(context, onConnect);
ortc_set_onDisconnected(context, onDisconnect);
ortc_set_onSubscribed(context, onSubscribe);
ortc_set_onUnsubscribed(context, onUnsubscribe);
ortc_set_onReconnected(context, onReconnect);
ortc_set_onReconnecting(context, onReconnecting);
ortc_set_onException(context, onException);
ortcClient = this;
}
void OrtcClient::connect(std::string* appkey, std::string* token){
const std::string::size_type size = appkey->size();
char *appkeybuffer = new char[size + 1];
memcpy(appkeybuffer, appkey->c_str(), size + 1);
const std::string::size_type tokenbuffersize = token->size();
char *tokenbuffer = new char[tokenbuffersize + 1];
memcpy(tokenbuffer, token->c_str(), tokenbuffersize + 1);
ortc_connect(context, (char*)appkeybuffer, (char*)tokenbuffer);
delete appkey;
delete token;
}
void OrtcClient::disconnect(){
ortc_disconnect(context);
}
void OrtcClient::subscribe(std::string* channel, int subscribeOnReconnected){
ortc_subscribe(context, (char*)channel->c_str(), subscribeOnReconnected, onMessage);
delete channel;
}
void OrtcClient::unsubscribe(std::string* channel){
ortc_unsubscribe(context, (char*)channel->c_str());
delete channel;
}
void OrtcClient::sendMessage(std::string* channel, std::string* message){
ortc_send(context, (char *)channel->c_str(), (char *)message->c_str());
delete channel;
delete message;
}
void OrtcClient::setCluster(std::string* clusterUrl){
const std::string::size_type size = clusterUrl->size();
char *clusterUrlbuffer = new char[size + 1];
memcpy(clusterUrlbuffer, clusterUrl->c_str(), size + 1);
ortc_set_cluster(context, (char*)clusterUrlbuffer);
delete clusterUrl;
}
std::string* OrtcClient::getCluster(){
return new std::string(ortc_get_cluster(context));
}
void OrtcClient::setURL(std::string* url){
const std::string::size_type size = url->size();
char *urlbuffer = new char[size + 1];
memcpy(urlbuffer, url->c_str(), size + 1);
ortc_set_url(context, (char*)urlbuffer);
delete url;
}
std::string* OrtcClient::getURL(){
return new std::string(ortc_get_url(context));
}
void OrtcClient::setConnectionMetadata(std::string* connection_metadata){
const std::string::size_type size = connection_metadata->size();
char *connection_metadatabuffer = new char[size + 1];
memcpy(connection_metadatabuffer, connection_metadata->c_str(), size + 1);
ortc_set_connection_metadata(context, (char*) connection_metadatabuffer);
delete connection_metadata;
}
std::string* OrtcClient::get_connection_metadata(){
return new std::string(ortc_get_connection_metadata(context));
}
void OrtcClient::set_announcementSubChannel(std::string* announcementSubChannel){
const std::string::size_type size = announcementSubChannel->size();
char *announcementSubChannelbuffer = new char[size + 1];
memcpy(announcementSubChannelbuffer, announcementSubChannel->c_str(), size + 1);
ortc_set_announcementSubChannel(context, (char*)announcementSubChannelbuffer);
delete announcementSubChannel;
}
std::string* OrtcClient::get_announcementSubChannel(){
return new std::string(ortc_get_announcementSubChannel(context));
}
std::string* OrtcClient::get_sessionId(){
return new std::string(ortc_get_sessionId(context));
}
int OrtcClient::is_connected(){
return ortc_is_connected(context);
}
int OrtcClient::is_subscribed(std::string* channel){
const std::string::size_type size = channel->size();
char *channelbuffer = new char[size + 1];
memcpy(channelbuffer, channel->c_str(), size + 1);
return ortc_is_subscribed(context, (char*)channelbuffer);
delete channel;
}
void OrtcClient::enable_presence(std::string* privateKey,
std::string* channel, int metadata){
const std::string::size_type size = privateKey->size();
char *privateKeybuffer = new char[size + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size + 1);
const std::string::size_type size2 = channel->size();
char *channelbuffer = new char[size2 + 1];
memcpy(channelbuffer, channel->c_str(), size2 + 1);
ortc_enable_presence(context, (char*)privateKeybuffer, (char*)channelbuffer, metadata, onEnable_presence);
delete privateKey;
delete channel;
}
void OrtcClient::enable_presence_ex(std::string* url, int isCluster,
std::string* appKey, std::string* privateKey, std::string* channel, int metadata,
void (*callback)(ortc_context*, char*, char*, char*)){
const std::string::size_type size = url->size();
char *urlbuffer = new char[size + 1];
memcpy(urlbuffer, url->c_str(), size + 1);
const std::string::size_type size2 = appKey->size();
char *appKeybuffer = new char[size2 + 1];
memcpy(appKeybuffer, appKey->c_str(), size2 + 1);
const std::string::size_type size3 = privateKey->size();
char *privateKeybuffer = new char[size3 + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size3 + 1);
const std::string::size_type size4 = channel->size();
char *channelbuffer = new char[size4 + 1];
memcpy(channelbuffer, channel->c_str(), size4 + 1);
ortc_enable_presence_ex(context, (char*)urlbuffer, isCluster, (char*)appKeybuffer, (char*)privateKeybuffer, (char*)channelbuffer, metadata, onEnable_presence);
delete url;
delete privateKey;
delete appKey;
delete channel;
}
void OrtcClient::disable_presence(std::string* privateKey,
std::string* channel, void (*callback)(ortc_context*, char*, char*, char*)){
const std::string::size_type size3 = privateKey->size();
char *privateKeybuffer = new char[size3 + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size3 + 1);
const std::string::size_type size4 = channel->size();
char *channelbuffer = new char[size4 + 1];
memcpy(channelbuffer, channel->c_str(), size4 + 1);
ortc_disable_presence(context, (char*)privateKeybuffer, (char*)channelbuffer, onDisable_presence);
delete privateKey;
delete channel;
}
void OrtcClient::disable_presence_ex(std::string* url, int isCluster,
std::string* appKey, std::string* privateKey, std::string* channel,
void (*callback)(ortc_context*, char*, char*, char*)){
const std::string::size_type size = url->size();
char *urlbuffer = new char[size + 1];
memcpy(urlbuffer, url->c_str(), size + 1);
const std::string::size_type size2 = appKey->size();
char *appKeybuffer = new char[size2 + 1];
memcpy(appKeybuffer, appKey->c_str(), size2 + 1);
const std::string::size_type size3 = privateKey->size();
char *privateKeybuffer = new char[size3 + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size3 + 1);
const std::string::size_type size4 = channel->size();
char *channelbuffer = new char[size4 + 1];
memcpy(channelbuffer, channel->c_str(), size4 + 1);
ortc_disable_presence_ex(context, (char*)urlbuffer, isCluster, (char*)appKeybuffer, (char*)privateKeybuffer, (char*)channelbuffer, onDisable_presence);
delete url;
delete privateKey;
delete appKey;
delete channel;
}
void OrtcClient::presence(std::string* channel,
void (*callback)(ortc_context*, char*, char*, ortc_presenceData*)){
const std::string::size_type size4 = channel->size();
char *channelbuffer = new char[size4 + 1];
memcpy(channelbuffer, channel->c_str(), size4 + 1);
ortc_presence(context, (char*)channel, onPresence);
delete channel;
}
void OrtcClient::presence_ex(std::string* url,
int isCluster, std::string* appKey, std::string* authToken, std::string* channel,
void (*callback)(ortc_context*, char*, char*, ortc_presenceData*)){
const std::string::size_type size = url->size();
char *urlbuffer = new char[size + 1];
memcpy(urlbuffer, url->c_str(), size + 1);
const std::string::size_type size2 = appKey->size();
char *appKeybuffer = new char[size2 + 1];
memcpy(appKeybuffer, appKey->c_str(), size2 + 1);
const std::string::size_type size3 = authToken->size();
char *authTokenbuffer = new char[size3 + 1];
memcpy(authTokenbuffer, authToken->c_str(), size3 + 1);
const std::string::size_type size4 = channel->size();
char *channelbuffer = new char[size4 + 1];
memcpy(channelbuffer, channel->c_str(), size4 + 1);
ortc_presence_ex(context, (char*)urlbuffer, isCluster, (char*)appKeybuffer, (char*)authTokenbuffer, (char*)channelbuffer, onPresence);
delete url;
delete authToken;
delete appKey;
delete channel;
}
void OrtcClient::save_authentication(
std::string *authToken, int isPrivate, int ttl, std::string *privateKey,
ortc_channelPermissions *permissions, int sizeOfChannelPermissions){
const std::string::size_type size = privateKey->size();
char *privateKeybuffer = new char[size + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size + 1);
const std::string::size_type size3 = authToken->size();
char *authTokenbuffer = new char[size3 + 1];
memcpy(authTokenbuffer, authToken->c_str(), size3 + 1);
ortc_save_authentication(context, (char*)authTokenbuffer, isPrivate, ttl, (char*)privateKeybuffer, permissions, sizeOfChannelPermissions, onSave_authentication);
delete authToken;
delete privateKey;
}
void OrtcClient::save_authentication_ex( std::string* url,
int isCluster, std::string* authToken, int isPrivate, std::string* appKey,
int ttl, std::string *privateKey, ortc_channelPermissions *permissions,
int sizeOfChannelPermissions){
const std::string::size_type size = url->size();
char *urlbuffer = new char[size + 1];
memcpy(urlbuffer, url->c_str(), size + 1);
const std::string::size_type size2 = appKey->size();
char *appKeybuffer = new char[size2 + 1];
memcpy(appKeybuffer, appKey->c_str(), size2 + 1);
const std::string::size_type size3 = privateKey->size();
char *privateKeybuffer = new char[size3 + 1];
memcpy(privateKeybuffer, privateKey->c_str(), size3 + 1);
const std::string::size_type size4 = authToken->size();
char *authTokenbuffer = new char[size4 + 1];
memcpy(authTokenbuffer, authToken->c_str(), size4 + 1);
ortc_save_authentication_ex(context, (char*)urlbuffer, isCluster, (char*)authTokenbuffer, isPrivate, (char*)appKeybuffer, ttl, (char*)privateKeybuffer, permissions, sizeOfChannelPermissions, onSave_authentication);
delete url;
delete authToken;
delete appKey;
delete privateKey;
}
void OrtcClient::disable_ca_verification(){
ortc_disable_ca_verification(context);
}
void OrtcClient::enable_ca_verification(){
ortc_enable_ca_verification(context);
}
int OrtcClient::getHeartbeatActive(){
return ortc_getHeartbeatActive(context);
}
int OrtcClient::getHeartbeatFails(){
return ortc_getHeartbeatFails(context);
}
int OrtcClient::getHeartbeatTime(){
return ortc_getHeartbeatTime(context);
}
void OrtcClient::setHeartbeatActive(int isActive){
ortc_setHeartbeatActive(context, isActive);
}
void OrtcClient::setHeartbeatFails(int newHeartbeatFails){
ortc_setHeartbeatFails(context, newHeartbeatFails);
}
void OrtcClient::setHeartbeatTime(int newHeartbeatTime){
ortc_setHeartbeatTime(context, newHeartbeatTime);
}
std::string* OrtcClient::getVersion(){
return new std::string(ortc_getVersion());
}
std::string* OrtcClient::getVersionVerbose(){
return new std::string(ortc_getVersionVerbose());
}