Ejemplo n.º 1
0
        public IEnumerator Recv(CothreadSocketRecvData recvData)
        {
            if (!Connected)
            {
                yield break;
            }
            recvData.Clear();
            var hub = CothreadHub.Instance;
            var rs  = sock.BeginReceive(recvData.Data, 0, recvData.Data.Length,
                                        SocketFlags.None, hub.cb, hub.current);

            if (!rs.IsCompleted)
            {
                yield return(rs);
            }
            if (hub.CurrentCothread.IsTimeout(false))
            {
                Close();
            }
            else
            {
                try {
                    recvData.Length = sock.EndReceive(rs);
                } catch (SocketException) {
                    Close();
                }
            }
        }
Ejemplo n.º 2
0
        //utf8 encoding
        public IEnumerator RecvString(CothreadSocketRecvData recvData, CothreadResult <string> result)
        {
            if (!Connected)
            {
                yield break;
            }
            yield return(Recv(recvData));

            result.Result = System.Text.Encoding.UTF8.GetString(recvData.Data, 0, recvData.Length);
        }
Ejemplo n.º 3
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();
            }
        }