@@ -138,9 +138,9 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
138
138
// Use process ID as a unique ID for this process at this time.
139
139
base_name.append (std::to_string (GetCurrentProcessId ()));
140
140
const std::string in_name = base_name + " \\ IN" ;
141
- child_std_IN_Rd = CreateNamedPipe (
141
+ child_std_IN_Wr = CreateNamedPipe (
142
142
in_name.c_str (),
143
- PIPE_ACCESS_INBOUND , // Reading for us
143
+ PIPE_ACCESS_OUTBOUND , // Writing for us
144
144
PIPE_TYPE_BYTE | PIPE_NOWAIT, // Bytes and non-blocking
145
145
PIPE_UNLIMITED_INSTANCES, // Probably doesn't matter
146
146
BUFSIZE,
@@ -156,9 +156,9 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
156
156
throw system_exceptiont (" Input pipe creation failed for child_std_IN_Rd" );
157
157
}
158
158
// Connect to the other side of the pipe
159
- child_std_IN_Wr = CreateFileA (
159
+ child_std_IN_Rd = CreateFile (
160
160
in_name.c_str (),
161
- GENERIC_WRITE , // Write side
161
+ GENERIC_READ , // Read side
162
162
FILE_SHARE_READ | FILE_SHARE_WRITE, // Shared read/write
163
163
&sec_attr, // Need this for inherit
164
164
OPEN_EXISTING, // Opening other end
@@ -168,7 +168,8 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
168
168
{
169
169
throw system_exceptiont (" Input pipe creation failed for child_std_IN_Wr" );
170
170
}
171
- if (!SetHandleInformation (child_std_IN_Rd, HANDLE_FLAG_INHERIT, 0 ))
171
+ if (!SetHandleInformation (
172
+ child_std_IN_Rd, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
172
173
{
173
174
throw system_exceptiont (
174
175
" Input pipe creation failed on SetHandleInformation" );
@@ -187,7 +188,7 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
187
188
{
188
189
throw system_exceptiont (" Output pipe creation failed for child_std_OUT_Rd" );
189
190
}
190
- child_std_OUT_Wr = CreateFileA (
191
+ child_std_OUT_Wr = CreateFile (
191
192
out_name.c_str (),
192
193
GENERIC_WRITE, // Write side
193
194
FILE_SHARE_READ | FILE_SHARE_WRITE, // Shared read/write
@@ -233,6 +234,8 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
233
234
// recognize that the child process has ended (but maybe we don't care).
234
235
CloseHandle (child_std_OUT_Wr);
235
236
CloseHandle (child_std_IN_Rd);
237
+ if (!success)
238
+ throw system_exceptiont (" Process creation failed." );
236
239
# else
237
240
238
241
if (pipe (pipe_input) == -1 )
@@ -335,18 +338,25 @@ piped_processt::~piped_processt()
335
338
# endif
336
339
}
337
340
341
+ NODISCARD
338
342
piped_processt::send_responset piped_processt::send (const std::string &message)
339
343
{
340
344
if (process_state != statet::RUNNING)
341
345
{
342
346
return send_responset::ERRORED;
343
347
}
344
348
#ifdef _WIN32
345
- if (!WriteFile (child_std_IN_Wr, message.c_str (), message.size (), NULL , NULL ))
349
+ const auto message_size = narrow<DWORD>(message.size ());
350
+ DWORD bytes_written = 0 ;
351
+ if (!WriteFile (
352
+ child_std_IN_Wr, message.c_str (), message_size, &bytes_written, NULL ))
346
353
{
347
354
// Error handling with GetLastError ?
348
355
return send_responset::FAILED;
349
356
}
357
+ INVARIANT (
358
+ message_size == bytes_written,
359
+ " Number of bytes written to sub process must match message size." );
350
360
#else
351
361
// send message to solver process
352
362
int send_status = fputs (message.c_str (), command_stream);
@@ -415,15 +425,22 @@ bool piped_processt::can_receive(optionalt<std::size_t> wait_time)
415
425
const int timeout = wait_time ? narrow<int >(*wait_time) : -1 ;
416
426
#ifdef _WIN32
417
427
int waited_time = 0 ;
418
- // The next four need to be initialised for compiler warnings
419
- DWORD buffer = 0 ;
420
- LPDWORD nbytes = 0 ;
421
- LPDWORD rbytes = 0 ;
422
- LPDWORD rmbytes = 0 ;
428
+ DWORD total_bytes_available = 0 ;
423
429
while (timeout < 0 || waited_time >= timeout)
424
430
{
425
- PeekNamedPipe (child_std_OUT_Rd, &buffer, 1 , nbytes, rbytes, rmbytes);
426
- if (buffer != 0 )
431
+ const LPVOID lpBuffer = nullptr ;
432
+ const DWORD nBufferSize = 0 ;
433
+ const LPDWORD lpBytesRead = nullptr ;
434
+ const LPDWORD lpTotalBytesAvail = &total_bytes_available;
435
+ const LPDWORD lpBytesLeftThisMessage = nullptr ;
436
+ PeekNamedPipe (
437
+ child_std_OUT_Rd,
438
+ lpBuffer,
439
+ nBufferSize,
440
+ lpBytesRead,
441
+ lpTotalBytesAvail,
442
+ lpBytesLeftThisMessage);
443
+ if (total_bytes_available > 0 )
427
444
{
428
445
return true ;
429
446
}
0 commit comments