示例#1
0
文件: LogOn.cs 项目: gezidan/ZYSOCKET
        /// <summary>
        /// 输入包输入处理
        /// </summary>
        /// <param name="Data"></param>
        void SocketManager_DataInput(byte[] Data)
        {
            ReadBytesV2 read = new ReadBytesV2(Data);

            int length;
            int cmd;

            if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && length == read.Length)
            {
                PACKTYPE cmds=(PACKTYPE)cmd;
                switch (cmds)
                {
                    case  PACKTYPE.LogOnRes: //如果是Message类型数据包
                        LOGONRES ms;
                        if (read.ReadObject<LOGONRES>(out ms))
                        {
                            
                            if (ms != null)
                            {
                                if (!ms.IsLogOn) //1登入失败
                                    MessageBox.Show(ms.Msg);
                                else if (ms.IsLogOn) //2登入成功
                                {
                                    Logins = true; //设置 LOGINS
                                    this.BeginInvoke(new EventHandler((o, p) => Close()));  //关闭窗口
                                   
                                }
                            }
                        }

                        break;
                }
            }
            
        }
示例#2
0
        /// <summary>
        /// 调用模块
        /// </summary>
        /// <param name="data"></param>
        /// <returns>true 属于次模块,false 不属于此模块数据</returns>
        public bool CallModule(byte[] data, SocketAsyncEventArgs e, out ZYClient_Result_Return returnRes)
        {
            returnRes = null;

            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt;
            int cmd;

            if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
            {
                switch (cmd)
                {
                    case 1001000:
                        {

                            ZYClientCall tmp;

                            if (read.ReadObject<ZYClientCall>(out tmp))
                            {
                                object returnValue;

                                CallContext.SetData("Current", e);
                             
                                if (RunModule(tmp, out returnValue))
                                {

                                    if (tmp.IsNeedReturn)
                                    {
                                        ZYClient_Result_Return var = new ZYClient_Result_Return()
                                        {
                                            Id = tmp.Id,
                                            CallTime = tmp.CallTime,
                                            Arguments = tmp.Arguments
                                        };

                                        if (returnValue != null)
                                        {
                                            var.Return = MsgPack.Serialization.SerializationContext.Default.GetSerializer(returnValue.GetType()).PackSingleObject(returnValue);
                                            var.ReturnType = returnValue.GetType().FullName;
                                        }

                                        returnRes = var;
                                    }

                                    return true;
                                }
                            }
                        }
                        break;

                }
            }

            return false;

        }
示例#3
0
文件: LogOn.cs 项目: gezidan/ZYSOCKET
        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.LogOnRes:
                        {
                            LogOnRes res;
                            if (read.ReadObject<LogOnRes>(out res))
                            {
                                if (res.IsOk)
                                {
                                    Path = res.Path;
                                    this.BeginInvoke(new EventHandler((a, b) =>
                                        {
                                            IsLogOn = true;
                                            SocketManager.BinaryInput -= new ZYSocket.ClientB.ClientBinaryInputHandler(SocketManager_BinaryInput); //注意这里须要取消事件,切勿遗忘
                                            SocketManager.Disconnet -= new ZYSocket.ClientB.ClientMessageInputHandler(SocketManager_Disconnet);
                                            Close();
                                        }));
                                }
                                else
                                {
                                    this.BeginInvoke(new EventHandler((a, b) =>
                                    {
                                        MessageBox.Show(res.Msg);
                                       
                                    }));
                                }
                            }

                        }
                        break;

                }

            }
        }
示例#4
0
文件: Form1.cs 项目: gezidan/ZYSOCKET
        void client_DataOn(byte[] Data)
        {
            ReadBytesV2 read = new ReadBytesV2(Data);

            int length;
            int cmd;

            if (read.ReadInt32(out length) && read.ReadInt32(out cmd) && length == read.Length)
            {
                PACKTYPE cmds=(PACKTYPE)cmd;
                switch (cmds)
                {
                   
                   case PACKTYPE.DataRes:
                        DATARES dox;
                        if (read.ReadObject<DATARES>(out dox)) //获取服务器发送过来的 DATASET 
                        {                           
                            if (dox != null)
                            {
                                this.BeginInvoke(new EventHandler((o, x) =>
                                {
                                    DATARES nn = o as DATARES;

                                    if (nn != null)
                                    {
                                        switch (nn.Type)
                                        {
                                            case 1:
                                                {
                                                    foreach (string p in nn.Res)
                                                    {
                                                        this.richTextBox1.AppendText(p + "\r\n");
                                                    }
                                                }
                                                break;
                                        }
                                    }
                                }), dox);
                            }
                        }
                        break;

                }
            }

        }
示例#5
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;
                }

            }
        }
示例#6
0
        /// <summary>
        /// 验证登入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        static void LogOnPack(byte[] data, SocketAsyncEventArgs e)
        {
            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.LogOn:
                        {
                            LogOn logOn;
                            if (read.ReadObject<LogOn>(out logOn))
                            {
                                if (logOn.UserName.Equals(UserName, StringComparison.Ordinal) && logOn.PassWord.Equals(PassWord, StringComparison.Ordinal))
                                {
                                    LogOnRes res = new LogOnRes()
                                    {
                                        IsOk = true,
                                        Msg = "登入成功"
                                    };

                                    UserManager userinfo = new UserManager()
                                    {
                                        Asyn = e,
                                        Stream = e.UserToken as ZYNetBufferReadStreamV2
                                    };

                                    e.UserToken = userinfo;


                                   server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress));

                                }
                                else
                                {
                                    LogOnRes res = new LogOnRes()
                                    {
                                        IsOk = false,
                                        Msg = "用户名或密码错误"
                                    };

                                 

                                    server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress));
                                }

                            }
                        }
                        break;

                }
            }
        }
示例#7
0
        /// <summary>
        /// 验证登入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        void LogOnPack(byte[] data, SocketAsyncEventArgs e)
        {
            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.LogOn:
                        {
                            LogOn logOn;
                            if (read.ReadObject<LogOn>(out logOn))
                            {
                                User user;

                                if ((user=UserList.Find(p=>p.UserName==logOn.UserName&&p.Password==p.Password))!=null)
                                {


                                    LogOnRes res = new LogOnRes()
                                    {
                                        Path = user.Path,
                                        IsOk = true,
                                        Msg = "登入成功"
                                    };

                                    UserManager userinfo = new UserManager()
                                    {
                                        Userinfo=user,
                                        UserName=user.UserName,
                                        Asyn = e,
                                        Stream = e.UserToken as ZYNetRingBufferPoolV2
                                    };

                                    e.UserToken = userinfo;


                                    server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress));

                                    AddLog(logOn.UserName + " 登入成功");

                                }
                                else
                                {
                                    LogOnRes res = new LogOnRes()
                                    {
                                        IsOk = false,
                                        Msg = "用户名或密码错误"
                                    };



                                    server.SendData(e.AcceptSocket, BufferFormatV2.FormatFCA(res, Deflate.Compress));
                                }

                            }
                        }
                        break;

                }
            }
        }
示例#8
0
文件: Form1.cs 项目: gezidan/ZYSOCKET
        void DataOn(byte[] data, UserManager e)
        {
           
                //建立一个读取数据包的类 参数是数据包
                //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用
                ReadBytesV2 read = new ReadBytesV2(data);

                int lengt; //数据包长度,用于验证数据包的完整性
                int cmd; //数据包命令类型

                //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
                if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
                {  //read.Read系列函数是不会产生异常的

                    PACKTYPE cmdType = (PACKTYPE)cmd;

                    //根据命令读取数据包
                    switch (cmdType)
                    {
                        case PACKTYPE.Data:
                            {
                                DATA tmp;

                                if (read.ReadObject<DATA>(out tmp))
                                {
                                    LogOut(e.UserName + "数据命令:" + tmp.CMD);

                                    switch (tmp.CMD)
                                    {
                                        case "GET":
                                            {
                                                DATARES _var1 = new DATARES()
                                                {
                                                    Type = 1,
                                                    Res = new List<string>()
                                                };

                                                _var1.Res.Add("数据1");
                                                _var1.Res.Add("数据2");
                                                _var1.Res.Add("数据3");
                                                _var1.Res.Add("数据4");
                                                _var1.Res.Add("数据5");

                                                

                                                server.SendData(e.Asyn.AcceptSocket, BufferFormatV2.FormatFCA(_var1));
                                            }
                                            break;

                                    }
                                }

                            }
                            break;
                    }

                }

          
        }
示例#9
0
文件: Form1.cs 项目: gezidan/ZYSOCKET
        void LogOnPack(byte[] data, SocketAsyncEventArgs asyn)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt; //数据包长度,用于验证数据包的完整性
            int cmd; //数据包命令类型

            //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
            if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd))
            {  //read.Read系列函数是不会产生异常的

                //根据命令读取数据包
                PACKTYPE cmdType=(PACKTYPE)cmd;

                switch (cmdType)
                {
                    case PACKTYPE.LogOn:
                        {
                            LOGON _logon;

                            if (read.ReadObject<LOGON>(out _logon))
                            {
                                //DOTO:验证用户

                                LogOut(_logon.username + "  登入成功");

                                UserManager tmp = new UserManager()
                                {
                                    Asyn=asyn,
                                    Stream = new ZYNetRingBufferPoolV2(),
                                    UserName=_logon.username
                                };

                                asyn.UserToken = tmp;

                                userlist.Add(tmp);


                                LOGONRES senddata = new LOGONRES()
                                {
                                    IsLogOn=true,
                                    Msg="登入成功"
                                };

                                server.SendData(asyn.AcceptSocket, BufferFormatV2.FormatFCA(senddata));

                            }
                        }
                        break;
                }

            }

        }
示例#10
0
        static void DataOn(byte[] data, SocketAsyncEventArgs e)
        {
            try
            {
                //建立一个读取数据包的类 参数是数据包
                //这个类的功能很强大,可以读取数据包的数据,并可以把你发送过来的对象数据,转换对象引用
                ReadBytes read = new ReadBytesV2(data);

                int lengt; //数据包长度,用于验证数据包的完整性
                int cmd; //数据包命令类型

                //注意这里一定要这样子写,这样子可以保证所有你要度的数据是完整的,如果读不出来 Raed方法会返回FALSE,从而避免了错误的数据导致崩溃
                if (read.ReadInt32(out lengt) && read.Length == lengt && read.ReadInt32(out cmd)) 
                {  //read.Read系列函数是不会产生异常的

                    //根据命令读取数据包
                    switch (cmd) 
                    {
                       
                        case 1000:
                            testClass.PPo temp;
                            if (read.ReadObject<testClass.PPo>(out temp)) 
                            {                              

                                if (temp != null)
                                {
                                    Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n GuidCount:{2} \r\n DataLength:{3} \r\n\r\n", temp.Id, temp.Message, temp.guidList.Count, read.Length,((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port);
                                  
                                }
                            }
                            break;
                        case 1001:
                            {
                                int id;
                                string mn;
                                Guid guid;                              
                                if (read.ReadInt32(out id) && read.ReadString(out mn) && read.ReadObject<Guid>(out guid))
                                {

                                    Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n Guid:{2} \r\n DataLength:{3} \r\n\r\n", id, mn, guid, read.Length, ((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port);
                                  
                                }

                            }
                            break;
                        case 1002:
                            {
                                int id;
                                string mn;
                                string guid;

                                if (read.ReadInt32(out id) && read.ReadString(out mn) && read.ReadString(out guid))
                                {

                                    Console.WriteLine("Port:{4} Id:{0}\r\n Mn:{1} \r\n Guid:{2} \r\n DataLength:{3} \r\n\r\n", id, mn, guid, read.Length, ((System.Net.IPEndPoint)e.AcceptSocket.RemoteEndPoint).Port);

                                }

                            }
                            break;
                        case 1003:
                            {
                                server.SendData(e.AcceptSocket, data);

                            }
                            break;
                        case 2000:
                            {
                                List<testClass.PPo2> tmp;

                                if (read.ReadObject<List<testClass.PPo2>>(out tmp))
                                {

                                }
                            }
                            break;


                    }


                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
            }
        }
示例#11
0
文件: Form1.cs 项目: gezidan/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;

                }
            }


        }
示例#12
0
文件: Form1.cs 项目: gezidan/ZYSOCKET
        private void SuccessData(UserInfo user, byte[] data)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

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

                            if (read.ReadObject<Success>(out success))
                            {
                                if (!success.IsRes)
                                {

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

                                        UpdateUserListView();

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

                                         
                                        }));
                                    }
                                    else
                                    {
                                        user.IsValidate = true;
                                        UpdateUserListView();
                                    }
                                }
                            }
                        }
                        break;
                    case FileCmd.GetFile:
                        {
                            GetFile dir;

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

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

                                            dir.FileSystemList.Sort(new Comparison<FileSystem>((a1, a2) =>
                                            {
                                                if (a1.FileType == FileType.Dir && a2.FileType == FileType.Dir)
                                                {
                                                    return a1.Name.CompareTo(a2.Name);

                                                }
                                                else if (a1.FileType != FileType.Dir && a2.FileType != FileType.Dir)
                                                {
                                                    return a1.Name.CompareTo(a2.Name);
                                                }
                                                else if (a1.FileType == FileType.Dir && a2.FileType == FileType.File)
                                                {
                                                    return -1;
                                                }
                                                else if (a2.FileType == FileType.Dir && a1.FileType == FileType.File)
                                                {
                                                    return 1;
                                                }
                                                else
                                                {
                                                    return 0;
                                                }

                                            }));

                                            int x1 = 0, x2 = 0;
                                            foreach (FileSystem p in dir.FileSystemList)
                                            {
                                                if (p.FileType == FileType.File)
                                                {

                                                    this.listView2.Items.Add(new ListViewItem()
                                                    {
                                                        Text = p.Name,
                                                        ImageIndex = GetImageIndex(p.Name),
                                                        Tag = p

                                                    });
                                                    x1++;
                                                }
                                                else
                                                {
                                                    this.listView2.Items.Add(new ListViewItem()
                                                    {
                                                        Text = p.Name,
                                                        ImageIndex = 0,
                                                        Tag = p
                                                    });

                                                    x2++;
                                                }

                                            }
                                        }
                                    }));

                                }

                            }

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

                                        }
                                        else
                                        {
                                            MessageBox.Show(down.Msg);
                                        }

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