Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab5spo.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- using namespace std;
- #define MAX_ARRAY_SIZE 1024
- #define _CRT_NON_CONFORMING_SWPRINTFS
- char FileContent[MAX_ARRAY_SIZE];
- bool Exit = false;
- HMODULE myLib;
- void ReadThread(HANDLE hWriteThread)
- {
- TCHAR curDir[MAX_ARRAY_SIZE], inDir[MAX_ARRAY_SIZE], fileName[MAX_ARRAY_SIZE];
- HANDLE hFind;
- HANDLE hCanRead = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanRead"));
- HANDLE hCanWrite = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanWrite"));
- WIN32_FIND_DATA data;
- HMODULE myLib = LoadLibrary(TEXT("dilib.dll"));
- int (*ASyncRead)(HANDLE fileName, char* FileContent, int size, DWORD offset);
- ASyncRead = (int (*)(HANDLE, char*, int, DWORD))GetProcAddress(myLib, "ASyncRead");
- if (ASyncRead == NULL)
- {
- printf("\nCan't find ASyncRead... %d\n\n", GetLastError());
- return;
- }
- GetCurrentDirectory(sizeof(curDir), curDir);
- _tcscpy_s(inDir, curDir);
- _tcscat_s(curDir, TEXT("\\input\\"));
- _tcscat_s(inDir, TEXT("\\input\\*.*"));
- hFind = FindFirstFile(inDir, &data);
- if (hFind != INVALID_HANDLE_VALUE)
- {
- do
- {
- if(_tcscmp(data.cFileName, _T(".")) && _tcscmp(data.cFileName, _T("..")))
- {
- _tcscpy_s(fileName, curDir);
- _tcscat_s(fileName, data.cFileName);
- HANDLE hFile = CreateFile(fileName, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- printf("\nCan't open file %ls...\nError: %d\n\n", fileName, GetLastError());
- return;
- }
- printf("Async reading %ls...", fileName);
- // ------------------------------------------------------------------------------------------------------------------
- int BytesReaded = 0;
- do
- {
- WaitForSingleObject(hCanRead, INFINITE);
- ResetEvent(hCanRead);
- memset(FileContent, 0, sizeof(FileContent));
- BytesReaded += ASyncRead(hFile, FileContent, sizeof(FileContent), BytesReaded);
- SetEvent(hCanWrite);
- }
- while(BytesReaded % MAX_ARRAY_SIZE == 0);
- // ------------------------------------------------------------------------------------------------------------------
- printf("done\n");
- CloseHandle(hFile);
- }
- }
- while (FindNextFile(hFind, &data));
- }
- Exit = true;
- SetEvent(hCanWrite);
- WaitForSingleObject(hWriteThread, INFINITE);
- FindClose(hFind);
- CloseHandle(hCanWrite);
- CloseHandle(hCanRead);
- FreeLibrary(myLib);
- }
- void WriteThread()
- {
- TCHAR outFileName[MAX_ARRAY_SIZE];
- GetCurrentDirectory(sizeof(outFileName), outFileName);
- _tcscat_s(outFileName, TEXT("\\output\\out.txt"));
- HANDLE hCanWrite = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanWrite"));
- HANDLE hCanRead = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanRead"));
- ResetEvent(hCanWrite);
- HANDLE hFile = CreateFile(outFileName, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- printf("\nCan't open file %ls...\nError: %d\n\n", outFileName, GetLastError());
- return;
- }
- HMODULE myLib = LoadLibrary(TEXT("dilib.dll"));
- void (*ASyncWrite)(HANDLE hFile, char* FileContent, DWORD* offset);
- ASyncWrite = (void (*)(HANDLE, char*, DWORD*))GetProcAddress(myLib, "ASyncWrite");
- DWORD offset = 0;
- while( true )
- {
- WaitForSingleObject(hCanWrite, INFINITE);
- ResetEvent(hCanWrite);
- if(Exit)
- break;
- // -----------------------------------------------------------------------------------------------------------------
- ASyncWrite(hFile, FileContent, &offset);
- // ------------------------------------------------------------------------------------------------------------------
- SetEvent(hCanRead);
- }
- SetEvent(hCanRead);
- CloseHandle(hFile);
- CloseHandle(hCanRead);
- CloseHandle(hCanWrite);
- }
- HANDLE StartThreads()
- {
- DWORD dwTreadID, dwWriteID;
- printf("Creating write tread...");
- HANDLE hWriteThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&WriteThread, NULL, 0, &dwWriteID);
- if(!hWriteThread)
- {
- puts("Create write thread failed!");
- return NULL;
- }
- printf("done\nCreating read tread...");
- HANDLE hReadThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&ReadThread, hWriteThread, 0, &dwTreadID);
- if(!hReadThread)
- {
- puts("Create read thread failed!");
- return NULL;
- }
- printf("done\n");
- return hReadThread;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- HANDLE hCanRead = CreateEvent(NULL, false, false, TEXT("CanRead"));
- HANDLE hCanWrite = CreateEvent(NULL, false, false, TEXT("CanWrite"));
- HANDLE hExit = StartThreads();
- if(!hExit)
- return 0;
- SetEvent(hCanRead);
- WaitForSingleObject(hExit, INFINITE);
- CloseHandle(hExit);
- CloseHandle(hCanRead);
- CloseHandle(hCanWrite);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement