Example #1
0
 public int InvokeOnce(float delay, CBInvoke cbInvoke)
 {
     if (null != cbInvoke)
     {
         InvokeItem item = null;
         if (m_recycled.Count > 0)
         {
             item = m_recycled[0];
             m_recycled.RemoveAt(0);
         }
         else
         {
             item = new InvokeItem();
         }
         item.flag            = 0;
         item.iId             = ++ms_handle_id;
         item.repeat          = 1;
         item.interval        = 0;
         item.repeat_interval = 0;
         item.delay           = (int)delay * m_fps;
         item.cbInvoke        = cbInvoke;
         m_actives.Add(item);
         return(item.iId);
     }
     return(-1);
 }
Example #2
0
 public int InvokeRepeate(float delay, float interval, CBInvoke cbInvoke, int repeat = -1, bool global = false)
 {
     if (null != cbInvoke)
     {
         InvokeItem item = null;
         if (m_recycled.Count > 0)
         {
             item = m_recycled[0];
             m_recycled.RemoveAt(0);
         }
         else
         {
             item = new InvokeItem();
         }
         item.flag = 0;
         if (repeat < 0)
         {
             item.flag |= FLAG_REPEAT_FOREVER;
         }
         if (global)
         {
             item.flag |= FLAG_GLOBAL;
         }
         item.iId             = ++ms_handle_id;
         item.repeat          = repeat;
         item.delay           = (int)delay * m_fps;
         item.interval        = (int)interval * m_fps;
         item.repeat_interval = 0;
         item.cbInvoke        = cbInvoke;
         m_actives.Add(item);
         return(item.iId);
     }
     return(-1);
 }
Example #3
0
 public void DoInvoke(TreeItemViewModel item)
 {
     InvokeItem?.Invoke(this, new InvokeEventArgs(item));
 }
        //[HttpPost]
        public async Task <ActionResult> BatchInvoke()
        {
            if (!Enabled)
            {
                return(this.JsonResult(-1, "ApiProxy is not enabled."));
            }

            InvokeContext context;

            if (HttpContext.Request.HttpMethod == "GET")
            {
                context = new InvokeContext()
                {
                    TraceEnabled = true,
                    InvokeItems  = new List <InvokeItem>(),
                };
                var item0 = new InvokeItem()
                {
                    Ready   = true,
                    Request = new InvokeRequest()
                    {
                        Url    = "http://www.baidu.com",
                        Method = "POST",
                        Body   = "abc",
                    },
                    Response = new InvokeResponse()
                    {
                    },
                    Script = "function postprocess(context, index, item) { if (item.Response.StatusCode==200) context.InvokeItems[1].Ready = true; }",
                };
                var item1 = new InvokeItem()
                {
                    Request = new InvokeRequest()
                    {
                        Url = "http://www.yipinapp.com",
                    },
                    Response = new InvokeResponse()
                    {
                    },
                    Script = "function preprocess(context, index, item) { logger.Info('preprocess'); item.Response.StatusCode=500; }\r\n function postprocess(context, index, item) { item.Response.Body = \"abc\"; item.Completed = true; logger.Error('postprocess'); }",
                };
                context.InvokeItems.Add(item0);
                context.InvokeItems.Add(item1);
            }
            else
            {
                var json = this.GetRequestContentJson() as JsonObject;
                context = InvokeContext.Deserialize(json);
            }

            UpdateInvokeItems(context);

            bool hasReadyItems = false;

            do
            {
                hasReadyItems = false;

                for (int index = 0; index < context.InvokeItems.Count; index++)
                {
                    var item = context.InvokeItems[index];
                    if (item.Ready && (!item.Completed))
                    {
                        hasReadyItems = true;

                        try
                        {
                            await RunInvokeItem(context, index);
                        }
                        catch (Exception ex)
                        {
                            context.LogTraceError("Failed to run invoke item " + index + " Exception:" + ex.ToString());
                        }

                        item.SetCompleted();
                    }
                }
            } while (hasReadyItems);

            var writer = new JsonWriter();

            InvokeContext.Serialize(writer, context, context.TraceEnabled ? Formats.BatchInvokeResultWithRequest : Formats.BatchInvokeResult);
            return(this.JsonResponseJson(0, "InvokeContext", writer.ToString()));
        }
        private async Task SendInvoke(InvokeContext context, InvokeItem item)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(item.Request.Url);

            request.Method  = item.Request.Method;
            request.Timeout = item.TimeoutInSeconds * 1000;

            foreach (var pair in item.Request.Headers)
            {
                switch (pair.Key.ToLowerInvariant())
                {
                case "content-type":
                    request.ContentType = pair.Value;
                    break;

                case "user-agent":
                    request.UserAgent = pair.Value;
                    break;

                default:
                    request.Headers[pair.Key] = pair.Value;
                    break;
                }
            }

            if (string.CompareOrdinal(request.Method, "GET") != 0)
            {
                var    contentData = new List <byte>();
                byte[] data        = Encoding.UTF8.GetBytes(item.Request.Body);
                contentData.AddRange(data);

                request.ContentLength = contentData.Count;
                Stream stream = await request.GetRequestStreamAsync();

                stream.Write(contentData.ToArray(), 0, contentData.Count);
                stream.Flush();
            }

            // Start invoking
            try
            {
                var response = (HttpWebResponse)await request.GetResponseAsync();

                item.Response.StatusCode = (int)response.StatusCode;

                item.Response.Headers.Clear();
                foreach (string key in response.Headers.AllKeys)
                {
                    item.Response.Headers[key] = response.Headers[key];
                }

                Stream responseContentStream = response.GetResponseStream();
                using (StreamReader reader = new StreamReader(responseContentStream))
                {
                    item.Response.Body = reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                if (ex.Response != null && ex.Response is HttpWebResponse)
                {
                    var httpWebResponse = ex.Response as HttpWebResponse;
                    item.Response.StatusCode = (int)httpWebResponse.StatusCode;
                }
                var stream  = ex.Response.GetResponseStream();
                var message = "Error: status:" + ex.Status + "\r\n" + (stream == null ? string.Empty : new StreamReader(stream).ReadToEnd());
                context.LogTraceError(message + " Exception: " + ex.ToString());
            }
            catch (Exception ex)
            {
                context.LogTraceError("Exception: " + ex.ToString());
            }
        }
Example #6
0
 public void Execute()
 {
     ManualResetEvent[] mre = new ManualResetEvent[Items.Count];
     Array.Copy(mMRES, mre, Items.Count);
     for (int i = 0; i < Items.Count; i++)
     {
         InvokeItem ii = new InvokeItem();
         ii.Item = Items[i];
         ii.MRE = mre[i];
         ii.MRE.Reset();
         System.Threading.ThreadPool.QueueUserWorkItem(OnExecute, ii);
     }
     System.Threading.WaitHandle.WaitAll(mre);
 }
Example #7
0
 public ConfigInvokeItem(InvokeItem item)
 {
     Type         = ConfigInvokeItemType.CustomAction;
     CustomAction = item;
 }