Advertisement
rnort

SPO-5-WIN

Dec 30th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. // lab5spo.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. #define MAX_ARRAY_SIZE 1024
  11. #define _CRT_NON_CONFORMING_SWPRINTFS
  12.  
  13. char FileContent[MAX_ARRAY_SIZE];
  14. bool Exit = false;
  15. HMODULE myLib;
  16.  
  17. void ReadThread(HANDLE hWriteThread)
  18. {
  19.     TCHAR curDir[MAX_ARRAY_SIZE], inDir[MAX_ARRAY_SIZE], fileName[MAX_ARRAY_SIZE];
  20.     HANDLE hFind;
  21.     HANDLE hCanRead = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanRead"));
  22.     HANDLE hCanWrite = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanWrite"));
  23.     WIN32_FIND_DATA data;
  24.  
  25.     HMODULE myLib = LoadLibrary(TEXT("dilib.dll"));
  26.     int (*ASyncRead)(HANDLE fileName, char* FileContent, int size, DWORD offset);
  27.     ASyncRead = (int (*)(HANDLE, char*, int, DWORD))GetProcAddress(myLib, "ASyncRead");
  28.     if (ASyncRead == NULL)
  29.     {
  30.         printf("\nCan't find ASyncRead... %d\n\n", GetLastError());
  31.         return;
  32.     }
  33.    
  34.     GetCurrentDirectory(sizeof(curDir), curDir);
  35.     _tcscpy_s(inDir, curDir);
  36.     _tcscat_s(curDir, TEXT("\\input\\"));
  37.     _tcscat_s(inDir, TEXT("\\input\\*.*"));
  38.  
  39.     hFind = FindFirstFile(inDir, &data);
  40.     if (hFind != INVALID_HANDLE_VALUE)
  41.     {
  42.         do
  43.         {
  44.             if(_tcscmp(data.cFileName, _T(".")) && _tcscmp(data.cFileName, _T("..")))
  45.             {
  46.                 _tcscpy_s(fileName, curDir);
  47.                 _tcscat_s(fileName, data.cFileName);
  48.                
  49.                 HANDLE hFile = CreateFile(fileName, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
  50.                 if (hFile == INVALID_HANDLE_VALUE)
  51.                 {
  52.                     printf("\nCan't open file %ls...\nError: %d\n\n", fileName, GetLastError());
  53.                     return;
  54.                 }
  55.  
  56.                 printf("Async reading %ls...", fileName);
  57. // ------------------------------------------------------------------------------------------------------------------
  58.                 int BytesReaded = 0;
  59.                 do
  60.                 {
  61.                     WaitForSingleObject(hCanRead, INFINITE);
  62.                     ResetEvent(hCanRead);
  63.                     memset(FileContent, 0, sizeof(FileContent));
  64.                     BytesReaded += ASyncRead(hFile, FileContent, sizeof(FileContent), BytesReaded);
  65.                     SetEvent(hCanWrite);
  66.                 }
  67.                 while(BytesReaded % MAX_ARRAY_SIZE == 0);
  68. // ------------------------------------------------------------------------------------------------------------------
  69.                 printf("done\n");
  70.                 CloseHandle(hFile);
  71.             }
  72.         }
  73.         while (FindNextFile(hFind, &data));
  74.     }
  75.     Exit = true;
  76.     SetEvent(hCanWrite);
  77.     WaitForSingleObject(hWriteThread, INFINITE);
  78.     FindClose(hFind);
  79.     CloseHandle(hCanWrite);
  80.     CloseHandle(hCanRead);
  81.     FreeLibrary(myLib);
  82. }
  83.  
  84. void WriteThread()
  85. {
  86.     TCHAR outFileName[MAX_ARRAY_SIZE];
  87.  
  88.     GetCurrentDirectory(sizeof(outFileName), outFileName);
  89.     _tcscat_s(outFileName, TEXT("\\output\\out.txt"));
  90.  
  91.     HANDLE hCanWrite = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanWrite"));
  92.     HANDLE hCanRead = OpenEvent(EVENT_ALL_ACCESS, NULL, TEXT("CanRead"));
  93.     ResetEvent(hCanWrite);
  94.  
  95.     HANDLE hFile = CreateFile(outFileName, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED, NULL);
  96.    
  97.     if (hFile == INVALID_HANDLE_VALUE)
  98.     {
  99.         printf("\nCan't open file %ls...\nError: %d\n\n", outFileName, GetLastError());
  100.         return;
  101.     }
  102.  
  103.     HMODULE myLib = LoadLibrary(TEXT("dilib.dll"));
  104.     void (*ASyncWrite)(HANDLE hFile, char* FileContent, DWORD* offset);
  105.     ASyncWrite = (void (*)(HANDLE, char*, DWORD*))GetProcAddress(myLib, "ASyncWrite");
  106.     DWORD offset = 0;
  107.  
  108.     while( true )
  109.     {
  110.         WaitForSingleObject(hCanWrite, INFINITE);
  111.         ResetEvent(hCanWrite);
  112.         if(Exit)
  113.             break;
  114. // -----------------------------------------------------------------------------------------------------------------
  115.         ASyncWrite(hFile, FileContent, &offset);
  116. // ------------------------------------------------------------------------------------------------------------------
  117.         SetEvent(hCanRead);
  118.     }
  119.     SetEvent(hCanRead);
  120.     CloseHandle(hFile);
  121.     CloseHandle(hCanRead);
  122.     CloseHandle(hCanWrite);
  123. }
  124.  
  125. HANDLE StartThreads()
  126. {
  127.     DWORD dwTreadID, dwWriteID;
  128.     printf("Creating write tread...");
  129.     HANDLE hWriteThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&WriteThread, NULL, 0, &dwWriteID);
  130.     if(!hWriteThread)
  131.     {
  132.         puts("Create write thread failed!");
  133.         return NULL;
  134.     }
  135.     printf("done\nCreating read tread...");
  136.     HANDLE hReadThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&ReadThread, hWriteThread, 0, &dwTreadID);
  137.     if(!hReadThread)
  138.     {
  139.         puts("Create read thread failed!");
  140.         return NULL;
  141.     }
  142.     printf("done\n");
  143.     return hReadThread;
  144. }
  145.  
  146. int _tmain(int argc, _TCHAR* argv[])
  147. {
  148.     HANDLE hCanRead = CreateEvent(NULL, false, false, TEXT("CanRead"));
  149.     HANDLE hCanWrite = CreateEvent(NULL, false, false, TEXT("CanWrite"));
  150.  
  151.     HANDLE hExit = StartThreads();
  152.     if(!hExit)
  153.         return 0;
  154.     SetEvent(hCanRead);
  155.  
  156.     WaitForSingleObject(hExit, INFINITE);
  157.     CloseHandle(hExit);
  158.     CloseHandle(hCanRead);
  159.     CloseHandle(hCanWrite);
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement