コード例 #1
0
 internal void AddAttr(string name, string v)
 {
     if (attrs == null)
     {
         attrs = new Roll <XAttr>(8);
     }
     attrs.Add(new XAttr(name, v));
 }
コード例 #2
0
        // to obtain a string key from a data object.
        protected Work(WorkContext wc) : base(wc.Name, null)
        {
            this.ctx = wc;

            // gather actions
            actions = new Roll <ActionInfo>(32);
            Type typ = GetType();

            typeinfo = typ.GetTypeInfo();

            foreach (MethodInfo mi in typ.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                // verify the return type
                Type ret = mi.ReturnType;
                bool async;
                if (ret == typeof(Task))
                {
                    async = true;
                }
                else if (ret == typeof(void))
                {
                    async = false;
                }
                else
                {
                    continue;
                }

                ParameterInfo[] pis = mi.GetParameters();
                ActionInfo      ai;
                if (pis.Length == 1 && pis[0].ParameterType == typeof(ActionContext))
                {
                    ai = new ActionInfo(this, mi, async, false);
                }
                else if (pis.Length == 2 && pis[0].ParameterType == typeof(ActionContext) && pis[1].ParameterType == typeof(int))
                {
                    LimitAttribute limit = (LimitAttribute)pis[1].GetCustomAttribute(typeof(LimitAttribute));
                    ai = new ActionInfo(this, mi, async, true, limit?.Value ?? 20);
                }
                else
                {
                    continue;
                }

                actions.Add(ai);
                if (ai.Name.Equals("default"))
                {
                    @default = ai;
                }
            }

            // gather ui actions
            int btns = 0;
            List <ActionInfo> uias = null;

            for (int i = 0; i < actions.Count; i++)
            {
                ActionInfo ai = actions[i];
                if (ai.HasUi)
                {
                    if (uias == null)
                    {
                        uias = new List <ActionInfo>();
                    }
                    uias.Add(ai);
                    if (ai.Ui.IsButton)
                    {
                        btns++;
                    }
                }
            }
            uiActions = uias?.ToArray();
            buttons   = btns;
        }
コード例 #3
0
ファイル: Service.cs プロジェクト: geffzhang/core
        protected Service(ServiceContext sc) : base(sc)
        {
            sc.Service = this;
            this.ctx   = sc;

            id = (Shard == null) ? sc.Name : sc.Name + "-" + Shard;

            // setup logging
            LoggerFactory factory = new LoggerFactory();

            factory.AddProvider(this);
            string     file = sc.GetFilePath('$' + DateTime.Now.ToString("yyyyMM") + ".log");
            FileStream fs   = new FileStream(file, FileMode.Append, FileAccess.Write);

            logWriter = new StreamWriter(fs, Encoding.UTF8, 1024 * 4, false)
            {
                AutoFlush = true
            };

            // create kestrel instance
            KestrelServerOptions options = new KestrelServerOptions();

            server = new KestrelServer(Options.Create(options), Application.Lifetime, factory);
            ICollection <string> addrcoll = server.Features.Get <IServerAddressesFeature>().Addresses;

            if (Addrs == null)
            {
                throw new ServiceException("missing 'addrs'");
            }
            foreach (string a in Addrs)
            {
                addrcoll.Add(a.Trim());
            }

            // events
            Type typ = GetType();

            foreach (MethodInfo mi in typ.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                // verify the return type
                Type ret = mi.ReturnType;
                bool async;
                if (ret == typeof(Task))
                {
                    async = true;
                }
                else if (ret == typeof(void))
                {
                    async = false;
                }
                else
                {
                    continue;
                }

                ParameterInfo[] pis = mi.GetParameters();
                EventInfo       evt;
                if (pis.Length == 1 && pis[0].ParameterType == typeof(EventContext))
                {
                    evt = new EventInfo(this, mi, async, false);
                }
                else if (pis.Length == 2 && pis[0].ParameterType == typeof(EventContext) && pis[1].ParameterType == typeof(string))
                {
                    evt = new EventInfo(this, mi, async, true);
                }
                else
                {
                    continue;
                }

                if (events == null)
                {
                    events = new Roll <EventInfo>(16);
                }
                events.Add(evt);
            }

            // cluster connectivity
            if (Cluster != null)
            {
                foreach (KeyValuePair <string, string> entry in Cluster)
                {
                    if (clients == null)
                    {
                        clients = new Roll <Client>(Cluster.Count * 2);
                    }
                    clients.Add(new Client(this, entry.Key, entry.Value));

                    if (queues == null)
                    {
                        queues = new Roll <EventQueue>(Cluster.Count * 2);
                    }
                    queues.Add(new EventQueue(entry.Key));
                }
            }

            // response cache
            cleaner = new Thread(Clean);
            cachies = new ConcurrentDictionary <string, Cachie>(Environment.ProcessorCount * 2, 1024);
        }