bool 
CNdasEventPreSubscriber::BeginWaitForConnection()
{
	XTLENSURE_RETURN_T(!m_hPipe.IsInvalid() && "Not initialized", false);
	XTLENSURE_RETURN_T(!m_hEvent.IsInvalid() && "Not initialized", false);
	XTLENSURE_RETURN_T( ResetOverlapped(), false);
	BOOL fConnected = ::ConnectNamedPipe(m_hPipe, &m_overlapped);
	if (fConnected) 
	{
		XTLENSURE_RETURN_T(::SetEvent(m_hEvent), false);
		return true;
	}
	DWORD error = ::GetLastError();
	switch (error)
	{
	case ERROR_IO_PENDING: 
		// The overlapped connection in progress
		break;
	case ERROR_PIPE_CONNECTED:
		// client is already connected, signal an event
		XTLENSURE_RETURN_T(::SetEvent(m_hEvent), false);
		break;
	default:
		return true;
	}
	return true;
}
bool
CNdasEventSubscriber::BeginWriteMessage(const NDAS_EVENT_MESSAGE& msg)
{
	XTLENSURE_RETURN_T(ResetOverlapped(), false);
	const DWORD cbToWrite = sizeof(NDAS_EVENT_MESSAGE);
	DWORD cbWritten;
	BOOL fSuccess = ::WriteFile(m_hPipe, &msg, cbToWrite, &cbWritten, &m_overlapped);
	if (!fSuccess && ::GetLastError() != ERROR_IO_PENDING)
	{
		return false;
	}
	return true;
}
Beispiel #3
0
bool AsyncIO::asyncWrite(size_t qIdx, const void* buf, size_t length, off_t offset) {
  OVERLAPPED& aio = m_queue[qIdx].first;
  if (aio.hEvent) {
#ifndef NDEBUG
    fprintf(stderr, "WARNING: synchronous kabufuda fallback, check access polling\n");
#endif
    _waitForOperation(qIdx);
  } else {
    aio.hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
  }
  ResetOverlapped(aio, DWORD(offset));
  m_maxBlock = std::max(m_maxBlock, qIdx + 1);
  BOOL res = WriteFile(m_fh, buf, length, nullptr, &aio);
  return res == TRUE || GetLastError() == ERROR_IO_PENDING;
}
bool 
CNdasEventPreSubscriber::Initialize()
{
	if (m_hEvent.IsInvalid())
	{
		m_hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
		if (m_hEvent.IsInvalid())
		{
			return false;
		}
	}
	if (!ResetPipeHandle())
	{
		return false;
	}
	XTLENSURE_RETURN_T(ResetOverlapped(), false);
	return true;
}
bool
CNdasEventPreSubscriber::ResetPipeHandle()
{
	XTLASSERT(m_hPipe.IsInvalid());
	XTLENSURE_RETURN_T(ResetOverlapped(), false);
	m_hPipe = ::CreateNamedPipe(
		PipeName,
		PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED,
		PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
		MaxInstances,
		sizeof(NDAS_EVENT_MESSAGE),
		0,
		NMPWAIT_USE_DEFAULT_WAIT,
		NULL);
	if (m_hPipe.IsInvalid())
	{
		return false;
	}
	return true;
}