Example #1
0
        /// <summary>
        /// private constructor
        /// </summary>
        private DeviceSettings()
            : base(null)
        {
            string folderPath = FolderUtil.GetFolderPath(ThingsSettingsFolder, false);

            if (!string.IsNullOrEmpty(folderPath))
            {
                SettingsFile = Path.Combine(folderPath, ThingsSettingsFile);
                IsValid      = LoadSettings();
            }
        }
        public async Task <ServerTaskResult> ReceiveFile()
        {
            return(await Task.Factory.StartNew(() =>
            {
                try
                {
                    FileData fileData = GetFileInfo();

                    byte[] receivedData = new byte[BUFFER_SIZE];

                    int receivedBytes = 0;
                    int totalBytes = 0;

                    FilePath = FolderUtil.GetFolderPath(fileData.FileName);

                    DeleteFile();

                    Stopwatch watch = Stopwatch.StartNew();

                    using (var fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write))
                    {
                        while (totalBytes != fileData.ByteSize)
                        {
                            receivedBytes = NetworkStream.Read(receivedData, 0, receivedData.Length);

                            fs.Write(receivedData, 0, receivedBytes);

                            totalBytes += receivedBytes;

                            string fileStatus = $"({NetworkUtil.GetFileSize(totalBytes)} / {fileData.FileSize}) {ProgressPercentage(totalBytes, fileData.ByteSize)}%";
                            Server.OnUpdateFileStatus(fileStatus);
                        }
                    }

                    watch.Stop();

                    int elapsedSeconds = (int)watch.ElapsedMilliseconds / 1000;

                    return new ServerTaskResult(true, $"\n{FilePath} [{elapsedSeconds}s] ({DateTime.Now})", MessageType.OK);
                }
                catch (Exception e)
                {
                    return new ServerTaskResult(false, e.Message, MessageType.ERROR);
                }
            }));
        }