Advertisement
Guest User

Untitled

a guest
Mar 24th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. #include <QMessageBox>
  2.  
  3. #include "Listener.h"
  4.  
  5. HANDLE hPipe;
  6.  
  7. Listener::Listener(PluginInterface_Listener_Return * returnObject)
  8.     : PluginInterface_Listener(returnObject)
  9. {
  10. }
  11.  
  12. /// \brief put this plugin in listen mode
  13. bool Listener::listen()
  14. {
  15.      // Create one event object for the connect operation.
  16.  
  17.     hConnectEvent = CreateEvent(
  18.        NULL,    // default security attribute
  19.        TRUE,    // manual reset event
  20.        TRUE,    // initial state = signaled
  21.        NULL);   // unnamed event object
  22.  
  23.     if (hConnectEvent == NULL)
  24.     {
  25.        printf("CreateEvent failed with %d.\n", GetLastError());
  26.        return 0;
  27.     }
  28.  
  29.     oConnect.hEvent = hConnectEvent;
  30.  
  31.      // Call a subroutine to create one instance, and wait for
  32.      // the client to connect.
  33.  
  34.     fPendingIO = CreateAndConnectInstance(&oConnect);
  35.     return true;
  36. }
  37.  
  38. /// \brief put close the listen
  39. void Listener::close()
  40. {
  41. }
  42.  
  43. /// \brief send when copy is finished
  44. void Listener::transferFinished(unsigned int orderId,bool withError)
  45. {
  46. }
  47.  
  48. /// \brief send when copy is canceled
  49. void Listener::transferCanceled(unsigned int orderId)
  50. {
  51. }
  52.  
  53. // CompletedWriteRoutine(DWORD, DWORD, LPOVERLAPPED)
  54. // This routine is called as a completion routine after writing to
  55. // the pipe, or when a new client has connected to a pipe instance.
  56. // It starts another read operation.
  57.  
  58. VOID WINAPI CompletedWriteRoutine(DWORD dwErr, DWORD cbWritten,
  59.    LPOVERLAPPED lpOverLap)
  60. {
  61.    LPPIPEINST lpPipeInst;
  62.    BOOL fRead = FALSE;
  63.  
  64. // lpOverlap points to storage for this instance.
  65.  
  66.    lpPipeInst = (LPPIPEINST) lpOverLap;
  67.  
  68. // The write operation has finished, so read the next request (if
  69. // there is no error).
  70.  
  71.    if ((dwErr == 0) && (cbWritten == lpPipeInst->cbToWrite))
  72.       fRead = ReadFileEx(
  73.      lpPipeInst->hPipeInst,
  74.      lpPipeInst->chRequest,
  75.      BUFSIZE*sizeof(TCHAR),
  76.      (LPOVERLAPPED) lpPipeInst,
  77.      (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedReadRoutine);
  78.  
  79. // Disconnect if an error occurred.
  80.  
  81.    if (! fRead)
  82.       DisconnectAndClose(lpPipeInst);
  83. }
  84.  
  85. // CompletedReadRoutine(DWORD, DWORD, LPOVERLAPPED)
  86. // This routine is called as an I/O completion routine after reading
  87. // a request from the client. It gets data and writes it to the pipe.
  88.  
  89. VOID WINAPI CompletedReadRoutine(DWORD dwErr, DWORD cbBytesRead,
  90.     LPOVERLAPPED lpOverLap)
  91. {
  92.    LPPIPEINST lpPipeInst;
  93.    BOOL fWrite = FALSE;
  94.  
  95. // lpOverlap points to storage for this instance.
  96.  
  97.    lpPipeInst = (LPPIPEINST) lpOverLap;
  98.  
  99. // The read operation has finished, so write a response (if no
  100. // error occurred).
  101.  
  102.    if ((dwErr == 0) && (cbBytesRead != 0))
  103.    {
  104.       GetAnswerToRequest(lpPipeInst);
  105.  
  106.       fWrite = WriteFileEx(
  107.      lpPipeInst->hPipeInst,
  108.      lpPipeInst->chReply,
  109.      lpPipeInst->cbToWrite,
  110.      (LPOVERLAPPED) lpPipeInst,
  111.      (LPOVERLAPPED_COMPLETION_ROUTINE) CompletedWriteRoutine);
  112.    }
  113.  
  114. // Disconnect if an error occurred.
  115.  
  116.    if (! fWrite)
  117.       DisconnectAndClose(lpPipeInst);
  118. }
  119.  
  120. // DisconnectAndClose(LPPIPEINST)
  121. // This routine is called when an error occurs or the client closes
  122. // its handle to the pipe.
  123.  
  124. VOID DisconnectAndClose(LPPIPEINST lpPipeInst)
  125. {
  126. // Disconnect the pipe instance.
  127.     QMessageBox::information(NULL,"toto","DisconnectAndClose");
  128.    if (! DisconnectNamedPipe(lpPipeInst->hPipeInst) )
  129.    {
  130.       printf("DisconnectNamedPipe failed with %d.\n", GetLastError());
  131.    }
  132.  
  133. // Close the handle to the pipe instance.
  134.  
  135.    CloseHandle(lpPipeInst->hPipeInst);
  136.  
  137. // Release the storage for the pipe instance.
  138.  
  139.    if (lpPipeInst != NULL)
  140.       GlobalFree(lpPipeInst);
  141. }
  142.  
  143. // CreateAndConnectInstance(LPOVERLAPPED)
  144. // This function creates a pipe instance and connects to the client.
  145. // It returns TRUE if the connect operation is pending, and FALSE if
  146. // the connection has been completed.
  147.  
  148. BOOL CreateAndConnectInstance(LPOVERLAPPED lpoOverlap)
  149. {
  150.     QMessageBox::information(NULL,"toto","CreateAndConnectInstance");
  151.     const char prefix[]="\\\\.\\pipe\\advanced-copier-";
  152.     char uname[1024];
  153.     char *m_pipename;
  154.     DWORD len=1023;
  155.     char *data;
  156.     // false ??
  157.     GetUserNameA(uname, &len);
  158.     // convert into hexa
  159.     data = toHex(uname);
  160.     m_pipename = (char *) malloc(sizeof(prefix)+strlen(data)+2);
  161. #if defined(_MFC_VER)
  162.     strcpy_s(m_pipename, _countof(prefix) ,prefix);
  163.     strcat_s(m_pipename, sizeof(prefix)+strlen(data)+2,data);
  164. #else
  165.     strcpy(m_pipename, prefix);
  166.     strcat(m_pipename, data);
  167. #endif
  168.     free(data);
  169.  
  170.  
  171.    //LPTSTR lpszPipename = m_pipename;
  172.  
  173.    hPipe = CreateNamedPipeA(
  174.       m_pipename,             // pipe name
  175.       PIPE_ACCESS_DUPLEX |      // read/write access
  176.       FILE_FLAG_OVERLAPPED,     // overlapped mode
  177.       PIPE_TYPE_MESSAGE |       // message-type pipe
  178.       PIPE_READMODE_MESSAGE |   // message read mode
  179.       PIPE_WAIT,                // blocking mode
  180.       PIPE_UNLIMITED_INSTANCES, // unlimited instances
  181.       BUFSIZE*sizeof(TCHAR),    // output buffer size
  182.       BUFSIZE*sizeof(TCHAR),    // input buffer size
  183.       PIPE_TIMEOUT,             // client time-out
  184.       NULL);                    // default security attributes
  185.    if (hPipe == INVALID_HANDLE_VALUE)
  186.    {
  187.       printf("CreateNamedPipe failed with %d.\n", GetLastError());
  188.       return 0;
  189.    }
  190.  
  191. // Call a subroutine to connect to the new client.
  192.  
  193.    return ConnectToNewClient(hPipe, lpoOverlap);
  194. }
  195.  
  196. BOOL ConnectToNewClient(HANDLE hPipe, LPOVERLAPPED lpo)
  197. {
  198.     QMessageBox::information(NULL,"toto","ConnectToNewClient");
  199.    BOOL fConnected, fPendingIO = FALSE;
  200.  
  201. // Start an overlapped connection for this pipe instance.
  202.    fConnected = ConnectNamedPipe(hPipe, lpo);
  203.  
  204. // Overlapped ConnectNamedPipe should return zero.
  205.    if (fConnected)
  206.    {
  207.       printf("ConnectNamedPipe failed with %d.\n", GetLastError());
  208.       return 0;
  209.    }
  210.  
  211.    switch (GetLastError())
  212.    {
  213.    // The overlapped connection in progress.
  214.       case ERROR_IO_PENDING:
  215.      fPendingIO = TRUE;
  216.      break;
  217.  
  218.    // Client is already connected, so signal an event.
  219.  
  220.       case ERROR_PIPE_CONNECTED:
  221.      if (SetEvent(lpo->hEvent))
  222.         break;
  223.  
  224.    // If an error occurs during the connect operation...
  225.       default:
  226.       {
  227.      printf("ConnectNamedPipe failed with %d.\n", GetLastError());
  228.      return 0;
  229.       }
  230.    }
  231.    return fPendingIO;
  232. }
  233.  
  234. VOID GetAnswerToRequest(LPPIPEINST pipe)
  235. {
  236.    _tprintf( TEXT("[%d] %s\n"), pipe->hPipeInst, pipe->chRequest);
  237.    lstrcpy(pipe->chReply, L"Default answer from server");//, BUFSIZE
  238.    pipe->cbToWrite = (lstrlen(pipe->chReply)+1)*sizeof(TCHAR);
  239. }
  240.  
  241. // Dump UTF16 (little endian)
  242. char * toHex(const char *str)
  243. {
  244.     char *p, *sz;
  245.     size_t len;
  246.     if (str==NULL)
  247.         return NULL;
  248.     len= strlen(str);
  249.     p = sz = (char *) malloc((len+1)*4);
  250.     // username goes hexa...
  251.     for (size_t i=0; i<len; i++)
  252.     {
  253.         #if defined(_MFC_VER)
  254.             sprintf_s(p, (len+1)*4, "%.2x00", str[i]);
  255.         #else
  256.             sprintf(p, "%.2x00", str[i]);
  257.         #endif
  258.         p+=4;
  259.     }
  260.     *p=0;
  261.     return sz;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement