Exemple #1
0
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="nationCode">区号(NULL则默认86)</param>
        /// <param name="phoneNumbers">群发电话号码</param>
        /// <param name="content">发送内容(必须按模板格式)</param>
        /// <returns>code:1=全部成功,0=部分成功,-1=全部失败</returns>
        public UnAttrRst sendMsg(string nationCode, List<string> phoneNumbers, string content)
        {
            UnAttrRst rst = new UnAttrRst();
            string retString = sendMsgs(nationCode, phoneNumbers, content);
            var dics = UnXMMPJson.jsonToDic(retString, null);
            if (dics != null)
            {
                int num = 0;
                foreach (var dic in dics)
                {
                    switch (dic["result"].ToString())
                    {
                        case "0":
                            num++;
                            break;
                        default:
                            break;
                    }
                }
                if (num > 0)
                {
                    if (num == dics.Count)
                    {
                        rst.code = 1;
                        rst.msg = "OK";
                    }
                    else
                    {
                        rst.code = 0;
                        rst.msg = retString;
                    }
                }
                else
                {
                    rst.code = -1;
                    rst.msg = retString;
                }

            }
            return rst;
        }
Exemple #2
0
 // UpFile-发送线程
 private void udpUpFileSendHandle()
 {
     List<UnUdpEntity> list = new List<UnUdpEntity>();
     while (!_isFinish)
     {
         if (!_isPause)
         {
             // 超时判断
             if (UnDate.ticksMSec() - timeOutTicks < timeOut)
             {
                 list = sendIntervals.FindAll(t => !t.isSend);
                 if (list.Count == 0)
                 {
                     if (UnDate.ticksMSec() - traTicks > resendTime)
                     {
                         // 如果首次查询已返回,则累加,否则一直默认为首次查询
                         if (queryState > 1)
                         {
                             queryState++;
                         }
                         udpSendPack(upp.getUpFileQueryPackage(queryState));
                         traTicks = UnDate.ticksMSec();
                     }
                     else
                     {
                         Thread.Sleep(sleepTime);
                     }
                 }
                 else
                 {
                     foreach (var uup in list)
                     {
                         udpSendPack(upp.getUpFileSendPackage(uup.PackNo));
                         //Thread.Sleep(1);
                     }
                     Thread.Sleep(sleepTime);
                 }
             }
             else
             {
                 _isPause = true;
                 pgss.Clear();
                 if (intTransfer != null)
                 {
                     UnAttrRst rst = new UnAttrRst();
                     rst.pid = _pid;
                     rst.code = -1;
                     rst.msg = "发送超时!";
                     rst.back = "";
                     intTransfer.error(rst);
                 }
             }
         }
         else
         {
             Thread.Sleep(1000);
         }
     }
 }
Exemple #3
0
        // DonwFile-返回包处理
        private void udpProDownFileBack(UnUdpEntity etBack)
        {
            // 刷新超时时间戳
            timeOutTicks = UnDate.ticksMSec();
            switch (etBack.Event.getUnUdpEveEnum())
            {
                case UnUdpEveEnum.downFileQueryBack:
                    sleepTime = Convert.ToInt32(UnDate.ticksMSec() - traTicks) + 1;
                    if (sleepTime > 10000)
                    {
                        sleepTime = 2000;
                    }
                    // 初始化
                    FileInfo cofFi = new FileInfo(UnUdpHelp.getDownFileTmpConfigPath(etBack.HashCode));
                    FileInfo tmpFi = new FileInfo(UnUdpHelp.getDownFileReceivePath(etBack.HashCode));
                    if (!cofFi.Exists)
                    {
                        DirectoryInfo di = new DirectoryInfo(UnUdpHelp.getDownFileTmpDirectory(etBack.HashCode));
                        if (!di.Exists)
                        {
                            di.Create();
                        }
                        cofFi.Create().Dispose();
                        tmpFi.Create().Dispose();
                        downQuery = etBack;
                    }

                    // 第一次初始化
                    if (downQuery.TotalPacks == 0)
                    {
                        // 获得配置文件
                        using (FileStream fs = cofFi.OpenRead())
                        {
                            byte[] b = new byte[fs.Length];
                            fs.Read(b, 0, b.Length);
                            downQuery = UnUdpHelp.analyzePackage(b);
                        }
                    }
                    else
                    {
                        downQuery.IntMin = etBack.IntMin;
                        downQuery.IntMax = etBack.IntMax;
                    }

                    // 写入配置文件
                    using (FileStream fs = new FileStream(UnUdpHelp.getDownFileTmpConfigPath(etBack.HashCode), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        byte[] prgBackBts = UnUdpHelp.assemblePackage(downQuery);
                        fs.SetLength(0);
                        fs.Seek(0, SeekOrigin.Begin);
                        fs.Write(prgBackBts, 0, prgBackBts.Length);
                    }

                    // 建立窗口
                    downIntervals = new List<UnUdpEntity>();
                    for (long i = downQuery.IntMin; i <= downQuery.IntMax; i++)
                    {
                        UnUdpEntity uup = new UnUdpEntity();
                        uup.PackNo = i;
                        upp.isSend = false;
                        downIntervals.Add(uup);
                    }
                    isdbPer = true;
                    break;
                case UnUdpEveEnum.downFileBack:
                    UnUdpEntity up = downIntervals.Find(t => t.PackNo == etBack.PackNo && t.isSend == false);
                    string tmpPath = UnUdpHelp.getDownFileReceivePath(downQuery.HashCode);
                    if (up != null)
                    {
                        // 写入数据
                        using (FileStream fs = new FileStream(tmpPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            fs.Lock(etBack.PackOffset, etBack.PackData.Length);
                            fs.Seek(etBack.PackOffset, SeekOrigin.Begin);
                            fs.Write(etBack.PackData, 0, etBack.PackData.Length);
                            fs.Unlock(etBack.PackOffset, etBack.PackData.Length);
                            downQuery.UpCount++;
                            up.isSend = true;
                        }
                        udpAddDownProgress(downQuery);
                    }
                    // 下载完成
                    if (downQuery.UpCount == downQuery.TotalPacks)
                    {
                        _isFinish = true;
                        // 转正式文件
                        string newPath = UnUdpHelp.getDownFileSavePath(downQuery.HashCode, downQuery.Extent);
                        UnFile.move(tmpPath, newPath, true);
                        // 删除临时文件夹
                        File.Delete(UnUdpHelp.getDownFileTmpConfigPath(downQuery.HashCode));
                        Directory.Delete(UnUdpHelp.getDownFileTmpDirectory(downQuery.HashCode));
                        if (intTransfer != null)
                        {
                            UnAttrRst rst = new UnAttrRst();
                            rst.pid = _pid;
                            rst.code = 1;
                            rst.msg = "下载完成";
                            rst.back = newPath;
                            intTransfer.success(rst);
                        }
                    }
                    break;
            }
        }
Exemple #4
0
 // SendMsg-发送线程
 private void udpMsgSendHandle()
 {
     while (!_isFinish)
     {
         if (!_isPause)
         {
             // 超时判断
             if (UnDate.ticksMSec() - timeOutTicks < timeOut)
             {
                 if (UnDate.ticksMSec() - traTicks > resendTime)
                 {
                     udpSendPack(upp.getMsgSendPackage());
                     traTicks = UnDate.ticksMSec();
                 }
                 else
                 {
                     Thread.Sleep(1000);
                 }
             }
             else
             {
                 _isFinish = true;
                 if (intTransfer != null)
                 {
                     UnAttrRst rst = new UnAttrRst();
                     rst.pid = _pid;
                     rst.code = -1;
                     rst.msg = "发送超时!";
                     rst.back = "";
                     intTransfer.error(rst);
                 }
             }
         }
         else
         {
             Thread.Sleep(1000);
         }
     }
 }
Exemple #5
0
        // DownFile-发送线程
        private void udpDownFileSendHandle()
        {
            List<UnUdpEntity> list = new List<UnUdpEntity>();
            while (!_isFinish)
            {
                if (!_isPause)
                {
                    // 超时判断
                    if (UnDate.ticksMSec() - timeOutTicks < timeOut)
                    {
                        list = downIntervals.FindAll(t => !t.isSend);
                        if (list.Count == 0)
                        {
                            // 如果接收窗口完成,设定开始新窗口的包编号
                            if (downIntervals.Count > 0)
                            {
                                downQuery.PackNo = downQuery.UpCount;
                            }
                            if (UnDate.ticksMSec() - traTicks > resendTime)
                            {
                                udpSendPack(upp.getDownFileQueryPackage(downQuery.PackNo));
                                traTicks = UnDate.ticksMSec();
                                Console.WriteLine("新窗口:" + downQuery.PackNo);
                            }
                            else
                            {
                                Thread.Sleep(sleepTime);
                            }
                        }
                        else
                        {
                            if (isdbPer && list.Count < downIntervals.Count)
                            {
                                isdbPer = false;
                                dbPer = (float)list.Count / (float)downIntervals.Count;
                                if (dbPer > dbPerMax)
                                {
                                    downSleep++;
                                    if (downSleep > downSleepMax)
                                    {
                                        downSleep = downSleepMax;
                                    }
                                }
                                else if (dbPer < dbPerMin)
                                {
                                    downSleep--;
                                    if (downSleep < 1)
                                    {
                                        downSleep = downSleepMin;
                                    }
                                }
                                //Console.WriteLine("掉包:" + list.Count + "/" + downIntervals.Count + "/" + sleepTime + "/" + dbPer + "/" + downSleep);
                            }

                            foreach (var uup in list)
                            {
                                byte[] send = upp.getDownFileSendPackage(uup.PackNo);
                                udpSendPack(send);
                                Thread.Sleep(downSleep);
                            }
                            Thread.Sleep(sleepTime);
                        }
                    }
                    else
                    {
                        _isPause = true;
                        pgss.Clear();
                        if (intTransfer != null)
                        {
                            UnAttrRst rst = new UnAttrRst();
                            rst.pid = _pid;
                            rst.code = -1;
                            rst.msg = "下载超时!";
                            rst.back = "";
                            intTransfer.error(rst);
                        }
                    }
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        // UpFile-处理数据包
        private byte[] proUpFilePackage(EndPoint point, UnUdpEntity entity)
        {
            byte[] data = null;
            // 配置文件
            UnUdpEntity cofUPP = upFileConfigs.Find(t => t.HashCode == entity.HashCode);
            if (cofUPP == null)
            {
                cofUPP = new UnUdpEntity();
                cofUPP.HashCode = entity.HashCode;
                cofUPP.TotalPacks = entity.TotalPacks;
                cofUPP.TotalSize = entity.TotalSize;
                cofUPP.Extent = entity.Extent;
                cofUPP.SubSize = entity.SubSize;
                FileInfo cofFi = new FileInfo(UnUdpHelp.getUpFileTmpConfigPath(cofUPP.HashCode));
                FileInfo tmpFi = new FileInfo(UnUdpHelp.getUpFileReceivePath(cofUPP.HashCode));
                if (!cofFi.Exists)
                {
                    long[] ls = UnUdpHelp.waitUps(1, entity.TotalPacks, interval);
                    cofUPP.IntMin = ls[0];
                    cofUPP.IntMax = ls[1];
                    cofUPP.isReceived = new List<long>();
                    DirectoryInfo di = new DirectoryInfo(UnUdpHelp.getUpFileTmpDirectory(cofUPP.HashCode));
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                    // 创建配置文件
                    using (FileStream fs = cofFi.Create())
                    {
                        byte[] prgBackBts = UnUdpHelp.assemblePackage(cofUPP);
                        fs.Seek(0, SeekOrigin.Begin);
                        fs.Write(prgBackBts, 0, prgBackBts.Length);
                    }
                    // 创建临时文件
                    tmpFi.Create().Dispose();
                }
                else
                {
                    // 获得配置文件
                    using (FileStream fs = cofFi.OpenRead())
                    {
                        byte[] b = new byte[fs.Length];
                        fs.Read(b, 0, b.Length);
                        cofUPP = UnUdpHelp.analyzePackage(b);
                        if (cofUPP != null)
                        {
                            if (cofUPP.IntMin > 0)
                            {
                                cofUPP.UpCount = cofUPP.IntMin - 1;
                            }
                            else
                            {
                                cofUPP.UpCount = cofUPP.TotalPacks;
                            }
                        }
                    }
                }
                // 配置文件出错则删除重置
                if (cofUPP == null)
                {
                    cofFi.Delete();
                    return null;
                }
                ss.addUpTaskNum(1);
                upFileConfigs.Add(cofUPP);
            }
            // 唤醒时间
            cofUPP.WakeTimeStamp = UnDate.ticksSec();
            // 返回对象
            UnUdpEntity back = new UnUdpEntity();
            UnAttrRst rst = new UnAttrRst();

            switch (entity.Event.getUnUdpEveEnum())
            {
                case UnUdpEveEnum.upFileQuery:
                    back.IntMin = cofUPP.IntMin;
                    back.IntMax = cofUPP.IntMax;
                    back.TotalPacks = cofUPP.TotalPacks;
                    back.TotalSize = cofUPP.TotalSize;
                    back.UpCount = cofUPP.UpCount;
                    back.SubSize = cofUPP.SubSize;
                    back.Event = UnUdpEveEnum.upFileQueryBack.getText();

                    // 是否秒传
                    bool isMC = false;
                    // 开始上传
                    if (entity.State == 1)
                    {
                        if (intServer != null)
                        {
                            rst = intServer.upStart(point, entity);
                            if (rst != null)
                            {
                                cofUPP.State = rst.code;
                                back.State = cofUPP.State;
                                switch (cofUPP.State)
                                {
                                    case 1:// 传输完成
                                        cofUPP.UpCount = back.TotalPacks;
                                        back.UpCount = cofUPP.UpCount;
                                        cofUPP.WakeTimeStamp -=30;
                                        isMC = true;
                                        break;
                                    case -1:
                                    case -2:
                                        cofUPP.WakeTimeStamp -= 30;
                                        break;
                                }
                            }
                        }
                        // 默认处理
                        if (rst == null)
                        {
                            rst = new UnAttrRst();
                            rst.code = 0;
                            rst.msg = "传输中";
                        }
                    }

                    // 文件不是秒传,上传成功处理临时文件
                    if (!isMC && cofUPP.UpCount == cofUPP.TotalPacks)
                    {
                        cofUPP.State = 1;
                        back.State = cofUPP.State;
                        // 临时路径
                        string oldPath = UnUdpHelp.getUpFileReceivePath(cofUPP.HashCode);
                        if (intServer != null)
                        {
                            entity.TmpPath = oldPath;
                            rst = intServer.upSuccess(point, entity);
                            if (rst != null)
                            {
                                rst.code = 1;
                                rst.msg = "传输完成";
                            }
                        }
                        // 默认处理
                        if (rst == null)
                        {
                            // 转正式文件
                            string newPath = UnUdpHelp.getUpFileSavePath(cofUPP.HashCode, cofUPP.Extent);
                            UnFile.move(oldPath, newPath, true);
                            rst = new UnAttrRst();
                            rst.code = 1;
                            rst.msg = "上传成功";
                            rst.back = newPath;
                        }
                        cofUPP.WakeTimeStamp -= 25;
                    }

                    back.PackData = UnInit.getEncoding().GetBytes(UnXMMPXml.tToXml(typeof(UnAttrRst), rst));
                    data = UnUdpHelp.assemblePackage(back);
                    return data;
                case UnUdpEveEnum.upFilePackage:
                    // 在区间内且上传未完成
                    if (entity.PackNo >= cofUPP.IntMin && entity.PackNo <= cofUPP.IntMax && cofUPP.UpCount < cofUPP.TotalPacks)
                    {
                        // 是否已传
                        bool isHave = cofUPP.isReceived.Exists(t => t == entity.PackNo);
                        if (!isHave)
                        {
                            // 写入数据
                            using (FileStream fs = new FileStream(UnUdpHelp.getUpFileReceivePath(cofUPP.HashCode), FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                fs.Lock(entity.PackOffset, entity.PackData.Length);
                                fs.Seek(entity.PackOffset, SeekOrigin.Begin);
                                fs.Write(entity.PackData, 0, entity.PackData.Length);
                                cofUPP.UpCount++;
                                cofUPP.isReceived.Add(entity.PackNo);

                                // 修改配置
                                if (cofUPP.UpCount == cofUPP.IntMax)
                                {
                                    long[] ls = UnUdpHelp.waitUps(cofUPP.IntMax + 1, entity.TotalPacks, interval);
                                    cofUPP.IntMin = ls[0];
                                    cofUPP.IntMax = ls[1];
                                    cofUPP.isReceived = new List<long>();
                                }
                                fs.Unlock(entity.PackOffset, entity.PackData.Length);
                            }
                        }
                    }

                    back.Event = UnUdpEveEnum.upFilePackageBack.getText();
                    back.PackNo = entity.PackNo;
                    back.UpCount = cofUPP.UpCount;
                    data = UnUdpHelp.assemblePackage(back);
                    return data;
            }
            return data;
        }
Exemple #8
0
            UnAttrRst UnIntUdpServer.upStart(EndPoint point, UnUdpEntity entity)
            {
                UnAttrRst rst = new UnAttrRst();
                BackInfo bi = new BackInfo();

                // 类型检测
                if (fileTypes.IndexOf(".*") < 0 && ("," + fileTypes.ToLower() + ",").IndexOf("," + entity.Extent.ToLower() + ",") < 0)
                {
                    bi.ReturnCode = -1;
                    bi.ReturnMsg = "只允许: " + fileTypes + " 格式";

                    rst.code = -1;
                    rst.msg = "文件类型错误";
                    rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);
                    return rst;
                }
                // 大小检测
                if (entity.TotalSize > fileSize)
                {
                    bi.ReturnCode = -2;
                    bi.ReturnMsg = "文件超过: " + fileSize + " 字节";

                    rst.code = -2;
                    rst.msg = "文件大小错误";
                    rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);
                    return rst;
                }
                // 查询文件是否已存在
                UnFileInfo fi = UnFile.findFromDir(Img1_Path, entity.HashCode);
                if (fi != null)
                {
                    bi.ReturnCode = 2;
                    bi.ReturnMsg = "极速秒传";
                    bi.MD5 = entity.HashCode;
                    bi.Url = Img1_Domain + fi.fullName.Replace(Img1_Path, "");

                    rst.code = 1;
                    rst.msg = "传输完成";
                    rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);

                    Console.WriteLine("***************极速秒传***************");
                    Console.WriteLine("【路径】" + fi.fullName);
                    Console.WriteLine("【URL】" + bi.Url);

                    return rst;
                }
                return null;
            }
Exemple #9
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;
        }
Exemple #10
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);
            }
        }
Exemple #11
0
        // 获取文件
        public void getFile(HttpContext context)
        {
            UnAttrRst rst = new UnAttrRst();
            string type = context.Request["type"];
            long allowSize = 0;
            string allowType = "";
            switch (type)
            {
                case "Image":
                    allowSize = 1024 * 1024;
                    allowType = ".jpg,.jpeg,.png,.gif,.bmp";
                    break;
                case "File":
                    allowSize = 1024 * 1024;
                    allowType = ".txt,.rar,.zip";
                    break;
                default:
                    break;
            }
            HttpPostedFile hpf = context.Request.Files[0];
            if (hpf.ContentLength > 0)
            {
                if (hpf.ContentLength <= allowSize)
                {
                    string ext = System.IO.Path.GetExtension(hpf.FileName).ToLower();
                    var imp = UnFileEvent.important;
                    var name = UnStrRan.getRandom() + ext;
                    string UpPath = imp.fullPath() + "/" + name;
                    DirectoryInfo di = new DirectoryInfo(UnFileEvent.important.fullPath());
                    if (di.Exists == false)
                    {
                        di.Create();
                    }
                    hpf.SaveAs(UpPath);

                    UnFileInfo fi = new UnFileInfo(UpPath, null, allowType, null);
                    if (!fi.isWrongExtens && !fi.isWrongType)
                    {
                        rst.pid = 0;
                        rst.code = 1;
                        rst.msg = "上传成功";

                        BackInfo bi = new BackInfo();
                        switch (type)
                        {
                            case "Image":
                                bi.ReturnCode = 1;
                                bi.ReturnMsg = "传输完成";
                                bi.Url = ImgPath + imp.floder() + "/" + name;
                                break;
                            case "File":
                                bi.ReturnCode = 2;
                                bi.ReturnMsg = "传输完成";
                                bi.UNCode = "UNCODE//:" + UnEncDES.encrypt(UpPath);
                                break;
                            default:
                                break;
                        }
                        rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);
                    }
                    else
                    {
                        File.Delete(fi.fullName);
                        rst.pid = 0;
                        rst.code = -3;
                        rst.msg = "允许类型:" + allowType;
                    }
                }
                else
                {
                    rst.pid = 0;
                    rst.code = -2;
                    rst.msg = "不能超过 " + allowSize / 1024 + "KB";
                }
            }
            else
            {
                rst.pid = 0;
                rst.code = -1;
                rst.msg = "文件不存在";
                rst.back = "";
            }

            context.Response.ContentType = "text/xml";
            var xml = UnXMMPXml.tToXml(typeof(UnAttrRst), rst);
            context.Response.Write(xml);
        }
Exemple #12
0
 bool UnIntTransfer.success(UnAttrRst rst)
 {
     Console.WriteLine(rst.back + "##");
     return true;
 }
Exemple #13
0
 void UnIntTransfer.error(UnAttrRst rst)
 {
     Console.WriteLine(rst.msg + "##");
 }
Exemple #14
0
 UnAttrRst UnIntUdpServer.msgReceived(EndPoint point, UnUdpEntity entity)
 {
     UnAttrRst rst = new UnAttrRst();
     return new UnAttrRst();
 }
Exemple #15
0
 /// <summary>
 /// 尝试错误回调
 /// </summary>
 /// <param name="rst"></param>
 private void tryError(UnAttrRst rst)
 {
     if (transfer != null)
     {
         transfer.error(rst);
     }
 }
Exemple #16
0
 UnAttrRst UnIntUdpServer.proPackage(EndPoint point, UnUdpEntity entity)
 {
     UnAttrRst rst = new UnAttrRst();
     if (entity == null)
     {
         // 超过最大包速则丢弃
         if (server.getStati().getReceiveNumSpeed() > packSpeed)
         {
             rst.code = -1;
         }
     }
     else
     {
         // 超过最大任务数则丢弃
         if (entity.PackNo == 1)
         {
             if (server.getStati().getUpTaskNum() > tasks)
             {
                 rst.code = -2;
             }
         }
     }
     return rst;
 }
Exemple #17
0
 /// <summary>
 /// 尝试回调完成监听
 /// </summary>
 /// <param name="rst"></param>
 /// <returns></returns>
 private bool trySuccess(UnAttrRst rst)
 {
     if (transfer != null)
     {
         return transfer.success(rst);
     }
     return false;
 }
Exemple #18
0
            UnAttrRst UnIntUdpServer.upSuccess(EndPoint point, UnUdpEntity entity)
            {
                // 转正式文件
                string dicName = DateTime.Now.ToString("yyyy-MM-dd") + "/" + entity.HashCode + entity.Extent;
                string newPath = Img1_Path + dicName;
                UnFile.move(entity.TmpPath, newPath, true);

                // 自定义返回
                BackInfo bi = new BackInfo();
                bi.ReturnCode = 1;
                bi.ReturnMsg = "上传成功";
                bi.MD5 = entity.HashCode;
                bi.Url = Img1_Domain + dicName;

                UnAttrRst rst = new UnAttrRst();
                rst.back = UnXMMPXml.tToXml(typeof(BackInfo), bi);

                Console.WriteLine("***************上传成功***************");
                Console.WriteLine("【路径】" + newPath);
                Console.WriteLine("【URL】" + bi.Url);
                return rst;
            }
Exemple #19
0
 // Msg-处理数据包
 private byte[] proMsgPackage(EndPoint point, UnUdpEntity entity)
 {
     UnUdpEntity msgBack = new UnUdpEntity();
     msgBack.Event = UnUdpEveEnum.msgPackageBack.getText();
     msgBack.HashCode = entity.HashCode;
     UnAttrRst rst = new UnAttrRst();
     rst.code = 0;
     rst.msg = "success";
     rst.back = "";
     if (intServer != null)
     {
         rst = intServer.msgReceived(point, entity);
     }
     return UnUdpHelp.assemblePackage(msgBack);
 }