Пример #1
0
        private void HandleRequest(IAsyncResult result)
        {
            //Get the listener context

            HttpListenerContext context = null;

            try {
                context = listener.EndGetContext(result);
            } catch {
                listener.Stop();
                listener.Start();
            }

            //Start listening for the next request
            listener.BeginGetContext(new AsyncCallback(HandleRequest), listener);

            if (context == null)
            {
                return;
            }

            //process query string and make CallConfig object
            CallConfig config = null;

            //missing on mobile platforms
            //HttpUtility.ParseQueryString(context.Request.Url.Query).Get("params");

            //if (context.Request.Url.ToString().Contains("callnative")) {
            //response = "native code reply";
            string url = context.Request.Url.ToString();
            string callConfigAsJson = Uri.UnescapeDataString(url.Split(new string[] { "callbackend?params=" }, StringSplitOptions.RemoveEmptyEntries)[1]);

            try {
                config = CallConfig.FromJsonString(callConfigAsJson);
            } catch (Exception ex) {
                DLogger.WriteLog(ex);
            }

            if (config == null)
            {
                SendServerErrorCodeToResponse(context);
                return;
            }

            CallResult handleRequestResult = null;

            try {
                handleRequestResult = OnHandleRequest.Invoke(config);
            } catch (Exception ex) {
                DLogger.WriteLog(ex);
                SendServerErrorCodeToResponse(context);
#if DEBUG
                throw ex;
#endif
            }

            if (handleRequestResult != null)
            {
                context.Response.AddHeader("Access-Control-Allow-Origin", "*");
                context.Response.ContentType     = handleRequestResult.ContentType;
                context.Response.StatusCode      = (int)HttpStatusCode.OK;
                context.Response.ContentLength64 = handleRequestResult.Bytes.LongLength;
                context.Response.OutputStream.Write(handleRequestResult.Bytes, 0, handleRequestResult.Bytes.Length);
                context.Response.OutputStream.Flush();
                context.Response.OutputStream.Close();
                context.Response.Close();
            }
        }
        public virtual CallResult ProcessCallFromFrontend(CallConfig config) {             //dict["name"]
            DLogger.WriteLog("Call from frontend of method name:{0} with arguments:{1}", config.MethodName, config.Params);

            object instance = webView.Page;
            MethodInfo theMethod = null;

            if (Page != null) {//first lets look for the method in the page
                theMethod = Page.GetType().GetMethod(config.MethodName);
            }

            if (theMethod == null) {
                theMethod = webView.GetType().GetMethod(config.MethodName);
                instance = webView;
            }

            if (theMethod == null) {//if the method is not found in current view, then lets search for it in other loaded views
                foreach (var view in LoadedViews.ViewForEachType) {
                    theMethod = view.GetType().GetMethod(config.MethodName);
                    if (theMethod != null) {
                        instance = view;
                        break;
                    }
                }
            }

            if (theMethod == null) {//if the method is not found in loaded views, then lets search for it in parent window
                theMethod = ParentWindow.GetType().GetMethod(config.MethodName);
                instance = ParentWindow;
            }

            if (theMethod == null) {//if the method is not found in parent window, then lets search for it in AppDelegate.Instance
                theMethod = BaseAppDelegate.Instance.GetType().GetMethod(config.MethodName);
                instance = BaseAppDelegate.Instance;
            }

            CallResult result = new CallResult(string.Empty);
            try {
				//BaseAppDelegate.Instance.InvokeOnMainThread(()=>{
                	result = (CallResult)theMethod.Invoke(instance, new object[] { config.Params });
				//});
            } catch (Exception ex) {
                DLogger.WriteLog(ex);
                try {//try parameter less
					//BaseAppDelegate.Instance.InvokeOnMainThread(()=>{
						result = (CallResult)theMethod.Invoke(instance, null);
					//});
                } catch (Exception ex2) {
                    DLogger.WriteLog(ex2);
                }
            }

            return result;
        }