Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<Windows.h>
- using namespace std;
- VOID WINAPI asyncReading(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV);
- VOID WINAPI asyncWrite(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV);
- void asynccopy();
- const int CLUSTER = 4096;//4 KB
- HANDLE infile, outfile;
- CHAR** buffers;
- int multiplier;
- int bytestouse;
- OVERLAPPED* OverLapIn, * OverLapOut;//структуры овелапд
- LONGLONG counter;
- LARGE_INTEGER fileSize;
- int main()
- {
- asynccopy();
- system("pause");
- return 0;
- }
- void asynccopy()
- {
- puts("Enter filepath. Spaces are not allowed");
- char path[MAX_PATH];
- cin >> path;
- infile = CreateFileA(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED| FILE_FLAG_NO_BUFFERING, NULL);
- if (infile == INVALID_HANDLE_VALUE)
- cout << "Error " << GetLastError() << endl;
- else
- {
- puts("File opened successfully");
- puts("Enter filepath where to copy file. Spaces are not allowed");
- char newpath[MAX_PATH];
- cin >> newpath;
- outfile = CreateFileA(newpath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED| FILE_FLAG_NO_BUFFERING , NULL);
- if (outfile == INVALID_HANDLE_VALUE)
- cout << "Error " << GetLastError() << endl;
- else
- {
- puts("File created successfully");
- puts("Enter cluster multipltier and number of overlaps");
- int num_ovrlp;
- cin >> multiplier >> num_ovrlp;
- bytestouse = multiplier * CLUSTER;
- OverLapIn = new OVERLAPPED[num_ovrlp];
- OverLapOut = new OVERLAPPED[num_ovrlp];
- buffers = new CHAR * [num_ovrlp];//буфферы
- for (int i = 0; i < num_ovrlp; i++)
- buffers[i] = new CHAR[bytestouse];
- int clustersInFile;
- LARGE_INTEGER posIn;
- GetFileSizeEx(infile, &fileSize);
- clustersInFile = fileSize.QuadPart / bytestouse;
- if (fileSize.QuadPart % bytestouse != 0)
- clustersInFile++;
- cout << "File Size: " << fileSize.QuadPart << " Bytes" << endl;
- cout << "Copying with given buffer will take " << clustersInFile*2 << " operations"<<endl;;
- posIn.QuadPart = 0;
- DWORD before = GetTickCount64();
- counter = 0;
- for (int i = 0; i < num_ovrlp; i++)
- {
- OverLapIn[i].hEvent = (HANDLE)i;
- OverLapOut[i].hEvent = (HANDLE)i;
- OverLapIn[i].Offset = posIn.LowPart;
- OverLapIn[i].OffsetHigh = posIn.HighPart;
- if (posIn.QuadPart < fileSize.QuadPart)
- ReadFileEx(infile, buffers[i], bytestouse, &OverLapIn[i],asyncReading);
- posIn.QuadPart += (LONGLONG)bytestouse;
- }
- while (counter < 2 * clustersInFile)
- SleepEx(INFINITE, true);
- DWORD after = GetTickCount64();
- cout << "Copying took " << float(after - before) << " miliseconds /n Overlaps: " << num_ovrlp << " Buffer size per operation (Bytes) : " << bytestouse << endl;
- delete[] OverLapIn;
- delete[]OverLapOut;
- for (int i = 0; i < num_ovrlp; i++)
- delete[] buffers[i];
- delete[] buffers;
- SetFilePointerEx(outfile, fileSize, 0, FILE_BEGIN);
- SetEndOfFile(outfile);
- CloseHandle(outfile);
- }
- }
- CloseHandle(infile);
- }
- VOID WINAPI asyncReading(DWORD code,DWORD nBytes,LPOVERLAPPED lpOV) {
- ++counter;
- LARGE_INTEGER posIn, posOut;
- DWORD i = (DWORD)(lpOV->hEvent);
- posIn.LowPart = OverLapIn[i].Offset;
- posIn.HighPart = OverLapIn[i].OffsetHigh;
- posOut.QuadPart = posIn.QuadPart;
- OverLapOut[i].Offset = posOut.LowPart;
- OverLapOut[i].OffsetHigh = posOut.HighPart;
- WriteFileEx(outfile, buffers[i], bytestouse, &OverLapOut[i], asyncWrite);
- posIn.QuadPart += bytestouse;
- OverLapIn[i].Offset = posIn.LowPart;
- OverLapIn[i].OffsetHigh = posIn.HighPart;
- }
- VOID WINAPI asyncWrite(DWORD code, DWORD nBytes, LPOVERLAPPED lpOV) {
- ++counter;
- LARGE_INTEGER posIn;
- DWORD i = (DWORD)(lpOV->hEvent);
- posIn.LowPart = OverLapIn[i].Offset;
- posIn.HighPart = OverLapIn[i].OffsetHigh;
- if (posIn.QuadPart < fileSize.QuadPart)
- ReadFileEx(infile, buffers[i], bytestouse, &OverLapIn[i], asyncReading);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement