6 #if !defined(NO_OS_DEPENDENCE) && defined(WINDOWS_PIPES_AVAILABLE) 12 #if defined(CRYPTOPP_WIN32_AVAILABLE) 13 # if ((WINVER >= 0x0602 ) || (_WIN32_WINNT >= 0x0602 )) 14 # include <synchapi.h> 15 # include <ioapiset.h> 16 # define USE_WINDOWS8_API 22 WindowsHandle::WindowsHandle(HANDLE
h,
bool own)
27 WindowsHandle::~WindowsHandle()
42 bool WindowsHandle::HandleValid()
const 44 return m_h && m_h != INVALID_HANDLE_VALUE;
47 void WindowsHandle::AttachHandle(HANDLE
h,
bool own)
57 HANDLE WindowsHandle::DetachHandle()
60 m_h = INVALID_HANDLE_VALUE;
65 void WindowsHandle::CloseHandle()
67 if (m_h != INVALID_HANDLE_VALUE)
70 m_h = INVALID_HANDLE_VALUE;
79 DWORD err = GetLastError();
80 throw Err(GetHandle(), operation, err);
83 WindowsPipe::Err::Err(HANDLE s,
const std::string& operation,
int error)
84 :
OS_Error(IO_ERROR,
"WindowsPipe: " + operation +
" operation failed with error 0x" +
IntToString(error, 16), operation, error)
91 WindowsPipeReceiver::WindowsPipeReceiver()
92 : m_lastResult(0), m_resultPending(false), m_eofReceived(false)
94 m_event.AttachHandle(CreateEvent(NULL,
true,
false, NULL),
true);
95 CheckAndHandleError(
"CreateEvent", m_event.HandleValid());
96 memset(&m_overlapped, 0,
sizeof(m_overlapped));
97 m_overlapped.hEvent = m_event;
100 bool WindowsPipeReceiver::Receive(
byte* buf,
size_t bufLen)
104 const HANDLE h = GetHandle();
106 if (ReadFile(h, buf,
UnsignedMin((DWORD)128*1024, bufLen), &m_lastResult, &m_overlapped))
108 if (m_lastResult == 0)
109 m_eofReceived =
true;
113 switch (GetLastError())
116 CheckAndHandleError(
"ReadFile",
false);
118 case ERROR_BROKEN_PIPE:
119 case ERROR_HANDLE_EOF:
121 m_eofReceived =
true;
123 case ERROR_IO_PENDING:
124 m_resultPending =
true;
127 return !m_resultPending;
133 container.AddHandle(m_event,
CallStack(
"WindowsPipeReceiver::GetWaitObjects() - result pending", &callStack));
134 else if (!m_eofReceived)
135 container.SetNoWait(
CallStack(
"WindowsPipeReceiver::GetWaitObjects() - result ready", &callStack));
138 unsigned int WindowsPipeReceiver::GetReceiveResult()
142 #if defined(USE_WINDOWS8_API) 143 BOOL result = GetOverlappedResultEx(GetHandle(), &m_overlapped, &m_lastResult, INFINITE, FALSE);
145 BOOL result = GetOverlappedResult(GetHandle(), &m_overlapped, &m_lastResult, FALSE);
149 if (m_lastResult == 0)
150 m_eofReceived =
true;
154 switch (GetLastError())
157 CheckAndHandleError(
"GetOverlappedResult",
false);
159 case ERROR_BROKEN_PIPE:
160 case ERROR_HANDLE_EOF:
162 m_eofReceived =
true;
165 m_resultPending =
false;
172 WindowsPipeSender::WindowsPipeSender()
173 : m_lastResult(0), m_resultPending(false)
175 m_event.AttachHandle(CreateEvent(NULL,
true,
false, NULL),
true);
176 CheckAndHandleError(
"CreateEvent", m_event.HandleValid());
177 memset(&m_overlapped, 0,
sizeof(m_overlapped));
178 m_overlapped.hEvent = m_event;
181 void WindowsPipeSender::Send(
const byte* buf,
size_t bufLen)
184 const HANDLE h = GetHandle();
186 if (WriteFile(h, buf,
UnsignedMin((DWORD)128*1024, bufLen), &written, &m_overlapped))
188 m_resultPending =
false;
189 m_lastResult = written;
193 if (GetLastError() != ERROR_IO_PENDING)
194 CheckAndHandleError(
"WriteFile",
false);
196 m_resultPending =
true;
203 container.AddHandle(m_event,
CallStack(
"WindowsPipeSender::GetWaitObjects() - result pending", &callStack));
205 container.SetNoWait(
CallStack(
"WindowsPipeSender::GetWaitObjects() - result ready", &callStack));
208 unsigned int WindowsPipeSender::GetSendResult()
212 const HANDLE h = GetHandle();
213 #if defined(USE_WINDOWS8_API) 214 BOOL result = GetOverlappedResultEx(h, &m_overlapped, &m_lastResult, INFINITE, FALSE);
215 CheckAndHandleError(
"GetOverlappedResultEx", result);
217 BOOL result = GetOverlappedResult(h, &m_overlapped, &m_lastResult, FALSE);
218 CheckAndHandleError(
"GetOverlappedResult", result);
220 m_resultPending =
false;
Base class for all exceptions thrown by the library.
bool error(const char *fmt, const Args &...args)
The operating system reported an error.
#define NAMESPACE_BEGIN(x)
Library configuration file.
void HandleError(const leveldb::Status &status)
Handle database error by throwing dbwrapper_error exception.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
#define CRYPTOPP_ASSERT(exp)
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.