public CHttpManage(string url, string loc, int num) { theUrl = url; location = loc; threadTotality = num; if (location.LastIndexOf("\\") != location.Length - 1) { location += "\\"; } FileName = theUrl.Substring(theUrl.LastIndexOf('/')); DateTime dt = DateTime.Now; /////////////////////////// HttpWebRequest request; long filesize = 0; try { request = (HttpWebRequest)HttpWebRequest.Create(theUrl); filesize = request.GetResponse().ContentLength; request.Abort(); } catch (System.Exception ex) { MessageBox.Show("ErrorEventArgs in stardDownload!" + ex.Message); } threadEnd = new bool[threadTotality]; snippet = new string[threadTotality]; fileStart = new int[threadTotality]; fileSize = new int[threadTotality]; DevidedSize = (int)filesize / threadTotality; string tempstr = ""; IntPair ip = new IntPair(); try { //续传 则打开保存每个临时快线程读取进度的文件 fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Open); StreamReader sr = new StreamReader(fileSwap); threadTotality = Convert.ToInt32(sr.ReadLine()); for (int i = 0; i < threadTotality; ++i ) { snippet[i] = location + FileName + "_" + i.ToString() + ".piece"; tempstr = sr.ReadLine(); ip = strTackle(tempstr); fileStart[ip.threadNum] = ip.threadBytes + DevidedSize * ip.threadNum; fileSize[ip.threadNum] = DevidedSize - 1 - ip.threadBytes; } sr.Close(); //将读取好的临时文件清空 fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Create); fileSwap.Close(); } catch (System.IO.FileNotFoundException ex) { fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Create); StreamWriter stw = new StreamWriter(fileSwap); stw.WriteLine(threadTotality); stw.Close(); for (int i = 0; i < threadTotality; ++i) { threadEnd[i] = false; snippet[i] = location + FileName + "_" + i.ToString() + ".piece"; if (i < threadTotality - 1) { fileStart[i] = DevidedSize * i; fileSize[i] = DevidedSize - 1; } else { fileStart[i] = DevidedSize * i; fileSize[i] = DevidedSize - 1; } } fileSwap.Close(); stw.Close(); } threadRunning = new Thread[threadTotality]; model = new CHttpSnippet[threadTotality]; for (int i = 0; i < threadTotality;++i ) { model[i] = new CHttpSnippet(this, i); model[i].StartEvent += new HttpEventHandler(OnStartReceive); model[i].EndEvent += new HttpEventHandler(OnEndReceive); model[i].StopSnippetEvent += new HttpEventHandler(model[i].StopSnippetProc); threadRunning[i] = new Thread(new ThreadStart(model[i].receive)); combineThread = new Thread(new ThreadStart(combineFile)); } }
public CHttpManage(string url, string loc, int num) { theUrl = url; location = loc; threadTotality = num; if (location.LastIndexOf("\\") != location.Length - 1) { location += "\\"; } FileName = theUrl.Substring(theUrl.LastIndexOf('/')); DateTime dt = DateTime.Now; /////////////////////////// HttpWebRequest request; long filesize = 0; try { request = (HttpWebRequest)HttpWebRequest.Create(theUrl); filesize = request.GetResponse().ContentLength; request.Abort(); } catch (System.Exception ex) { MessageBox.Show("ErrorEventArgs in stardDownload!" + ex.Message); } threadEnd = new bool[threadTotality]; snippet = new string[threadTotality]; fileStart = new int[threadTotality]; fileSize = new int[threadTotality]; DevidedSize = (int)filesize / threadTotality; string tempstr = ""; IntPair ip = new IntPair(); try { //续传 则打开保存每个临时快线程读取进度的文件 fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Open); StreamReader sr = new StreamReader(fileSwap); threadTotality = Convert.ToInt32(sr.ReadLine()); for (int i = 0; i < threadTotality; ++i) { snippet[i] = location + FileName + "_" + i.ToString() + ".piece"; tempstr = sr.ReadLine(); ip = strTackle(tempstr); fileStart[ip.threadNum] = ip.threadBytes + DevidedSize * ip.threadNum; fileSize[ip.threadNum] = DevidedSize - 1 - ip.threadBytes; } sr.Close(); //将读取好的临时文件清空 fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Create); fileSwap.Close(); } catch (System.IO.FileNotFoundException ex) { fileSwap = new FileStream(location + FileName + ".tempinfo", FileMode.Create); StreamWriter stw = new StreamWriter(fileSwap); stw.WriteLine(threadTotality); stw.Close(); for (int i = 0; i < threadTotality; ++i) { threadEnd[i] = false; snippet[i] = location + FileName + "_" + i.ToString() + ".piece"; if (i < threadTotality - 1) { fileStart[i] = DevidedSize * i; fileSize[i] = DevidedSize - 1; } else { fileStart[i] = DevidedSize * i; fileSize[i] = DevidedSize - 1; } } fileSwap.Close(); stw.Close(); } threadRunning = new Thread[threadTotality]; model = new CHttpSnippet[threadTotality]; for (int i = 0; i < threadTotality; ++i) { model[i] = new CHttpSnippet(this, i); model[i].StartEvent += new HttpEventHandler(OnStartReceive); model[i].EndEvent += new HttpEventHandler(OnEndReceive); model[i].StopSnippetEvent += new HttpEventHandler(model[i].StopSnippetProc); threadRunning[i] = new Thread(new ThreadStart(model[i].receive)); combineThread = new Thread(new ThreadStart(combineFile)); } }
private IntPair strTackle(string strin) { IntPair ret = new IntPair(); int index = strin.IndexOf(':'); ret.threadNum = Convert.ToInt32(strin.Substring(0, index)); ret.threadBytes = Convert.ToInt32(strin.Substring(index + 1)); return ret; }