/// <summary> /// Gets the information about the file. /// </summary> public void GetFileInfo(RelativePath relativePath, out bool fileExists, out DateTime fileAge, out long fileLength) { RestoreConnection(); DataPacket request = CreateRequest(FunctionID.GetFileInfo); int index = ArgumentIndex; CopyFileName(relativePath.GetDirectoryID(), relativePath.Path, outBuf, ref index); request.BufferLength = index; SendRequest(request); ReceiveResponse(request); index = ArgumentIndex; fileExists = GetBool(inBuf, ref index); fileAge = GetTime(inBuf, ref index); fileLength = GetInt64(inBuf, ref index); }
/// <summary> /// Uploads the file. /// </summary> public void UploadFile(string srcFileName, RelativePath destPath, out bool fileAccepted) { if (!File.Exists(srcFileName)) { throw new ScadaException(CommonPhrases.FileNotFound); } RestoreConnection(); using (FileStream stream = new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { // request permission to upload file const int FileDataIndex = ArgumentIndex + 9; const int BlockCapacity = BufferLenght - FileDataIndex; long bytesToReadTotal = stream.Length; int blockCount = (int)Math.Ceiling((double)bytesToReadTotal / BlockCapacity); DataPacket request = CreateRequest(FunctionID.UploadFile); int index = ArgumentIndex; CopyInt32(0, outBuf, ref index); CopyInt32(blockCount, outBuf, ref index); CopyFileName(destPath.GetDirectoryID(), destPath.Path, outBuf, ref index); request.BufferLength = index; SendRequest(request); ReceiveResponse(request); fileAccepted = inBuf[ArgumentIndex] > 0; // upload file if (fileAccepted) { int blockNumber = 0; long bytesReadTotal = 0; bool endOfFile = false; request = null; while (!endOfFile) { // read from file int bytesToRead = (int)Math.Min(bytesToReadTotal - bytesReadTotal, BlockCapacity); int bytesRead = stream.Read(outBuf, FileDataIndex, bytesToRead); bytesReadTotal += bytesRead; endOfFile = bytesRead < bytesToRead || bytesReadTotal == bytesToReadTotal; // send data request = CreateRequest(FunctionID.UploadFile, 0, false); index = ArgumentIndex; CopyInt32(++blockNumber, outBuf, ref index); CopyBool(endOfFile, outBuf, ref index); CopyInt32(bytesRead, outBuf, ref index); request.BufferLength = FileDataIndex + bytesRead; SendRequest(request); OnProgress(blockNumber, blockCount); } if (request != null) { ReceiveResponse(request); } } } }
/// <summary> /// Downloads the file. /// </summary> public void DownloadFile(RelativePath relativePath, long offset, int count, bool readFromEnd, DateTime newerThan, Func <Stream> createStreamFunc, out DateTime fileAge, out FileReadingResult readingResult, out Stream stream) { if (createStreamFunc == null) { throw new ArgumentNullException(nameof(createStreamFunc)); } RestoreConnection(); DataPacket request = CreateRequest(FunctionID.DownloadFile); int index = ArgumentIndex; CopyFileName(relativePath.GetDirectoryID(), relativePath.Path, outBuf, ref index); CopyInt64(offset, outBuf, ref index); CopyInt32(count, outBuf, ref index); CopyBool(readFromEnd, outBuf, ref index); CopyTime(newerThan, outBuf, ref index); request.BufferLength = index; SendRequest(request); int prevBlockNumber = 0; fileAge = DateTime.MinValue; readingResult = FileReadingResult.Successful; stream = null; try { while (readingResult == FileReadingResult.Successful) { ReceiveResponse(request); index = ArgumentIndex; int blockNumber = GetInt32(inBuf, ref index); int blockCount = GetInt32(inBuf, ref index); fileAge = GetTime(inBuf, ref index); readingResult = (FileReadingResult)GetByte(inBuf, ref index); if (blockNumber != prevBlockNumber + 1) { ThrowBlockNumberException(); } if (readingResult == FileReadingResult.Successful || readingResult == FileReadingResult.EndOfFile) { if (stream == null) { stream = createStreamFunc(); } int bytesToWrite = GetInt32(inBuf, ref index); stream.Write(inBuf, index, bytesToWrite); } prevBlockNumber = blockNumber; OnProgress(blockNumber, blockCount); } } catch { stream?.Dispose(); stream = null; throw; } }