foobar2000 SDK  2015-08-03
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
ProcessUtils::PipeIO

#include <ProcessUtils.h>

+ Inheritance diagram for ProcessUtils::PipeIO:

Public Member Functions

 PipeIO (HANDLE handle, HANDLE hEvent, bool processMessages, DWORD timeOut=INFINITE)
 
 ~PipeIO ()
 
 PFC_DECLARE_EXCEPTION (timeout, exception_io,"Timeout")
 
size_t read (void *p_buffer, size_t p_bytes, abort_callback &abort)
 
size_t readPass (void *p_buffer, size_t p_bytes, abort_callback &abort)
 
void write (const void *p_buffer, size_t p_bytes, abort_callback &abort)
 

Private Member Functions

void _cancel (OVERLAPPED &ol)
 
DWORD myWait (DWORD count, const HANDLE *handles)
 

Static Private Member Functions

static void ProcessPendingMessages ()
 

Private Attributes

HANDLE m_event
 
HANDLE m_handle
 
const bool m_processMessages
 
const DWORD m_timeOut
 

Detailed Description

Definition at line 4 of file ProcessUtils.h.

Constructor & Destructor Documentation

ProcessUtils::PipeIO::PipeIO ( HANDLE  handle,
HANDLE  hEvent,
bool  processMessages,
DWORD  timeOut = INFINITE 
)
inline

Definition at line 7 of file ProcessUtils.h.

7  : m_handle(handle), m_event(hEvent), m_processMessages(processMessages), m_timeOut(timeOut) {
8  }
const DWORD m_timeOut
Definition: ProcessUtils.h:129
const bool m_processMessages
Definition: ProcessUtils.h:130
ProcessUtils::PipeIO::~PipeIO ( )
inline

Definition at line 9 of file ProcessUtils.h.

9  {
10  }

Member Function Documentation

void ProcessUtils::PipeIO::_cancel ( OVERLAPPED &  ol)
inlineprivate

Definition at line 115 of file ProcessUtils.h.

115  {
116  #if _WIN32_WINNT >= 0x600
117  CancelIoEx(m_handle,&ol);
118  #else
119  CancelIo(m_handle);
120  #endif
121  }
DWORD ProcessUtils::PipeIO::myWait ( DWORD  count,
const HANDLE handles 
)
inlineprivate

Definition at line 101 of file ProcessUtils.h.

101  {
102  if (m_processMessages) {
103  for(;;) {
104  DWORD state = MsgWaitForMultipleObjects(count, handles, FALSE, m_timeOut, QS_ALLINPUT);
105  if (state == WAIT_OBJECT_0 + count) {
107  } else {
108  return state;
109  }
110  }
111  } else {
112  return WaitForMultipleObjects(count, handles, FALSE, m_timeOut);
113  }
114  }
DWORD WaitForMultipleObjects(DWORD nCount, const HANDLE *lpHandles, BOOL bWaitAll, DWORD dwMilliseconds)
Definition: pp-winapi.h:20
const DWORD m_timeOut
Definition: ProcessUtils.h:129
const bool m_processMessages
Definition: ProcessUtils.h:130
static void ProcessPendingMessages()
Definition: ProcessUtils.h:122
ProcessUtils::PipeIO::PFC_DECLARE_EXCEPTION ( timeout  ,
exception_io  ,
"Timeout"   
)
static void ProcessUtils::PipeIO::ProcessPendingMessages ( )
inlinestaticprivate

Definition at line 122 of file ProcessUtils.h.

122  {
123  MSG msg = {};
124  while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
125  }
size_t ProcessUtils::PipeIO::read ( void *  p_buffer,
size_t  p_bytes,
abort_callback abort 
)
inline

Definition at line 46 of file ProcessUtils.h.

46  {
47  uint8_t * ptr = (uint8_t*) p_buffer;
48  size_t done = 0;
49  while(done < p_bytes) {
50  abort.check();
51  size_t delta = readPass(ptr + done, p_bytes - done, abort);
52  if (delta == 0) break;
53  done += delta;
54  }
55  return done;
56  }
size_t readPass(void *p_buffer, size_t p_bytes, abort_callback &abort)
Definition: ProcessUtils.h:57
size_t ProcessUtils::PipeIO::readPass ( void *  p_buffer,
size_t  p_bytes,
abort_callback abort 
)
inline

Definition at line 57 of file ProcessUtils.h.

57  {
58  if (p_bytes == 0) return 0;
59  OVERLAPPED ol = {};
60  ol.hEvent = m_event;
61  ResetEvent(m_event);
62  DWORD bytesDone;
63  SetLastError(NO_ERROR);
64  if (ReadFile( m_handle, p_buffer, pfc::downcast_guarded<DWORD>(p_bytes), &bytesDone, &ol)) {
65  // succeeded already?
66  return bytesDone;
67  }
68 
69  {
70  const DWORD code = GetLastError();
71  switch(code) {
72  case ERROR_HANDLE_EOF:
73  return 0;
74  case ERROR_IO_PENDING:
75  break; // continue
76  default:
78  };
79  }
80 
81  const HANDLE handles[] = {m_event, abort.get_abort_event()};
82  SetLastError(NO_ERROR);
83  DWORD state = myWait(_countof(handles), handles);
84  if (state == WAIT_OBJECT_0) {
85  SetLastError(NO_ERROR);
86  if (!GetOverlappedResult(m_handle,&ol,&bytesDone,TRUE)) {
87  const DWORD code = GetLastError();
88  if (code == ERROR_HANDLE_EOF) bytesDone = 0;
89  else {
90  _cancel(ol);
92  }
93  }
94  return bytesDone;
95  }
96  _cancel(ol);
97  abort.check();
98  throw timeout();
99  }
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
void _cancel(OVERLAPPED &ol)
Definition: ProcessUtils.h:115
DWORD myWait(DWORD count, const HANDLE *handles)
Definition: ProcessUtils.h:101
void ProcessUtils::PipeIO::write ( const void *  p_buffer,
size_t  p_bytes,
abort_callback abort 
)
inline

Definition at line 12 of file ProcessUtils.h.

12  {
13  if (p_bytes == 0) return;
14  OVERLAPPED ol = {};
15  ol.hEvent = m_event;
16  ResetEvent(m_event);
17  DWORD bytesWritten;
18  SetLastError(NO_ERROR);
19  if (WriteFile( m_handle, p_buffer, pfc::downcast_guarded<DWORD>(p_bytes), &bytesWritten, &ol)) {
20  // succeeded already?
21  if (bytesWritten != p_bytes) throw exception_io();
22  return;
23  }
24 
25  {
26  const DWORD code = GetLastError();
27  if (code != ERROR_IO_PENDING) exception_io_from_win32(code);
28  }
29  const HANDLE handles[] = {m_event, abort.get_abort_event()};
30  SetLastError(NO_ERROR);
31  DWORD state = myWait(_countof(handles), handles);
32  if (state == WAIT_OBJECT_0) {
33  try {
34  WIN32_IO_OP( GetOverlappedResult(m_handle,&ol,&bytesWritten,TRUE) );
35  } catch(...) {
36  _cancel(ol);
37  throw;
38  }
39  if (bytesWritten != p_bytes) throw exception_io();
40  return;
41  }
42  _cancel(ol);
43  abort.check();
44  throw timeout();
45  }
PFC_NORETURN void exception_io_from_win32(DWORD p_code)
Definition: filesystem.cpp:773
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
void _cancel(OVERLAPPED &ol)
Definition: ProcessUtils.h:115
DWORD myWait(DWORD count, const HANDLE *handles)
Definition: ProcessUtils.h:101

Field Documentation

HANDLE ProcessUtils::PipeIO::m_event
private

Definition at line 128 of file ProcessUtils.h.

HANDLE ProcessUtils::PipeIO::m_handle
private

Definition at line 127 of file ProcessUtils.h.

const bool ProcessUtils::PipeIO::m_processMessages
private

Definition at line 130 of file ProcessUtils.h.

const DWORD ProcessUtils::PipeIO::m_timeOut
private

Definition at line 129 of file ProcessUtils.h.


The documentation for this class was generated from the following file: