Exemplo n.º 1
0
bool WindowsReadPipe::ReceiveResultReady(unsigned long timeout)
{
	if (!m_inProgress)
		return true;

	switch (WaitForSingleObject(m_event, timeout))
	{
	default:
		CheckAndHandleError("WaitForSingleObject", false);
	case WAIT_TIMEOUT:
		return false;
	case WAIT_OBJECT_0:
		;
	}

	BOOL result = GetOverlappedResult(m_h, &m_overlapped, &m_lastResult, false);
	if (!result)
	{
		switch (GetLastError())
		{
		default:
			CheckAndHandleError("GetOverlappedResult", false);
		case ERROR_BROKEN_PIPE:
		case ERROR_HANDLE_EOF:
			m_lastResult = 0;
			m_eofReceived = true;
		}
	}

	m_inProgress = false;
	return true;
}
Exemplo n.º 2
0
bool WindowsPipeReceiver::Receive(byte* buf, size_t bufLen)
{
	CRYPTOPP_ASSERT(!m_resultPending && !m_eofReceived);

	HANDLE h = GetHandle();
	// don't queue too much at once, or we might use up non-paged memory
	if (ReadFile(h, buf, UnsignedMin((DWORD)128*1024, bufLen), &m_lastResult, &m_overlapped))
	{
		if (m_lastResult == 0)
			m_eofReceived = true;
	}
	else
	{
		switch (GetLastError())
		{
		default:
			CheckAndHandleError("ReadFile", false);
		case ERROR_BROKEN_PIPE:
		case ERROR_HANDLE_EOF:
			m_lastResult = 0;
			m_eofReceived = true;
			break;
		case ERROR_IO_PENDING:
			m_resultPending = true;
		}
	}
	return !m_resultPending;
}
Exemplo n.º 3
0
unsigned int WindowsPipeReceiver::GetReceiveResult()
{
	if (m_resultPending)
	{
		HANDLE h = GetHandle();
		if (GetOverlappedResult(h, &m_overlapped, &m_lastResult, false))
		{
			if (m_lastResult == 0)
				m_eofReceived = true;
		}
		else
		{
			switch (GetLastError())
			{
			default:
				CheckAndHandleError("GetOverlappedResult", false);
			case ERROR_BROKEN_PIPE:
			case ERROR_HANDLE_EOF:
				m_lastResult = 0;
				m_eofReceived = true;
			}
		}
		m_resultPending = false;
	}
	return m_lastResult;
}
Exemplo n.º 4
0
unsigned int WindowsPipeSender::GetSendResult()
{
	if (m_resultPending)
	{
		const HANDLE h = GetHandle();
#if defined(USE_WINDOWS8_API)
		BOOL result = GetOverlappedResultEx(h, &m_overlapped, &m_lastResult, INFINITE, FALSE);
		CheckAndHandleError("GetOverlappedResultEx", result);
#else
		BOOL result = GetOverlappedResult(h, &m_overlapped, &m_lastResult, FALSE);
		CheckAndHandleError("GetOverlappedResult", result);
#endif
		m_resultPending = false;
	}
	return m_lastResult;
}
Exemplo n.º 5
0
unsigned int WindowsPipeReceiver::GetReceiveResult()
{
	if (m_resultPending)
	{
#if defined(USE_WINDOWS8_API)
		BOOL result = GetOverlappedResultEx(GetHandle(), &m_overlapped, &m_lastResult, INFINITE, FALSE);
#else
		BOOL result = GetOverlappedResult(GetHandle(), &m_overlapped, &m_lastResult, FALSE);
#endif
		if (result)
		{
			if (m_lastResult == 0)
				m_eofReceived = true;
		}
		else
		{
			switch (GetLastError())
			{
			default:
				CheckAndHandleError("GetOverlappedResult", false);
				// Fall through for non-fatal
			case ERROR_BROKEN_PIPE:
			case ERROR_HANDLE_EOF:
				m_lastResult = 0;
				m_eofReceived = true;
			}
		}
		m_resultPending = false;
	}
	return m_lastResult;
}
Exemplo n.º 6
0
WindowsPipeReceiver::WindowsPipeReceiver()
	: m_resultPending(false), m_eofReceived(false)
{
	m_event.AttachHandle(CreateEvent(NULL, true, false, NULL), true);
	CheckAndHandleError("CreateEvent", m_event.HandleValid());
	memset(&m_overlapped, 0, sizeof(m_overlapped));
	m_overlapped.hEvent = m_event;
}
Exemplo n.º 7
0
void Socket::Create(int nType)
{
	CRYPTOPP_ASSERT(m_s == INVALID_SOCKET);
	m_s = socket(AF_INET, nType, 0);
	CheckAndHandleError("socket", m_s);
	m_own = true;
	SocketChanged();
}
Exemplo n.º 8
0
WindowsReadPipe::WindowsReadPipe(HANDLE h, bool own)
	: WindowsPipe(h, own), m_inProgress(false), m_lastResult(0), m_eofReceived(false)
{
	m_event.AttachHandle(CreateEvent(NULL, true, false, NULL), true);
	CheckAndHandleError("CreateEvent", m_event.HandleValid());
	memset(&m_overlapped, 0, sizeof(m_overlapped));
	m_overlapped.hEvent = m_event;
}
Exemplo n.º 9
0
bool Socket::Accept(Socket& target, sockaddr *psa, socklen_t *psaLen)
{
	CRYPTOPP_ASSERT(m_s != INVALID_SOCKET);
	socket_t s = accept(m_s, psa, psaLen);
	if (s == INVALID_SOCKET && GetLastError() == SOCKET_EWOULDBLOCK)
		return false;
	CheckAndHandleError("accept", s);
	target.AttachSocket(s, true);
	return true;
}
Exemplo n.º 10
0
unsigned int WindowsPipeSender::GetSendResult()
{
	if (m_resultPending)
	{
		HANDLE h = GetHandle();
		BOOL result = GetOverlappedResult(h, &m_overlapped, &m_lastResult, false);
		CheckAndHandleError("GetOverlappedResult", result);
		m_resultPending = false;
	}
	return m_lastResult;
}
Exemplo n.º 11
0
bool WindowsWritePipe::SendResultReady(unsigned long timeout)
{
	if (!m_inProgress)
		return true;

	switch (WaitForSingleObject(m_event, timeout))
	{
	default:
		CheckAndHandleError("WaitForSingleObject", false);
	case WAIT_TIMEOUT:
		return false;
	case WAIT_OBJECT_0:
		break;
	}

	BOOL result = GetOverlappedResult(m_h, &m_overlapped, &m_lastResult, false);
	CheckAndHandleError("GetOverlappedResult", result);
	m_inProgress = false;
	return true;
}
Exemplo n.º 12
0
void WindowsPipeSender::Send(const byte* buf, size_t bufLen)
{
	DWORD written = 0;
	HANDLE h = GetHandle();
	// don't queue too much at once, or we might use up non-paged memory
	if (WriteFile(h, buf, UnsignedMin((DWORD)128*1024, bufLen), &written, &m_overlapped))
	{
		m_resultPending = false;
		m_lastResult = written;
	}
	else
	{
		if (GetLastError() != ERROR_IO_PENDING)
			CheckAndHandleError("WriteFile", false);

		m_resultPending = true;
	}
}
Exemplo n.º 13
0
bool WindowsWritePipe::Send(const byte* buf, unsigned int bufLen)
{
	DWORD written = 0;
	BOOL result = WriteFile(m_h, buf, bufLen, &written, &m_overlapped);
	if (result)
	{
		m_inProgress = false;
		m_lastResult = written;
		return true;
	}
	else
	{
		if (GetLastError() != ERROR_IO_PENDING)
			CheckAndHandleError("WriteFile", false);

		m_inProgress = true;
		return false;
	}
}
Exemplo n.º 14
0
bool WindowsReadPipe::Receive(byte* buf, unsigned int bufLen)
{
	assert(!m_inProgress && !m_eofReceived);

	DWORD read = 0;
	BOOL result = ReadFile(m_h, buf, bufLen, &read, &m_overlapped);
	if (result)
		m_lastResult = read;
	else switch (GetLastError())
	{
	default:
		CheckAndHandleError("ReadFile", false);
	case ERROR_BROKEN_PIPE:
	case ERROR_HANDLE_EOF:
		m_lastResult = 0;
		m_eofReceived = true;
		break;
	case ERROR_IO_PENDING:
		m_inProgress = true;
	}
	return !m_inProgress;
}