Example #1
0
        void CheckDown(UserManager user, long key, List <CheckB> IsCheckTable)
        {
            CheckB p = IsCheckTable.Find(x => x.Checkd == false);

            if (p == null)
            {
                if (user.UpFileList.ContainsKey(key))
                {
                    FileStream stream;
                    user.UpFileList.TryRemove(key, out stream);
                    stream.Close();
                }

                if (user.IsCheckTable.ContainsKey(key))
                {
                    List <CheckB> checkblist;
                    user.IsCheckTable.TryRemove(key, out checkblist);
                }

                BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                buffx.AddItem(key);
                server.SendData(user.Asyn.AcceptSocket, buffx.Finish());
            }
            else
            {
                BufferFormatV2 buff = new BufferFormatV2((int)PackType.ReBytes);
                buff.AddItem(key);
                buff.AddItem(p.StartPostion);
                buff.AddItem(p.Size);
                server.SendData(user.Asyn.AcceptSocket, buff.Finish());
            }
        }
Example #2
0
        void DataOn(byte[] data, UserManager user)
        {
            ReadBytesV2 read = new ReadBytesV2(data, Deflate.Decompress);

            int lengt;
            int cmd;

            if (!read.ReadInt32(out lengt) || !read.ReadInt32(out cmd) || cmd == 0)
            {
                read = new ReadBytesV2(data);
            }
            else
            {
                read.Postion = 0;
            }

            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))
                    {
                        string dirname = dir.DirName;

                        if (string.IsNullOrEmpty(dirname) || dirname == "/")
                        {
                            dirname     = user.Userinfo.Path;
                            dir.DirName = dirname;
                        }



                        DirectoryInfo dirinfo = new DirectoryInfo(dirname);

                        if (dirinfo.Exists)
                        {
                            string name = dirinfo.FullName;

                            if (name.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                dir.IsSuccess = false;
                                dir.Msg       = "无法找到目录:" + dir.DirName;
                                server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dir, Deflate.Compress));
                                return;
                            }


                            FileSystemInfo[] files = dirinfo.GetFileSystemInfos();
                            dir.FileSystemList = new List <FileSystem>();
                            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       = "无法找到目录:" + dir.DirName;
                        }

                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dir, Deflate.Compress));
                    }
                }
                break;

                case PackType.DelFile:
                {
                    DelFile dfile;
                    if (read.ReadObject <DelFile>(out dfile))
                    {
                        foreach (DelFileName x in dfile.DelFileList)
                        {
                            if (x.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                x.IsSuccess = false;
                                x.Msg       = "没有找到:" + x.FullName;
                                continue;
                            }


                            if (x.FType == FileType.Dir)
                            {
                                if (Directory.Exists(x.FullName))
                                {
                                    Directory.Delete(x.FullName, true);

                                    x.IsSuccess = true;
                                }
                                else
                                {
                                    x.IsSuccess = false;
                                    x.Msg       = "没有找到目录:" + x.FullName;
                                }
                            }
                            else
                            {
                                if (File.Exists(x.FullName))
                                {
                                    try
                                    {
                                        File.Delete(x.FullName);
                                        x.IsSuccess = true;

                                        AddLog(user.UserName + " 删除文件" + x.FullName);
                                    }
                                    catch (Exception er)
                                    {
                                        x.IsSuccess = false;
                                        x.Msg       = "删除文件 " + x.FullName + "发生错误:" + er.Message;
                                    }
                                }
                                else
                                {
                                    x.IsSuccess = false;
                                    x.Msg       = "没有找到文件:" + x.FullName;
                                }
                            }
                        }

                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(dfile, Deflate.Compress));
                    }
                }
                break;

                case PackType.NewDir:
                {
                    NewDir ndir;
                    if (read.ReadObject <NewDir>(out ndir))
                    {
                        try
                        {
                            DirectoryInfo _ndir = new DirectoryInfo(ndir.DirName);

                            if (_ndir.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                ndir.IsSuccess = false;
                                ndir.Msg       = "无法创建" + ndir.DirName;
                                server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(ndir, Deflate.Compress));
                                return;
                            }

                            Directory.CreateDirectory(_ndir.FullName);
                            ndir.IsSuccess = true;
                            AddLog(user.UserName + " 创建文件夹" + _ndir.FullName);
                        }
                        catch (Exception er)
                        {
                            ndir.IsSuccess = false;
                            ndir.Msg       = er.Message;
                        }
                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(ndir, Deflate.Compress));
                    }
                }
                break;

                case PackType.MoveFileSystem:
                {
                    MoveFileSystem mfs;

                    if (read.ReadObject <MoveFileSystem>(out mfs))
                    {
                        if ((mfs.OldName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0) || (mfs.NewName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0))
                        {
                            mfs.Msg       = "无法移动,没有权限";
                            mfs.IsSuccess = false;
                            server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(mfs, Deflate.Compress));
                            return;
                        }

                        if (mfs.FileType == FileType.Dir)
                        {
                            try
                            {
                                DirectoryInfo dirinfo = new DirectoryInfo(mfs.OldName);
                                dirinfo.MoveTo(mfs.NewName);
                                mfs.IsSuccess = true;

                                AddLog(user.UserName + " 移动文件 " + mfs.OldName + "->" + mfs.NewName);
                            }
                            catch (Exception er)
                            {
                                mfs.Msg       = er.Message;
                                mfs.IsSuccess = false;
                            }
                        }
                        else
                        {
                            try
                            {
                                FileInfo fileinfo = new FileInfo(mfs.OldName);
                                fileinfo.MoveTo(mfs.NewName);
                                mfs.IsSuccess = true;
                            }
                            catch (Exception er)
                            {
                                mfs.Msg       = er.Message;
                                mfs.IsSuccess = false;
                            }
                        }

                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(mfs, Deflate.Compress));
                    }
                }
                break;

                case PackType.Run:
                {
                    Run run;
                    if (read.ReadObject <Run>(out run))
                    {
                        try
                        {
                            // System.Diagnostics.Process.Start(run.File, run.Arge);
                            run.Msg       = "不支持运行";
                            run.IsSuccess = false;
                        }
                        catch (Exception er)
                        {
                            run.Msg       = er.Message;
                            run.IsSuccess = false;
                        }

                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(run, Deflate.Compress));
                    }
                }
                break;

                case PackType.Down:
                {
                    Down down;
                    if (read.ReadObject <Down>(out down))
                    {
                        FileInfo file = new FileInfo(down.FullName);

                        if (file.Exists)
                        {
                            if (file.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                down.IsSuccess = false;
                                down.Msg       = "没有权限";
                                server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(down, Deflate.Compress));
                                return;
                            }


                            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;


                            AddLog(user.UserName + " 下载文件 " + down.FullName);
                        }
                        else
                        {
                            down.IsSuccess = false;
                            down.Msg       = "未找到文件:" + down.FullName;
                        }

                        server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(down, Deflate.Compress));
                    }
                }
                break;

                case PackType.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))
                            {
                                if (filename.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                                {
                                    BufferFormatV2 buff = new BufferFormatV2(2001);
                                    buff.AddItem(downkey);
                                    buff.AddItem("没有权限");
                                    server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                                    return;
                                }

                                FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                                user.StreamList.TryAdd(downkey, stream);


                                Task SendFlie = new Task(new Action(() =>
                                    {
                                        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);

                                                        user.Asyn.AcceptSocket.Send(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);
                                                        user.Asyn.AcceptSocket.Send(buff.Finish());

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


                                            BufferFormatV2 buffcheck = new BufferFormatV2(2003);
                                            buffcheck.AddItem(downkey);
                                            user.Asyn.AcceptSocket.Send(buffcheck.Finish());
                                        }
                                        catch (Exception er)
                                        {
                                            stream.Close();
                                            FileStream strem;
                                            user.StreamList.TryRemove(downkey, out strem);

                                            BufferFormatV2 buff = new BufferFormatV2(2001);
                                            buff.AddItem(downkey);
                                            buff.AddItem(er.Message);
                                            server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                                        }
                                    }));

                                SendFlie.Start();
                            }
                            else
                            {
                                BufferFormatV2 buff = new BufferFormatV2(2001);
                                buff.AddItem(downkey);
                                buff.AddItem("文件不存在");
                                server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                            }
                        }
                        else
                        {
                            BufferFormatV2 buff = new BufferFormatV2(2001);
                            buff.AddItem(downkey);
                            buff.AddItem("DownKey 不存在");
                            server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                        }
                    }
                }
                break;

                case PackType.DownClose:
                {
                    long downkey;

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

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

                            user.StreamList.TryRemove(downkey, out strem);



                            strem.Close();
                        }
                    }
                }
                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 (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);
                                server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                            }
                            else
                            {
                                BufferFormatV2 buff = new BufferFormatV2(2001);
                                buff.AddItem(downkey);
                                buff.AddItem("DownKey 不存在");
                                server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                            }
                        }
                    }
                }
                break;

                case PackType.UpFile:
                {
                    UpFile upFile;
                    if (read.ReadObject <UpFile>(out upFile))
                    {
                        if (!user.UpFileList.ContainsKey(upFile.UpKey))
                        {
                            try
                            {
                                FileInfo _upfile = new FileInfo(upFile.FullName);

                                if (_upfile.FullName.IndexOf(user.Userinfo.Path, StringComparison.OrdinalIgnoreCase) != 0)
                                {
                                    upFile.IsSuccess = false;
                                    upFile.Msg       = "没有权限";
                                    server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress));
                                    return;
                                }

                                FileStream stream = new FileStream(_upfile.FullName, FileMode.Create, FileAccess.Write);
                                stream.SetLength(upFile.Size);
                                user.UpFileList.TryAdd(upFile.UpKey, stream);

                                user.IsCheckTable.TryAdd(upFile.UpKey, LoadingCheckTable(upFile.Size));

                                upFile.IsSuccess = true;

                                server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress));

                                AddLog(user.UserName + " 上传文件 " + upFile.FullName);
                            }
                            catch (Exception er)
                            {
                                upFile.IsSuccess = false;
                                upFile.Msg       = er.Message;
                                server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress));
                            }
                        }
                        else
                        {
                            upFile.IsSuccess = false;
                            upFile.Msg       = "Key重复";

                            server.SendData(user.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(upFile, Deflate.Compress));
                        }
                    }
                }
                break;

                case PackType.UpClose:
                {
                    long key;
                    if (read.ReadInt64(out key))
                    {
                        if (user.UpFileList.ContainsKey(key))
                        {
                            FileStream stream;
                            user.UpFileList.TryRemove(key, out stream);
                            stream.Close();
                        }

                        if (user.IsCheckTable.ContainsKey(key))
                        {
                            List <CheckB> checkblist;
                            user.IsCheckTable.TryRemove(key, out checkblist);
                        }


                        BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                        buff.AddItem(key);
                        server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                    }
                }
                break;

                case PackType.DateUp:
                {
                    long Key;

                    if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key))
                    {
                        long   startp;
                        long   endp;
                        byte[] buff;

                        if (read.ReadInt64(out startp) && read.ReadInt64(out endp) && read.ReadByteArray(out buff))
                        {
                            //Task task = new Task(() =>
                            //    {
                            try
                            {
                                if (user.IsCheckTable.ContainsKey(Key) && user.UpFileList.ContainsKey(Key))
                                {
                                    List <CheckB> IsCheckTable = user.IsCheckTable[Key];
                                    FileStream    FStream      = user.UpFileList[Key];

                                    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);
                                        }
                                    }
                                    else
                                    {
                                        if (user.UpFileList.ContainsKey(Key))
                                        {
                                            FileStream stream;
                                            user.UpFileList.TryRemove(Key, out stream);
                                            stream.Close();
                                        }

                                        if (user.IsCheckTable.ContainsKey(Key))
                                        {
                                            List <CheckB> checkblist;
                                            user.IsCheckTable.TryRemove(Key, out checkblist);
                                        }

                                        Console.WriteLine("数据验证出错");
                                        BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                                        buffx.AddItem(Key);
                                        server.SendData(user.Asyn.AcceptSocket, buffx.Finish());
                                    }
                                }
                            }
                            catch (Exception er)
                            {
                                Console.WriteLine(er.Message);
                            }
                            //    });

                            //task.Start();
                        }
                    }
                    else
                    {
                        BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                        buff.AddItem(Key);
                        server.SendData(user.Asyn.AcceptSocket, buff.Finish());
                    }
                }
                break;

                case PackType.UpCheck:
                {
                    long Key;

                    if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key))
                    {
                        //Task task = new Task(() =>
                        //           {
                        CheckDown(user, Key, user.IsCheckTable[Key]);
                        //           });

                        //task.Start();
                    }
                    else
                    {
                        BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                        buffx.AddItem(Key);
                        server.SendData(user.Asyn.AcceptSocket, buffx.Finish());
                    }
                }
                break;

                case PackType.DataSet:
                {
                    long Key;

                    if (read.ReadInt64(out Key) && user.UpFileList.ContainsKey(Key))
                    {
                        long   startp;
                        byte[] buff;

                        if (read.ReadInt64(out startp) && read.ReadByteArray(out buff))
                        {
                            //Task task = new Task(() =>
                            //    {
                            if (user.UpFileList.ContainsKey(Key) && user.IsCheckTable.ContainsKey(Key))
                            {
                                List <CheckB> IsCheckTable = user.IsCheckTable[Key];
                                FileStream    FStream      = user.UpFileList[Key];

                                CheckB cb = IsCheckTable.Find(p => p.StartPostion == startp);

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

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

                                CheckDown(user, Key, IsCheckTable);
                            }
                            else
                            {
                                BufferFormatV2 buffx = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                                buffx.AddItem(Key);
                                server.SendData(user.Asyn.AcceptSocket, buffx.Finish());
                            }


                            //    });

                            //task.Start();
                        }
                    }
                }
                break;
                }
            }
        }