Beispiel #1
0
BOOL 
CLpxDatagramSocket::RecvFrom(DWORD cbBufferMax, DWORD dwRecvFlags)
{
	LockRecvQueue();

	_ASSERTE(cbBufferMax > 0);

	BOOL fSuccess = FALSE;

	fSuccess = AllocRecvBuf(cbBufferMax);
	if (!fSuccess) {
		UnlockRecvQueue();
		return FALSE;
	}

	fSuccess = ::ResetEvent(m_hReceivedEvent);
	_ASSERTE(fSuccess);

	m_ovReceive.hEvent = m_hReceivedEvent;
	m_ovReceive.Internal = 
	m_ovReceive.InternalHigh = 
	m_ovReceive.Offset = 
	m_ovReceive.OffsetHigh = 0;

	DWORD cbReceived = 0;
	m_receiveFlags = dwRecvFlags;
	m_iRemoteAddrLen = sizeof(m_remoteAddr);

	INT iResult = ::WSARecvFrom(
		m_sock,
		&m_wsaReceiveBuffer, 1,
		&cbReceived,
		&m_receiveFlags,
		(struct sockaddr*) &m_remoteAddr,
		&m_iRemoteAddrLen,
		&m_ovReceive,
		NULL);

	if (0 == iResult) {
		fSuccess = ::SetEvent(m_hReceivedEvent);
		_ASSERTE(fSuccess);
		return TRUE;
	}

	if (WSA_IO_PENDING == ::WSAGetLastError()) {
		return TRUE;
	}

	UnlockRecvQueue();
	return FALSE;
}
Beispiel #2
0
BOOL
CLpxAsyncSocket::Close()
{
	_ASSERTE(INVALID_SOCKET != m_sock);

	//
	// We need to reset recv and send event
	// If there is a previous data is not fetched, 
	// Data Received/Sent event will be fetched on a closed socket.
	//
	BOOL fSuccess = ::ResetEvent(m_hReceivedEvent);
	_ASSERTE(fSuccess);

	fSuccess = ::ResetEvent(m_hSentEvent);
	_ASSERTE(fSuccess);

	if (m_lSendQueueLocks > 0) {
		UnlockSendQueue();
		_ASSERTE(0 == m_lSendQueueLocks);
	}
	if (m_lRecvQueueLocks > 0) {
		UnlockRecvQueue();
		_ASSERTE(0 == m_lRecvQueueLocks);
	}

	//
	// Reset Queue Locks
	//
	m_lSendQueueLocks = 0;
	m_lRecvQueueLocks = 0;

	INT iResult = ::closesocket(m_sock);
	if (0 != iResult) {
		XTLTRACE("Closing a sock %p failed, error=0x%X\n", 
			reinterpret_cast<PVOID>(m_sock), GetLastError());
		return FALSE;
	}

	m_sock = INVALID_SOCKET;
	return TRUE;
}
Beispiel #3
0
BOOL
CLpxAsyncSocket::Close()
{
	_ASSERTE(INVALID_SOCKET != m_sock);

	//
	// We need to reset recv and send event
	// If there is a previous data is not fetched, 
	// Data Received/Sent event will be fetched on a closed socket.
	//
	BOOL fSuccess = ::ResetEvent(m_hReceivedEvent);
	_ASSERTE(fSuccess);

	fSuccess = ::ResetEvent(m_hSentEvent);
	_ASSERTE(fSuccess);

	if (m_lSendQueueLocks > 0) {
		UnlockSendQueue();
		_ASSERTE(0 == m_lSendQueueLocks);
	}
	if (m_lRecvQueueLocks > 0) {
		UnlockRecvQueue();
		_ASSERTE(0 == m_lRecvQueueLocks);
	}

	//
	// Reset Queue Locks
	//
	m_lSendQueueLocks = 0;
	m_lRecvQueueLocks = 0;

	INT iResult = ::closesocket(m_sock);
	if (0 != iResult) {
		DBGPRT_ERR_EX(_FT("Closing a sock %p failed: "), m_sock);
		return FALSE;
	}

	m_sock = INVALID_SOCKET;
	return TRUE;
}
Beispiel #4
0
BOOL
CLpxDatagramSocket::GetRecvFromResult(
	OUT SOCKADDR_LPX* pRemoteAddr, 
	OUT LPDWORD lpcbReceived, 
	OUT BYTE** ppbData, 
	OUT LPDWORD lpdwFlags)
{
	BOOL fSuccess = CLpxAsyncSocket::_GetRecvResult(
		lpcbReceived, 
		ppbData, 
		lpdwFlags);

	if (!fSuccess)
	{
		DBGPRT_ERR_EX(_FT("_GetRecvResult failed: "));
		return FALSE;
	}

	if (pRemoteAddr) *pRemoteAddr = m_remoteAddr;
	UnlockRecvQueue();
	return TRUE;
}
Beispiel #5
0
BOOL
CLpxDatagramSocket::GetRecvFromResult(
	OUT SOCKADDR_LPX* pRemoteAddr, 
	OUT LPDWORD lpcbReceived, 
	OUT BYTE** ppbData, 
	OUT LPDWORD lpdwFlags)
{
	BOOL fSuccess = CLpxAsyncSocket::_GetRecvResult(
		lpcbReceived, 
		ppbData, 
		lpdwFlags);

	if (!fSuccess)
	{
		XTLTRACE("CLpxDatagramSocket._GetRecvResult failed, socket=%p, error=0x%X\n", 
			reinterpret_cast<PVOID>(m_sock), GetLastError());
		return FALSE;
	}

	if (pRemoteAddr) *pRemoteAddr = m_remoteAddr;
	UnlockRecvQueue();
	return TRUE;
}