//u3d IEnumerator testU3d(int count) { var w1 = new WWW("http://www.baidu.com"); yield return(w1); if (!w1.isDone) { CothreadHub.Log("[testU3d] www error"); } else { CothreadHub.Log("[testU3d] www ok: size:" + w1.text.Length.ToString()); } var result = new CothreadResult <bool>(); var u1 = UnityHub.Active.StartCoroutine(_u3dCoroutine1(result)); yield return(u1); if (result.Result.Equals(true)) { CothreadHub.Log("[testU3d]StartCoroutine ok"); } else { CothreadHub.Log("[testU3d]StartCoroutine error"); } }
IEnumerator testThread() { var sw = new Stopwatch(); sw.Start(); List <int> rs = new List <int>(); var c = 1000; yield return(hub.StartThread(delegate { Thread.Sleep(2000); for (int i = 0; i < c; i++) { rs.Add(i); } })); sw.Stop(); var t = sw.ElapsedMilliseconds; if (rs.Count == c) { CothreadHub.Log("[testThread] OK! pass time:" + t.ToString()); } else { CothreadHub.Log("[testThread]ERROR! pass time:" + t.ToString()); } }
IEnumerator testTimeout2() { var timeout = CothreadTimeout.NewWithStart(1005); yield return(hub.Sleep(1010)); try { timeout.Cancel(true); CothreadHub.Log("[testTimeout2] CothreadTimeoutError error"); } catch (CothreadTimeoutError) { CothreadHub.Log("[testTimeout2] CothreadTimeoutError ok"); } }
IEnumerator testCothread() { var ev = new CothreadEvent(); yield return(hub.StartCoroutine(_testEvent2(ev))); if (ev.Get().Equals(1)) { CothreadHub.Log("[testCothread] ok"); } else { CothreadHub.Log("[testCothread] error"); } }
IEnumerator _testEvent1(int i, CothreadEvent ev, CothreadEvent nev) { yield return(ev.Wait(0)); var result = (string)ev.Get(""); var msg = string.Format("{0} {1} ->", result, i.ToString()); if (nev != null) { nev.Set(msg); } else { CothreadHub.Log("result: " + msg); } }
IEnumerator testJoin() { var timeout = CothreadTimeout.NewWithStart(1); var ct1 = hub.StartCoroutine(_testJoin()); yield return(ct1.Join()); if (!timeout.Timeout) { CothreadHub.Log("[testJoin] error"); } else { CothreadHub.Log("[testJoin] ok"); } }
IEnumerator testSock1(int i) { string stri = i.ToString(); CothreadHub.Log("testSock:" + stri); CothreadTimeout timeout = CothreadTimeout.NewWithStart(5); CothreadSocket sock = new CothreadSocket(); yield return(sock.Connect("192.168.0.210", 81)); //web server //yield return sock.Connect("www.baidu.com", 80); //www.baidu.com //yield return sock.Connect("115.239.210.27", 80); //www.baidu.com //yield return hub.Sleep(rt); //CothreadHub.Log(stri + "-socket connected:" + sock.Connected); yield return(sock.SendString("GET / HTTP/1.0\n\n")); //CothreadHub.Log(stri + "-Send ok"); var recvData = new CothreadSocketRecvData(2048); var result = new CothreadResult <string>(); yield return(hub.Sleep(1000)); yield return(sock.RecvString(recvData, result)); string s1 = (string)result.Result; if (string.IsNullOrEmpty(s1)) { CothreadHub.Log("testSock(" + stri + ") error" + "-passTime:" + timeout.PassTime.ToString()); } else { CothreadHub.Log("testSock(" + stri + ") ok:" + s1.Length.ToString() + " data:" + s1.Substring(0, Math.Min(500, s1.Length))); } try { timeout.Cancel(true); } catch (CothreadTimeoutError) { CothreadHub.Log(stri + "-testSock timeout"); } finally { sock.Close(); } }
public IEnumerator Send(byte[] data) { if (!Connected) { yield break; } var hub = CothreadHub.Instance; var rs = sock.BeginSend(data, 0, data.Length, SocketFlags.None, hub.cb, hub.current); //hub.Print("$(GetHashCode().ToString()) sock.BeginSend .....") if (!rs.IsCompleted) { yield return(rs); } //hub.Print("$(GetHashCode().ToString()) sock.EndSending .....") int l; if (hub.CurrentCothread.IsTimeout(false)) { Close(); CothreadHub.Log(string.Format("[AsyncSocket.send]:{0} Send Timeout", GetHashCode())); yield break; } else { try { l = sock.EndSend(rs); //hub.Print("$(GetHashCode().ToString()) sock.EndSend") } catch (SocketException) { Close(); CothreadHub.Log(string.Format("[AsyncSocket.send]:{0} sock.EndSend SocketException", GetHashCode())); yield break; } } if (l != data.Length) { CothreadHub.Log(string.Format("[AsyncSocket.send]:size({0}) != len(data)({1})", l, data.Length)); } }
IEnumerator testU3dStartCothread() { var ev = new CothreadEvent(); var obj = GameObject.CreatePrimitive(PrimitiveType.Cube); var th = hub.StartCothread(_testEvent2(ev), obj); yield return(hub.Sleep(10)); GameObject.Destroy(obj); yield return(hub.Sleep(100)); if (th.Closed && ev.Get() == null) { CothreadHub.Log("[testU3dStartCothread] ok"); } else { CothreadHub.Log("[testU3dStartCothread] error"); } }
IEnumerator testEvent2() { var ev = new CothreadEvent(); hub.StartCoroutine(_testEvent2(ev)); yield return(ev.Wait()); if (!ev.Get().Equals(1)) { CothreadHub.Log("[testEvent2] error"); } else { CothreadHub.Log("[testEvent2] ok"); } ev.Clear(); hub.StartCoroutine(_testEvent2(ev)); yield return(ev); AssertDebug.Assert(ev.Get().Equals(1), "[testEvent2] yield event error!"); }
public void Close() { if (Closed) { return; } Closed = true; if (_ev != null) { try { _ev.Set(this); } catch (Exception err) { CothreadHub.Log(err); } _ev = null; } CothreadHub.Instance.delCothread(IE); }
static IEnumerable callIEnumerable(IEnumerator ie, Cothread th) { IEnumerable rss; bool ok; var hub = CothreadHub.Instance; while (true) { //check thread is closed? if (!hub.threads.ContainsKey(th.IE)) { yield break; } ok = false; try { ok = ie.MoveNext(); } catch (Exception err) { ok = false; CothreadHub.Log(err); } if (!ok) { yield break; } rss = null; if (ie.Current is CothreadEvent) { } else if (ie.Current is IEnumerable) { rss = callIEnumerable(ie.Current as IEnumerable, th); } else if (ie.Current is IEnumerator) { rss = callIEnumerable(ie.Current as IEnumerator, th); } else if (GlobalAsyncHandle != null) { var o1 = GlobalAsyncHandle(ie); if (o1 is IEnumerable) { rss = o1 as IEnumerable; } } if (rss != null) { foreach (var i in rss) { yield return(i); } } else { yield return(ie.Current); } } }