Exemple #1
0
        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;

            MainQueueDispatcher.Dispatch(() =>
            {
                if (listener != null)
                {
                    listener.OnUserDefinedPush(route, rawData);
                }
            });
        }
Exemple #2
0
        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");
            }

            MainQueueDispatcher.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);
                }
            });
        }
 public PitayaClient(bool enableReconnect = false, string certificateName = null, int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT, IPitayaQueueDispatcher queueDispatcher = null)
 {
     if (queueDispatcher == null)
     {
         queueDispatcher = MainQueueDispatcher.Create();
     }
     PitayaBinding.QueueDispatcher = queueDispatcher;
     Init(certificateName, certificateName != null, false, enableReconnect, connectionTimeout);
 }
Exemple #4
0
        public static MainQueueDispatcher Create(bool dontDestroyOnLoad = true, bool shouldThrowExceptions = false)
        {
            if (_instance != null)
            {
                return(_instance);
            }

            _instance = MainQueueDispatcherFactory.Create(dontDestroyOnLoad, shouldThrowExceptions);
            return(_instance);
        }
Exemple #5
0
        private static void Create()
        {
            if (_instance != null)
            {
                return;
            }

            var go = new GameObject(typeof(MainQueueDispatcher).FullName);

            _instance = go.AddComponent <MainQueueDispatcher> ();
            DontDestroyOnLoad(_instance.gameObject);
        }
Exemple #6
0
 public PitayaClient(bool enableReconnect = false, string certificateName = null, int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT, IPitayaQueueDispatcher queueDispatcher = null, IPitayaBinding binding = null)
 {
     if (binding != null)
     {
         _binding = binding;
     }
     if (queueDispatcher == null)
     {
         queueDispatcher = MainQueueDispatcher.Create();
     }
     _binding.QueueDispatcher = queueDispatcher;
     Init(certificateName, certificateName != null, false, enableReconnect, connectionTimeout, new SerializerFactory());
 }
        public static MainQueueDispatcher Create(bool dontDestroyOnLoad = true)
        {
            if (_instance != null)
            {
                return(_instance);
            }

            var go = new GameObject(typeof(MainQueueDispatcher).FullName);

            _instance = go.AddComponent <MainQueueDispatcher> ();
            if (dontDestroyOnLoad)
            {
                DontDestroyOnLoad(_instance.gameObject);
            }
            return(_instance);
        }
Exemple #8
0
        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);

            MainQueueDispatcher.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);
                }
            });
        }
Exemple #9
0
        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;

            MainQueueDispatcher.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");
        }