示例#1
0
        private void DataOn(UserInfo user, byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt;
            int cmd;

            if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd))
            {
                FileCmd Fcmd = (FileCmd)cmd;

                switch (Fcmd)
                {
                case FileCmd.Success:
                {
                    Success success;

                    if (read.ReadObject <Success>(out success))
                    {
                        if (!success.IsRes)
                        {
                            if (success.Key != null && success.Key.Equals(Config.Default.ConnentKey, StringComparison.Ordinal))
                            {
                                user.IsSuccess    = true;
                                success.IsSuccess = true;
                                success.IsRes     = true;
                                MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success));
                                UpdateUserListView();
                            }
                            else
                            {
                                user.IsSuccess    = false;
                                success.IsSuccess = false;
                                success.IsRes     = true;
                                MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success));
                            }
                        }
                        else
                        {
                            if (!success.IsSuccess)
                            {
                                user.IsValidate = false;
                                UpdateUserListView();
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        if (!user.Client.IsProxy)
                                        {
                                            MessageBox.Show("连接到:" + user.Client.Host + ":" + user.Client.Port + " 密码错误");
                                        }
                                        else
                                        {
                                            MessageBox.Show("连接到:" + user.Client.Key + " 密码错误");
                                        }
                                    }));
                            }
                            else
                            {
                                user.IsValidate = true;
                                UpdateUserListView();
                            }
                        }
                    }
                }
                break;

                case FileCmd.GetFile:
                {
                    GetFile dir;

                    if (read.ReadObject <GetFile>(out dir))
                    {
                        if (!dir.IsRes)
                        {
                            string dirname = dir.DirName;

                            if (string.IsNullOrEmpty(dirname))
                            {
                                dirname = Config.Default.SharePath;
                            }


                            DirectoryInfo dirinfo = new DirectoryInfo(dirname);

                            if (dirinfo.Parent != null)
                            {
                                dir.DirName = dirinfo.Parent.FullName;
                            }
                            else
                            {
                                dir.DirName = Config.Default.SharePath;
                            }
                            if (!(dirinfo.FullName.IndexOf(Config.Default.SharePath) == 0))
                            {
                                dir.IsSuccess = false;
                                dir.Msg       = "无法找到目录:" + dirinfo.FullName;
                                MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(dir));
                                return;
                            }

                            dir.IsRes = true;
                            if (dirinfo.Exists)
                            {
                                dir.FileSystemList = new List <FileSystem>();

                                FileSystemInfo[] files = dirinfo.GetFileSystemInfos();

                                foreach (var p in files)
                                {
                                    FileSystem tmp = new FileSystem()
                                    {
                                        Name     = p.Name,
                                        FullName = p.FullName,
                                        FileType = p is DirectoryInfo ? FileType.Dir : FileType.File,
                                        Size     = p is DirectoryInfo ? 0 : (p as FileInfo).Length,
                                        EditTime = p.LastWriteTime
                                    };

                                    dir.FileSystemList.Add(tmp);
                                }

                                dir.IsSuccess = true;
                            }
                            else
                            {
                                dir.IsSuccess = false;
                                dir.Msg       = "无法找到目录:" + dirname;
                            }

                            MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(dir));
                        }
                        else
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (dir.IsSuccess)
                                    {
                                        this.listView2.Clear();

                                        dir.FileSystemList.Add(new FileSystem()
                                        {
                                            FileType = FileType.Dir,
                                            EditTime = DateTime.Now,
                                            FullName = dir.DirName,
                                            Name     = "..",
                                            Size     = 0
                                        });

                                        dir.FileSystemList.Sort(new Comparison <FileSystem>((a1, a2) =>
                                        {
                                            if (a1.FileType == FileType.Dir && a2.FileType == FileType.Dir)
                                            {
                                                return(a1.Name.CompareTo(a2.Name));
                                            }
                                            else if (a1.FileType != FileType.Dir && a2.FileType != FileType.Dir)
                                            {
                                                return(a1.Name.CompareTo(a2.Name));
                                            }
                                            else if (a1.FileType == FileType.Dir && a2.FileType == FileType.File)
                                            {
                                                return(-1);
                                            }
                                            else if (a2.FileType == FileType.Dir && a1.FileType == FileType.File)
                                            {
                                                return(1);
                                            }
                                            else
                                            {
                                                return(0);
                                            }
                                        }));

                                        int x1 = 0, x2 = 0;
                                        foreach (FileSystem p in dir.FileSystemList)
                                        {
                                            if (p.FileType == FileType.File)
                                            {
                                                this.listView2.Items.Add(new ListViewItem()
                                                {
                                                    Text       = p.Name,
                                                    ImageIndex = GetImageIndex(p.Name),
                                                    Tag        = p
                                                });
                                                x1++;
                                            }
                                            else
                                            {
                                                this.listView2.Items.Add(new ListViewItem()
                                                {
                                                    Text       = p.Name,
                                                    ImageIndex = 0,
                                                    Tag        = p
                                                });

                                                x2++;
                                            }
                                        }
                                    }
                                }));
                        }
                    }
                }
                break;

                case FileCmd.Down:
                {
                    Down down;
                    if (read.ReadObject <Down>(out down))
                    {
                        if (!down.IsRes)
                        {
                            down.IsRes = true;

                            FileInfo file = new FileInfo(down.FullName);

                            if (file.Exists)
                            {
                                down.Size = file.Length;

Re:
                                long key = DateTime.Now.Ticks;

                                if (user.DownKeyList.ContainsKey(key))
                                {
                                    System.Threading.Thread.Sleep(1);
                                    goto Re;
                                }
                                user.DownKeyList.Add(key, file.FullName);

                                down.DownKey   = key;
                                down.IsSuccess = true;
                            }
                            else
                            {
                                down.IsSuccess = false;
                                down.Msg       = "未找到文件:" + down.FullName;
                            }

                            MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(down));

                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (!user.Client.IsProxy)
                                    {
                                        this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 下载文件:" + file.FullName + "\r\n");
                                    }
                                    else
                                    {
                                        this.richTextBox1.AppendText(user.Client.Key + " 下载文件:" + file.FullName + "\r\n");
                                    }
                                }));
                        }
                        else
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (down.IsSuccess)
                                    {
                                        DownFile downwin = new DownFile(user, down);
                                        downwin.Show();
                                    }
                                    else
                                    {
                                        MessageBox.Show(down.Msg);
                                    }
                                }));
                        }
                    }
                }
                break;

                case FileCmd.DownNow:
                {
                    long downkey;

                    if (read.ReadInt64(out downkey))
                    {
                        if (user.DownKeyList.ContainsKey(downkey))
                        {
                            string filename = user.DownKeyList[downkey];
                            user.DownKeyList.Remove(downkey);

                            if (File.Exists(filename))
                            {
                                FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                                lock (user.Stream)
                                {
                                    user.StreamList.Add(downkey, stream);
                                }

                                System.Threading.ThreadPool.QueueUserWorkItem((a) =>
                                    {
                                        try
                                        {
                                            byte[] buffa = new byte[xlengt];

                                            int r  = 0;
                                            long p = 0;
                                            do
                                            {
                                                try
                                                {
                                                    r = stream.Read(buffa, 0, buffa.Length);

                                                    if (r < buffa.Length && r > 0)
                                                    {
                                                        byte[] buffb = new byte[r];

                                                        Buffer.BlockCopy(buffa, 0, buffb, 0, buffb.Length);

                                                        BufferFormatV2 buff = new BufferFormatV2(2002);
                                                        buff.AddItem(downkey);
                                                        buff.AddItem(p);
                                                        buff.AddItem(p + r - 1);
                                                        buff.AddItem(buffb);


                                                        MClient.SendData(user.Client.Key, buff.Finish());

                                                        break;
                                                    }
                                                    else if (r > 0)
                                                    {
                                                        BufferFormatV2 buff = new BufferFormatV2(2002);
                                                        buff.AddItem(downkey);
                                                        buff.AddItem(p);
                                                        buff.AddItem(p + r - 1);
                                                        buff.AddItem(buffa);
                                                        MClient.SendData(user.Client.Key, buff.Finish());

                                                        p += r;
                                                    }
                                                }
                                                catch
                                                {
                                                    break;
                                                }
                                            } while (r > 0);


                                            BufferFormatV2 buffcheck = new BufferFormatV2(2003);
                                            buffcheck.AddItem(downkey);
                                            MClient.SendData(user.Client.Key, buffcheck.Finish());
                                        }
                                        catch (Exception er)
                                        {
                                            stream.Close();
                                            lock (user.StreamList)
                                            {
                                                user.StreamList.Remove(downkey);
                                            }

                                            BufferFormatV2 buff = new BufferFormatV2(2001);
                                            buff.AddItem(downkey);
                                            buff.AddItem(er.Message);
                                            MClient.SendData(user.Client.Key, buff.Finish());
                                        }
                                    }, null);
                            }
                            else
                            {
                                BufferFormatV2 buff = new BufferFormatV2(2001);
                                buff.AddItem(downkey);
                                buff.AddItem("文件不存在");
                                MClient.SendData(user.Client.Key, buff.Finish());
                            }
                        }
                        else
                        {
                            BufferFormatV2 buff = new BufferFormatV2(2001);
                            buff.AddItem(downkey);
                            buff.AddItem("DownKey 不存在");
                            MClient.SendData(user.Client.Key, buff.Finish());
                        }
                    }
                }
                break;

                case FileCmd.DownClose:
                {
                    long downkey;

                    if (read.ReadInt64(out downkey))
                    {
                        if (user.DownKeyList.ContainsKey(downkey))
                        {
                            user.DownKeyList.Remove(downkey);
                        }

                        if (user.StreamList.ContainsKey(downkey))
                        {
                            FileStream strem;

                            lock (user.StreamList)
                            {
                                strem = user.StreamList[downkey];
                                user.StreamList.Remove(downkey);
                            }

                            strem.Close();


                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (!user.Client.IsProxy)
                                    {
                                        this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 下载文件完毕!" + "\r\n");
                                    }
                                    else
                                    {
                                        this.richTextBox1.AppendText(user.Client.Key + " 下载文件完毕!" + "\r\n");
                                    }
                                }));
                        }
                    }
                }
                break;

                case FileCmd.ReBytes:
                {
                    long downkey;

                    if (read.ReadInt64(out downkey))
                    {
                        long startpostion;
                        int  size;

                        if (read.ReadInt64(out startpostion) && read.ReadInt32(out size))
                        {
                            if (user.StreamList.ContainsKey(downkey))
                            {
                                FileStream strem = user.StreamList[downkey];

                                strem.Position = startpostion;

                                byte[] xdata = new byte[size];

                                strem.Read(xdata, 0, xdata.Length);


                                BufferFormatV2 buff = new BufferFormatV2(2004);
                                buff.AddItem(downkey);
                                buff.AddItem(startpostion);
                                buff.AddItem(xdata);
                                MClient.SendData(user.Client.Key, buff.Finish());
                            }
                            else
                            {
                                BufferFormatV2 buff = new BufferFormatV2(2001);
                                buff.AddItem(downkey);
                                buff.AddItem("DownKey 不存在");
                                MClient.SendData(user.Client.Key, buff.Finish());
                            }
                        }
                    }
                }
                break;

                default:
                {
                    if (user.DownDataOn != null)
                    {
                        user.DownDataOn(data);
                    }
                }
                break;
                }
            }
        }
示例#2
0
        private void SuccessData(UserInfo user, byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt;
            int cmd;

            if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd))
            {
                FileCmd Fcmd = (FileCmd)cmd;
                switch (Fcmd)
                {
                case FileCmd.Success:
                {
                    Success success;

                    if (read.ReadObject <Success>(out success))
                    {
                        if (!success.IsRes)
                        {
                            if (success.Key != null && success.Key.Equals(Config.Default.ConnentKey, StringComparison.Ordinal))
                            {
                                user.IsSuccess    = true;
                                success.IsSuccess = true;
                                success.IsRes     = true;
                                MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success));

                                UpdateUserListView();

                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        if (!user.Client.IsProxy)
                                        {
                                            this.richTextBox1.AppendText(user.Client.Host + ":" + user.Client.Port + " 登入成功\r\n");
                                        }
                                        else
                                        {
                                            this.richTextBox1.AppendText(user.Client.Key + " 登入成功\r\n");
                                        }
                                    }));
                            }
                            else
                            {
                                user.IsSuccess    = false;
                                success.IsSuccess = false;
                                success.IsRes     = true;
                                MClient.SendData(user.Client.Key, BufferFormatV2.FormatFCA(success));
                            }
                        }
                        else
                        {
                            if (!success.IsSuccess)
                            {
                                user.IsValidate = false;
                                UpdateUserListView();
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        if (!user.Client.IsProxy)
                                        {
                                            MessageBox.Show("连接到:" + user.Client.Host + ":" + user.Client.Port + " 密码错误");
                                        }
                                        else
                                        {
                                            MessageBox.Show("连接到:" + user.Client.Key + " 密码错误");
                                        }
                                    }));
                            }
                            else
                            {
                                user.IsValidate = true;
                                UpdateUserListView();
                            }
                        }
                    }
                }
                break;

                case FileCmd.GetFile:
                {
                    GetFile dir;

                    if (read.ReadObject <GetFile>(out dir))
                    {
                        if (dir.IsRes)
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (dir.IsSuccess)
                                    {
                                        this.listView2.Clear();

                                        dir.FileSystemList.Add(new FileSystem()
                                        {
                                            FileType = FileType.Dir,
                                            EditTime = DateTime.Now,
                                            FullName = dir.DirName,
                                            Name     = "..",
                                            Size     = 0
                                        });

                                        dir.FileSystemList.Sort(new Comparison <FileSystem>((a1, a2) =>
                                        {
                                            if (a1.FileType == FileType.Dir && a2.FileType == FileType.Dir)
                                            {
                                                return(a1.Name.CompareTo(a2.Name));
                                            }
                                            else if (a1.FileType != FileType.Dir && a2.FileType != FileType.Dir)
                                            {
                                                return(a1.Name.CompareTo(a2.Name));
                                            }
                                            else if (a1.FileType == FileType.Dir && a2.FileType == FileType.File)
                                            {
                                                return(-1);
                                            }
                                            else if (a2.FileType == FileType.Dir && a1.FileType == FileType.File)
                                            {
                                                return(1);
                                            }
                                            else
                                            {
                                                return(0);
                                            }
                                        }));

                                        int x1 = 0, x2 = 0;
                                        foreach (FileSystem p in dir.FileSystemList)
                                        {
                                            if (p.FileType == FileType.File)
                                            {
                                                this.listView2.Items.Add(new ListViewItem()
                                                {
                                                    Text       = p.Name,
                                                    ImageIndex = GetImageIndex(p.Name),
                                                    Tag        = p
                                                });
                                                x1++;
                                            }
                                            else
                                            {
                                                this.listView2.Items.Add(new ListViewItem()
                                                {
                                                    Text       = p.Name,
                                                    ImageIndex = 0,
                                                    Tag        = p
                                                });

                                                x2++;
                                            }
                                        }
                                    }
                                }));
                        }
                    }
                }
                break;

                case FileCmd.Down:
                {
                    Down down;
                    if (read.ReadObject <Down>(out down))
                    {
                        if (down.IsRes)
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (down.IsSuccess)
                                    {
                                        DownFile downwin = new DownFile(user, down);
                                        downwin.Show();
                                    }
                                    else
                                    {
                                        MessageBox.Show(down.Msg);
                                    }
                                }));
                        }
                    }
                }
                break;

                default:
                {
                    if (user.DownDataOn != null)
                    {
                        user.DownDataOn(data);
                    }
                }
                break;
                }
            }
        }