Exemplo n.º 1
0
Arquivo: Form1.cs Projeto: pr0gr4m/FM
        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (listServer.SelectedItems[0].Tag.ToString() == "F")
            {
                RecvFile(servFile);
            }
            else if (listServer.SelectedItems[0].Tag.ToString() == "D")
            {
                FileMeta finfo = new FileMeta(0, servFile, "");
                finfo.Type = (int)PacketType.ReqDir;
                Packet.Serialize(finfo).CopyTo(this.sendBuf, 0);
                this.Send();
                RecvFile(servFile + ".tmp.zip");
                PUtility.ExtractDirectoryTmp(dirPath + "\\" + Path.GetFileName(servFile + ".tmp.zip"));
                File.Delete(dirPath + "\\" + Path.GetFileName(servFile + ".tmp.zip"));
            }
            var item = viewClient.SelectedNode;

            viewClient.SelectedNode = null;
            viewClient.SelectedNode = item;
        }
Exemplo n.º 2
0
        public void Run()
        {
            // Connect
            try
            {
                this.listener = new TcpListener(PUtility.PORT_NUM);
                this.listener.Start();

                this.Invoke(new MethodInvoker(delegate()
                {
                    txtLog.AppendText("waiting for client access...\r\n");
                }));

                TcpClient client = this.listener.AcceptTcpClient();
                this.stream = client.GetStream();
                this.Invoke(new MethodInvoker(delegate()
                {
                    txtLog.AppendText("Client Access!!\r\n");
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            InitCase();
            SetCase();

            // IO
            while (true)
            {
                if (Recv() == 0)
                {
                    return;
                }

                Packet packet = (Packet)Packet.Deserialize(this.recvBuf);

                switch ((int)packet.Type)
                {
                case (int)PacketType.FileMeta:
                    HandleFileMeta();
                    break;

                case (int)PacketType.SendDir:
                    FileMeta finfo = (FileMeta)Packet.Deserialize(this.recvBuf);
                    Recv();
                    HandleFileMeta();
                    PUtility.ExtractDirectoryTmp(pathCur +
                                                 Path.GetFileName(finfo.fileName));
                    File.Delete(pathCur +
                                Path.GetFileName(finfo.fileName));
                    break;

                case (int)PacketType.ReqDirList:
                    HandleReqDirList();
                    break;

                case (int)PacketType.ReqFileList:
                    HandleReqFileList();
                    break;

                case (int)PacketType.ReqFile:
                    HandleReqFile();
                    break;

                case (int)PacketType.ReqDir:
                    FileMeta dinfo = (FileMeta)Packet.Deserialize(this.recvBuf);
                    PUtility.CompressDirectoryTmp(pathCur + dinfo.fileName);
                    Recv();
                    HandleReqFile();
                    File.Delete(pathCur + dinfo.fileName + ".tmp.zip");
                    break;
                }
            }
        }