Esempio n. 1
0
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <returns></returns>
        public Byte[] Read()
        {
            if (!Active)
            {
                return(null);
            }

            List <NetData> list = new List <NetData>();

            Byte[] buf = new Byte[1024 * 8];
            try
            {
                //如果没数据,等100ms
                if (Socket.Available == 0 && Socket.Connected)
                {
                    Socket.Poll(100000 /* 100ms */, SelectMode.SelectRead);
                    //如果还是没数据,等10秒
                    if (Socket.Available == 0 && Socket.Connected)
                    {
                        Socket.Poll(Session.TimeOut * 1000 /* 10sec */, SelectMode.SelectRead);
                        //如果还是没有数据,退出
                        if (Socket.Available == 0 && Socket.Connected)
                        {
                            return(null);
                        }
                    }
                }

                do
                {
                    //读数据
                    Int32 count = Stream.Read(buf, 0, buf.Length);
                    if (count == 0)
                    {
                        break;
                    }
                    list.Add(new NetData(buf, 0, count));
                } while (Socket.Available != 0 && Socket.Connected);
            }
            catch { }

            if (list == null || list.Count < 1)
            {
                return(null);
            }
            return(NetData.Join(list));
        }