示例#1
0
        private void ThreadFunUpload_Directory(object obj)
        {
            StructByte     S_B = new StructByte();
            SockInfo_U     s   = (SockInfo_U)obj;
            Directory_info d   = new Directory_info();
            List <string>  l   = new List <string>();

            d.name = s.di.Name;
            GetAllDirList(s.di.FullName, l, d);
            byte[] bu = new byte[270];
            string Root_directory_name = s.di.FullName.Substring(s.di.FullName.LastIndexOf(@"\") + 1);

            bu = S_B.StructToBytes(new info(Root_directory_name, (UInt32)d.size, 2, (UInt32)l.Count));
            s.s.Send(bu, bu.Length, SocketFlags.None);
            Thread.Sleep(1000);
            s.s.Receive(bu, SocketFlags.None);
            int i = BitConverter.ToInt32(bu, 0);

            Thread.Sleep(10);
            try
            {
                for (; i < l.Count; i++)
                {
                    string        name = l[i];
                    DirectoryInfo di   = new DirectoryInfo(name);
                    string        n    = name.Substring(name.IndexOf(Root_directory_name));
                    if (di.Attributes == FileAttributes.Directory)
                    {
                        bu = S_B.StructToBytes(new info(n, 0, 2, 1));
                        s.s.Send(bu, bu.Length, SocketFlags.None);
                    }
                    else if (di.Attributes == FileAttributes.Normal || di.Attributes == FileAttributes.Archive)
                    {
                        Upload_File(new FileStream((name), FileMode.Open, FileAccess.Read, FileShare.Read), s.s, n);
                    }
                }
                bu = S_B.StructToBytes(new info("asdf", 0, 0, 0));
                s.s.Send(bu, bu.Length, SocketFlags.None);
                s.s.Close();
            }
            catch (Exception)
            {
                ;
            }
            //  Console.WriteLine("asdf");
        }
示例#2
0
        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");////////////////////////////////////////
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }