public void Update(TMessage curr)
                {
                    if (curr is null)
                    {
                        throw new ArgumentNullException(nameof(curr));
                    }

                    var key = _serializer.GetKey(curr);

                    lock (_snapshots.SyncRoot)
                    {
                        var prev = _snapshots.TryGetValue(key);

                        if (prev is null)
                        {
                            if (curr.SecurityId == default)
                            {
                                throw new ArgumentException(curr.ToString());
                            }

                            _snapshots.Add(key, curr.TypedClone());
                        }
                        else
                        {
                            _serializer.Update(prev, curr);
                        }

                        _dirtyKeys.Add(key);
                    }
                }
Esempio n. 2
0
 public Message(TMessage TMessage)
 {
     Text = TMessage.ToString();
     if (TMessage == TMessage.TRUE)
     {
         Type = "alert";
     }
     else
     {
         Type = "danger";
     }
 }
Esempio n. 3
0
        /// <summary>
        /// 定期调用以驱动异步代码回调
        /// </summary>
        public int Update()
        {
            int ret = 0;

            foreach (var iprot in protocols_.Values)
            {
                // 无数据就直接返回,将来做成个循环条件
                bool has_data = iprot.Transport.Peek();
                if (!has_data)
                {
                    // 用来准备清理超时会话的变量,只选择空闲时做清理
                    DateTime now = DateTime.Now;
                    async_rpc_sessions_ = async_rpc_sessions_.Where((kv) =>
                    {
                        TimeSpan spend_time = now - kv.Value.CreateTime;
                        //清理超时会话
                        if (spend_time.Seconds >= kv.Value.Timeout)
                        {
                            try
                            {
                                kv.Value.Callback(new TimeoutException("session timeout " + spend_time.Seconds + " seconds."));
                            }
                            catch (Exception e)
                            {
                                Debug.LogError("timeout callback exception:" + e + ", stack:" + e.StackTrace);
                            }
                            return(false);
                        }
                        else
                        {
                            return(true);
                        }
                    }).ToDictionary((kv) => kv.Key, (kv) => kv.Value);

                    continue;
                }

                try
                {
                    // 读取包头准备分发
                    TMessage msg = iprot.ReadMessageBegin();

                    // 按消息类型分发
                    if (TMessageType.Call == msg.Type ||
                        TMessageType.Oneway == msg.Type)
                    {
                        int index = msg.Name.IndexOf(":");
                        if (index < 0)
                        {
                            TProtocolUtil.Skip(iprot, TType.Struct);
                            iprot.ReadMessageEnd();
                            Debug.LogError("invalid msg." + msg.ToString());
                            ret++;
                            continue;
                        }

                        string     serviceName = msg.Name.Substring(0, index);
                        TProcessor processor;
                        if (!service_processor_map_.TryGetValue(serviceName, out processor))
                        {
                            TProtocolUtil.Skip(iprot, TType.Struct);
                            iprot.ReadMessageEnd();
                            Debug.LogError("calling unexisted service.");
                            ret++;
                            continue;
                        }

                        TMessage newMessage = new TMessage(
                            msg.Name.Substring(serviceName.Length + 1),
                            msg.Type,
                            msg.SeqID);
                        processor.Process(new StoredMessageProtocol(iprot, newMessage), iprot);
                        ret++;
                        continue;
                    }

                    AsyncRpcSessoin async_rpc_session;
                    if (!async_rpc_sessions_.TryGetValue(msg.SeqID, out async_rpc_session))
                    {
                        TProtocolUtil.Skip(iprot, TType.Struct);
                        iprot.ReadMessageEnd();
                        ret++;
                        continue;
                    }
                    async_rpc_sessions_.Remove(msg.SeqID);

                    if (msg.Type == TMessageType.Exception)
                    {
                        TApplicationException aex = TApplicationException.Read(iprot);
                        iprot.ReadMessageEnd();
                        async_rpc_session.Callback(aex);
                    }
                    else
                    {
                        async_rpc_session.Callback(null);
                    }

                    ret++;
                }
                catch (Exception e)
                {
                    byte[] tmp = new byte[128];
                    while (iprot.Transport.Read(tmp, 0, tmp.Length) > 0)
                    {
                    }                                                       //clear read buffer
                    throw e;
                }
            }
            return(ret);
        }