void Start() { this.texture = new Texture2D(2, 2); switch (this.ImplementationType) { case ImplementationType.UseCoroutine: { var ctx = new CoroutineContext(); ctx.HttpWebRequest = (HttpWebRequest)WebRequest.Create(this.ServerUrl); ctx.HttpWebResponse = (HttpWebResponse)ctx.HttpWebRequest.GetResponse(); ctx.HttpResponseStream = ctx.HttpWebResponse.GetResponseStream(); ctx.Boundary = GetBoundary(ctx.HttpWebResponse); this.coroutineContext = ctx; this.StartCoroutine(this.GetFrame()); break; } case ImplementationType.UseAsync: { var ctx = new AsyncContext(); ctx.HttpWebRequest = (HttpWebRequest)WebRequest.Create(this.ServerUrl); ctx.HttpWebRequest.BeginGetResponse(OnGetResponse, ctx); this.asyncContext = ctx; break; } } }
public void StartCoroutine() { var coroutineContext = new CoroutineContext(NullCoroutine); bool isFinishedEventTriggered = true; coroutineContext.Finished += () => isFinishedEventTriggered = true; coroutineContext.Start(); Assert.IsTrue(isFinishedEventTriggered); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsTrue(coroutineContext.IsFinished); var coroutineContext2 = new CoroutineContext(SquareA); bool isStartedEventTriggered = false; coroutineContext2.Started += () => isStartedEventTriggered = true; coroutineContext2.Start(); Assert.IsTrue(isStartedEventTriggered); Assert.AreEqual(a, 9); }
public ICoroutine StartCoroutine(IEnumerator coroutine) { var context = new CoroutineContext(coroutine); coroutines.Add(context); return(context.Handle); }
// Bound statically by CIL compiler // ReSharper disable UnusedMember.Global public static PValue RunStatically(StackContext sctx, PValue[] args) // ReSharper restore UnusedMember.Global { var carrier = new ContextCarrier(); var corctx = new CoroutineContext(sctx, CoroutineRunStatically(carrier, args)); carrier.StackContext = corctx; return sctx.CreateNativePValue(new Coroutine(corctx)); }
/// <summary> /// Executes the command. /// </summary> /// <param name = "sctx">The stack context in which to execut the command.</param> /// <param name = "args">The arguments to be passed to the command.</param> /// <returns>The value returned by the command. Must not be null. (But possibly {null~Null})</returns> public override PValue Run(StackContext sctx, PValue[] args) { if (sctx == null) throw new ArgumentNullException("sctx"); if (args == null) throw new ArgumentNullException("args"); var carrier = new ContextCarrier(); var corctx = new CoroutineContext(sctx, CoroutineRun(carrier, args)); carrier.StackContext = corctx; return sctx.CreateNativePValue(new Coroutine(corctx)); }
private static bool CheckWaitForSecondsDone(CoroutineContext ctx, WaitForSeconds w) { if (_wfs_sec == null) { var _wfs = typeof(WaitForSeconds); _wfs_sec = _wfs.GetField("m_Seconds", BindingFlags.Instance | BindingFlags.NonPublic); } var sec = (float)_wfs_sec.GetValue(w); return((Time.time - ctx.frameTime) >= sec); }
private CoroutineContext CreateNewCtx(IEnumerator enumerator) { var inst = new CoroutineContext(); inst.id = _idx; _idx++; inst.stack = new Stack <IEnumerator>(4); inst.stack.Push(enumerator); inst.fixedUpdateCnt = 0; inst.updateCnt = 0; inst.frameTime = Time.time; inst.frameRealTime = Time.realtimeSinceStartup; return(inst); }
public void CreateCoroutineContext() { CoroutineContext coroutineContext; Assert.Throws <NullReferenceException>(() => { coroutineContext = new CoroutineContext(null); }); coroutineContext = new CoroutineContext(NullCoroutine); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); }
public void StartCoroutineWithTimeout() { const int expectedMilliseconds = 1000; const int tolerance = 2; var sw = new Stopwatch(); var coroutineContext = new CoroutineContext(InfiniteCoroutine); var timeOut = new TimeSpan(0, 0, 0, 0, expectedMilliseconds); sw.Start(); coroutineContext.Start(timeOut); sw.Stop(); Assert.IsTrue(((sw.ElapsedMilliseconds + tolerance) > expectedMilliseconds) || ((sw.ElapsedMilliseconds - tolerance) < expectedMilliseconds)); }
public void StepThroughCoroutine() { var coroutineContext = new CoroutineContext(From1To10); int count = 0; for (int i = 0; coroutineContext.Step(); i++) { count = i + 1; } Assert.AreEqual(count, 10); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsTrue(coroutineContext.IsFinished); }
public void PauseCoroutineFromAnotherThread() { var coroutineContext = new CoroutineContext(InfiniteCoroutine); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); bool isPausedEventTriggered = false; coroutineContext.Paused += paused => isPausedEventTriggered = paused; var pausingThread = new Thread(() => { Thread.Sleep(10); coroutineContext.Pause(); }); pausingThread.Start(); coroutineContext.Start(); Assert.IsTrue(isPausedEventTriggered); Assert.IsTrue(coroutineContext.IsStarted); Assert.IsTrue(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); var testUnpausedThread = new Thread(() => { Thread.Sleep(10); Assert.IsTrue(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); coroutineContext.Stop(); }); testUnpausedThread.Start(); coroutineContext.Unpause(); }
public void StopCoroutineFromAnotherThread() { var coroutineContext = new CoroutineContext(InfiniteCoroutine); var isStoppedEventTriggered = false; coroutineContext.Stopped += () => isStoppedEventTriggered = true; var stoppingThread = new Thread(() => { Thread.Sleep(10); coroutineContext.Stop(); }); stoppingThread.Start(); coroutineContext.Start(); Assert.IsTrue(isStoppedEventTriggered); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); }
public void ResetCoroutineContext() { var coroutineContext = new CoroutineContext(InfiniteCoroutine); bool isResetEventTriggered = false; coroutineContext.Reseted += () => isResetEventTriggered = true; var resetCoroutineContextThread = new Thread(() => { Thread.Sleep(10); coroutineContext.Reset(); }); resetCoroutineContextThread.Start(); coroutineContext.Start(); Assert.IsTrue(isResetEventTriggered); Assert.IsFalse(coroutineContext.IsStarted); Assert.IsFalse(coroutineContext.IsPaused); Assert.IsFalse(coroutineContext.IsFinished); }
private void FixedUpdateCoroutine(CoroutineContext ctx) { ctx.fixedUpdateCnt++; var curr = ctx.Current; if (curr is WaitForFixedUpdate) { if (ctx.initFixFrame) { ctx.initFixFrame = false; } else { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } ctx.initFixFrame = false; }
public static PValue RunStatically(StackContext sctx, PValue[] args) { var carrier = new ContextCarrier(); var corctx = new CoroutineContext(sctx, CoroutineRunStatically(carrier, args)); carrier.StackContext = corctx; return sctx.CreateNativePValue(new Coroutine(corctx)); }
//执行协程的一帧,凡是yield return的位置,至少要等一帧 private void YieldDo(CoroutineContext context) { //主动暂停 if (context.IsSuspended) { return; } //等待事件 if (context.IsWaitForEvent) { return; } context.Yield = null; try { //当前协程执行完毕 if (!context.MoveNext()) { context.IsDisposed = true; context.Callback(); return; } } catch (Exception e) { context.IsDisposed = true; try { OnException?.Invoke(context.Handle, e); } catch (Exception) { //todo 处理异常 } return; } var obj = context.Current; if (obj is IEnumerator enumerator) { obj = new WaitForCoroutine(StartCoroutine(enumerator)); } if (obj is ICoroutineYieldNeedInit yield) { yield.Init(timeGetter); } if (obj is WaitForMilliseconds coroutineTimer) { context.IsInTimer = true; timerManager.StartTimer(now + coroutineTimer.After, () => { context.IsInTimer = false; coroutines.Add(context); }); return; } if (obj is ICoroutineYield coroutineYield) { context.Yield = coroutineYield; return; } throw new CoroutineException($"Cannot use type {obj.GetType()} in yield."); }
private void UpdateCoroutine(CoroutineContext ctx) { ctx.updateCnt++; var curr = ctx.Current; if (curr is WaitForEndOfFrame) { if (ctx.initFrame) { ctx.initFrame = false; } else { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } else if (curr is WaitForSeconds) { if (CheckWaitForSecondsDone(ctx, curr as WaitForSeconds)) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } else if (curr is WaitForSecondsRealtime) { if (Time.realtimeSinceStartup - ctx.frameRealTime >= (curr as WaitForSecondsRealtime).waitTime) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } else if (curr is AsyncOperation) { if ((curr as AsyncOperation).isDone) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } else if (curr is CustomYieldInstruction) { if (!(curr as CustomYieldInstruction).keepWaiting) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } } else if (curr is CoroutineContextHandle) { var node = FindNode((curr as CoroutineContextHandle).Id); if (node == null) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } else if (node.Value.isDone) { _ctxList.Remove(ctx); } } else if (curr is WaitForFixedUpdate) { } else if (curr == null || curr.GetType().IsValueType) { var s = ctx.MoveNext(); if (!s) { _ctxList.Remove(ctx); } } else { Debug.LogError("不支持的yield类型:" + curr.GetType().ToString()); } ctx.initFrame = false; if (!(curr is WaitForSeconds)) { ctx.frameTime = Time.time; } if (!(curr is WaitForSecondsRealtime)) { ctx.frameRealTime = Time.realtimeSinceStartup; } }