コード例 #1
0
    bool DownPart(CHttp http, string url, int nFileOffset, int nDownSize, int nFileSize, DownResFile resFile)
    {
        // 调用Http下载的代码
        nDownSize = http.PrepareDown(url, nFileOffset, nDownSize, nDownSize == 0);
        if (nDownSize <= 0)
        {
            Debug.LogError("文件下载失败,url:" + url + "(" + nFileOffset + "-" + nDownSize + ")");
            return(false);
        }

        // 将下载的内容提交到写线程
        byte[] szTempBuf    = null;
        int    nCurDownSize = 0;
        int    nRecTotal    = 0;
        int    nRecLen      = 0;
        int    nOffset      = 0;

        nCurDownSize = nDownSize > 4096 ? 4096 : nDownSize;
        MemBlock pBlock = AllockBlock(url, nFileOffset, nCurDownSize, nFileSize);  // 从内存池中取一个4K的内存片

        while (nDownSize > 0 && !m_bNeedStop)
        {
            // 必要的话,在这里添加限速功能或限制接收速度的功能,以免网速太快,导致一秒内分配太多内存
            //LimitSpeed();
            nRecLen = http.FastReceiveMax(ref szTempBuf, ref nOffset, 4096 - nRecTotal);
            if (nRecLen > 0)
            {
                OnReceive(nRecLen);  // 统计下载的流量
                Array.Copy(szTempBuf, nOffset, pBlock.data, nRecTotal, nRecLen);
                nRecTotal += nRecLen;
                // 如果当前块接收满了
                if (nRecTotal >= nCurDownSize)
                {
                    pBlock.resFile = resFile;
                    PushWrite(pBlock);// 提交写文件
                    nRecTotal    = 0;
                    nDownSize   -= nCurDownSize;
                    nFileOffset += nCurDownSize;
                    nCurDownSize = nDownSize > 4096 ? 4096 : nDownSize;
                    // 必要的话,加上限额等待
                    if (nCurDownSize > 0)
                    {
                        WaitBlock(1024 * 1024);                                          // 检测当前内存池分配的总量,超过就挂起
                        pBlock = AllockBlock(url, nFileOffset, nCurDownSize, nFileSize); // 从内存池中取一个4K的内存片
                    }
                }
            }
            else
            {
                return(false); // 文件读取失败,可能是网络出问题了
            }
        }
        return(true);
    }
コード例 #2
0
ファイル: HttpDown.cs プロジェクト: mengflyQ/UnityTools
    // 功能:下载整个文件
    // 说明:这个接口是阻塞模式的,请不要在主线程调用
    static public bool DownFile(string url, out byte[] szFileData, int nDownOffset = 0, int nNeedDownSize = 0, long nRandSeed = 0, LPOnReceiveDownFile lpReceiveFunc = null)
    {
        szFileData = null;
        CHttp  http = new CHttp(nRandSeed);
        string szServerIP = string.Empty, szObj = string.Empty;
        int    nPort = 80;

        http.ParseURL(url, ref szServerIP, ref szObj, ref nPort);

        string szAnswer = string.Empty;

        if (!http.QueryFile(ref szAnswer, szServerIP, nPort, szObj, nDownOffset, nNeedDownSize, nNeedDownSize <= 0))
        {
            http.Close();
            return(false);
        }
        int nDownSize = 0, nFileSize = 0;

        if (!http.AnlyseFileLengthFromAnswer(szAnswer, ref nDownSize, ref nFileSize))
        {
            http.Close();
            return(false);
        }
        nDownSize = nFileSize;
        byte[] szTempBuf = null;

        szFileData = new byte[nFileSize];

        int nRecTotal = 0;
        int nRecLen   = 0;
        int nOffset   = 0;

        while (nRecTotal < nDownSize)
        {
            nRecLen = http.FastReceiveMax(ref szTempBuf, ref nOffset);
            // 写入文件吧
            if (nRecLen > 0)
            {
                if (lpReceiveFunc != null)
                {
                    lpReceiveFunc(nRecLen);
                }
                Array.Copy(szTempBuf, nOffset, szFileData, nRecTotal, nRecLen);
                nRecTotal += nRecLen;
            }
            else
            {
                break;
            }
        }
        http.Close();
        return(nRecTotal == nDownSize);
    }
コード例 #3
0
ファイル: HttpDown.cs プロジェクト: mengflyQ/UnityTools
    // 功能:下载文件,并将下载的文件保存到本地
    // 参数:url - 网络URL的地址
    //       szLocalPathName - 本地保存地址
    // 说明:这个接口是阻塞模式的,请不要在主线程调用
    static public bool DownFile(string url, string szLocalPathName, int nNeedDownOffset, int nNeedDownSize, long nRandSeed = 0, LPOnReceiveDownFile lpReceiveFunc = null)
    {
        CHttp  http = new CHttp(nRandSeed);
        string szServerIP = string.Empty, szObj = string.Empty;
        int    nPort = 80;

        http.ParseURL(url, ref szServerIP, ref szObj, ref nPort);

        string szAnswer = string.Empty;

        if (!http.QueryFile(ref szAnswer, szServerIP, nPort, szObj, nNeedDownOffset, nNeedDownSize, nNeedDownSize <= 0))
        {
            http.Close();
            return(false);
        }
        int nDownSize = 0, nFileSize = 0;

        if (!http.AnlyseFileLengthFromAnswer(szAnswer, ref nDownSize, ref nFileSize))
        {
            http.Close();
            return(false);
        }
        if (nNeedDownSize <= 0)
        {
            nDownSize = nFileSize;  // 这是全部下载
        }
        // 打开本地文件
        if (File.Exists(szLocalPathName))
        {
            File.Delete(szLocalPathName);
        }
        FileStream pFile = new FileStream(szLocalPathName, FileMode.CreateNew, FileAccess.Write);

        // 开始下载吧
        byte[] szTempBuf = null;

        int nRecTotal = 0;
        int nRecLen   = 0;
        int nOffset   = 0;

        while (nRecTotal < nDownSize)
        {
            nRecLen = http.FastReceiveMax(ref szTempBuf, ref nOffset);
            // 写入文件吧
            if (nRecLen > 0)
            {
                if (lpReceiveFunc != null)
                {
                    lpReceiveFunc(nRecLen);
                }
                nRecTotal += nRecLen;
                pFile.Write(szTempBuf, nOffset, nRecLen);
            }
            else
            {
                break;
            }
        }
        http.Close();
        pFile.Close();
        return(nRecTotal == nDownSize);
    }