Beispiel #1
0
        private async Task <int> SendFiles(string path)
        {
            List <FileInfo> files  = stLib_CS.File.FileHelper.GetFiles(path);
            int             i      = 0;
            NStream         stream = m_Client.stream;

            if (!CurrentPingStatus())
            {
                m_sleeptime = 0;
            }
            await stream.WriteInt32(files.Count);

            await stream.WriteString(GetCurrentHandIn().Path + path.Substring(path.LastIndexOf('\\')));

            foreach (var file in files)
            {
                if (m_sleeptime == 0)
                {
                    m_sleeptime = file.Length * 1000 / 1024 / 200;
                }
                Sleep();
                FileStream fileStream;
                try {
                    fileStream = file.OpenRead();
                } catch (Exception e) {
                    MessageBox.Show("无法打开指定的文件");
                    return(0);
                }
                await stream.WriteInt64(file.Length);

                await stream.WriteString(file.Name);

                int res = await stream.WriteBigFrom(fileStream);

                i++;
                this.Text = "正在发送第" + i.ToString() + "个文件";
            }
            MessageBox.Show("传输成功");
            RefreshText();
            return(0);
        }
Beispiel #2
0
        private async Task <int> SendFile(string path)
        {
            NStream  stream = m_Client.stream;
            FileInfo file   = new FileInfo(path);

            if (!CurrentPingStatus())
            {
                m_sleeptime = 0;
            }
            await stream.WriteInt32(1);

            await stream.WriteString(GetCurrentHandIn().Path);

            if (m_sleeptime == 0)
            {
                m_sleeptime = file.Length * 1000 / 1024 / 200;
            }
            Sleep();

            FileStream fileStream;

            try {
                fileStream = file.OpenRead();
            } catch (Exception e) {
                MessageBox.Show("无法打开指定的文件");
                return(0);
            }
            await stream.WriteInt64(file.Length);

            await stream.WriteString(file.Name);

            int res = await stream.WriteBigFrom(fileStream);

            MessageBox.Show("传输成功");
            RefreshText();
            return(0);
        }
        public async Task <int> Send(Android.Content.Context context, CoordinatorLayout cl, string workName, string serverName, string folderName, List <string> paths)
        {
            HHI_HandIn     hi         = GetCurrentHandIn(workName);
            HHI_ServerInfo serverInfo = GetCurrentServerInfo(serverName);

            if (client == null)
            {
                client = new stLib_CS.Net.Client(serverInfo.IP, serverInfo.Port);
            }
            else
            {
                if (client.Connected())
                {
                    if (!client.IsServerDisconnected())
                    {
                        HHI_Android.ShowSimpleAlertView(context, "提示", "上一次传输还未完成,请稍后尝试。");
                        return(-1);
                    }
                }
                client.Disconnect();
                client = new stLib_CS.Net.Client(serverInfo.IP, serverInfo.Port);
            }

            //--- Connect ---
            try {
                if (1 == await client.Connect())
                {
                    HHI_Android.ShowSimpleAlertView(context, "错误", "IP地址不合法");
                }
            } catch (Exception ex) {
                HHI_Android.ShowSimpleAlertView(context, "错误", "连接错误,错误信息:\n" + ex.Message.ToString());
                return(-1);
            }
            // Update state Connected
            Snackbar.Make(cl, "连接成功!", Snackbar.LengthShort).Show();

            // Prepare for zip
            // Create path for tmp
            string targetFolderPath = System.IO.Path.Combine(stLib_CS.Compress.tmppath, folderName);

            if (Directory.Exists(stLib_CS.Compress.tmppath))
            {
                DeleteFolder(stLib_CS.Compress.tmppath);
            }
            Directory.CreateDirectory(stLib_CS.Compress.tmppath);
            if (!Directory.Exists(targetFolderPath))
            {
                Directory.CreateDirectory(targetFolderPath);
            }

            // Update state creating cache
            try {
                foreach (var path in paths)
                {
                    // TODO compress images
                    // Compress and Save
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.InJustDecodeBounds = false;
                    options.InSampleSize       = 4;
                    Bitmap bitmap     = BitmapFactory.DecodeFile(path, options);
                    var    fileStream = new FileStream(System.IO.Path.Combine(targetFolderPath, System.IO.Path.GetFileName(path)), FileMode.Create);
                    bitmap.Compress(Bitmap.CompressFormat.Png, 100, fileStream);
                    fileStream.Close();
                    // File.Copy( path, System.IO.Path.Combine( targetFolderPath, fileName ) );
                }
            } catch (Exception ex) {
                client.Disconnect();
                HHI_Android.ShowSimpleAlertView(context, "错误", "临时文件复制错误,错误信息:\n" + ex.Message.ToString());
                return(-1);
            }
            Snackbar.Make(cl, "临时文件创建成功!", Snackbar.LengthShort).Show();
            // Compress Zip File
            if (!Compress.DoZipFile(Compress.tmpname, targetFolderPath))
            {
                client.Disconnect();
                HHI_Android.ShowSimpleAlertView(context, "错误", "打包临时文件时出现错误");
                return(-1);
            }
            Snackbar.Make(cl, "缓存压缩成功!", Snackbar.LengthShort).Show();

            // Check Zip File openable
            FileInfo   zip = new FileInfo(Compress.tmpname);
            FileStream zipStream;

            try {
                zipStream = zip.OpenRead();
            } catch (Exception ex) {
                client.Disconnect();
                HHI_Android.ShowSimpleAlertView(context, "错误", "压缩包缓存无法打开,错误信息:\n" + ex.Message.ToString());
                return(-1);
            }
            Snackbar.Make(cl, "压缩缓存校验成功!", Snackbar.LengthShort).Show();
            // Send File
            NStream stream = client.stream;

            try {
                await stream.WriteString(System.IO.Path.Combine(hi.Path, folderName));

                System.Threading.Thread.Sleep(10);
                await stream.WriteInt64(zip.Length);

                System.Threading.Thread.Sleep(10);
                await stream.WriteBigFrom(zipStream);

                System.Threading.Thread.Sleep(50);
            } catch (Exception ex) {
                client.Disconnect();
                HHI_Android.ShowSimpleAlertView(context, "错误", "网络流写错误,错误信息:\n" + ex.Message.ToString());
                return(-1);
            }
            Snackbar.Make(cl, "传输完成,等待服务器接受完毕!结果可长按按钮查看!", Snackbar.LengthLong).Show();
            return(0);
        }