Remote Call Framework 3.4
FileTransferService.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2023, Delta V Software. All rights reserved.
6 // https://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF under GPL terms.
12 //
13 // Version: 3.4
14 // Contact: support <at> deltavsoft.com
15 //
16 //******************************************************************************
17 
18 #ifndef INCLUDE_RCF_FILETRANSFERSERVICE_HPP
19 #define INCLUDE_RCF_FILETRANSFERSERVICE_HPP
20 
21 #include <map>
22 #include <set>
23 
24 #include <RCF/FileStream.hpp>
25 #include <RCF/RcfFwd.hpp>
26 #include <RCF/Service.hpp>
27 
28 #include <SF/vector.hpp>
29 #include <SF/map.hpp>
30 
31 #include <RCF/FileSystem.hpp>
32 
33 #if RCF_FEATURE_SF==1
34 
35 namespace SF {
36 
37  RCF_EXPORT void serialize(SF::Archive &ar, RCF::Path &p);
38 
39 } // namespace SF
40 
41 #endif
42 
43 namespace RCF {
44 
45  class FileTransferService;
46 
47  class FileUploadInfo;
48  class FileDownloadInfo;
49 
50  class RcfSession;
51 
52  class FileUploadInfo;
53  class FileDownloadInfo;
54 
55  typedef std::shared_ptr<FileUploadInfo> FileUploadInfoPtr;
56  typedef std::shared_ptr<FileDownloadInfo> FileDownloadInfoPtr;
57 
58  typedef std::function<bool(const FileUploadInfo &)> UploadAccessCallback;
59  typedef std::function<bool(const FileDownloadInfo &)> DownloadAccessCallback;
60 
61  class FileIoRequest;
62  typedef std::shared_ptr<FileIoRequest> FileIoRequestPtr;
63 
65  class FileUploadInfo : Noncopyable
66  {
67  public:
69  ~FileUploadInfo();
70 
71  std::string getUploadId() const;
72 
73  FileManifest mManifest;
74  std::uint32_t mCurrentFile;
75  std::uint64_t mCurrentPos;
76  Path mUploadDir;
77 
78  private:
79 
80  friend class FileTransferService;
81 
82  FileHandlePtr mFileHandle;
83  Path mFinalFilePath;
84  FileIoRequestPtr mWriteOp;
85 
86  bool mCompleted;
87  bool mResume;
88  std::uint32_t mTimeStampMs;
89 
90  std::uint32_t mSessionLocalId;
91  std::string mUploadId;
92 
93  BandwidthQuotaPtr mQuotaPtr;
94  };
95 
97  class FileDownloadInfo : Noncopyable
98  {
99  public:
101  ~FileDownloadInfo();
102 
103  Path mDownloadPath;
104  FileManifest mManifest;
105  std::uint32_t mCurrentFile;
106  std::uint64_t mCurrentPos;
107  bool mCancel;
108 
109  private:
110 
111  friend class FileTransferService;
112 
113  FileHandlePtr mFileHandle;
114  Path mFileHandlePath;
115  FileIoRequestPtr mReadOp;
116  ByteBuffer mReadBuffer;
117  ByteBuffer mSendBuffer;
118  ByteBuffer mSendBufferRemaining;
119 
120  bool mResume;
121 
122  Timer mTransferWindowTimer;
123  std::uint32_t mTransferWindowBytesSoFar;
124  std::uint32_t mTransferWindowBytesTotal;
125 
126  std::uint32_t mSessionLocalId;
127  std::string mServerDownloadId;
128 
129  BandwidthQuotaPtr mQuotaPtr;
130  };
131 
132  typedef std::shared_ptr<FileUploadInfo> FileUploadInfoPtr;
133  typedef std::shared_ptr<FileDownloadInfo> FileDownloadInfoPtr;
134 
135  class FileChunk;
136  class FileTransferRequest;
137 
138  class TransferInfo
139  {
140  public:
141  Path mPath;
142  BandwidthQuotaPtr mBandwidthQuotaPtr;
143  };
144 
145  class RCF_EXPORT FileTransferService : public I_Service
146  {
147  public:
148  FileTransferService();
149 
150  typedef std::function<BandwidthQuotaPtr(RcfSession &)> BandwidthQuotaCallback;
151  typedef BandwidthQuotaCallback UploadQuotaCallback;
152  typedef BandwidthQuotaCallback DownloadQuotaCallback;
153 
154  // For testing.
155  void setTransferWindowS(std::uint32_t transferWindowS);
156  std::uint32_t getTransferWindowS();
157 
158  //----------------------------------------------------------------------
159  // Remotely accessible.
160 
161  void BeginUpload(
162  const FileManifest & manifest,
163  const std::vector<FileChunk> & chunks,
164  FileChunk & startPos,
165  std::uint32_t & maxMessageLength,
166  std::string & uploadId,
167  std::uint32_t & bps,
168  std::uint32_t sessionLocalId);
169 
170  void UploadChunks(
171  const std::vector<FileChunk> & chunks,
172  std::uint32_t & bps);
173 
174  void BeginDownload(
175  FileManifest & manifest,
176  const FileTransferRequest & request,
177  std::vector<FileChunk> & chunks,
178  std::uint32_t & maxMessageLength,
179  std::uint32_t & bps,
180  std::uint32_t sessionLocalId,
181  const std::string & serverDownloadId);
182 
183  void TrimDownload(
184  const FileChunk & startPos);
185 
186  void DownloadChunks(
187  const FileTransferRequest & request,
188  std::vector<FileChunk> & chunks,
189  std::uint32_t & adviseWaitMs,
190  std::uint32_t & bps);
191 
192  //----------------------------------------------------------------------
193 
194  private:
195 
196  friend class RcfServer;
197  friend class RcfSession;
198 
199  void addFileTransfer(const std::string & transferId, const TransferInfo & transferInfo);
200  void removeFileTransfer(const std::string & transferId);
201  TransferInfo findFileTransfer(const std::string & transferId);
202 
203  Mutex mFileTransfersInProgressMutex;
204  std::map<std::string, TransferInfo> mFileTransfersInProgress;
205 
206  void checkForUploadCompletion(FileUploadInfoPtr uploadInfoPtr);
207 
208 
209 
210  void onServerStart(RcfServer & server);
211  void onServerStop(RcfServer & server);
212 
213  Path mUploadDirectory;
214 
215  DownloadProgressCallback mDownloadProgressCb;
216  UploadProgressCallback mUploadProgressCb;
217 
218  std::uint32_t mTransferWindowS;
219 
220  BandwidthQuotaPtr mUploadQuota;
221  BandwidthQuotaPtr mDownloadQuota;
222 
223  BandwidthQuotaCallback mUploadQuotaCallback;
224  BandwidthQuotaCallback mDownloadQuotaCallback;
225  };
226 
227  typedef std::shared_ptr<FileTransferService> FileTransferServicePtr;
228 
229 } // namespace RCF
230 
231 #endif // ! INCLUDE_RCF_FILETRANSFERSERVICE_HPP
Represents an archive, in which serialized objects are stored.
Definition: Archive.hpp:31
RCF_FILESYSTEM_NS::path Path
Typedef for standard C++ path type.
Definition: FileSystem.hpp:31
std::function< BandwidthQuotaPtr(RcfSession &)> BandwidthQuotaCallback
Describes user-provided callback functions for assigning custom bandwidth quotas to a RcfSession...
Definition: RcfFwd.hpp:140
std::function< void(RcfSession &, FileDownloadInfo &)> DownloadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file download.
Definition: RcfFwd.hpp:134
Represents a server side session, associated with a client connection.
Definition: RcfSession.hpp:64
std::shared_ptr< BandwidthQuota > BandwidthQuotaPtr
Reference counted wrapper for RCF::BandwidthQuota.
Definition: RcfFwd.hpp:127
Server-side information about a file download taking place from a RcfServer.
Definition: FileTransferService.hpp:97
Definition: ByteBuffer.hpp:188
Provides RCF server-side functionality.
Definition: RcfServer.hpp:53
std::function< void(RcfSession &, FileUploadInfo &)> UploadProgressCallback
Describes user-provided callback functions for server-side monitoring of a file upload.
Definition: RcfFwd.hpp:137
Definition: ByteBuffer.hpp:39
Definition: AmiIoHandler.hpp:23
Server-side information about a file upload taking place to a RcfServer.
Definition: FileTransferService.hpp:65