public static void HandleDoDownloadFile(FileTransferRequest command, Networking.Client client)
        {
            new Thread(() =>
            {
                LimitThreads.WaitOne();
                try
                {
                    using (var srcFile = new FileSplit(command.RemotePath, FileAccess.Read))
                    {
                        ActiveTransfers[command.Id] = srcFile;
                        foreach (var chunk in srcFile)
                        {
                            if (!client.Connected || !ActiveTransfers.ContainsKey(command.Id))
                            {
                                break;
                            }

                            // blocking sending might not be required, needs further testing
                            client.SendBlocking(new FileTransferChunk
                            {
                                Id       = command.Id,
                                FilePath = command.RemotePath,
                                FileSize = srcFile.FileSize,
                                Chunk    = chunk
                            });
                        }
                    }
                }
                catch (Exception)
                {
                    client.Send(new FileTransferCancel
                    {
                        Id     = command.Id,
                        Reason = "Error reading file"
                    });
                }
                finally
                {
                    RemoveFileTransfer(command.Id);
                    LimitThreads.Release();
                }
            }).Start();
        }
Exemplo n.º 2
0
        public static void HandleDoDownloadFile(Paketler.ServerPaketleri.DoDownloadFile command, Client client)
        {
            new Thread(() =>
            {
                LimitThreads.WaitOne();
                try
                {
                    FileSplit srcFile = new FileSplit(command.RemotePath);
                    if (srcFile.MaxBlocks < 0)
                    {
                        throw new Exception(srcFile.LastError);
                    }

                    for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++)
                    {
                        if (!client.Connected || _canceledDownloads.ContainsKey(command.ID))
                        {
                            break;
                        }

                        byte[] block;

                        if (!srcFile.ReadBlock(currentBlock, out block))
                        {
                            throw new Exception(srcFile.LastError);
                        }

                        new Paketler.ClientPaketleri.DoDownloadFileResponse(command.ID,
                                                                            Path.GetFileName(command.RemotePath), block, srcFile.MaxBlocks, currentBlock,
                                                                            srcFile.LastError).Execute(client);
                    }
                }
                catch (Exception ex)
                {
                    new Paketler.ClientPaketleri.DoDownloadFileResponse(command.ID, Path.GetFileName(command.RemotePath), new byte[0], -1, -1, ex.Message)
                    .Execute(client);
                }
                LimitThreads.Release();
            }).Start();
        }