-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeClient.cpp
More file actions
83 lines (73 loc) · 1.85 KB
/
PipeClient.cpp
File metadata and controls
83 lines (73 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "PipeClient.h"
/**
* @brief 생성자
*/
CPipeClient::CPipeClient()
{
m_hPipe = INVALID_HANDLE_VALUE;
}
/**
* @brief 소멸자
*/
CPipeClient::~CPipeClient()
{
CloseHandle(m_hPipe);
}
/**
* @brief 파이프 생성
* @param[in] lpszPipeName 생성할 파이프 이름
* @param[in] dwTimeOut 파이프 생성 타임아웃(ms)
* @return 성공시 true, 실패시 false 반환
*/
bool CPipeClient::Create(std::wstring& wstrPipeName, DWORD dwTimeOut)
{
bool nRet = true;
std::wstring wstrPipePath(L"\\\\.\\pipe\\");
wstrPipePath.append(wstrPipeName);
while (true) {
m_hPipe = CreateFile(
wstrPipePath.c_str(),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (m_hPipe == INVALID_HANDLE_VALUE) {
DWORD dwError = GetLastError();
if (dwError == ERROR_PIPE_BUSY) {
if (!WaitNamedPipe(wstrPipePath.c_str(), dwTimeOut)) {
// 대기 시간이 지난 경우
nRet = false;
break;
}
}
else {
// 다른 오류가 발생한 경우
nRet = false;
break;
}
}
else {
break;
}
}
return nRet;
}
/**
* @brief 파이프에 데이터 쓰기
* @param[in] lpBuffer 쓸 데이터
* @param[in] dwBufferSize 데이터 크기
* @param[out] lpBytesWritten 실제로 쓴 데이터 크기
* @return 성공시 true, 실패시 false 반환
*/
bool CPipeClient::Write(LPVOID lpBuffer, DWORD dwBufferSize, LPDWORD lpBytesWritten)
{
if (m_hPipe == INVALID_HANDLE_VALUE) {
return false;
}
if (!WriteFile(m_hPipe, lpBuffer, dwBufferSize, lpBytesWritten, NULL)) {
return false;
}
return true;
}