-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServerPipe.cpp
176 lines (146 loc) · 4.69 KB
/
ServerPipe.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: SERVERPIPE.CPP
** COMPONENT: Network & Comms Library
** DESCRIPTION: CServerPipe class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "ServerPipe.hpp"
#include "PipeException.hpp"
#include <malloc.h>
#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) // GCC 4.2+
// missing initializer for member 'X'
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
/******************************************************************************
**
** Constants.
**
*******************************************************************************
*/
const DWORD CServerPipe::DEF_OPEN_MODE = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED;
const DWORD CServerPipe::DEF_PIPE_MODE = PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT;
const DWORD CServerPipe::DEF_BUF_SIZE = 4096;
/******************************************************************************
** Method: Constructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CServerPipe::CServerPipe()
{
}
/******************************************************************************
** Method: Destructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CServerPipe::~CServerPipe()
{
Close();
}
/******************************************************************************
** Method: Create()
**
** Description: Create an instance of the named pipe.
**
** Parameters: pszName The pipes' name.
**
** Returns: Nothing.
**
** Exceptions: CPipeException.
**
*******************************************************************************
*/
void CServerPipe::Create(const tchar* pszName)
{
SECURITY_DESCRIPTOR* pSecDescriptor = (SECURITY_DESCRIPTOR*) alloca(SECURITY_DESCRIPTOR_MIN_LENGTH);
// Create a security descriptor to allow everyone access.
if (::InitializeSecurityDescriptor(pSecDescriptor, SECURITY_DESCRIPTOR_REVISION) == 0)
throw CPipeException(CPipeException::E_CREATE_FAILED, ::GetLastError());
if (::SetSecurityDescriptorDacl(pSecDescriptor, TRUE, nullptr, FALSE) == 0)
throw CPipeException(CPipeException::E_CREATE_FAILED, ::GetLastError());
SECURITY_ATTRIBUTES oSecAttributes = { 0 };
// Bind descriptor to attributes handle.
oSecAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
oSecAttributes.bInheritHandle = TRUE;
oSecAttributes.lpSecurityDescriptor = pSecDescriptor;
// Reset error flag, to detect ERROR_ALREADY_EXISTS.
::SetLastError(NO_ERROR);
// Attempt to create the pipe.
m_hPipe = ::CreateNamedPipe(pszName, DEF_OPEN_MODE, DEF_PIPE_MODE,
PIPE_UNLIMITED_INSTANCES, DEF_BUF_SIZE,
DEF_BUF_SIZE, m_dwTimeOut, &oSecAttributes);
// Create failed?
if (m_hPipe == INVALID_HANDLE_VALUE)
throw CPipeException(CPipeException::E_CREATE_FAILED, ::GetLastError());
// Already exists?
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
Close();
throw CPipeException(CPipeException::E_CREATE_FAILED, ERROR_ALREADY_EXISTS);
}
// Connection waiting?
BOOL bResult = ::ConnectNamedPipe(m_hPipe, &m_oReadIO);
// Connection already established?
if ( (bResult == FALSE) && (::GetLastError() == ERROR_PIPE_CONNECTED) )
bResult = TRUE;
// Error occurred?
if ( (bResult == FALSE) && (::GetLastError() != ERROR_IO_PENDING) )
throw CPipeException(CPipeException::E_ACCEPT_FAILED, ::GetLastError());
}
/******************************************************************************
** Method: Accept()
**
** Description: Accept a connection if one is outstanding.
**
** Parameters: None.
**
** Returns: true or false.
**
** Exceptions: CPipeException.
**
*******************************************************************************
*/
bool CServerPipe::Accept()
{
ASSERT(m_hPipe != INVALID_HANDLE_VALUE);
DWORD dwRead;
// Connection accepted?
BOOL bResult = ::GetOverlappedResult(m_hPipe, &m_oReadIO, &dwRead, FALSE);
// Error occurred?
if ( (bResult == FALSE) && (::GetLastError() != ERROR_IO_INCOMPLETE) )
throw CPipeException(CPipeException::E_ACCEPT_FAILED, ::GetLastError());
return bResult;
}
/******************************************************************************
** Method: Close()
**
** Description: Close the named pipe.
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
void CServerPipe::Close()
{
if (m_hPipe != INVALID_HANDLE_VALUE)
::DisconnectNamedPipe(m_hPipe);
CNamedPipe::Close();
}