public async Task <string> GetFile(byte[] fileHash)
        {
            var requestMessage = new GetFileMessage(fileHash);

            var subDir = new DirectoryInfo(Path.Combine(TempStorageDirectory, Hasher.GetDirectoryNameFromHash(fileHash)));

            if (!subDir.Exists)
            {
                subDir.Create();
            }
            var fileName = Hasher.GetFileNameFromHash(fileHash);
            var filePath = Path.Combine(subDir.FullName, fileName);

            FilesInProgress.Add(fileHash, File.OpenWrite(filePath));

            await Communicator.SendMessage(requestMessage);

            // wait for transfer
            var    gotVerificationMessage = false;
            string resultsPath            = null;

            while (!gotVerificationMessage)
            {
                if (VerificationMessages.ContainsKey(fileHash))
                {
                    var verifyMessage = VerificationMessages[fileHash] as FileStoredVerificationMessage;
                    resultsPath = (verifyMessage?.Success ?? false) ? verifyMessage.FilePath : null;
                    VerificationMessages.Remove(fileHash);
                    gotVerificationMessage = true;
                }

                await Task.Delay(1);
            }

            return(resultsPath);
        }
        public async Task <bool> StoreFile(IBackupFile file, byte[] hash)
        {
            var success = false;

            await Communicator.SendMessage(new FileStartMessage(hash));

            var totalBytes = file.Length;
            var buffer     = new byte[BufferSize];

            var position       = 0L;
            var remainingBytes = totalBytes;
            var message        = new FileChunckMessage()
            {
                FileHash = hash
            };

            if (FileTransferResponse.ContainsKey(hash))
            {
                FileTransferResponse.Remove(hash);
            }
            FileTransferResponse.Add(hash, false);
            using (var fileStream = file.OpenRead())
            {
                while (position < totalBytes)
                {
                    var bytesToRead = (int)((remainingBytes > BufferSize) ? BufferSize : remainingBytes);
                    if (bytesToRead < BufferSize)
                    {
                        buffer = new byte[bytesToRead];
                    }
                    position        += fileStream.Read(buffer, 0, bytesToRead);
                    remainingBytes   = totalBytes - position;
                    message.FileData = buffer;
                    await Communicator.SendMessage(message);

                    while (!FileTransferResponse[hash])
                    {
                        await Task.Delay(1);
                    }

                    FileTransferResponse[hash] = false;
                }
            }

            await Communicator.SendMessage(new FileEndMessage(hash));

            // Watch the inbound messages for a verification message
            var gotVerificationMessage = false;

            while (!gotVerificationMessage)
            {
                if (VerificationMessages.ContainsKey(hash))
                {
                    var verifyMessage = VerificationMessages[hash] as FileStoredVerificationMessage;
                    success = verifyMessage?.Success ?? false;
                    VerificationMessages.Remove(hash);
                    gotVerificationMessage = true;
                }

                await Task.Delay(1);
            }

            return(success);
        }