Esempio n. 1
0
 // UpFile-添加进度
 private void udpAddUpProgress(UnUdpEntity up)
 {
     UnAttrPgs pgs = new UnAttrPgs();
     pgs.pid = _pid;
     if (up.UpCount < upp.TotalPacks - 1)
     {
         pgs.length = up.UpCount * upp.SubSize;
     }
     else
     {
         if (up.UpCount == upp.TotalPacks)
         {
             prgMax = 0;
             pgss.Clear();
         }
         pgs.length = upp.TotalSize;
     }
     pgs.totalLength = upp.TotalSize;
     float per = pgs.percentage();
     if (per > prgMax)
     {
         if (pgss.Count < 20)
         {
             prgMax = per;
             pgss.Add(pgs);
         }
         else
         {
             if ((int)per > (int)prgMax)
             {
                 prgMax = per;
                 pgss.Add(pgs);
                 pgss.RemoveAt(0);
             }
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 上传
        /// </summary>
        private void up()
        {
            HttpWebRequest request = UnHttpHelp.createPost(_url, _timeOut, null, _eve.text());
            UnFileInfo _f = new UnFileInfo(_fileName);
            request.Headers["md5"] = _f.md5;
            request.Headers["extens"] = _f.extens;

            using (FileStream fs = new FileStream(_fileName, FileMode.Open))
            {
                request.ContentLength = fs.Length;
                BinaryReader r = new BinaryReader(fs);
                // 每次读取大小
                int bufferLength = 1024;
                byte[] buffer = new byte[bufferLength];

                // 开始发送
                long fSize = fs.Length;
                long dSize = 0;
                int size = r.Read(buffer, 0, bufferLength);
                using (Stream post = request.GetRequestStream())
                {
                    while (size > 0)
                    {
                        dSize += size;
                        post.Write(buffer, 0, size);
                        size = r.Read(buffer, 0, bufferLength);

                        UnAttrPgs pgs = new UnAttrPgs();
                        pgs.pid = _pid;
                        pgs.totalLength = fSize;
                        pgs.length = dSize;
                        this.tryProgress(pgs);
                    }
                }
            }
            // 开始传输
            try
            {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    byte[] back = UnHttpHelp.getResponseData(response);
                    UnAttrRst rst = new UnAttrRst();
                    rst.pid = _pid;
                    rst.code = 1;
                    rst.msg = "通讯成功";
                    rst.back = en.GetString(back);
                    rst.data = rst.back;
                    bool _bool = this.trySuccess(rst);
                }
            }
            catch(Exception e)
            {
                UnAttrRst rst = new UnAttrRst();
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 尝试回调进度监听
 /// </summary>
 /// <param name="pgs"></param>
 private void tryProgress(UnAttrPgs pgs)
 {
     if (transfer != null)
     {
         transfer.progress(pgs);
     }
 }
Esempio n. 4
0
        /// <summary>
        ///  发送
        /// </summary>
        /// <returns>返回结果</returns>
        private UnAttrRst send()
        {
            UnAttrRst rst = new UnAttrRst();
            UnAttrPgs pgs = new UnAttrPgs();
            try
            {
                byte[] send = en.GetBytes(_msg);
                // md5加密文件名
                string md5 = (_msg + _url).md5Hash();
                string path = UnFileEvent.caches.fullPath() + md5 + ".txt";
                // 缓存文件
                UnFileInfo f = new UnFileInfo(path, null, null, _cacheTimeOut);
                if (_cacheTimeOut > 0 && f.exists && !f.isLate)
                {
                    // 进度
                    pgs.pid = _pid;
                    pgs.totalLength = f.length;
                    pgs.length = f.length;
                    this.tryProgress(pgs);

                    // 完成
                    rst.pid = _pid;
                    rst.code = 2;
                    rst.msg = "返回缓存";
                    using (FileStream inf_fs = new FileStream(f.fullName, FileMode.Open))
                    {
                        byte[] data = new byte[inf_fs.Length];
                        inf_fs.Read(data, 0, data.Length);
                        rst.back = en.GetString(data);
                        rst.data = rst.back;
                    }
                    this.trySuccess(rst);
                    return rst;
                }

                // 设置参数
                HttpWebRequest request = UnHttpHelp.createPost(_url, _timeOut, "text/xml", _eve.text(), _cerPath, _cerPass);
                request.ContentLength = send.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(send, 0, send.Length);
                }

                // 下载数据
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    byte[] back = UnHttpHelp.getResponseData(response);

                    pgs.pid = _pid;
                    pgs.totalLength = back.Length;
                    pgs.length = back.Length;
                    this.tryProgress(pgs);

                    rst.pid = _pid;
                    rst.code = 1;
                    rst.msg = "提交成功";
                    rst.back = en.GetString(back);
                    rst.data = rst.back;
                    bool b = this.trySuccess(rst);
                    if (_cacheTimeOut > 0 && b)
                    {
                        UnFile.createDirectory(UnFileEvent.caches);
                        using (FileStream inf_fs = new FileStream(f.fullName, FileMode.Create))
                        {
                            inf_fs.Seek(0, SeekOrigin.Begin);
                            inf_fs.Write(back, 0, (int)back.Length);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
            return rst;
        }
Esempio n. 5
0
        /// <summary>
        /// 下载
        /// </summary>
        private void down()
        {
            UnAttrPgs pgs = new UnAttrPgs();
            UnAttrRst rst = new UnAttrRst();
            try
            {
                string kzm = _url.Substring(_url.LastIndexOf("."));
                if (kzm.Length < 2)
                {
                    kzm = ".no";
                }
                // md5加密文件名
                string md5 = _url.md5Hash();
                string path = UnFileEvent.caches.fullPath() + md5 + kzm;
                // 缓存文件
                UnFileInfo f = new UnFileInfo(path, null, null, _cacheTimeOut);
                if (_cacheTimeOut > 0 && f.exists && !f.isLate)
                {
                    pgs.pid = _pid;
                    pgs.totalLength = f.length;
                    pgs.length = f.length;
                    this.tryProgress(pgs);

                    rst.pid = _pid;
                    rst.code = 2;
                    rst.msg = "返回缓存";
                    rst.back = path;
                    rst.data = f;
                    this.trySuccess(rst);
                    return;
                }

                // 下载数据
                using (HttpWebResponse response = UnHttpHelp.creageGet(_url, _timeOut).GetResponse() as HttpWebResponse)
                {
                    using (Stream rsps = response.GetResponseStream())
                    {
                        UnFile.createDirectory(UnFileEvent.caches);
                        UnFile.createDirectory(UnFileEvent.tmp);
                        //创建本地文件写入流
                        using (Stream stream = new FileStream(f.fullNameTmp, FileMode.Create))
                        {
                            long fSize = response.ContentLength;
                            long dSize = 0;
                            byte[] buff = new byte[1024];
                            int size = rsps.Read(buff, 0, buff.Length);
                            while (size > 0)
                            {
                                dSize += size;
                                stream.Write(buff, 0, size);
                                size = rsps.Read(buff, 0, buff.Length);

                                pgs.pid = _pid;
                                pgs.totalLength = fSize;
                                pgs.length = dSize;
                                this.tryProgress(pgs);
                            }
                        }
                    }
                }

                // 转正式文件
                if (f.exists)
                {
                    f.baseInfo.Delete();
                }
                File.Move(f.fullNameTmp, f.fullName);
                f = new UnFileInfo(f.fullName);
                rst.pid = _pid;
                rst.code = 1;
                rst.msg = "下载完成";
                rst.data = f;
                bool b = this.trySuccess(rst);
                // 不缓存
                if (!b)
                {
                    f.baseInfo.Delete();
                }
            }
            catch (Exception e)
            {
                rst.pid = _pid;
                rst.code = -1;
                rst.msg = e.ToString();
                this.tryError(rst);
            }
        }
Esempio n. 6
0
 void UnIntTransfer.progress(UnAttrPgs pgs)
 {
     //Console.WriteLine(rst.back);
 }