private void AcceptCallback_Continuous(IAsyncResult ar) { AllDone.Set(); Socket sock = (Socket)ar.AsyncState; Socket newSock = null; try { newSock = sock.EndAccept(ar); } catch (Exception) { //Console.WriteLine(e.Message); return; } byte[] bu = new byte[270]; newSock.Receive(bu, SocketFlags.None); if (lls.l_S.Count >= Channel) { newSock.Close(); return; } SockInfo si = new SockInfo(newSock); si.i.type = (UInt16)lls.PointInfo.type; si.i.size = (UInt32)lls.PointInfo.File.Length; si.i.name = lls.PointInfo.name; si.i.number = lls.PointInfo.total_file_number; si.file = lls.PointInfo.File; lls.l_S.Add(si); Serialization(lls.PointInfo); CreatThreadPool_Download(lls); }
private void ThreadFun_Download_File(SockInfo s, SocketModule sm) { SockInfo s_i = s; SocketModule SM = sm; if (Download_File(ref s_i, ref SM)) { s_i.s.Close(); } }
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 bool Download_File(ref SockInfo s, ref SocketModule sm) { SockInfo s_i = s; FileStream file = s_i.file; SocketModule SM = sm; module_num = 100; if (s_i.i.type == 2 && file.Length <= module_l * 100) { module_num = (int)(file.Length / module_l) + 1; } #region 本线程的局部变量 StructByte S_B = new StructByte(); int module_m = (int)(file.Length / module_l / 100) + 1; if ((file.Length / module_l) < 99 * module_m && module_m != 1) { module_m = (int)(file.Length / module_l / 10) + 1; module_num = 10; } int module = module_l + module_l_Head; int num1 = 0; //粘包造成的第二次起初数组位置 int j = 0; //粘包造成的第二次复制起初数组位置 byte[] buf = new byte[module]; byte[] bu = new byte[module]; byte[] p_buf = new byte[module_l]; byte[] p_Head = new byte[module_l_Head]; byte[] b = new byte[8]; byte[] b1 = new byte[4]; byte[] b2 = new byte[4]; int nRecv = 0; UInt32 sPos = 0; UInt32 sPos_n = 0; UInt32 num = 0; long nLn = 0; #endregion #region 开始定位 int _i = 0; for (; _i < module_num; _i++) { if (!SM.m.RW[_i] && !SM.m.Complete[_i] || SM.m.sPos_n[_i] != 0) { SM.PointInfo.RW[_i] = SM.m.RW[_i] = true; b1 = BitConverter.GetBytes(_i); b2 = BitConverter.GetBytes(SM.m.sPos_n[_i]); Array.Copy(b1, b, b1.Length); Array.Copy(b2, 0, b, b1.Length, b2.Length); try { s_i.s.Send(b); } catch (Exception) { lls.l_S.RemoveAt(s_i.ID); for (int i = 0; i < lls.l_S.Count; i++) { lls.l_S[i].ID = i; } return(false); } break; } } if (_i == module_num) { b1 = BitConverter.GetBytes(-1); b2 = BitConverter.GetBytes(0); Array.Copy(b1, b, b1.Length); Array.Copy(b2, 0, b, b1.Length, b2.Length); try { s_i.s.Send(b); } catch (Exception) { lls.l_S.RemoveAt(s_i.ID); for (int i = 0; i < lls.l_S.Count; i++) { lls.l_S[i].ID = i; } return(false); } if (s_i.i.type == 1) { FileInfo d = new FileInfo(SM.PointInfo.name + ".dat"); d.Delete(); } nRecv = -1; } #endregion do { if (nRecv == -1) { break; } #region 线程主工作循环 try { nRecv = s_i.s.Receive(buf, SocketFlags.None); } catch (Exception) { break; } if (nRecv == 0) { break; } #region 粘包处理算法 if (module >= num1 + nRecv) { Array.Copy(buf, 0, bu, num1, nRecv); num1 += nRecv; } else { Array.Copy(buf, 0, bu, num1, module - num1); j = module - num1; num1 = bu.Length; //Sticky_num++; } #endregion if (num1 == bu.Length) { Array.Copy(bu, 0, p_Head, 0, p_Head.Length); Array.Copy(bu, p_Head.Length, p_buf, 0, p_buf.Length); Package p = (Package)S_B.TestStruct(p_Head); sPos_n = p.module_m; sPos = p.num; num = p.module_n; nLn += p.bufLen; #region 缓冲区算法 Array.Copy(p_buf, 0, s_i.buf, s_i.BufNum * module_l, p_buf.Length); s_i.BufNum++; if (s_i.I_BufNum == s_i.BufNum || sPos_n == num + 1) { file.Seek((sPos * module_m + num - s_i.BufNum + 1) * module_l, SeekOrigin.Begin); file.Write(s_i.buf, 0, (s_i.BufNum - 1) * module_l + p.bufLen); SM.PointInfo.sPos_n[sPos] = num; SM.DownLoadSize = SM.PointInfo.DownLoadSize += nLn; nLn = 0; s_i.BufNum = 0; Serialization(SM.PointInfo); if (Stop_DownLoad) { break; } } #endregion #region 呼叫让上传段发送某个块 if (sPos_n == num + 1) { int i; for (i = 0; i < module_num; i++) { if (i == sPos) { SM.PointInfo.sPos_n[i] = 0; SM.PointInfo.RW[i] = SM.m.RW[sPos] = false; SM.PointInfo.Complete[i] = SM.m.Complete[sPos] = true; } if (!SM.m.RW[i] && !SM.m.Complete[i]) { SM.PointInfo.RW[i] = SM.m.RW[i] = true; b1 = BitConverter.GetBytes(i); b2 = BitConverter.GetBytes(SM.m.sPos_n[i]); Array.Copy(b1, b, b1.Length); Array.Copy(b2, 0, b, b1.Length, b2.Length); try { s_i.s.Send(b, SocketFlags.None); } catch (Exception) { ; } break; } } if (i == module_num) { nRecv = 0; if (s_i.i.type == 1) { FileInfo d = new FileInfo(SM.PointInfo.name + ".dat"); d.Delete(); } break; } } #endregion num1 = 0; if (j != 0) { Array.Copy(buf, j, bu, num1, nRecv - j); num1 += nRecv - j; j = 0; } } //#if DEBUG // Console.WriteLine("4!!!{0} {1} {2} {3} {4} {5}", sPos, nRecv, num, num1, j, Sticky_num); //#endif #endregion Thread.Sleep(1); } while (nRecv > 0); SM.PointInfo.RW[sPos] = SM.m.RW[sPos] = false; SM.PointInfo.RW[sPos + 1] = SM.m.RW[sPos + 1] = false; b = BitConverter.GetBytes(-1); try { s_i.s.Send(b, SocketFlags.None); } catch (Exception) { lls.l_S.RemoveAt(s_i.ID); for (int i = 0; i < lls.l_S.Count; i++) { lls.l_S[i].ID = i; } return(false); } file.Close(); return(true); }