code_app/modules/base/HGNamedPipe.cpp

228 lines
4.7 KiB
C++
Raw Normal View History

2022-07-26 05:52:12 +00:00
#include "HGNamedPipe.h"
#include "HGInc.h"
struct HGNamedPipeOutImpl
{
HGNamedPipeOutImpl()
{
#if defined(HG_CMP_MSC)
m_hPipe = INVALID_HANDLE_VALUE;
#else
m_fdPipe = -1;
#endif
}
~HGNamedPipeOutImpl()
{
#if defined(HG_CMP_MSC)
if (INVALID_HANDLE_VALUE != m_hPipe)
{
CloseHandle(m_hPipe);
m_hPipe = INVALID_HANDLE_VALUE;
}
#else
if (-1 != m_fdPipe)
{
close(m_fdPipe);
m_fdPipe = -1;
}
if (!m_filePath.empty())
unlink(m_filePath.c_str());
#endif
}
#if defined(HG_CMP_MSC)
HANDLE m_hPipe;
#else
std::string m_filePath;
int m_fdPipe;
#endif
};
struct HGNamedPipeInImpl
{
HGNamedPipeInImpl()
{
#if defined(HG_CMP_MSC)
m_hPipe = INVALID_HANDLE_VALUE;
#else
m_fdPipe = -1;
#endif
}
~HGNamedPipeInImpl()
{
#if defined(HG_CMP_MSC)
if (INVALID_HANDLE_VALUE != m_hPipe)
{
CloseHandle(m_hPipe);
m_hPipe = INVALID_HANDLE_VALUE;
}
#else
if (-1 != m_fdPipe)
{
close(m_fdPipe);
m_fdPipe = -1;
}
#endif
}
#if defined(HG_CMP_MSC)
HANDLE m_hPipe;
#else
int m_fdPipe;
#endif
};
HGResult HGAPI HGBase_CreateNamedPipe(const HGChar* pipeName, HGNamedPipeOut* pipeOut)
{
if (NULL == pipeName || NULL == pipeOut)
{
return HGBASE_ERR_INVALIDARG;
}
#if defined(HG_CMP_MSC)
char name[256];
sprintf(name, "\\\\.\\pipe\\%s", pipeName);
HANDLE hPipe = CreateNamedPipeA(name, PIPE_ACCESS_OUTBOUND, PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, 0, 0, 0, NULL);
if (INVALID_HANDLE_VALUE == hPipe)
return HGBASE_ERR_FAIL;
if (!ConnectNamedPipe(hPipe, NULL))
{
CloseHandle(hPipe);
return HGBASE_ERR_FAIL;
}
HGNamedPipeOutImpl* pipeOutImpl = new HGNamedPipeOutImpl;
pipeOutImpl->m_hPipe = hPipe;
#else
char name[256];
sprintf(name, "/tmp/%s", pipeName);
if (access(name, F_OK) != -1)
return HGBASE_ERR_FAIL;
if (0 != mkfifo(name, 0777))
return HGBASE_ERR_FAIL;
int fdPipe = open(name, O_WRONLY);
if (-1 == fdPipe)
return HGBASE_ERR_FAIL;
HGNamedPipeOutImpl* pipeOutImpl = new HGNamedPipeOutImpl;
pipeOutImpl->m_filePath = name;
pipeOutImpl->m_fdPipe = fdPipe;
#endif
*pipeOut = (HGNamedPipeOut)pipeOutImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_DestroyNamedPipe(HGNamedPipeOut pipeOut)
{
if (NULL == pipeOut)
{
return HGBASE_ERR_INVALIDARG;
}
HGNamedPipeOutImpl* pipeOutImpl = (HGNamedPipeOutImpl*)pipeOut;
delete pipeOutImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_WriteNamedPipe(HGNamedPipeOut pipeOut, const HGByte* data, HGUInt size, HGUInt *writeSize)
{
if (NULL == pipeOut || NULL == data || 0 == size)
{
return HGBASE_ERR_INVALIDARG;
}
HGNamedPipeOutImpl* pipeOutImpl = (HGNamedPipeOutImpl*)pipeOut;
#if defined(HG_CMP_MSC)
DWORD dwNumerOfWriteBytes = 0;
if (!WriteFile(pipeOutImpl->m_hPipe, data, size, &dwNumerOfWriteBytes, NULL))
return HGBASE_ERR_FAIL;
if (NULL != writeSize)
*writeSize = dwNumerOfWriteBytes;
#else
int res = write(pipeOutImpl->m_fdPipe, data, size);
if (-1 == res)
return HGBASE_ERR_FAIL;
if (NULL != writeSize)
*writeSize = res;
#endif
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_OpenNamedPipe(const HGChar* pipeName, HGNamedPipeIn* pipeIn)
{
if (NULL == pipeName || NULL == pipeIn)
{
return HGBASE_ERR_INVALIDARG;
}
#if defined(HG_CMP_MSC)
char name[256];
sprintf(name, "\\\\.\\pipe\\%s", pipeName);
if (!WaitNamedPipeA(pipeName, NMPWAIT_USE_DEFAULT_WAIT))
return HGBASE_ERR_FAIL;
HANDLE hPipe = CreateFileA(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hPipe)
return HGBASE_ERR_FAIL;
HGNamedPipeInImpl* pipeInImpl = new HGNamedPipeInImpl;
pipeInImpl->m_hPipe = hPipe;
#else
char name[256];
sprintf(name, "/tmp/%s", pipeName);
int fdPipe = open(name, O_RDONLY);
if (-1 == fdPipe)
return HGBASE_ERR_FAIL;
HGNamedPipeInImpl* pipeInImpl = new HGNamedPipeInImpl;
pipeInImpl->m_fdPipe = fdPipe;
#endif
*pipeIn = (HGNamedPipeIn)pipeInImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_CloseNamedPipe(HGNamedPipeIn pipeIn)
{
if (NULL == pipeIn)
{
return HGBASE_ERR_INVALIDARG;
}
HGNamedPipeInImpl* pipeInImpl = (HGNamedPipeInImpl*)pipeIn;
delete pipeInImpl;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_ReadNamedPipe(HGNamedPipeIn pipeIn, HGByte* data, HGUInt size, HGUInt* readSize)
{
if (NULL == pipeIn || NULL == data || 0 == size)
{
return HGBASE_ERR_INVALIDARG;
}
HGNamedPipeInImpl* pipeInImpl = (HGNamedPipeInImpl*)pipeIn;
#if defined(HG_CMP_MSC)
DWORD dwNumerOfReadBytes = 0;
if (!ReadFile(pipeInImpl->m_hPipe, data, size, &dwNumerOfReadBytes, NULL))
return HGBASE_ERR_FAIL;
if (NULL != readSize)
*readSize = dwNumerOfReadBytes;
#else
int res = read(pipeInImpl->m_fdPipe, data, size);
if (-1 == res)
return HGBASE_ERR_FAIL;
if (NULL != readSize)
*readSize = res;
#endif
return HGBASE_ERR_OK;
}