Пример #1
0
            //public bool Send(SockTcp tcpObj,bool encode,ref bool life){
            public bool Send(SockTcp tcpObj,bool encode,ILife iLife)
            {
                if(_kindBuf == KindBuf.Memory) {
                    if(encode) {
                        if(-1 == tcpObj.SendUseEncode(_doc))
                            return false;
                    } else {
                        if(-1 == tcpObj.SendNoEncode(_doc))
                            return false;
                    }
                } else {
                    using(var fs = new FileStream(_fileName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)) {
                        using(var br = new BinaryReader(fs)) {
                            fs.Seek(_rangeFrom,SeekOrigin.Begin);
                            var start = _rangeFrom;
                            while(iLife.IsLife()) {
                                long size = _rangeTo - start + 1;
                                if(size > 1048560)
                                    size = 1048560;
                                if(size <= 0)
                                    break;
                                _doc = new byte[size];
                                int len = br.Read(_doc,0,(int)size);
                                if(len <= 0)
                                    break;

                                if(len != size) {
                                    var tmp = new byte[len];
                                    Buffer.BlockCopy(_doc,0,tmp,0,len);
                                    _doc = tmp;
                                }

                                if(encode) {
                                    if(-1 == tcpObj.SendUseEncode(_doc)) {
                                        return false;
                                    }
                                } else {
                                    if(-1 == tcpObj.SendNoEncode(_doc)) {
                                        return false;
                                    }
                                }
                                start += _doc.Length;
                                if(_rangeTo - start <= 0)
                                    break;
                                Thread.Sleep(1);
                            }
                            br.Close();
                        }
                        fs.Close();
                    }
                }
                return true;
            }