Beispiel #1
0
        private static async Task InitQueueDataEndpoint(IWebServer server, string sessionKey, string ip, IEnumerable <FileSendInfo> files)
        {
            FileInfoListGenerator generator = new FileInfoListGenerator(files, ip);
            var queueData = await generator.GenerateAsync();

            server.AddResponseUrl($"/{sessionKey}/", queueData.FileInfoListJsonLegacy);
            server.AddResponseUrl($"/{sessionKey}/queueInfo/", queueData.FileInfoListJson);
        }
Beispiel #2
0
        private async Task InitFileReceiveEndpoints(IWebServer server, FileSendInfo fileInfo, FileSendProgressCalculator transferProgress)
        {
            await fileInfo.InitSlicingAsync();

            fileSliceSender = new FileSliceSender(fileInfo);
            transferProgress.AddFileSliceSender(fileSliceSender);
            fileSliceSender.SliceRequested += transferProgress.SliceRequestReceived;

            for (int i = 0; i < fileInfo.SlicesCount; i++)
            {
                server.AddResponseUrl($"/{fileInfo.UniqueKey}/{i}/", (Func <IWebServer, RequestDetails, Task <byte[]> >)fileSliceSender.GetFileSlice);
            }
        }
Beispiel #3
0
        private List <KeyValuePair <string, IWebServer> > StartListeners(List <string> IPs, string communicationKey)
        {
            var servers = new List <KeyValuePair <string, IWebServer> >();

            foreach (var item in IPs)
            {
                IWebServer ws = webServerGenerator.GenerateInstance();
                ws.StartWebServer(item, Constants.IPFinderCommunicationPort);

                ws.AddResponseUrl("/" + communicationKey + "/", (Func <IWebServer, RequestDetails, string>)WebServerFetched);

                System.Diagnostics.Debug.WriteLine($"Started listener at {item}:{Constants.IPFinderCommunicationPort}. url is /{communicationKey}/");

                servers.Add(new KeyValuePair <string, IWebServer>(item, ws));
            }

            return(servers);
        }
Beispiel #4
0
 private void InitFinishQueueEndpoint(IWebServer server, string sessionKey)
 {
     server.AddResponseUrl($"/{sessionKey}/finishQueue/", (Func <IWebServer, RequestDetails, string>)FinishQueue);
 }
Beispiel #5
0
 private void InitVersionCheckEndpoint(IWebServer server, string sessionKey)
 {
     server.AddResponseUrl($"/{sessionKey}/versionCheck/", (Func <IWebServer, RequestDetails, string>)VersionCheck);
 }
Beispiel #6
0
        /// <param name="files">A list of Tuple(Relative directory path, StorageFile) objects.</param>
        public async Task <FileTransferResult> SendQueue(CancellationToken cancellationToken, List <Tuple <string, IFile> > files, string parentDirectoryName)
        {
            if ((ipFinderResult == null) || (ipFinderResult.Success == false))
            {
                await Handshake();

                if (ipFinderResult == null)
                {
                    ipFinderResult = new IPDetectionCompletedEventArgs
                    {
                        Success = false,
                    }
                }
                ;
            }

            if (ipFinderResult.Success == false)
            {
                return(FileTransferResult.FailedOnHandshake);
            }


            InitServer();

            Dictionary <IFile, string> sFileKeyPairs = new Dictionary <IFile, string>();

            IFileStats[] fs = new IFileStats[files.Count];

            ulong totalSlices = 0;

            for (int i = 0; i < files.Count; i++)
            {
                var item = files[i];

                var key = GenerateUniqueRandomKey();

                fs[i] = await item.Item2.GetFileStats();

                var slicesCount = (uint)Math.Ceiling(((double)fs[i].Length) / ((double)Constants.FileSliceMaxLength));

                totalSlices += slicesCount;

                keyTable.Add(key, new FileDetails
                {
                    storageFile       = item.Item2,
                    lastPieceAccessed = 0,
                    lastSliceSize     = (uint)((ulong)fs[i].Length % Constants.FileSliceMaxLength),
                    lastSliceId       = slicesCount - 1
                });

                sFileKeyPairs.Add(item.Item2, key);

                InitUrls(key, slicesCount);
            }

            var queueFinishKey = RandomFunctions.RandomString(15);

            server.AddResponseUrl("/" + queueFinishKey + "/finishQueue/", (Func <IWebServer, RequestDetails, string>)QueueFinished);
            System.Diagnostics.Debug.WriteLine("/" + queueFinishKey + "/finishQueue/");

            queueFinishTcs = new TaskCompletionSource <string>();
            fileSendTcs    = null;

            ulong finishedSlices = 0;

            ClearInternalEventSubscribers();
            FileTransferProgressInternal += (s, ee) =>
            {
                FileTransferProgress?.Invoke(s, new FileTransferProgressEventArgs
                {
                    State       = ee.State,
                    CurrentPart = finishedSlices + ee.CurrentPart,
                    Total       = totalSlices
                });

                if (ee.State == FileTransferState.Finished)
                {
                    finishedSlices += ee.Total;
                }
            };

            cancellationToken.Register(() =>
            {
                queueFinishTcs?.TrySetResult(TRANSFER_CANCELLED_MESSAGE);
                server?.Dispose();
            });

            if (await SendQueueInit(totalSlices, queueFinishKey, parentDirectoryName) == false)
            {
                return(FileTransferResult.FailedOnQueueInit);
            }

            bool infoSendResult = await SendQueueInfo(files, sFileKeyPairs, fs);

            return(await WaitQueueToFinish(cancellationToken));
        }