Advertisement
Guest User

Pipe communication

a guest
Jan 23rd, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. #include <stdio.h>
  4. #include <cstdio>
  5. #include <conio.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <tchar.h>
  10. #include "json.hpp"
  11.  
  12. #define BUFSIZE 8092
  13. using namespace std;
  14. using json = nlohmann::json;
  15.  
  16. // string stripper to prepare buffered data for JSON parser
  17. bool invalidChar(char c) {
  18.     return !(c >= 0 && c <128);
  19. }
  20. void stripUnicode(string &str)
  21. {
  22.     str.erase(remove_if(str.begin(), str.end(), invalidChar), str.end());
  23. }
  24.  
  25. int _tmain(int argc, TCHAR *argv[])
  26. {
  27.     vector<char> buffer;
  28.     // named pipes parameters
  29.     HANDLE hPipe;
  30.     BOOL   fSuccess = FALSE;
  31.     DWORD  cbToWrite, cbTotalRead=0, cbRead, dwToWrite;
  32.     LPCTSTR lpszPipename = TEXT("\\\\.\\pipe\\JointsPipe");
  33.  
  34.     // Try to open a named pipe; wait for it, if necessary.
  35.     while (1)
  36.     {
  37.         hPipe = CreateFile(
  38.             lpszPipename,   // pipe name
  39.             GENERIC_READ |  // read and write access
  40.             GENERIC_WRITE,
  41.             0,              // no sharing
  42.             NULL,           // default security attributes
  43.             OPEN_EXISTING,  // opens existing pipe
  44.             0,              // default attributes
  45.             NULL);          // no template file
  46.  
  47.         // Break if the pipe handle is valid.
  48.  
  49.         if (hPipe != INVALID_HANDLE_VALUE)
  50.             break;
  51.  
  52.         // Exit if an error other than ERROR_PIPE_BUSY occurs.
  53.  
  54.         if (GetLastError() != ERROR_PIPE_BUSY)
  55.         {
  56.             _tprintf(TEXT("Could not open pipe. GLE=%d\n"), GetLastError());
  57.             return -1;
  58.         }
  59.  
  60.         // All pipe instances are busy, so wait for 20 seconds.
  61.  
  62.         if (!WaitNamedPipe(lpszPipename, 20000))
  63.         {
  64.             printf("Could not open pipe: 20 second wait timed out.");
  65.             return -1;
  66.         }
  67.     }
  68.  
  69.     while (1) {
  70.         char * buf = new char[BUFSIZE];
  71.         // -------------------------------------READING DATA
  72.         do
  73.         {
  74.             fSuccess = ReadFile(hPipe, buf, BUFSIZE , &cbRead, NULL);
  75.             cbTotalRead += cbRead; // if there is more data than standard 4096 bytes buffer, repeat reading
  76.             if (!fSuccess && GetLastError() != ERROR_MORE_DATA)
  77.                 break;
  78.             buffer.insert(buffer.end(), buf, buf+cbRead);
  79.         } while (!fSuccess);
  80.  
  81.         string sResponse = string(buffer.begin(), buffer.end());
  82.         if (!fSuccess)
  83.         {
  84.             _tprintf(TEXT("ReadFile from pipe failed. GLE=%d\n"), GetLastError());
  85.             return -1;
  86.         }
  87.         else {
  88.             stripUnicode(sResponse);
  89.             json j = sResponse.c_str(); // creating JSON object from Response string
  90.         }
  91.  
  92.         // ------------------------------------ WRITTING DATA
  93.         dwToWrite = sResponse.size();
  94.         fSuccess = WriteFile(
  95.             hPipe,                  // pipe handle
  96.             buf,                    // message
  97.             dwToWrite,              // message length
  98.             &cbToWrite,             // bytes written
  99.             NULL);                  // not overlapped
  100.  
  101.         if (!fSuccess)
  102.         {
  103.             _tprintf(TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError());
  104.             return -1;
  105.         }
  106.         delete[] buf;
  107.         buffer.clear();
  108.         sResponse.clear();
  109.         cbTotalRead = cbToWrite = dwToWrite = 0;
  110.     }
  111.     FlushFileBuffers(hPipe);
  112.     DisconnectNamedPipe(hPipe);
  113.     CloseHandle(hPipe);
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement