Пример #1
0
        //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");
            }
        }
Пример #2
0
        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());
            }
        }
Пример #3
0
        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");
            }
        }
Пример #4
0
        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");
            }
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
        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");
            }
        }
Пример #7
0
        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();
            }
        }
Пример #8
0
        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));
            }
        }
Пример #9
0
        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");
            }
        }
Пример #10
0
        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!");
        }
Пример #11
0
 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);
 }
Пример #12
0
        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);
                }
            }
        }