Beispiel #1
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:
                        {
                             GetDisk tmp;
                             if (read.ReadObject<GetDisk>(out tmp))
                             {
                                 this.BeginInvoke(new EventHandler((a, b) =>
                                     {
                                         this.button1.Enabled = false;

                                         this.listView1.Items.Clear();

                                         foreach (DiskInfo info in tmp.DiskList)
                                         {
                                             if (info.DriveType == DriveType.CDRom)
                                             {
                                                 this.listView1.Items.Add(new ListViewItem()
                                                 {
                                                     Text = string.IsNullOrEmpty(info.VolumeLabel)?info.Name:info.Name+"("+info.VolumeLabel+")",
                                                     Tag = info,
                                                     ImageIndex = 1
                                                 });
                                             }
                                             else
                                             {
                                                 this.listView1.Items.Add(new ListViewItem()
                                                 {
                                                     Text = string.IsNullOrEmpty(info.VolumeLabel) ? info.Name : info.Name + "(" + info.VolumeLabel + ")",
                                                     Tag = info,
                                                     ImageIndex = 0
                                                 });
                                             }


                                         }

                                         this.textBox1.Text = "系统根目录";

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

                                                   }));

                                               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 = 2,
                                                           Tag = p
                                                       });

                                                       x2++;
                                                   }

                                               }

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

            }
        }
Beispiel #2
0
 public CallBack(DownFile down)
 {
     this.down = down;
 }
Beispiel #3
0
        private void mnuDownload_Click(object sender, EventArgs e)
        {
            if (selectedList.Tag.ToString() == "D")
            {
                MessageBox.Show("폴더는 다운로드를 지원하지 않습니다.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DownFile df = new DownFile();

            df.name       = selectedList.Text;
            df.ClientPath = txtPath.Text;
            df.ServerPath = selectedPath + "\\" + selectedList.Text;
            df.Type       = (int)PacketType.DOWNLOAD;
            Packet.Serialize(df).CopyTo(this.sendBuffer, 0);
            Send();

            FileInfo f = files[selectedPath + "\\" + selectedList.Text];

            FileStream   file = null;
            BinaryWriter bw   = null;

            try
            {
                // 파일 스트림 생성
                file = new FileStream(df.ClientPath + "\\" + df.name, FileMode.Create);
                bw   = new BinaryWriter(file);
                //파일이 이미 존재하는 경우 unAuto.... 예외발생
                byte[] bytesFileBuffer = new byte[f.Length];
                int    bytes           = m_Stream.Read(bytesFileBuffer, 0, (int)f.Length);
                bw.Write(bytesFileBuffer);
            }
            catch (UnauthorizedAccessException ex)
            {
                Messenger ms = new Messenger("파일 수신 접근권한거부 : " + ex.StackTrace);
                ms.Type = (int)PacketType.MESSAGE;
                Packet.Serialize(ms).CopyTo(this.sendBuffer, 0);
                Send();
                return;
            }
            catch (Exception ex)
            {
                Messenger ms = new Messenger("파일 수신 실패 : " + ex.Message);
                ms.Type = (int)PacketType.MESSAGE;
                Packet.Serialize(ms).CopyTo(this.sendBuffer, 0);
                Send();
                return;
            }
            finally
            {
                // 파일 스트림 닫기
                if (bw != null)
                {
                    bw.Close();
                }
                if (file != null)
                {
                    file.Close();
                }
            }

            Messenger ms2 = new Messenger(df.name + " 데이터 다운로드 완료...");

            ms2.Type = (int)PacketType.MESSAGE;
            Packet.Serialize(ms2).CopyTo(this.sendBuffer, 0);
            Send();
        }
Beispiel #4
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;
                }
            }
        }