<commit_before>/*
 * Win32RequestResponsePipe.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
#include <core/system/Win32RequestResponsePipe.hpp>

#include <core/system/Environment.hpp>
#include <core/SafeConvert.hpp>

namespace core {
namespace system {

namespace {

const int MAX_PACKET_SIZE = 1000000;
const std::string ENV_REQ_READ("RS_RRPIPE_REQ_READ");
const std::string ENV_REQ_WRITE("RS_RRPIPE_REQ_WRITE");
const std::string ENV_RESP_READ("RS_RRPIPE_RESP_READ");
const std::string ENV_RESP_WRITE("RS_RRPIPE_RESP_WRITE");
const std::string ENV_EVENT("RS_RRPIPE_EVENT");

void closeHandle(HANDLE* pHandle)
{
   if (*pHandle != INVALID_HANDLE_VALUE)
   {
      ::CloseHandle(*pHandle);
      *pHandle = INVALID_HANDLE_VALUE;
   }
}

bool writeToPipe(HANDLE hPipeWrite,
                 const char* pBuffer,
                 uint32_t bytes)
{
   DWORD bytesToWrite = bytes;
   while (bytesToWrite > 0)
   {
      DWORD bytesWritten;
      if (!::WriteFile(hPipeWrite,
                       pBuffer,
                       bytesToWrite,
                       &bytesWritten,
                       NULL))
      {
         return false;
      }
      bytesToWrite -= bytesWritten;
      pBuffer += bytesWritten;
   }
   return true;
}

bool readFromPipe(HANDLE hPipeRead,
                  std::vector<char>* pBuffer,
                  uint32_t bytes)
{
   pBuffer->resize(bytes);
   char* pBuf = &((*pBuffer)[0]);

   DWORD bytesToRead = bytes;
   while (bytesToRead > 0)
   {
      DWORD bytesRead;
      if (!::ReadFile(hPipeRead,
                      pBuf,
                      bytesToRead,
                      &bytesRead,
                      NULL))
      {
         return false;
      }
      bytesToRead -= bytesRead;
      pBuffer += bytesRead;
   }
   return true;
}

bool writePacket(HANDLE hPipeWrite,
                 const char* pBuffer,
                 uint32_t bytes)
{
   return writeToPipe(hPipeWrite,
                      reinterpret_cast<char*>(&bytes),
                      sizeof(bytes))
         && writeToPipe(hPipeWrite, pBuffer, bytes);
}

bool readPacket(HANDLE hPipeRead,
                std::vector<char>* pBuffer)
{
   std::vector<char> numBuff;
   numBuff.resize(sizeof(uint32_t));
   if (!readFromPipe(hPipeRead, &numBuff, sizeof(uint32_t)))
      return false;

   uint32_t packetSize = *(reinterpret_cast<uint32_t*>(&(numBuff[0])));
   if (packetSize > MAX_PACKET_SIZE)
   {
      ::SetLastError(ERROR_BUFFER_OVERFLOW);
      return false;
   }

   pBuffer->resize(packetSize);
   return readFromPipe(hPipeRead, pBuffer, packetSize);
}

void setEnvHandle(const std::string& name,
                  HANDLE handle)
{
   setenv(name,
          boost::lexical_cast<std::string>(handle));
}

HANDLE getEnvHandle(const std::string& name)
{
   std::string value = getenv(name);
   if (value.empty())
      return INVALID_HANDLE_VALUE;
   return safe_convert::stringTo<HANDLE>(value, INVALID_HANDLE_VALUE);
}

} // namespace

Win32RequestResponsePipe::Win32RequestResponsePipe()
   : hRequestReadPipe_(INVALID_HANDLE_VALUE),
     hRequestWritePipe_(INVALID_HANDLE_VALUE),
     hResponseReadPipe_(INVALID_HANDLE_VALUE),
     hResponseWritePipe_(INVALID_HANDLE_VALUE),
     hManualResetEvent_(INVALID_HANDLE_VALUE),
     isChild_(false)
{
}

Win32RequestResponsePipe::~Win32RequestResponsePipe()
{
   closeHandle(&hRequestReadPipe_);
   closeHandle(&hRequestWritePipe_);
   closeHandle(&hResponseReadPipe_);
   closeHandle(&hResponseWritePipe_);
   closeHandle(&hManualResetEvent_);
}<commit_msg>add destructor exception handler to Win32RequestResponsePipe<commit_after>/*
 * Win32RequestResponsePipe.cpp
 *
 * Copyright (C) 2009-11 by RStudio, Inc.
 *
 * This program is licensed to you under the terms of version 3 of the
 * GNU Affero General Public License. This program is distributed WITHOUT
 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
 *
 */
#include <core/system/Win32RequestResponsePipe.hpp>

#include <core/system/Environment.hpp>
#include <core/SafeConvert.hpp>

namespace core {
namespace system {

namespace {

const int MAX_PACKET_SIZE = 1000000;
const std::string ENV_REQ_READ("RS_RRPIPE_REQ_READ");
const std::string ENV_REQ_WRITE("RS_RRPIPE_REQ_WRITE");
const std::string ENV_RESP_READ("RS_RRPIPE_RESP_READ");
const std::string ENV_RESP_WRITE("RS_RRPIPE_RESP_WRITE");
const std::string ENV_EVENT("RS_RRPIPE_EVENT");

void closeHandle(HANDLE* pHandle)
{
   if (*pHandle != INVALID_HANDLE_VALUE)
   {
      ::CloseHandle(*pHandle);
      *pHandle = INVALID_HANDLE_VALUE;
   }
}

bool writeToPipe(HANDLE hPipeWrite,
                 const char* pBuffer,
                 uint32_t bytes)
{
   DWORD bytesToWrite = bytes;
   while (bytesToWrite > 0)
   {
      DWORD bytesWritten;
      if (!::WriteFile(hPipeWrite,
                       pBuffer,
                       bytesToWrite,
                       &bytesWritten,
                       NULL))
      {
         return false;
      }
      bytesToWrite -= bytesWritten;
      pBuffer += bytesWritten;
   }
   return true;
}

bool readFromPipe(HANDLE hPipeRead,
                  std::vector<char>* pBuffer,
                  uint32_t bytes)
{
   pBuffer->resize(bytes);
   char* pBuf = &((*pBuffer)[0]);

   DWORD bytesToRead = bytes;
   while (bytesToRead > 0)
   {
      DWORD bytesRead;
      if (!::ReadFile(hPipeRead,
                      pBuf,
                      bytesToRead,
                      &bytesRead,
                      NULL))
      {
         return false;
      }
      bytesToRead -= bytesRead;
      pBuffer += bytesRead;
   }
   return true;
}

bool writePacket(HANDLE hPipeWrite,
                 const char* pBuffer,
                 uint32_t bytes)
{
   return writeToPipe(hPipeWrite,
                      reinterpret_cast<char*>(&bytes),
                      sizeof(bytes))
         && writeToPipe(hPipeWrite, pBuffer, bytes);
}

bool readPacket(HANDLE hPipeRead,
                std::vector<char>* pBuffer)
{
   std::vector<char> numBuff;
   numBuff.resize(sizeof(uint32_t));
   if (!readFromPipe(hPipeRead, &numBuff, sizeof(uint32_t)))
      return false;

   uint32_t packetSize = *(reinterpret_cast<uint32_t*>(&(numBuff[0])));
   if (packetSize > MAX_PACKET_SIZE)
   {
      ::SetLastError(ERROR_BUFFER_OVERFLOW);
      return false;
   }

   pBuffer->resize(packetSize);
   return readFromPipe(hPipeRead, pBuffer, packetSize);
}

void setEnvHandle(const std::string& name,
                  HANDLE handle)
{
   setenv(name,
          boost::lexical_cast<std::string>(handle));
}

HANDLE getEnvHandle(const std::string& name)
{
   std::string value = getenv(name);
   if (value.empty())
      return INVALID_HANDLE_VALUE;
   return safe_convert::stringTo<HANDLE>(value, INVALID_HANDLE_VALUE);
}

} // namespace

Win32RequestResponsePipe::Win32RequestResponsePipe()
   : hRequestReadPipe_(INVALID_HANDLE_VALUE),
     hRequestWritePipe_(INVALID_HANDLE_VALUE),
     hResponseReadPipe_(INVALID_HANDLE_VALUE),
     hResponseWritePipe_(INVALID_HANDLE_VALUE),
     hManualResetEvent_(INVALID_HANDLE_VALUE),
     isChild_(false)
{
}

Win32RequestResponsePipe::~Win32RequestResponsePipe()
{
   try
   {
      closeHandle(&hRequestReadPipe_);
      closeHandle(&hRequestWritePipe_);
      closeHandle(&hResponseReadPipe_);
      closeHandle(&hResponseWritePipe_);
      closeHandle(&hManualResetEvent_);
   }
   catch(...)
   {
   }
}
{'fraction_non_alphanumeric': 0.10423246999368288, 'fraction_numerical': 0.011876184459886292, 'mean_word_length': 2.8690127077223853, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4619815', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2464, 'n_words': 483}