コード例 #1
0
ファイル: UpFiles.cs プロジェクト: gezidan/ZYSOCKET
        void main_UpFileClose(long key)
        {
            if (key == Key)
            {
                if (RConfig.ReadInt("IsCloseUP") != 1)
                {
                    IsClose = true;
                    this.Text = "上传完毕";
                    this.button1.Text = "完成";
                    this.Text += "----完成";

                    BufferFormatV2 buffcheck = new BufferFormatV2((int)PackType.UpCheck);
                    buffcheck.AddItem(Key);
                    SocketManager.Send(buffcheck.Finish());
                }
                else
                {
                    IsClose = true;
                    this.Text = "上传完毕";
                    this.button1.Text = "完成";
                    this.Text += "----完成";
                    BufferFormatV2 buffcheck = new BufferFormatV2((int)PackType.UpCheck);
                    buffcheck.AddItem(Key);
                    SocketManager.Send(buffcheck.Finish());
                    main.GotoDir();
                    this.Close();
                }
            }
        }
コード例 #2
0
ファイル: UpFiles.cs プロジェクト: gezidan/ZYSOCKET
        void UpFiles_FormClosed(object sender, FormClosedEventArgs e)
        {
            BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose);
            buff.AddItem(Key);
            SocketManager.Send(buff.Finish());

            main.UpFileClose -= new UpFileCloseHandler(main_UpFileClose);
        }
コード例 #3
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        void DownFile_FormClosed(object sender, FormClosedEventArgs e)
        {
            BufferFormatV2 buff = new BufferFormatV2((int)FileCmd.DownClose);
            buff.AddItem(down.DownKey);
            user.SendData(buff.Finish());

            if (FStream != null)
                FStream.Close();

            user.DownDataOn = null;
        }
コード例 #4
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        void DownFile_FormClosed(object sender, FormClosedEventArgs e)
        {
            BufferFormatV2 buff = new BufferFormatV2((int)PackType.DownClose);
            buff.AddItem(down.DownKey);
            SocketManager.Send(buff.Finish());


            if (FStream != null)
                FStream.Close();

            SocketManager.BinaryInput -= new ZYSocket.ClientB.ClientBinaryInputHandler(SocketManager_BinaryInput);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: gezidan/ZYSOCKET
        static ZYNetRingBufferPoolV2 BufferPool = new ZYNetRingBufferPoolV2(4096); //128Bit
        static void Main(string[] args)
        {
          

            Task.Factory.StartNew(() =>
            {

                while (true)
                {
                    byte[] datax;
                    while (BufferPool.Read(out datax))
                    {
                        DataOn(datax); //打印数据包内容
                    }


                }


            });

            for (int i = 0; i < 20000; i++)
            {

                BufferFormatV2 buff1 = new BufferFormatV2(i);
                buff1.AddItem("HHHHHHHHHHH");

                byte[] data = buff1.Finish();


               // byte[] pdata = new byte[1024];

                // Buffer.BlockCopy(data, 0, pdata, 500, data.Length);


                // BufferPool.Write(data);
                foreach (var item in data)
                {
                    BufferPool.Write(new byte[] { item });
                      
                }

                // BufferPool.Write(pdata, 500, data.Length);

                System.Threading.Thread.Sleep(1);

            }


            Console.WriteLine("Full Count:" + Lengt);
            Console.ReadLine();
        }
コード例 #6
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        public void DataIn(byte[] data)
        {

            ReadBytesV2 read = new ReadBytesV2(data);

            int lengt;
            int cmd;

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

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

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

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

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


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

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

                                                }
                                            }
                                            else
                                            {
                                                this.BeginInvoke(new EventHandler((a1, b1) =>
                                                {
                                                    BufferFormatV2 bufff = new BufferFormatV2((int)FileCmd.DownClose);
                                                    bufff.AddItem(down.DownKey);
                                                    user.SendData(bufff.Finish());
                                                    MessageBox.Show("数据验证出错??");
                                                    Close();
                                                }));
                                            }

                                        });
                                    }
                                }

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

                            }


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

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

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

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


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

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

                                            }


                                            CheckDown();
                                        }));
                                    }
                                }

                            }

                        }
                        break;

                }

            }
        }
コード例 #7
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        private void DownFile_Load(object sender, EventArgs e)
        {
            LoadingCheckTable();

            ProcessMax = IsCheckTable.Count;

            this.progressBar1.Maximum = ProcessMax;
            this.progressBar1.Value = ProcessValue;

            FileInfo test = new FileInfo(down.FullName);

            saveFileDialog1.FileName = test.Name;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FStream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite);
                DownDir = saveFileDialog1.FileName;
                if (down.Size == 0)
                {
                    this.Text = DownDir + "--下载完毕";
                    this.button1.Text = "完成";
                    MessageBox.Show(Text);

                    Close();
                    return;
                }

                BufferFormatV2 buff = new BufferFormatV2((int)FileCmd.DownNow);
                buff.AddItem(down.DownKey);
                user.SendData(buff.Finish());
            }
            else
            {
                this.Close();
            }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: gezidan/ZYSOCKET
        static 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());
            }
        }
コード例 #9
0
ファイル: UpFiles.cs プロジェクト: gezidan/ZYSOCKET
        private void UpFileThread(object o)
        {
            try
            {
                byte[] data = new byte[bytelengt];

                int r = 0;
                long p = 0;
                do
                {
                    if (IsClose)
                        break;

                    r = Stream.Read(data, 0, data.Length);

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

                        Buffer.BlockCopy(data, 0, buffb, 0, buffb.Length);
                        BufferFormatV2 buff = new BufferFormatV2((int)PackType.DateUp);
                        buff.AddItem(Key);
                        buff.AddItem(p);
                        buff.AddItem(p + r - 1);
                        buff.AddItem(buffb);

                        SocketManager.Send(buff.Finish());

                        sendlength += r;

                        this.BeginInvoke(new EventHandler((a, b) =>
                        {
                            //if( this.progressBar1.Value< this.progressBar1.Maximum)
                            //    this.progressBar1.Value++;
                            this.label1.Text = Math.Round(((double)sendlength / 1024 / 1024), 4) + "MB/" + Math.Round(((double)fileLength / 1024 / 1024), 4) + "MB";

                            double x = ((double)sendlength / fileLength * 1000);

                            this.progressBar1.Value = (int)x;
                        }));

                        break;
                    }
                    else if (r > 0)
                    {
                        BufferFormatV2 buff = new BufferFormatV2((int)PackType.DateUp);
                        buff.AddItem(Key);
                        buff.AddItem(p);
                        buff.AddItem(p + r - 1);
                        buff.AddItem(data);
                        SocketManager.Send(buff.Finish());

                        p += r;
                    }

                    sendlength += r;

                    this.BeginInvoke(new EventHandler((a, b) =>
                        {

                            this.label1.Text = Math.Round(((double)sendlength / 1024 / 1024), 4) + "MB/" + Math.Round(((double)fileLength / 1024 / 1024), 4) + "MB";

                            double x =((double)sendlength / fileLength * 1000);

                            this.progressBar1.Value = (int)x;
                        }));


                } while (r > 0);



                BufferFormatV2 buffcheck = new BufferFormatV2((int)PackType.UpCheck);
                buffcheck.AddItem(Key);
                SocketManager.Send(buffcheck.Finish());

                this.BeginInvoke(new EventHandler((a, b) =>
                    {
                        button2.Enabled = true;
                    }));

            }
            catch (System.Net.Sockets.SocketException e)
            {
                MessageBox.Show("上传文件发生错误:" + e.Message);
                IsClose = true;
            }
            catch (Exception er)
            {
                BufferFormatV2 buff = new BufferFormatV2((int)PackType.UpClose, Deflate.Compress);
                buff.AddItem(Key);
                SocketManager.Send(buff.Finish());
                IsClose = true;
                MessageBox.Show("上传文件发生错误:" + er.Message);
            }
        }
コード例 #10
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
 /// <summary>
 /// 获取全部用户
 /// </summary>
 public void BeginGetAllUser()
 {
     BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLUSER);
     Mainclient.Send(tmp.Finish());
 }
コード例 #11
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
 /// <summary>
 /// 重新连接所有的用户
 /// </summary>
 public void ResetConnClient()
 {           
     BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK);
     Mainclient.Send(tmp.Finish());
 }
コード例 #12
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
        public void ConToServer()
        {
         
            Mainclient = new SocketClient();
            Mainclient.BinaryInput += new ClientBinaryInputHandler(DataIn);
            Mainclient.MessageInput += new ClientMessageInputHandler(Exption);

            if (Mainclient.Connect(Host, Port))
            {               
                Mainclient.StartRead();

                LogOut.LogIn("成功连接服务器", ActionType.ServerConn);

                string localip = ((IPEndPoint)(Mainclient.Sock.LocalEndPoint)).Address.ToString(); //获取本地局域网IP地址

                BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.REGION);
                tmp.AddItem(Key);
                tmp.AddItem(localip);
                tmp.AddItem(Mac);
                Mainclient.Send(tmp.Finish());
              
            }
            else
            {             
                LogOut.LogIn("不能连接到服务器", ActionType.ServerNotConn);

                if (ServerDiscon != null)
                    ServerDiscon("不能连接到服务器");
            }

        }
コード例 #13
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;

                }
            }


        }
コード例 #14
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        private void DownFile_Load(object sender, EventArgs e)
        {
          

            SocketManager.BinaryInput += new ZYSocket.ClientB.ClientBinaryInputHandler(SocketManager_BinaryInput);

            LoadingCheckTable();

            ProcessMax = IsCheckTable.Count;

            this.progressBar1.Maximum = ProcessMax;
            this.progressBar1.Value = ProcessValue;

            FileInfo test = new FileInfo(down.FullName);

            saveFileDialog1.FileName = test.Name;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FStream = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite);

               // FStream.SetLength(down.Size);

                DownDir = saveFileDialog1.FileName;
                if (down.Size == 0)
                {
                    this.Text = DownDir + "--下载完毕";
                    this.button1.Text = "完成";
                    MessageBox.Show(Text);

                    Close();
                    return;
                }

                BufferFormatV2 buff = new BufferFormatV2((int)PackType.DownNow);
                buff.AddItem(down.DownKey);
                SocketManager.Send(buff.Finish());
            }
            else
            {
                this.Close();
            }
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: gezidan/ZYSOCKET
        static void InputData(byte[] data, UserInfo user)
        {
            ReadBytesV2 read = new ReadBytesV2(data);

            int length;
            int Cmd;
            if (read.ReadInt32(out length) && length == read.Length && read.ReadInt32(out Cmd))
            {
                PCMD pcmd = (PCMD)Cmd;


                switch (pcmd)
                {
                    case PCMD.REGION: //注册                      
                        string key1;
                        string lanhost;
                        string mac;

                        if (read.ReadString(out key1) && read.ReadString(out lanhost)&&read.ReadString(out mac))
                        {
                            user.Paw = key1;
                            user.LANhost = lanhost;
                            user.Mac = mac;

                            if (!UserList.ContainsKey(user.Paw))
                            {
                                if (UserList.TryAdd(user.Paw, user))
                                {
                                    Console.WriteLine(user.Asyn.AcceptSocket.RemoteEndPoint.ToString() + "连接服务器 用户KEY:" + user.Paw + " 内网IP:" + user.LANhost + " 外网IP地址:" + user.WANIP);
                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.SET);
                                    MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish());
                                }
                            }
                        }
                        break;
                    case PCMD.GETALLMASK: //获取所有用户KEY列表
                        if (user.Paw != null)
                        {

                            BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ALLUSER);


                            IEnumerable<UserInfo> userlist = UserList.Values.Where(p => p.Mac == user.Mac && p.Paw != user.Paw);

                            tmp.AddItem(userlist.Count());

                            foreach (var item in userlist)
                            {
                                tmp.AddItem(item.Paw);
                            }

                            MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish());
                        }
                        break;
                    case PCMD.CONN: //连接目标主机
                        string key;

                        if (read.ReadString(out key))//读取客户端KEY
                        {

                            if (UserList.ContainsKey(key) && !string.IsNullOrEmpty(user.WANhost) && !string.IsNullOrEmpty(user.CPort)) //检查此客户单是否是可以提供连接
                            {

                                UserInfo info = UserList[key]; //读取用户对象


                                if (info.Mac != user.Mac)
                                {
                                    return;
                                }

                                if (!user.WANIP.Equals(info.WANIP)) //如果不在同一个局域网
                                {

                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.NOWCONN);
                                    tmp.AddItem(user.WANhost + ":" + user.CPort);
                                    tmp.AddItem(user.Paw);
                                    MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish());                                   
                                }
                                else
                                { 
                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.NOWCONN);
                                    tmp.AddItem(user.LANhost + ":" + user.NatNetPort);
                                    tmp.AddItem(user.Paw);
                                    MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish());
                                }

                            }
                        }
                        break;
                    case PCMD.LEFTCONN:
                        string key2;
                        if (read.ReadString(out key2))
                        {
                            if (UserList.ContainsKey(key2) && !string.IsNullOrEmpty(user.WANhost) && !string.IsNullOrEmpty(user.CPort))
                            {
                                UserInfo info = UserList[key2];  //读取用户对象

                                if (info.Mac != user.Mac)
                                {
                                    return;
                                }

                                if (!user.WANIP.Equals(info.WANIP)) //如果不在同一个局域网
                                {
                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.LEFTCONN);
                                    tmp.AddItem(user.WANhost + ":" + user.CPort);
                                    tmp.AddItem(user.Paw);
                                    MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish());
                                }
                                else
                                {
                                   
                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.LEFTCONN);
                                    tmp.AddItem(user.LANhost + ":" + user.NatNetPort);
                                    tmp.AddItem(user.Paw);
                                    MainServer.SendData(info.Asyn.AcceptSocket, tmp.Finish());
                                }
                            }
                        }
                        break;
                    case PCMD.GETALLUSER:
                        {
                            BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLUSER);

                            IEnumerable<UserInfo> userlist = UserList.Values.Where(p => p.Mac == user.Mac&&p.Paw!=user.Paw);

                            tmp.AddItem(userlist.Count());

                            foreach (var item in userlist)
                            {
                                tmp.AddItem(item.Paw);
                            }

                            MainServer.SendData(user.Asyn.AcceptSocket, tmp.Finish());
                        }
                        break;
                    case PCMD.ProxyData:
                        {
                            string keys;
                            byte[] datas;

                            if (read.ReadString(out keys) && read.ReadByteArray(out datas))
                            {
                                if (UserList.ContainsKey(keys))
                                {
                                    BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ProxyData);
                                    tmp.AddItem(user.Paw);
                                    tmp.AddItem(datas);
                                    MainServer.SendData(UserList[keys].Asyn.AcceptSocket, tmp.Finish());
                                }
                            }

                        }
                        break;
                }

            }
        }
コード例 #16
0
ファイル: UpFiles.cs プロジェクト: gezidan/ZYSOCKET
 private void button2_Click(object sender, EventArgs e)
 {
     BufferFormatV2 buffcheck = new BufferFormatV2((int)PackType.UpCheck);
     buffcheck.AddItem(Key);
     SocketManager.Send(buffcheck.Finish());
 }
コード例 #17
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
 /// <summary>
 /// 发送数据到指定的客户端
 /// </summary>
 /// <param name="key"></param>
 /// <param name="data"></param>
 public void SendData(string key, byte[] data)
 {
     if (ConnUserList.ContainsKey(key))
     {
         ConnUserList[key].SendData(data);
     }
     else
     {
         BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.ProxyData);
         tmp.AddItem(key);
         tmp.AddItem(data);
         Mainclient.Send(tmp.Finish());
     }
 }
コード例 #18
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
        /// <summary>
        /// 数据包处理
        /// </summary>
        /// <param name="data"></param>
        private void BufferIn(byte[] data)
        {

            ReadBytesV2 read = new ReadBytesV2(data);
            int length;
            if (read.ReadInt32(out length) && length == read.Length)
            {
                int cmd;

                if (read.ReadInt32(out cmd))
                {
                    PCMD pcmd = (PCMD)cmd;


                    switch (pcmd)
                    {
                        case PCMD.SET: //准备就绪
                          
                            BufferFormatV2 tmp = new BufferFormatV2((int)PCMD.GETALLMASK);
                            Mainclient.Send(tmp.Finish());
                            break;
                        case PCMD.ALLUSER: //获取全部用户列表
                            try
                            {
                                int count;
                                if (read.ReadInt32(out count))
                                {
                                    for (int i = 0; i < count; i++)
                                    {
                                        string usermask;

                                        if (read.ReadString(out usermask))
                                        {
                                            UserMaskList.Enqueue(usermask);
                                        }
                                    }


                                    RunQueueList();
                                }
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                            }
                            break;
                        case PCMD.NOWCONN:  //立刻连接到指定IP端口
                            string host;
                            string key;

                            if (read.ReadString(out host) && read.ReadString(out key))
                            {
                                host = host + ":" + key;

                                SocketClient client = new SocketClient();
                            Tp:
                                IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定端口
                                if (BindPort >= 60000)
                                    BindPort = 1000;

                                try
                                {
                                    client.Sock.Bind(endpoint); //如果无法绑定那么重新选个端口
                                }
                                catch
                                {
                                    goto Tp;
                                }

                                if (client.Connect(this.Host, RegIpPort)) //连接到注册端口
                                {
                                   
                                    BufferFormat tmpX = new BufferFormat(100);
                                    tmpX.AddItem(Key);
                                    tmpX.AddItem(BindPort);
                                    client.Send(tmpX.Finish());

                                    System.Threading.Thread.Sleep(50);

                                  
                                    BufferFormatV2 tmpX2 = new BufferFormatV2((int)PCMD.LEFTCONN);
                                    tmpX2.AddItem(key);
                                    Mainclient.Send(tmpX2.Finish());

                                    client.Close();

                                    System.Threading.Thread.Sleep(50);

                                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host);

                                }
                            }
                            break;
                        case PCMD.LEFTCONN:
                            string host2;
                            string key2;
                            if (read.ReadString(out host2) && read.ReadString(out key2))
                            {
                                host2 = host2 + ":" + key2;
                                System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(RunConnToMe), host2);
                            }
                            break;
                        case PCMD.GETALLUSER:
                            {
                                int count;

                                if (read.ReadInt32(out count))
                                {
                                    AllUser = new List<string>();

                                    for (int i = 0; i < count; i++)
                                    {
                                        string var;
                                        if (read.ReadString(out var))
                                        {
                                            AllUser.Add(var);
                                        }
                                        else
                                            break;
                                    }

                                    if (GetAllUserList != null)
                                        GetAllUserList(AllUser);

                                }

                            }
                            break;
                        case PCMD.ProxyData:
                            {
                                string keys;
                                byte[] buff;

                                if (read.ReadString(out keys) && read.ReadByteArray(out buff))
                                {
                                    if (ProxyList.ContainsKey(keys))
                                    {
                                        client_DataOutPut(keys, ProxyList[keys], buff);
                                    }
                                    else
                                    {
                                        ConClient client = new ConClient(keys);

                                        if (ProxyList.TryAdd(client.Key, client))
                                        {
                                            client_DataOutPut(keys, client, buff);
                                        }
                                    }
                                }

                            }
                            break;

                    }
                }
            }


        }
コード例 #19
0
ファイル: ClientInfo.cs プロジェクト: gezidan/ZYSOCKET
        void RunQueueList()
        {
            try
            {
                if (UserMaskList.Count > 0) //如果列队数量大于0
                {
                Re:
                    string userkey;
                    
                    if (UserMaskList.TryDequeue(out userkey)) //挤出一个用户ID
                    {

                        if (userkey == Key)
                            goto Re;

                        SocketClient client = new SocketClient(); //建立一个 SOCKET客户端
                    Pt:
                        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, BindPort++); //绑定当前端口

                        if (BindPort >= 60000)
                            BindPort = 1000;

                        try
                        {
                            client.Sock.Bind(endpoint); //如果无法绑定此端口 那么换个端口
                        }
                        catch
                        {
                            goto Pt;
                        }

                        if (client.Connect(Host, RegIpPort)) //连接注册服务器端口
                        {
                            BufferFormat tmp = new BufferFormat(100);
                            tmp.AddItem(Key);
                            tmp.AddItem(BindPort);
                            client.Send(tmp.Finish());

                            System.Threading.Thread.Sleep(50); //等待 50毫秒

                            BufferFormatV2 tmp2 = new BufferFormatV2((int)PCMD.CONN);
                            tmp2.AddItem(userkey);
                            Mainclient.Send(tmp2.Finish());
                            client.Close();//关闭客户端

                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogOut.LogIn(e.ToString(),ActionType.Error);
            }
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: gezidan/ZYSOCKET
        static 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:
                        {
                            GetDisk tmp;
                            if (read.ReadObject<GetDisk>(out tmp))
                            {
                                DriveInfo[] MyDrives = DriveInfo.GetDrives();

                                foreach (var p in MyDrives)
                                {
                                    if (p.IsReady)
                                    {
                                        DiskInfo disk = new DiskInfo()
                                           {
                                               AvailableFreeSpace = p.AvailableFreeSpace,
                                               DriveFormat = p.DriveFormat,
                                               DriveType = p.DriveType,
                                               IsReady = p.IsReady,
                                               Name = p.Name,
                                               TotalFreeSpace = p.TotalFreeSpace,
                                               TotalSize = p.TotalSize,
                                               VolumeLabel = p.VolumeLabel

                                           };

                                        tmp.DiskList.Add(disk);
                                    }
                                    else
                                    {
                                        DiskInfo disk = new DiskInfo()
                                        { 
                                            DriveType = p.DriveType,
                                            IsReady = p.IsReady,
                                            Name = p.Name    
                                        };
                                        tmp.DiskList.Add(disk);
                                    }

                                }


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

                            }

                        }
                        break;
                    case PackType.Dir:
                        {
                            Dir dir;
                            if (read.ReadObject<Dir>(out dir))
                            {
                                string dirname = dir.DirName;

                                if (string.IsNullOrEmpty(dirname))
                                {
                                    dirname = "/";

                                }


                                DirectoryInfo dirinfo = new DirectoryInfo(dirname);

                                if (dirinfo.Exists)
                                {

                                    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 = "无法找到目录:" + 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.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;
                                             }
                                             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
                                 {
                                     Directory.CreateDirectory(ndir.DirName);
                                     ndir.IsSuccess = true;
                                 }
                                 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.FileType == FileType.Dir)
                                {
                                    try
                                    {
                                        DirectoryInfo dirinfo = new DirectoryInfo(mfs.OldName);
                                        dirinfo.MoveTo(mfs.NewName);
                                        mfs.IsSuccess = true;
                                    }
                                    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.IsSuccess = true;
                                }
                                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)
                                {
                                    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;
                                }

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

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

                }

            }


        }
コード例 #21
0
ファイル: DownFile.cs プロジェクト: gezidan/ZYSOCKET
        private void CheckDown()
        {
            CheckB p = IsCheckTable.Find(x => x.Checkd == false);

            if (p == null)
            {
                BufferFormatV2 buff = new BufferFormatV2((int)FileCmd.DownClose);
                buff.AddItem(down.DownKey);
                user.SendData(buff.Finish());

                FStream.Close();



                this.BeginInvoke(new EventHandler((a, b) =>
                {
                    this.Text = DownDir + "--下载完毕";
                    this.button1.Text = "完成";
                    MessageBox.Show(Text);

                }));
            }
            else
            {
                BufferFormatV2 buff = new BufferFormatV2((int)FileCmd.ReBytes);
                buff.AddItem(down.DownKey);
                buff.AddItem(p.StartPostion);
                buff.AddItem(p.Size);
                user.SendData(buff.Finish());

            }
        }
コード例 #22
0
ファイル: WinMain.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.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;
                }

            }
        }
コード例 #23
0
ファイル: Program.cs プロジェクト: gezidan/ZYSOCKET
        static void Main(string[] args)
        {

         

            client.DataOn += new DataOn(client_DataOn); //数据包进入事件

            client.Disconnection += new ExceptionDisconnection(client_Disconnection); //数据包断开事件

            if (client.ConnectionTo(RConfig.ReadString("Host"), RConfig.ReadInt("Port"))) //使用同步连接到服务器,一步就用Begin开头的那个
            {
                int i = 0;
                while (true)
                {
                    Console.ReadLine();




                    testClass.PPo temp = new testClass.PPo();
                    temp.Id = i;
                    temp.Message = "通过对象通讯";
                    temp.guidList = new List<Guid>();

                    i++;

                    for (int x = 0; x <= i; x++)
                    {
                        temp.guidList.Add(Guid.NewGuid());
                    }



                    client.SendTo(new byte[] { 1, 2, 3, 4, 5, 6 });

                    client.SendTo(BufferFormatV2.FormatFCA(temp));  //讲一个PPO对象发送出去


                 
                    BufferFormat buffmat = new BufferFormatV2(1001);
                    buffmat.AddItem(i);
                    buffmat.AddItem("通过组合数据包通讯,GUID is object");
                    buffmat.AddItem(Guid.NewGuid());
                    client.SendTo(buffmat.Finish()); //用组合数据包模拟PPO对象



                    BufferFormat buffmat2 = new BufferFormatV2(1002);
                    buffmat2.AddItem(i);
                    buffmat2.AddItem("通过组合数据包通讯 all buff");
                    buffmat2.AddItem(Guid.NewGuid().ToString());
                    client.SendTo(buffmat2.Finish()); //用组合数据包模拟PPO对象 但GUID 是字符串类型



                    List<testClass.PPo2> tmplist = new List<testClass.PPo2>();


                    for (int l = 0; l < 30; l++)
                    {

                        testClass.PPo2 ver2 = new testClass.PPo2();
                        ver2.Id = i;
                        ver2.Message = "列表复用";
                        ver2.PPoList = new List<testClass.PPo>();

                        for (int j = 0; j < 10; j++)
                        {
                            testClass.PPo temp2 = new testClass.PPo();
                            temp2.Id = i;
                            temp2.Message = "通过对象通讯";
                            temp2.guidList = new List<Guid>();

                            for (int x = 0; x <= 20; x++)
                            {
                                temp2.guidList.Add(Guid.NewGuid());
                            }

                            ver2.PPoList.Add(temp2);
                        }

                        tmplist.Add(ver2);
                    }

                    BufferFormatV2 buffer = new BufferFormatV2(2000);
                    buffer.AddItem(tmplist);


                    client.SendTo(buffer.Finish());
                    
                }

            }
            else
            {
                Console.WriteLine("无法连接服务器");
            }

            Console.ReadLine();
        }