Example #1
0
            /// <summary>
            /// Executes code on the background thread.
            /// </summary>
            /// <param name="backgroundAction">The background action.</param>
            /// <param name="uiCallback">The UI callback.</param>
            /// <param name="progressChanged">The progress change callback.</param>
            public IBackgroundTask ExecuteOnBackgroundThread(Action backgroundAction, Action <BackgroundTaskCompletedEventArgs> uiCallback, Action <BackgroundTaskProgressChangedEventArgs> progressChanged)
            {
                var task = new ForegroundTask(backgroundAction);

                if (uiCallback != null)
                {
                    task.Completed += (s, e) => uiCallback(e);
                }

                if (progressChanged != null)
                {
                    task.ProgressChanged += (s, e) => progressChanged(e);
                }

                task.Enqueue(null);

                return(task);
            }
Example #2
0
            public IBackgroundTask ExecuteOnBackgroundThread(Action backgroundAction, RunWorkerCompletedEventHandler uiCallback, ProgressChangedEventHandler progressChanged)
            {
                var task = new ForegroundTask(backgroundAction);

                if (uiCallback != null)
                {
                    task.Completed += uiCallback;
                }

                if (progressChanged != null)
                {
                    task.ProgressChanged += progressChanged;
                }

                task.Start(null);

                return(task);
            }
Example #3
0
            /// <summary>
            /// Executes code on the background thread.
            /// </summary>
            /// <param name="backgroundAction">The background action.</param>
            /// <param name="uiCallback">The UI callback.</param>
            /// <param name="progressChanged">The progress change callback.</param>
            public IBackgroundTask ExecuteOnBackgroundThread(Action backgroundAction, Action<BackgroundTaskCompletedEventArgs> uiCallback, Action<BackgroundTaskProgressChangedEventArgs> progressChanged)
            {
                var task = new ForegroundTask(backgroundAction);

                if (uiCallback != null)
                    task.Completed += (s, e) => uiCallback(e);

                if (progressChanged != null)
                    task.ProgressChanged += (s, e) => progressChanged(e);

                task.Enqueue(null);

                return task;
            }
Example #4
0
            public IBackgroundTask ExecuteOnBackgroundThread(Action backgroundAction, RunWorkerCompletedEventHandler uiCallback, ProgressChangedEventHandler progressChanged)
            {
                var task = new ForegroundTask(backgroundAction);

                if (uiCallback != null)
                    task.Completed += uiCallback;

                if (progressChanged != null)
                    task.ProgressChanged += progressChanged;

                task.Start(null);

                return task;
            }
Example #5
0
        IEnumerator ServeHTTP()
        {
            yield return(null);

            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var endPoint = new IPEndPoint(IPAddress.Any, port);

            listener.Bind(endPoint);
            listener.Listen(5);
            var background = new BackgroundTask();
            var foreground = new ForegroundTask();

            var host = "http://localhost:" + port.ToString();

            while (true)
            {
                yield return(background);

                Request       request = null;
                NetworkStream stream  = null;
                try {
                    var client = listener.Accept();
                    stream  = new NetworkStream(client);
                    request = Request.BuildFromStream(host, stream);
                } catch (HTTPException) {
                    Shutdown();
                    yield break;
                } catch (ThreadAbortException) {
                    Shutdown();
                    yield break;
                } catch (Exception e) {
                    Debug.LogError("Exception in server thread: " + e.ToString());
                    Shutdown();
                    yield break;
                }

                yield return(foreground);

                RouteRequest(request);
                yield return(background);

                try {
                    request.response.headers.Set("Connection", "Close");
                    HTTPProtocol.WriteResponse(stream, request.response.status, request.response.message, request.response.headers, request.response.Bytes);
                    stream.Flush();
                    stream.Dispose();
                } catch (HTTPException) {
                    Shutdown();
                    yield break;
                } catch (ThreadAbortException) {
                    Shutdown();
                    yield break;
                } catch (Exception e) {
                    Debug.LogError("Exception in server thread: " + e.ToString());
                    Shutdown();
                    yield break;
                }
                if (logRequests)
                {
                    Debug.Log(string.Format("{0} {1} {2} \"{3}\" {4}", System.DateTime.Now.ToString("yyyy/mm/dd H:mm:ss zzz"), request.response.status, request.method.ToUpper(), request.uri, request.response.Bytes.Length));
                }
            }
        }