private void Download_Directory(object obj) { SocketModule s_i = (SocketModule)obj; SockInfo si = null; Socket s = s_i.l_S[0].s; try { s.Send(BitConverter.GetBytes(s_i.PointInfo.Di_file_num), SocketFlags.None); } catch (Exception) { s_i.l_S.RemoveAt(0); return; } int r = 0; info i = new info(); int s_num = 0; bool break_off = true; #region 主循环 do { s_num++; byte[] bu = new byte[270]; int n = 0; try { n = r = s.Receive(bu, SocketFlags.None); if (n == 0) { return; } while (n != 270) { r = s.Receive(bu, r, bu.Length, SocketFlags.None); n += r; } } catch (Exception) { s_i.l_S.RemoveAt(0); return; } StructByte S_B = new StructByte(); i = (info)S_B.BytesToStruct(bu, i.GetType()); if (i.type == 0) { break; } if (i.type == 1) { s_i.PointInfo.filename = _rootDirectory + i.name; int bufnum = cache_num / module_l;//单位为(module_l(b)大小) FileStream file = new FileStream(_rootDirectory + i.name, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write); file.SetLength(i.size); si = new SockInfo(s_i.l_S[0].s, s_i.l_S[0].i, file); si.buf = new byte[bufnum * module_l]; si.I_BufNum = bufnum; break_off = Download_File(ref si, ref s_i); if (!Stop_DownLoad && break_off) { s_i.m = new ModuleFalt(); s_i.PointInfo.Complete = new bool[100]; s_i.PointInfo.RW = new bool[100]; s_i.PointInfo.sPos_n = new uint[100]; Serialization(s_i.PointInfo); } } else { DirectoryInfo target = new DirectoryInfo(_rootDirectory + i.name); target.Create(); } s_i.PointInfo.Di_file_num++; if (s_i.l_S.Count == 0) { s_i.PointInfo.Di_file_num--; break; } } while (s_num < s_i.l_S[0].i.number && !Stop_DownLoad && break_off); #endregion if (!Stop_DownLoad && break_off) { FileInfo d = new FileInfo(s_i.PointInfo.name + ".dat"); d.Delete(); } if (break_off) { s.Close(); } // Console.WriteLine("asdf");//////////////////////////////////////// }
private void CreatProject(Socket newSock, LoadInfo d) { int r; info i = new info(); FileStream file = null; #region 获取信息后,打开文件或者创建文件,目录 do { byte[] bu = new byte[270]; int n = 0; n = r = newSock.Receive(bu, SocketFlags.None); while (n != 270) { r = newSock.Receive(bu, r, bu.Length, SocketFlags.None); n += r; } StructByte s = new StructByte(); i = (info)s.BytesToStruct(bu, i.GetType()); } while (r < 0); if (lls.l_S.Count >= Channel) { newSock.Close(); return; } DriveInfo D = new DriveInfo(DriveInfoName); if (D.TotalFreeSpace >= i.size) { if (i.type == 1) { try { file = new FileStream((RootDirectory + d.name), FileMode.Open, FileAccess.Write, FileShare.Write); } catch (System.IO.FileNotFoundException) { file = new FileStream((RootDirectory + d.name), FileMode.Create, FileAccess.Write, FileShare.Write); file.SetLength(d.size); } i.name = d.name; i.size = (UInt32)d.size; #region 限制文件大小小于module_l * 100这个数的不能开第2条线程以上 if (file.Length <= module_l * 100) { if (l_S.Count < 1) { l_S.Add(new SockInfo(newSock, i, file)); module_num = (int)(file.Length / module_l) + 1; } else { byte[] b = new byte[4]; b = BitConverter.GetBytes(-1); newSock.Send(b); newSock.Close(); } } else { lls.l_S.Add(new SockInfo(newSock, i, file)); module_num = 100; } #endregion } else { DirectoryInfo target = new DirectoryInfo(RootDirectory + d.name); target.Create(); lls.l_S.Add(new SockInfo(newSock, i, target)); } } else ///报错 { newSock.Close(); return; } #endregion #region 创建数据 if (lls.l_S.Count == 1) { lls.PointInfo.type = lls.l_S[0].i.type; lls.PointInfo.Di_file_num = lls.PointInfo.Di_file_num; lls.t_size = lls.PointInfo.t_Size = lls.l_S[0].i.size; if (lls.l_S[0].i.type == 1) { lls.type = lls.l_S[0].file.Name.Substring(lls.l_S[0].file.Name.LastIndexOf(".")); lls.filename = lls.PointInfo.name = lls.l_S[0].file.Name; lls.PointInfo.filename = lls.l_S[0].file.Name; } else { lls.type = "文件夹"; lls.filename = lls.PointInfo.name = lls.l_S[0].di.FullName; lls.PointInfo.total_file_number = lls.l_S[0].i.number; } lls.name = lls.filename.Substring(lls.filename.LastIndexOf(@"\") + 1); } #endregion CreatThreadPool_Download(lls); }