-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPipeConnection.cpp
279 lines (232 loc) · 6.69 KB
/
PipeConnection.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "framework.h"
bool cPipe::create( std::string pipe_name )
{
//Checks if the pipe is already created/connected
if ( pipe_handle != INVALID_HANDLE_VALUE )
return false;
//Truncates the chosen name to the relative path
pipe_final_name.append( pipe_name );
//Creates the named pipe
pipe_handle = CreateNamedPipeA( pipe_final_name.c_str( ) , //Pipe name
PIPE_ACCESS_DUPLEX , //Pipe access
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT , //Type of the messages
PIPE_UNLIMITED_INSTANCES , //Accepts multiple instances
BUFSIZE , //Output buffer size
BUFSIZE , //Input buffer size
NMPWAIT_USE_DEFAULT_WAIT , //Connection time-out
NULL ); //Default security attributes
if ( pipe_handle == INVALID_HANDLE_VALUE ) //If it fails to create the pipe, return false
{
return false;
}
ov.hEvent = CreateEvent( NULL , false , true , NULL );
ov.Offset = 0;
ov.OffsetHigh = 0;
thread_handle = CreateThread(
NULL , // No security attribute
0 , // Default stack size
pipe_thread , // Thread function
ReCa < LPVOID >( this ) , // Thread parameter
0 , // Not suspended
&thread_id ); // Returns thread ID
if ( thread_handle == 0 ) //If CreateThread fails, disconnect pipe and return false
{
CloseHandle( pipe_handle );
return false;
}
created_pipe = true; //Sets the flag that the pipe has been created successfuly
return true;
}
bool cPipe::connect( std::string pipe_name )
{
//Checks if the pipe is already created/connected
if ( pipe_handle != INVALID_HANDLE_VALUE )
return false;
//Truncates the chosen name to the relative path
pipe_final_name.append( pipe_name );
//Connects to the pipe
pipe_handle = CreateFile( pipe_final_name.c_str( ) ,
GENERIC_WRITE | GENERIC_READ ,
FILE_SHARE_READ | FILE_SHARE_WRITE ,
0 ,
OPEN_EXISTING ,
FILE_FLAG_OVERLAPPED ,
0 );
if ( pipe_handle == INVALID_HANDLE_VALUE ) //If it fails to create the pipe, return false
return false;
thread_handle = CreateThread(
NULL , // No security attribute
0 , // Default stack size
pipe_thread , // Thread function
ReCa < LPVOID >( this ) , // Thread parameter
0 , // Not suspended
&thread_id ); // Returns thread ID
if ( thread_handle == 0 ) //If CreateThread fails, disconnect pipe and return false
{
CloseHandle( pipe_handle );
return false;
}
ov.hEvent = CreateEvent( NULL , false , true , NULL );
ov.Offset = 0;
ov.OffsetHigh = 0;
return true;
}
bool cPipe::disconnect( void )
{
if ( created_pipe ) //If the instance created a named pipe, it has to disconnect
{
TerminateThread( thread_handle , 0 );
return DisconnectNamedPipe( pipe_handle ) && CloseHandle( pipe_handle );
}
TerminateThread( thread_handle , 0 );
return CloseHandle( pipe_handle );
}
int cPipe::get_last_error( void )
{
int last = last_error; //Gets the value from the last_error to a holder
last_error = PIPE_NO_ERROR; //Sets the holder to NO_ERROR
return last;
}
void cPipe::write( std::string message )
{
DWORD written;
if ( !WriteFile( pipe_handle , message.c_str( ) , message.length( ) , &written , NULL ) )
{
set_last_error( PIPE_WRITE_ERROR );
}
}
void cPipe::read( )
{
read_flag = true; //Sets the flag of reading
while ( !has_text && last_error == PIPE_NO_ERROR ) //Wait until there thread returns an status
{
Sleep( 1 );
}
}
std::string cPipe::get_message( void )
{
char* holder = new char [ read_buffer.length( ) ];
has_text = false; //Sets the flag for exiting read() loop
strcpy( holder , read_buffer.c_str( ) ); //Gets the text from the holder
read_buffer = ""; //Sets the holder to an empty string
return holder;
}
HANDLE cPipe::get_pipe_handle( void )
{
return pipe_handle;
}
HANDLE cPipe::get_thread_handle( void )
{
return thread_handle;
}
bool cPipe::can_write( void )
{
return write_flag;
}
bool cPipe::can_read( void )
{
return read_flag;
}
void cPipe::clear_write( void )
{
finished_writing = true;
write_flag = false;
}
void cPipe::clear_read( void )
{
read_flag = false;
}
void cPipe::set_message( std::string message )
{
has_text = true;
read_buffer = message;
}
std::string cPipe::get_write_message( void )
{
return write_buffer;
}
void cPipe::set_last_error( int error )
{
last_error = error;
}
bool cPipe::is_server( void )
{
return created_pipe;
}
cPipe* pipe;
DWORD WINAPI cPipe::pipe_thread( LPVOID param )
{
pipe = ReCa < cPipe* >( param );
if ( pipe->is_server( ) )
{
//Waits for client connection
bool connected = ConnectNamedPipe( pipe->get_pipe_handle( ) , &pipe->ov ) ? TRUE : ( GetLastError( ) == ERROR_PIPE_CONNECTED );
if ( !connected ) //If there's no connection, close pipe and return false
{
CloseHandle( pipe->get_pipe_handle( ) );
return false;
}
}
bool is_done_reading = false;
DWORD bytes_read;
DWORD written;
while ( true )
{
if ( pipe->can_read( ) )
{
do
{
DWORD available = 0;
//Using PeekNamedPipe to determine if there's a message available without locking the pipe
if ( PeekNamedPipe( pipe->get_pipe_handle( ) , NULL , NULL , NULL , &available , NULL ) )
{
if ( available > 0 )
{
//There is a message available, so it reads it
is_done_reading = ReadFile( pipe->get_pipe_handle( ) , pipe->out_message , 1024 * sizeof( TCHAR ) , &bytes_read , &pipe->ov );
//Checks if an error occoured ( if there is more data or a pending operations, just ignore it and wait )
if ( !is_done_reading && GetLastError( ) != ERROR_MORE_DATA && GetLastError( ) != ERROR_IO_PENDING )
{
pipe->set_last_error( PIPE_READ_ERROR );
}
if ( GetLastError( ) == ERROR_IO_PENDING )
{
while ( !HasOverlappedIoCompleted( &pipe->ov ) )
{
Sleep( 1 );
}
continue;
}
//Check the overlapped status
is_done_reading = GetOverlappedResult(
pipe->get_pipe_handle( ) , // handle to pipe
&pipe->ov , // OVERLAPPED structure
&bytes_read , // bytes transferred
false ); // do not wait
if ( !is_done_reading && GetLastError( ) != ERROR_IO_PENDING )
{
pipe->set_last_error( PIPE_READ_ERROR );
}
}
else
{
//There is no message, so clear the buffer and exit the loop
is_done_reading = true;
ZeroMemory( pipe->out_message , 2048 );
}
}
else
{
//There was an error in PeekNamedPipe, clear the buffer, set the error flag and exit
pipe->set_last_error( PIPE_READ_ERROR );
is_done_reading = true;
ZeroMemory( pipe->out_message , 2048 );
}
} while ( !is_done_reading );
//Clear the reading flag and set the message
pipe->clear_read( );
pipe->set_message( ReCa < char* > ( pipe->out_message ) );
}
}
return 0;
}