forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.cpp
53 lines (38 loc) · 1.09 KB
/
pipe.cpp
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
// pipe.cpp
#include "pipe.h"
// === pipe server ==========================================================
bool CPipeServer::Create(const char *name)
{
if (hPipe != INVALID_HANDLE_VALUE) return false;
hPipe = CreateNamedPipe(name, PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 2, 512, 512, 10000 /* ms */, NULL);
return hPipe != INVALID_HANDLE_VALUE;
}
void CPipeServer::WaitConnection()
{
if (!ConnectNamedPipe(hPipe, NULL)) throw int(3);
}
void CPipeServer::Close()
{
if (hPipe != INVALID_HANDLE_VALUE)
{
CloseHandle(hPipe);
hPipe = INVALID_HANDLE_VALUE;
}
}
// === pipe client ==========================================================
bool CPipeClient::Open(const char *name)
{
if (hPipe != INVALID_HANDLE_VALUE) return false;
hPipe = CreateFile(name, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
return hPipe != INVALID_HANDLE_VALUE;
}
void CPipeClient::Close()
{
if (hPipe != INVALID_HANDLE_VALUE)
{
CloseHandle(hPipe);
hPipe = INVALID_HANDLE_VALUE;
}
}