Пример #1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            byte[] b1 = new byte[268];
            Socket s  = (Socket)ar.AsyncState;
            int    i  = 0;

            try
            {
                i = s.EndReceive(ar);
            }
            catch (Exception)
            {
                ;
            }
            if (i > 0)
            {
                Array.Copy(b, b1, b1.Length);
                SenderInfo sri = (SenderInfo)S_B.BytesToStruct(b1, (new SenderInfo()).GetType());
                if (sri.State == 0)
                {
                    link = false;
                    s.Close();
                    s = null;
                    return;
                }
                byte[] b2 = new byte[sri.StrNum];
                Array.Copy(b, b1.Length, b2, 0, sri.StrNum);
                string   send = System.Text.Encoding.UTF8.GetString(b2);
                DateTime now  = DateTime.Now;
                string   name = sri.Name + "  " + now;
                if (sri.State == 2 || sri.State == 3)
                {
                    ReceiveFile(send, sri.State);
                }
                //Console.WriteLine(sri.Name + "  " + now);
                //Console.WriteLine(send);

                if (RecvMessage != null && sri.State == 1)
                {
                    foreach (EventHandler <MessageEventArgs> item in RecvMessage.GetInvocationList())
                    {
                        item.BeginInvoke(this, new MessageEventArgs()
                        {
                            Name = name, Message = send
                        }, null, null);
                    }
                }

                try
                {
                    s.BeginReceive(b, 0, b.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), s);
                }
                catch (Exception)
                {
                    ;
                }
            }
        }
Пример #2
0
        public FileInfoDataList ReceiveShare()
        {
            Share sh = new Share();

            if (newSock == null)
            {
                return(null);
            }
            byte[] b = new byte[315];
            int    r = newSock.Receive(b, SocketFlags.None);

            if (r == 0)
            {
                return(null);
            }
            sh = (Share)S_B.BytesToStruct(b, new Share().GetType());
            FileInfoDataList si = new FileInfoDataList(sh.name, sh.type, sh.size, sh.hash, null, null);

            return(si);
        }
Пример #3
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");////////////////////////////////////////
        }
Пример #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);
        }