Пример #1
0
        public static void HandleDoDownloadFile(DoDownloadFile command, Networking.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);
                        }


                        client.SendBlocking(new DoDownloadFileResponse
                        {
                            Id            = command.Id,
                            Filename      = Path.GetFileName(command.RemotePath),
                            Block         = block,
                            MaxBlocks     = srcFile.MaxBlocks,
                            CurrentBlock  = currentBlock,
                            CustomMessage = srcFile.LastError
                        });
                    }
                }
                catch (Exception ex)
                {
                    client.SendBlocking(new DoDownloadFileResponse
                    {
                        Id            = command.Id,
                        Filename      = Path.GetFileName(command.RemotePath),
                        Block         = new byte[0],
                        MaxBlocks     = -1,
                        CurrentBlock  = -1,
                        CustomMessage = ex.Message
                    });
                }
                _limitThreads.Release();
            }).Start();
        }
Пример #2
0
        public static void doDownloadFile(DoDownloadFile command, ClientMosaique 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 DoDownloadFileResponse(command.id, Path.GetFileName(command.remotePath), block, srcFile.MaxBlocks, currentBlock, srcFile.LastError).Execute(client);
                    }
                }
                catch (Exception ex)
                {
                    new DoDownloadFileResponse(command.id, Path.GetFileName(command.remotePath), new byte[0], -1, -1, ex.Message).Execute(client);
                }

                _limitThreads.Release();
            }).Start();
        }