Ejemplo n.º 1
0
        internal static int ProcessPublish(string path, string json, Session ses)
        {
            Topic cur = Topic.root.Get(path, true, ses == null?null:ses.owner);

            if (string.IsNullOrEmpty(json) || json == "null")               // Remove
            {
                cur.Remove();
            }
            else
            {
                cur.SetState(JsLib.ParseJson(json), ses == null?null:ses.owner);
            }
            return(200);
        }
Ejemplo n.º 2
0
        public void Publish(string path, string payload)
        {
            if (!_subEn)
            {
                return;
            }
            string lp = (path.Length > remotePrefix.Length) ? path.Substring(remotePrefix.Length + 1) : string.Empty;

            try {
                var o = JsLib.ParseJson(payload);
                var t = Owner.Get(lp, true, Owner);
                if (!t.CheckAttribute(Topic.Attribute.Internal))
                {
                    t.SetState(o, Owner);
                }
            }
            catch (Exception ex) {
                if (_pl.verbose)
                {
                    Log.Warning("{0}{1} R {2} - {3}", Client, path, payload, ex.Message);
                }
            }
        }
Ejemplo n.º 3
0
        private static void Import(XElement xElement, Topic owner, string path)
        {
            if (xElement == null || ((xElement.Attribute("n") == null || owner == null) && path == null))
            {
                return;
            }
            Version ver;
            Topic   cur = null;
            bool    setVersion;

            if (xElement.Attribute("ver") != null && Version.TryParse(xElement.Attribute("ver").Value, out ver))
            {
                if (owner == null ? Topic.root.Exist(path, out cur) : owner.Exist(xElement.Attribute("n").Value, out cur))
                {
                    Version oldVer;
                    var     ov_js = cur.GetField("version");
                    string  ov_s;
                    if (ov_js.ValueType == JSValueType.String && (ov_s = ov_js.Value as string) != null && ov_s.StartsWith("¤VR") && Version.TryParse(ov_s.Substring(3), out oldVer) && oldVer >= ver)
                    {
                        return; // don't import older version
                    }
                }
                setVersion = true;
            }
            else
            {
                ver        = default(Version);
                setVersion = false;
            }
            JSValue state = null, manifest = null;

            if (xElement.Attribute("m") != null)
            {
                try {
                    manifest = JsLib.ParseJson(xElement.Attribute("m").Value);
                }
                catch (Exception ex) {
                    Log.Warning("Import({0}).m - {1}", xElement.ToString(), ex.Message);
                }
            }
            if (setVersion)
            {
                manifest = JsLib.SetField(manifest, "version", "¤VR" + ver.ToString());
            }

            if (xElement.Attribute("s") != null)
            {
                try {
                    state = JsLib.ParseJson(xElement.Attribute("s").Value);
                }
                catch (Exception ex) {
                    Log.Warning("Import({0}).s - {1}", xElement.ToString(), ex.Message);
                }
            }


            if (owner == null)
            {
                cur = Topic.I.Get(Topic.root, path, true, null, false, false);
            }
            else
            {
                cur = Topic.I.Get(owner, xElement.Attribute("n").Value, true, null, false, false);
            }
            Topic.I.Fill(cur, state, manifest, null);
            foreach (var xNext in xElement.Elements("i"))
            {
                Import(xNext, cur, null);
            }
        }
Ejemplo n.º 4
0
        private void RcvProcess(IAsyncResult ar)
        {
            bool first = true;
            int  len;
            byte b;

            try {
                len = _stream.EndRead(ar);
            }
            catch (IOException) {
                this.Dispose(true);
                return;
            }
            catch (ObjectDisposedException) {
                return;
            }
            if (len > 0)
            {
                try {
                    do
                    {
                        if (first)
                        {
                            first = false;
                            b     = _rcvBuf[0];
                        }
                        else
                        {
                            b = (byte)_stream.ReadByte();
                        }
                        if (_rcvState < 0)
                        {
                            if (_rcvState < -1)
                            {
                                if (b == 0)
                                {
                                    _rcvState  = -1;
                                    _rcvLength = 0;
                                }
                            }
                            else
                            {
                                _rcvLength = (_rcvLength << 7) | (b & 0x7F);
                                if (b < 0x80)
                                {
                                    if (_rcvLength < 3 || _rcvLength > int.MaxValue / 2048) // 1 MB
                                    {
                                        _rcvState = -2;                                     // Bad Msg.Len
                                    }
                                    else
                                    {
                                        _rcvState = 0;
                                        if (_rcvLength >= _rcvMsgBuf.Length)
                                        {
                                            int l = _rcvMsgBuf.Length;
                                            while (l < _rcvLength)
                                            {
                                                l = l * 2;
                                            }
                                            _rcvMsgBuf = new byte[l];
                                        }
                                    }
                                }
                            }
                        }
                        else if (_rcvState < _rcvLength)
                        {
                            _rcvMsgBuf[_rcvState] = b;
                            _rcvState++;
                        }
                        else
                        {
                            if (b == 0xFF) // Paranoic mode On
                            {
                                string ms = null;
                                try {
                                    ms = Encoding.UTF8.GetString(_rcvMsgBuf, 0, _rcvState);
                                    var mj = JsLib.ParseJson(ms) as JST.Array;
                                    if (verbose)
                                    {
                                        Log.Debug("{0}.Rcv({1})", this.ToString(), ms);
                                    }
                                    if (mj != null && mj.Count() > 0)
                                    {
                                        _callback(new EsMessage(this, mj));
                                    }
                                }
                                catch (Exception ex) {
                                    Log.Warning("{0}.Rcv({1}) - {2}", this.ToString(), ms ?? BitConverter.ToString(_rcvMsgBuf, 0, _rcvState), ex.Message);
                                }
                            }
                            else
                            {
                                if (verbose)
                                {
                                    Log.Warning("{0}.Rcv - Paranoic", this.ToString());
                                }
                            }
                            _rcvState = -2;
                        }
                    } while(_stream.DataAvailable);
                }
                catch (ObjectDisposedException) {
                    return;
                }
                catch (Exception ex) {
                    _rcvState = -2;
                    Log.Warning(ex.ToString());
                }
            }
            else
            {
                this.Dispose(true);
                return;
            }

            try {
                _stream.BeginRead(_rcvBuf, 0, 1, _rcvCB, _stream);
            }
            catch (IOException) {
                this.Dispose(true);
                return;
            }
            catch (ObjectDisposedException ex) {
                Log.Warning("EsConnection.RcvProcess {0}", ex.Message);
                return;
            }
        }