1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "CacheStrategy.h"
12 #include "File.h"
13 #include "IFile.h"
14 #include "threads/CriticalSection.h"
15 #include "threads/Thread.h"
16 
17 #include <atomic>
18 #include <memory>
19 
20 namespace XFILE
21 {
22 
23   class CFileCache : public IFile, public CThread
24   {
25   public:
26     explicit CFileCache(const unsigned int flags);
27     ~CFileCache() override;
28 
29     // CThread methods
30     void Process() override;
31     void OnExit() override;
32     void StopThread(bool bWait = true) override;
33 
34     // IFIle methods
35     bool Open(const CURL& url) override;
36     void Close() override;
37     bool Exists(const CURL& url) override;
38     int Stat(const CURL& url, struct __stat64* buffer) override;
39 
40     ssize_t Read(void* lpBuf, size_t uiBufSize) override;
41 
42     int64_t Seek(int64_t iFilePosition, int iWhence) override;
43     int64_t GetPosition() override;
44     int64_t GetLength() override;
45 
46     int IoControl(EIoControl request, void* param) override;
47 
48     IFile *GetFileImp();
49 
50     const std::string GetProperty(XFILE::FileProperty type, const std::string &name = "") const override;
51 
52     const std::vector<std::string> GetPropertyValues(XFILE::FileProperty type, const std::string& name = "") const override
53     {
54       return std::vector<std::string>();
55     }
56 
57   private:
58     std::unique_ptr<CCacheStrategy> m_pCache;
59     int m_seekPossible;
60     CFile m_source;
61     std::string m_sourcePath;
62     CEvent m_seekEvent;
63     CEvent m_seekEnded;
64     int64_t m_nSeekResult;
65     int64_t m_seekPos;
66     int64_t m_readPos;
67     int64_t m_writePos;
68     unsigned m_chunkSize;
69     unsigned m_writeRate;
70     unsigned m_writeRateActual;
71     int64_t m_forwardCacheSize;
72     bool m_bFilling;
73     bool m_bLowSpeedDetected;
74     std::atomic<int64_t> m_fileSize;
75     unsigned int m_flags;
76     CCriticalSection m_sync;
77   };
78 
79 }
80