-
Notifications
You must be signed in to change notification settings - Fork 40
/
PitayaBinding.cs
703 lines (590 loc) · 29.7 KB
/
PitayaBinding.cs
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
using System;
using System.IO;
using System.Text;
using UnityEngine;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AOT;
using Pitaya;
using Pitaya.SimpleJson;
using Protos;
//typedef void (*request_callback)(pc_client_t* client, unsigned int cbid, const char* resp);
using NativeRequestCallback = System.Action<System.IntPtr, uint, System.IntPtr>;
// typedef void (*request_error_callback)(pc_client_t* client, unsigned int cbid, pc_error_t* error);
using NativeErrorCallback = System.Action<System.IntPtr, uint, System.IntPtr>;
using NativeNotifyCallback = System.Action<System.IntPtr, System.IntPtr>;
internal delegate void NativeEventCallback(IntPtr client, int ev, IntPtr exData, IntPtr arg1Ptr, IntPtr arg2Ptr);
internal delegate void NativePushCallback(IntPtr client, IntPtr route, IntPtr payloadBuffer);
internal delegate void NativeAssertCallback(IntPtr e, IntPtr file, int line);
public delegate void NativeLogFunction(PitayaLogLevel level, string msg);
#pragma warning disable 649
internal struct PitayaBindingError
{
public int Code;
public PitayaBuffer Buffer;
public int Uvcode;
}
internal struct PitayaBuffer
{
public IntPtr Data;
public long Len;
}
#pragma warning restore 649
namespace Pitaya
{
public interface IPitayaListener
{
void OnRequestResponse(uint rid, byte[] body);
void OnRequestError(uint rid, PitayaError error);
void OnNetworkEvent(PitayaNetWorkState state, NetworkError error);
void OnUserDefinedPush(string route, byte[] serializedBody);
}
public interface IPitayaBinding
{
IPitayaQueueDispatcher QueueDispatcher { get; set; }
IntPtr CreateClient(bool enableTls, bool enablePolling, bool enableReconnect, int connTimeout, IPitayaListener listener);
void Connect(IntPtr client, string host, int port, string handshakeOpts);
void Disconnect(IntPtr client);
void SetCertificateName(string name);
void SetCertificatePath(string path);
void Request(IntPtr client, string route, byte[] msg, uint reqtId, int timeout);
void Notify(IntPtr client, string route, byte[] msg, int timeout);
int Quality(IntPtr client);
PitayaClientState State(IntPtr client);
void Dispose(IntPtr client);
ProtobufSerializer.SerializationFormat ClientSerializer(IntPtr client);
void AddPinnedPublicKeyFromCertificateString(string caString);
void AddPinnedPublicKeyFromCertificateFile(string name);
void SkipKeyPinCheck(bool shouldSkip);
void ClearPinnedPublicKeys();
}
public class PitayaBinding : IPitayaBinding
{
public IPitayaQueueDispatcher QueueDispatcher
{
get => StaticPitayaBinding.QueueDispatcher;
set => StaticPitayaBinding.QueueDispatcher = value;
}
public IntPtr CreateClient(bool enableTls, bool enablePolling, bool enableReconnect, int connTimeout, IPitayaListener listener) { return StaticPitayaBinding.CreateClient(enableTls, enablePolling, enableReconnect, connTimeout, listener); }
public void SetLogLevel(PitayaLogLevel logLevel) { StaticPitayaBinding.SetLogLevel(logLevel); }
public void Connect(IntPtr client, string host, int port, string handshakeOpts) { StaticPitayaBinding.Connect(client, host, port, handshakeOpts); }
public void Disconnect(IntPtr client) { StaticPitayaBinding.Disconnect(client); }
public void SetCertificateName(string name) { StaticPitayaBinding.SetCertificateName(name); }
public void SetCertificatePath(string path) { StaticPitayaBinding.SetCertificatePath(path); }
public void Request(IntPtr client, string route, byte[] msg, uint reqtId, int timeout) { StaticPitayaBinding.Request(client, route, msg, reqtId, timeout); }
public void Notify(IntPtr client, string route, byte[] msg, int timeout) { StaticPitayaBinding.Notify(client, route, msg, timeout); }
public int Quality(IntPtr client) { return StaticPitayaBinding.Quality(client); }
public PitayaClientState State(IntPtr client) { return StaticPitayaBinding.State(client); }
public void Dispose(IntPtr client) { StaticPitayaBinding.Dispose(client); }
public ProtobufSerializer.SerializationFormat ClientSerializer(IntPtr client) { return StaticPitayaBinding.ClientSerializer(client); }
public void AddPinnedPublicKeyFromCertificateString(string caString) { StaticPitayaBinding.AddPinnedPublicKeyFromCertificateString(caString); }
public void AddPinnedPublicKeyFromCertificateFile(string name) { StaticPitayaBinding.AddPinnedPublicKeyFromCertificateFile(name); }
public void SkipKeyPinCheck(bool shouldSkip) { StaticPitayaBinding.SkipKeyPinCheck(shouldSkip); }
public void ClearPinnedPublicKeys() { StaticPitayaBinding.ClearPinnedPublicKeys(); }
}
public static class StaticPitayaBinding
{
private static readonly NativeNotifyCallback NativeNotifyCallback;
private static readonly NativeRequestCallback NativeRequestCallback;
private static readonly NativeEventCallback NativeEventCallback;
private static readonly NativePushCallback NativePushCallback;
private static readonly NativeErrorCallback NativeErrorCallback;
private static readonly Dictionary<IntPtr, WeakReference> Listeners = new Dictionary<IntPtr, WeakReference>();
private static readonly Dictionary<IntPtr, int> EventHandlersIds = new Dictionary<IntPtr, int>();
private static PitayaLogLevel _currentLogLevel = PitayaLogLevel.Disable;
private static bool IsNativeLibInitialized;
public static IPitayaQueueDispatcher QueueDispatcher { get; set; } = new NullPitayaQueueDispatcher();
private static void DLog(object data)
{
if (_currentLogLevel != PitayaLogLevel.Disable)
{
Debug.Log(data);
}
}
static StaticPitayaBinding()
{
NativeRequestCallback = OnRequest;
NativeEventCallback = OnEvent;
NativePushCallback = OnPush;
NativeNotifyCallback = OnNotify;
NativeErrorCallback = OnError;
SetLogFunction(LogFunction);
#if UNITY_ANDROID
InitializeNativeLib();
#endif
}
public static void SetLogFunction(NativeLogFunction fn)
{
int rc = NativeInitLogFunction(fn);
if (rc != 0)
{
throw new Exception("Cannot initialize log function");
}
}
private static string BuildNumber()
{
switch (Application.platform)
{
case RuntimePlatform.IPhonePlayer:
return _PitayaGetCFBundleVersion();
case RuntimePlatform.Android:
return _AndroidBuildNumber();
default:
return "1";
}
}
private static string _AndroidBuildNumber()
{
var contextCls = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
var context = contextCls.GetStatic<AndroidJavaObject>("currentActivity");
var packageMngr = context.Call<AndroidJavaObject>("getPackageManager");
var packageName = context.Call<string>("getPackageName");
var packageInfo = packageMngr.Call<AndroidJavaObject>("getPackageInfo", packageName, 0);
return (packageInfo.Get<int>("versionCode")).ToString();
}
private static string Platform()
{
switch (Application.platform)
{
case RuntimePlatform.Android:
return "android";
case RuntimePlatform.LinuxEditor:
case RuntimePlatform.LinuxPlayer:
return "linux";
case RuntimePlatform.WindowsEditor:
case RuntimePlatform.WindowsPlayer:
return "windows";
case RuntimePlatform.IPhonePlayer:
return "ios";
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
return "mac";
default:
return Application.platform.ToString();
}
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void OnEditorInitialize()
{
if (Application.isEditor)
{
#if !UNITY_ANDROID
InitializeNativeLib();
#endif
NativeLibUpdateClientInfo(Platform(), BuildNumber(), Application.version);
}
}
private static void InitializeNativeLib()
{
NativeLibInit((int)_currentLogLevel, null, null, OnAssert, Platform(), BuildNumber(), Application.version);
IsNativeLibInitialized = true;
}
public static IntPtr CreateClient(bool enableTls, bool enablePolling, bool enableReconnect, int connTimeout, IPitayaListener listener)
{
if (!IsNativeLibInitialized) InitializeNativeLib();
var client = NativeCreate(enableTls, enablePolling, enableReconnect, connTimeout);
if (client == IntPtr.Zero)
{
throw new Exception("Fail to create a client");
}
var handlerId = NativeAddEventHandler(client, NativeEventCallback, IntPtr.Zero, IntPtr.Zero);
NativeAddPushHandler(client, NativePushCallback);
Listeners[client] = new WeakReference(listener);
EventHandlersIds[client] = handlerId;
return client;
}
public static void SetLogLevel(PitayaLogLevel logLevel)
{
NativeLibSetLogLevel((int)logLevel);
_currentLogLevel = logLevel;
}
public static void Connect(IntPtr client, string host, int port, string handshakeOpts)
{
CheckClient(client);
var opts = string.IsNullOrEmpty(handshakeOpts) ? null : handshakeOpts;
switch (NativeConnect(client, host, port, opts))
{
case PitayaConstants.PcRcOk:
return;
case PitayaConstants.PcRcInvalidJson:
throw new Exception("Cannot connect: invalid handshake options json data");
default:
throw new Exception("Error when Connect was called");
}
}
public static void Disconnect(IntPtr client)
{
CheckClient(client);
NativeDisconnect(client);
}
public static void SetCertificateName(string name)
{
var certPath = FindCertPathFromName(name);
NativeSetCertificatePath(certPath, null);
}
public static void SetCertificatePath(string path)
{
NativeSetCertificatePath(path, null);
}
public static void Request(IntPtr client, string route, byte[] msg, uint reqtId, int timeout)
{
var length = 0;
if (msg != null)
length = msg.Length;
var rc = NativeBinaryRequest(client, route, msg, length, reqtId, timeout, NativeRequestCallback, NativeErrorCallback);
if (rc != PitayaConstants.PcRcOk)
{
var rcStr = RcToStr(rc);
DLog(string.Format("request - failed to perform request {0}", rcStr));
WeakReference reference;
if (!Listeners.TryGetValue(client, out reference) || !reference.IsAlive) return;
var listener = reference.Target as IPitayaListener;
if (listener != null) listener.OnRequestError(reqtId, new PitayaError(rcStr, "Failed to send request"));
}
}
public static void Notify(IntPtr client, string route, byte[] msg, int timeout)
{
var length = 0;
if (msg != null)
length = msg.Length;
NativeBinaryNotify(client, route, msg, length, IntPtr.Zero, timeout, NativeNotifyCallback);
}
public static int Quality(IntPtr client)
{
CheckClient(client);
return NativeQuality(client);
}
public static PitayaClientState State(IntPtr client)
{
CheckClient(client);
return (PitayaClientState)NativeState(client);
}
public static void Dispose(IntPtr client)
{
NativeRemoveEventHandler(client, EventHandlersIds[client]);
Listeners.Remove(client);
EventHandlersIds.Remove(client);
NativeDestroy(client);
}
public static ProtobufSerializer.SerializationFormat ClientSerializer(IntPtr client)
{
IntPtr nativeSerializer = NativeSerializer(client);
var serializer = Marshal.PtrToStringAnsi(nativeSerializer);
NativeFreeSerializer(nativeSerializer);
return PitayaConstants.SerializerJson.Equals(serializer) ? ProtobufSerializer.SerializationFormat.Json : ProtobufSerializer.SerializationFormat.Protobuf;
}
public static void AddPinnedPublicKeyFromCertificateString(string caString)
{
var rc = NativeAddPinnedPublicKeyFromCertificateString(caString);
if (rc != PitayaConstants.PcRcOk)
{
throw new Exception(string.Format("AddPineedPublicKeyFromCertificateString: {0}", RcToStr(rc)));
}
SkipKeyPinCheck(false);
}
public static void AddPinnedPublicKeyFromCertificateFile(string name)
{
var certPath = FindCertPathFromName(name);
int rc = NativeAddPinnedPublicKeyFromCertificateFile(certPath);
if (rc != PitayaConstants.PcRcOk)
{
throw new Exception(string.Format("AddPineedPublicKeyFromCertificateFile: {0}", RcToStr(rc)));
}
SkipKeyPinCheck(false);
}
public static void SkipKeyPinCheck(bool shouldSkip)
{
NativeSkipKeyPinCheck(shouldSkip);
}
public static void ClearPinnedPublicKeys()
{
NativeClearPinnedPublicKeys();
}
//--------------------HELPER METHODS----------------------------------//
private static string EvToStr(int ev)
{
return Marshal.PtrToStringAnsi(NativeEvToStr(ev));
}
private static string RcToStr(int rc)
{
return Marshal.PtrToStringAnsi(NativeRcToStr(rc));
}
private static string FindCertPathFromName(string name)
{
string certPath = Path.Combine(Application.streamingAssetsPath, name);
if (Application.platform == RuntimePlatform.Android)
{
var persistentPath = Path.Combine(Application.persistentDataPath, name);
if (!PlayerPrefs.HasKey(persistentPath))
{
var reader = new WWW(certPath);
while (!reader.isDone) { }
System.IO.File.WriteAllBytes(persistentPath, reader.bytes);
PlayerPrefs.SetInt(persistentPath, 1);
}
certPath = persistentPath;
}
return certPath;
}
private static PitayaError CreatePitayaError(PitayaBindingError errorBinding, ProtobufSerializer.SerializationFormat format)
{
var rawData = new byte[errorBinding.Buffer.Len];
Marshal.Copy(errorBinding.Buffer.Data, rawData, 0, (int)errorBinding.Buffer.Len);
if (format == ProtobufSerializer.SerializationFormat.Protobuf)
{
Error error = new ProtobufSerializer(format).Decode<Error>(rawData);
return new PitayaError(error.Code, error.Msg, error.Metadata);
}
var jsonStr = Encoding.UTF8.GetString(rawData);
var json = SimpleJson.SimpleJson.DeserializeObject<Dictionary<string, object>>(jsonStr);
var code = (string)json["code"];
var msg = (string)json["msg"];
Dictionary<string, string> metadata;
if (json.ContainsKey("metadata"))
{
metadata = (Dictionary<string, string>)SimpleJson.SimpleJson.CurrentJsonSerializerStrategy.DeserializeObject(json["metadata"],
typeof(Dictionary<string, string>), new Dictionary<string, string>());
}
else
{
metadata = new Dictionary<string, string>();
}
return new PitayaError(code, msg, metadata);
}
//-------------------------PRIVATE METHODS------------------------------//
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
private static void CheckClient(IntPtr client)
{
if (client == IntPtr.Zero)
throw new NullReferenceException("invalid client");
}
//-----------------------NATIVE CALLBACKS-------------------------------//
[MonoPInvokeCallback(typeof(NativeAssertCallback))]
private static void OnAssert(IntPtr e, IntPtr file, int line)
{
var eName = Marshal.PtrToStringAnsi(e);
var fileName = Marshal.PtrToStringAnsi(file);
Debug.LogAssertion(string.Format("{0}:{1} Failed assertion {2}", fileName, line, eName));
}
[MonoPInvokeCallback(typeof(NativeErrorCallback))]
private static void OnError(IntPtr client, uint rid, IntPtr errorPtr)
{
var errBinding = (PitayaBindingError)Marshal.PtrToStructure(errorPtr, typeof(PitayaBindingError));
PitayaError error;
if (errBinding.Code == PitayaConstants.PcRcServerError)
{
error = CreatePitayaError(errBinding, ClientSerializer(client));
}
else
{
var code = RcToStr(errBinding.Code);
error = new PitayaError(code, "Internal Pitaya error");
}
QueueDispatcher.Dispatch(() =>
{
WeakReference reference;
if (!Listeners.TryGetValue(client, out reference) || !reference.IsAlive) return;
var listener = reference.Target as IPitayaListener;
if (listener != null) listener.OnRequestError(rid, error);
});
}
[MonoPInvokeCallback(typeof(NativeRequestCallback))]
private static void OnRequest(IntPtr client, uint rid, IntPtr respPtr)
{
var buffer = (PitayaBuffer)Marshal.PtrToStructure(respPtr, typeof(PitayaBuffer));
var rawData = new byte[buffer.Len];
Marshal.Copy(buffer.Data, rawData, 0, (int)buffer.Len);
QueueDispatcher.Dispatch(() =>
{
WeakReference reference;
if (!Listeners.TryGetValue(client, out reference) || !reference.IsAlive) return;
var listener = reference.Target as IPitayaListener;
if (listener != null) listener.OnRequestResponse(rid, rawData);
});
}
[MonoPInvokeCallback(typeof(NativeNotifyCallback))]
private static void OnNotify(IntPtr req, IntPtr error)
{
var errBinding = (PitayaBindingError)Marshal.PtrToStructure(error, typeof(PitayaBindingError));
DLog(string.Format("OnNotify | rc={0}", RcToStr(errBinding.Code)));
}
[MonoPInvokeCallback(typeof(NativePushCallback))]
private static void OnPush(IntPtr client, IntPtr routePtr, IntPtr payloadBufferPtr)
{
var route = Marshal.PtrToStringAnsi(routePtr);
var buffer = (PitayaBuffer)Marshal.PtrToStructure(payloadBufferPtr, typeof(PitayaBuffer));
var rawData = new byte[buffer.Len];
Marshal.Copy(buffer.Data, rawData, 0, (int)buffer.Len);
WeakReference reference;
if (!Listeners.TryGetValue(client, out reference) || !reference.IsAlive)
{
DLog(string.Format("OnEvent - no listener fond for client ev={0}", client));
return;
}
var listener = reference.Target as IPitayaListener;
QueueDispatcher.Dispatch(() =>
{
if (listener != null) listener.OnUserDefinedPush(route, rawData);
});
}
[MonoPInvokeCallback(typeof(NativeEventCallback))]
private static void OnEvent(IntPtr client, int ev, IntPtr exData, IntPtr arg1Ptr, IntPtr arg2Ptr)
{
DLog(string.Format("OnEvent - pinvoke callback START | ev={0} client={1}", EvToStr(ev), client));
if (arg1Ptr != IntPtr.Zero)
{
DLog(string.Format("OnEvent - msg={0}", Marshal.PtrToStringAnsi(arg1Ptr)));
}
WeakReference reference;
if (!Listeners.TryGetValue(client, out reference) || !reference.IsAlive)
{
DLog(string.Format("OnEvent - no listener fond for client ev={0}", client));
return;
}
var listener = reference.Target as IPitayaListener;
QueueDispatcher.Dispatch(() =>
{
switch (ev)
{
case PitayaConstants.PcEvConnected:
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Connected, null);
break;
case PitayaConstants.PcEvConnectError:
{
string error = Marshal.PtrToStringAnsi(arg1Ptr);
string description = Marshal.PtrToStringAnsi(arg2Ptr);
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.FailToConnect, new NetworkError(error, description));
break;
}
case PitayaConstants.PcEvConnectFailed:
{
string error = Marshal.PtrToStringAnsi(arg1Ptr);
string description = Marshal.PtrToStringAnsi(arg2Ptr);
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.FailToConnect, new NetworkError(error, description));
break;
}
case PitayaConstants.PcEvDisconnect:
{
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Disconnected, null);
break;
}
case PitayaConstants.PcEvKickedByServer:
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Kicked, null);
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Disconnected, null);
break;
case PitayaConstants.PcEvUnexpectedDisconnect:
{
string error = Marshal.PtrToStringAnsi(arg1Ptr);
string description = Marshal.PtrToStringAnsi(arg2Ptr);
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Disconnected, new NetworkError(error, description));
break;
}
case PitayaConstants.PcEvProtoError:
{
string error = Marshal.PtrToStringAnsi(arg1Ptr);
string description = Marshal.PtrToStringAnsi(arg2Ptr);
if (listener != null) listener.OnNetworkEvent(PitayaNetWorkState.Error, new NetworkError(error, description));
break;
}
}
DLog("OnEvent - main thread END");
});
DLog("OnEvent - pinvoke callback END");
}
[MonoPInvokeCallback(typeof(NativeLogFunction))]
private static void LogFunction(PitayaLogLevel level, string msg)
{
switch (level)
{
case PitayaLogLevel.Debug:
Debug.Log("[DEBUG] " + msg);
break;
case PitayaLogLevel.Info:
Debug.Log("[INFO] " + msg);
break;
case PitayaLogLevel.Warn:
Debug.Log("[WARN] " + msg);
break;
case PitayaLogLevel.Error:
Debug.Log("[ERROR] " + msg);
break;
case PitayaLogLevel.Disable:
// Don't do anything
break;
}
}
#if (UNITY_IPHONE || UNITY_XBOX360) && !UNITY_EDITOR
private const string LibName = "__Internal";
#elif (UNITY_ANDROID) && !UNITY_EDITOR
private const string LibName = "libpitaya-android";
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX)
private const string LibName = "libpitaya-mac";
#elif (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN)
private const string LibName = "pitaya-windows";
#else
private const string LibName = "pitaya-linux";
#endif
// ReSharper disable UnusedMember.Local
[DllImport(LibName, EntryPoint = "tr_uv_tls_set_ca_file")]
private static extern void NativeSetCertificatePath(string caFile, string caPath);
[DllImport(LibName, EntryPoint = "pc_unity_lib_init")]
private static extern void NativeLibInit(int logLevel, string caFile, string caPath, NativeAssertCallback assert, string platform, string buildNumber, string version);
[DllImport(LibName, EntryPoint = "pc_unity_update_client_info")]
private static extern void NativeLibUpdateClientInfo(string platform, string buildNumber, string version);
[DllImport(LibName, EntryPoint = "pc_lib_set_default_log_level")]
private static extern void NativeLibSetLogLevel(int logLevel);
[DllImport(LibName, EntryPoint = "pc_client_ev_str")]
private static extern IntPtr NativeEvToStr(int ev);
[DllImport(LibName, EntryPoint = "pc_client_rc_str")]
private static extern IntPtr NativeRcToStr(int rc);
[DllImport(LibName, EntryPoint = "pc_unity_create")]
private static extern IntPtr NativeCreate(bool enableTls, bool enablePoll, bool enableReconnect, int connTimeout);
[DllImport(LibName, EntryPoint = "pc_unity_destroy")]
private static extern int NativeDestroy(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_client_connect")]
private static extern int NativeConnect(IntPtr client, string host, int port, string handshakeOpts);
[DllImport(LibName, EntryPoint = "pc_client_disconnect")]
private static extern int NativeDisconnect(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_unity_request")]
private static extern int NativeRequest(IntPtr client, string route, string msg, uint cbUid, int timeout, NativeRequestCallback callback, NativeErrorCallback errorCallback);
[DllImport(LibName, EntryPoint = "pc_unity_binary_request")]
private static extern int NativeBinaryRequest(IntPtr client, string route, byte[] data, long len, uint cbUid, int timeout, NativeRequestCallback callback, NativeErrorCallback errorCallback);
[DllImport(LibName, EntryPoint = "pc_string_notify_with_timeout")]
private static extern int NativeNotify(IntPtr client, string route, string msg, IntPtr exData, int timeout, NativeNotifyCallback callback);
[DllImport(LibName, EntryPoint = "pc_binary_notify_with_timeout")]
private static extern int NativeBinaryNotify(IntPtr client, string route, byte[] data, long len, IntPtr exData, int timeout, NativeNotifyCallback callback);
[DllImport(LibName, EntryPoint = "pc_client_poll")]
private static extern int NativePoll(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_client_add_ev_handler")]
private static extern int NativeAddEventHandler(IntPtr client, NativeEventCallback callback, IntPtr exData, IntPtr destructor);
[DllImport(LibName, EntryPoint = "pc_client_set_push_handler")]
private static extern int NativeAddPushHandler(IntPtr client, NativePushCallback callback);
[DllImport(LibName, EntryPoint = "pc_client_rm_ev_handler")]
private static extern int NativeRemoveEventHandler(IntPtr client, int handlerId);
[DllImport(LibName, EntryPoint = "pc_client_conn_quality")]
private static extern int NativeQuality(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_client_state")]
private static extern int NativeState(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_client_serializer")]
private static extern IntPtr NativeSerializer(IntPtr client);
[DllImport(LibName, EntryPoint = "pc_client_free_serializer")]
private static extern IntPtr NativeFreeSerializer(IntPtr serializer);
// ReSharper restore UnusedMember.Local
[DllImport(LibName, EntryPoint = "pc_lib_add_pinned_public_key_from_certificate_string")]
private static extern int NativeAddPinnedPublicKeyFromCertificateString(string ca_string);
[DllImport(LibName, EntryPoint = "pc_lib_add_pinned_public_key_from_certificate_file")]
private static extern int NativeAddPinnedPublicKeyFromCertificateFile(string caPath);
[DllImport(LibName, EntryPoint = "pc_lib_skip_key_pin_check")]
private static extern void NativeSkipKeyPinCheck(bool shouldSkip);
[DllImport(LibName, EntryPoint = "pc_lib_clear_pinned_public_keys")]
private static extern void NativeClearPinnedPublicKeys();
[DllImport(LibName, EntryPoint = "pc_unity_init_log_function")]
private static extern int NativeInitLogFunction(NativeLogFunction fn);
#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern string _PitayaGetCFBundleVersion();
#else
private static string _PitayaGetCFBundleVersion() { return "1"; }
#endif
}
}