Esempio n. 1
0
        // RENDERER PROCESS PIPE EVENT LOOP
        private async Task _processClientMessages()
        {
            var message = await readObjectAsync(syncPipeIn);

            if (message == null)
            {
                NKLogging.log("!Renderer Received Empty Message");
                cancelTokenSource.Cancel();
                return;
            }

            if (message.command == NKRemotingMessage.Command.NKScriptMessageSync)
            {
                var name = message.args[0];
                Dictionary <string, object> body = null;
                try
                {
                    body = context.NKdeserialize(message.args[1]) as Dictionary <string, object>;
                }
                catch (Exception ex)
                {
                    NKLogging.log("!Renderer Message Deserialization Error: " + ex.Message);
                }
                var nks = new NKScriptMessage(name, body);

                NKScriptMessageHandler handler = null;
                if (_localHandlers.ContainsKey(name))
                {
                    handler = _localHandlers[name];
                }
                else
                {
                    int target = Int32.Parse(body["$target"].ToString());
                    handler = NKScriptChannel.getNative(target);
                }
                if (handler != null)
                {
                    var nkr = new NKRemotingMessage();
                    nkr.command = NKRemotingMessage.Command.NKScriptMessageSyncReply;

                    try
                    {
                        var result = handler.didReceiveScriptMessageSync(nks);
                        nkr.args = new string[] { context.NKserialize(result) };
                    } catch (Exception ex)
                    {
                        NKLogging.log("!Renderer Message Processing Error: " + ex.Message);
                        NKLogging.log(ex.StackTrace);
                        nkr.args = new string[] { };
                    }
                    writeObject(syncPipeOut, nkr);
                }
                else
                {
                    NKLogging.log("+Renderer Received Unknown Script Message Sync");
                }
            }
            else if (message.command == NKRemotingMessage.Command.NKScriptMessage)
            {
                var name = message.args[0];
                Dictionary <string, object> body = null;
                try
                {
                    body = context.NKdeserialize(message.args[1]) as Dictionary <string, object>;
                }
                catch (Exception ex)
                {
                    NKLogging.log("!Renderer Message Deserialization Error: " + ex.Message);
                }
                var nks = new NKScriptMessage(name, body);

                NKScriptMessageHandler handler = null;
                if (_localHandlers.ContainsKey(name))
                {
                    handler = _localHandlers[name];
                }
                else
                {
                    int target = Int32.Parse(body["$target"].ToString());
                    handler = NKScriptChannel.getNative(target);
                }
                if (handler != null)
                {
                    handler.didReceiveScriptMessage(nks);
                }
                else
                {
                    NKLogging.log("+Renderer Received Unknown Script Message " + message.args[1]);
                }
            }
            else if (message.command == NKRemotingMessage.Command.NKRemotingClose)
            {
                try
                {
                    syncPipeIn.Close();
                    syncPipeOut.Close();
                    asyncPipe.Close();
                }
                catch { }

                Environment.Exit(0);
            }
            else if (message.command == NKRemotingMessage.Command.NKEvent)
            {
                var     eventType   = message.args[0];
                NKEvent eventObject = new NKEvent((IDictionary <string, object>)(NKData.jsonDeserialize(message.args[1])));
                NKEventEmitter.global.emit <NKEvent>(eventType, eventObject, false);
            }

            if (!cancelToken.IsCancellationRequested)
            {
                await _processClientMessages();
            }
            else
            {
                return;
            }
        }
        public override void didReceiveScriptMessage(NKScriptMessage message)
        {
            // A workaround for when postMessage(undefined)
            if (message.body == null)
            {
                return;
            }

            var body = message.body as Dictionary <string, object>;

            if (body != null && body.ContainsKey("$opcode"))
            {
                string opcode = body["$opcode"] as String;
                int    target = Int32.Parse(body["$target"].ToString());
                NKScriptMessageHandler proxy = null;
                if (_proxies.ContainsKey(target))
                {
                    proxy = _proxies[target];
                }
                else if (target > 1500)
                {
                    var targetFloor = target - (target % NKRANGEPERPROCESS);
                    if (_proxiesNatives.ContainsKey(targetFloor))
                    {
                        proxy = _proxiesNatives[targetFloor];
                    }
                }

                if (proxy != null)
                {
                    if (opcode == "-")
                    {
                        if (target == 0)
                        {
                            // TRANSFER TO REMOTE, WITH SYNC
                            var _ = proxy.didReceiveScriptMessageSync(message);
                            this.unbind();
                        }
                        else
                        {
                            // TRANSFER TO REMOTE, WITH SYNC
                            var _ = proxy.didReceiveScriptMessageSync(message);

                            // UNBIND PROXY
                            _cancelTokens[target].Cancel();
                        }
                    }
                    else if (typeInfo.ContainsProperty(opcode))
                    {
                        // ALSO TRANSFER TO REMOTE
                        proxy.didReceiveScriptMessage(message);
                    }
                    else if (typeInfo.ContainsMethod(opcode))
                    {
                        // Invoke method

                        // TRANSFER TO REMOTE ONLY
                        proxy.didReceiveScriptMessage(message);
                    }
                    else
                    {
                        NKLogging.log(String.Format("!Invalid member name: {0}", opcode));
                    }
                }
                else if (opcode == "+")
                {
                    throw new NotImplementedException("+ opcode must be called using synchronous messages");
                }
                else
                {
                    // else Unknown opcode
                    var obj = _principal.plugin as NKScriptMessageHandler;
                    if (obj != null)
                    {
                        obj.didReceiveScriptMessage(message);
                    }
                    else
                    {
                        // discard unknown message
                        NKLogging.log(String.Format("!Unknown message: {0}", message.body.ToString()));
                    }
                }
            }
            else
            {
                // null body, ignore
            }
        }