コード例 #1
0
        void SocketManager_BinaryInput(byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt;
            int cmd;

            if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd))
            {
                switch (cmd)
                {
                case 2001:
                {
                    long Key;
                    if (read.ReadInt64(out Key))
                    {
                        if (Key == down.DownKey)
                        {
                            string msg;
                            if (read.ReadString(out msg))
                            {
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        MessageBox.Show(msg);
                                        this.Close();
                                    }));
                            }
                        }
                    }
                }
                break;

                case 2002:
                {
                    long Key;
                    if (read.ReadInt64(out Key))
                    {
                        if (Key == down.DownKey)
                        {
                            long   startp;
                            long   endp;
                            byte[] buff;

                            if (read.ReadInt64(out startp) && read.ReadInt64(out endp) && read.ReadByteArray(out buff))
                            {
                                System.Threading.ThreadPool.QueueUserWorkItem((a) =>
                                    {
                                        CheckB cb = IsCheckTable.Find(p => p.StartPostion == startp);

                                        if (cb != null)
                                        {
                                            if (cb.EndPostion == endp && buff.Length >= cb.Size)
                                            {
                                                cb.Checkd = true;

                                                FStream.Position = cb.StartPostion;
                                                FStream.Write(buff, 0, cb.Size);
                                                SizeR += cb.Size;


                                                this.BeginInvoke(new EventHandler((a1, b1) =>
                                                {
                                                    ProcessValue++;
                                                    if (ProcessValue <= this.progressBar1.Maximum)
                                                    {
                                                        this.progressBar1.Value = ProcessValue;
                                                    }
                                                    else
                                                    {
                                                        this.progressBar1.Value = this.progressBar1.Maximum;
                                                    }

                                                    this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB";
                                                }));
                                            }
                                        }
                                        else
                                        {
                                            this.BeginInvoke(new EventHandler((a1, b1) =>
                                            {
                                                BufferFormatV2 bufff = new BufferFormatV2((int)PackType.DownClose);
                                                bufff.AddItem(down.DownKey);
                                                SocketManager.Send(bufff.Finish());
                                                MessageBox.Show("数据验证出错??");
                                                Close();
                                            }));
                                        }
                                    });
                            }
                        }
                    }
                }
                break;

                case 2003:
                {
                    long Key;
                    if (read.ReadInt64(out Key))
                    {
                        if (Key == down.DownKey)
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    CheckDown();
                                }));
                        }
                    }
                }
                break;

                case 2004:
                {
                    long Key;
                    if (read.ReadInt64(out Key))
                    {
                        if (Key == down.DownKey)
                        {
                            long   startP;
                            byte[] xdata;

                            if (read.ReadInt64(out startP) && read.ReadByteArray(out xdata))
                            {
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        CheckB cb = IsCheckTable.Find(p => p.StartPostion == startP);

                                        if (xdata.Length >= cb.Size)
                                        {
                                            cb.Checkd = true;

                                            FStream.Position = cb.StartPostion;
                                            FStream.Write(xdata, 0, cb.Size);
                                            SizeR += cb.Size;


                                            this.BeginInvoke(new EventHandler((a1, b1) =>
                                            {
                                                ProcessValue++;
                                                this.progressBar1.Value = ProcessValue;

                                                this.label1.Text = Math.Round(((double)SizeR / 1024 / 1024), 2) + "MB /" + Math.Round((double)down.Size / 1024 / 1024, 2) + "MB";
                                            }));
                                        }


                                        CheckDown();
                                    }));
                            }
                        }
                    }
                }
                break;
                }
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: zhaopan-vip/ZYSOCKET
        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;
                }
            }
        }
コード例 #3
0
        void SocketManager_BinaryInput(byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data, Deflate.Decompress);
            int         lengt;
            int         cmd;

            if (read.ReadInt32(out lengt) && lengt == read.Length && read.ReadInt32(out cmd))
            {
                PackType cmdtype = (PackType)cmd;

                switch (cmdtype)
                {
                case PackType.GetDisk:
                case PackType.Dir:
                {
                    Dir dir;
                    if (read.ReadObject <Dir>(out dir))
                    {
                        this.BeginInvoke(new EventHandler((a, b) =>
                            {
                                if (dir.IsSuccess)
                                {
                                    this.button1.Enabled = true;
                                    this.button1.Text    = "上层目录";

                                    this.listView1.Clear();

                                    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);
                                        }
                                    }));


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

                                            x2++;
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(Path))
                                    {
                                        string apath = dir.DirName;

                                        apath = apath.Remove(0, Path.Length);

                                        if (string.IsNullOrEmpty(apath))
                                        {
                                            apath = "\\";
                                        }

                                        this.textBox1.Text = apath;
                                    }
                                    else
                                    {
                                        this.textBox1.Text = dir.DirName;
                                    }

                                    CurrentDir = dir.DirName;

                                    this.label2.Text = "共找到目录:" + x2 + "个,文件:" + x1 + "个";
                                }
                                else
                                {
                                    MessageBox.Show(dir.Msg);
                                }
                            }));
                    }
                }
                break;

                case PackType.DelFile:
                {
                    DelFile dfile;
                    if (read.ReadObject <DelFile>(out dfile))
                    {
                        this.BeginInvoke(new EventHandler((a, b) =>
                            {
                                string isErr = "";
                                foreach (DelFileName p in dfile.DelFileList)
                                {
                                    if (!p.IsSuccess)
                                    {
                                        isErr += p.Msg + "\r\n";
                                    }
                                }

                                if (isErr == "")
                                {
                                    MessageBox.Show("删除成功");
                                }
                                else
                                {
                                    MessageBox.Show(isErr);
                                }

                                GotoDir();
                            }));
                    }
                }
                break;

                case PackType.NewDir:
                {
                    PackHandler.NewDir ndir;
                    if (read.ReadObject <PackHandler.NewDir>(out ndir))
                    {
                        this.BeginInvoke(new EventHandler((a, b) =>
                            {
                                if (ndir.IsSuccess)
                                {
                                    GotoDir();
                                }
                                else
                                {
                                    MessageBox.Show(ndir.Msg);
                                    GotoDir();
                                }
                            }));
                    }
                }
                break;

                case PackType.MoveFileSystem:
                {
                    PackHandler.MoveFileSystem mfs;

                    if (read.ReadObject <PackHandler.MoveFileSystem>(out mfs))
                    {
                        this.BeginInvoke(new EventHandler((a, b) =>
                            {
                                if (!mfs.IsSuccess)
                                {
                                    MessageBox.Show(mfs.Msg);
                                    GotoDir();
                                }

                                if (this.MoveFile != null)
                                {
                                    if (mfs.OldName == MoveFile.FullName)
                                    {
                                        GotoDir();

                                        this.MoveFile = null;
                                    }
                                }
                            }));
                    }
                }
                break;

                case PackType.Run:
                {
                    Run run;
                    if (read.ReadObject <Run>(out run))
                    {
                        this.BeginInvoke(new EventHandler((a, b) =>
                            {
                                if (run.IsSuccess)
                                {
                                    MessageBox.Show("运行成功");
                                }
                                else
                                {
                                    MessageBox.Show(run.Msg);
                                }
                            }));
                    }
                }
                break;

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

                case PackType.UpFile:
                {
                    UpFile upFile;
                    if (read.ReadObject <UpFile>(out upFile))
                    {
                        if (!upFile.IsSuccess)
                        {
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    MessageBox.Show("上传文件发生错误:" + upFile.Msg);
                                }));
                        }
                        else
                        {
                            if (UpFileList.ContainsKey(upFile.UpKey))
                            {
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        FileStream stream = UpFileList[upFile.UpKey];

                                        UpFiles win = new UpFiles(this, upFile.FullName, upFile.UpKey, stream);
                                        win.Show();
                                    }));
                            }
                            else
                            {
                                this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        MessageBox.Show("上传文件发生错误:无法找到KEY所指定的文件");
                                    }));
                            }
                        }
                    }
                }
                break;

                case PackType.UpClose:
                {
                    long key;
                    if (read.ReadInt64(out key))
                    {
                        if (UpFileList.ContainsKey(key))
                        {
                            this.UpFileList[key].Close();
                            this.UpFileList.Remove(key);
                            this.BeginInvoke(new EventHandler((a, b) =>
                                {
                                    if (UpFileClose != null)
                                    {
                                        UpFileClose(key);
                                    }
                                }));
                        }
                    }
                }
                break;

                case PackType.ReBytes:
                {
                    long downkey;

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

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

                                strem.Position = startpostion;

                                byte[] xdata = new byte[size];

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


                                BufferFormatV2 buff = new BufferFormatV2((int)PackType.DataSet);
                                buff.AddItem(downkey);
                                buff.AddItem(startpostion);
                                buff.AddItem(xdata);
                                SocketManager.Send(buff.Finish());
                            }
                            else
                            {
                                BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose);
                                buff.AddItem(downkey);
                                SocketManager.Send(buff.Finish());
                            }
                        }
                    }
                }
                break;
                }
            }
        }