Пример #1
0
int CTcpAgent::SendItem(TSocketObj* pSocketObj)
{
	int result = NO_ERROR;

	while(pSocketObj->sndBuff.Size() > 0)
	{
		::InterlockedIncrement(&pSocketObj->sndCount);

		TItemPtr itPtr(m_itPool, pSocketObj->sndBuff.PopFront());

		int iBufferSize = itPtr->Size();
		ASSERT(iBufferSize > 0 && iBufferSize <= (int)m_dwSocketBufferSize);

		pSocketObj->pending   -= iBufferSize;
		TBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);
		memcpy(pBufferObj->buff.buf, itPtr->Ptr(), iBufferSize);

		result = ::PostSendNotCheck(pSocketObj, pBufferObj);

		if(result != NO_ERROR)
		{
			if(result != WSA_IO_PENDING)
				AddFreeBufferObj(pBufferObj);;

			break;
		}
	}

	return result;
}
Пример #2
0
BOOL CTcpServer::DoAccept()
{
	BOOL isOK = FALSE;

	if(HasStarted())
	{
		SOCKET		soClient	= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		TBufferObj*	pBufferObj	= GetFreeBufferObj();

		ASSERT(soClient != INVALID_SOCKET);

		isOK = (::PostAccept(m_pfnAcceptEx, m_soListen, soClient, pBufferObj) == NO_ERROR);

		if(!isOK)
		{
			VERIFY(!HasStarted());

			::ManualCloseSocket(soClient);
			AddFreeBufferObj(pBufferObj);
		}
	}

	if(!isOK)
	{
		::InterlockedDecrement(&m_iRemainAcceptSockets);
		ASSERT(m_iRemainAcceptSockets >= 0);
	}

	return isOK;
}
Пример #3
0
int CTcpAgent::SendDirect(TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength)
{
	int result	= NO_ERROR;
	int iRemain	= iLength;

	while(iRemain > 0)
	{
		int iBufferSize = min(iRemain, (int)m_dwSocketBufferSize);
		TBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);
		memcpy(pBufferObj->buff.buf, pBuffer, iBufferSize);

		result = ::PostSend(pSocketObj, pBufferObj);

		if(result != NO_ERROR)
		{
			AddFreeBufferObj(pBufferObj);
			break;
		}

		iRemain -= iBufferSize;
		pBuffer += iBufferSize;
	}

	if(result != NO_ERROR)
		CheckError(pSocketObj->connID, SO_SEND, result);

	return result;
}
Пример #4
0
int CUdpServer::SendItem(TUdpSocketObj* pSocketObj)
{
	int result = NO_ERROR;

	while(pSocketObj->sndBuff.Size() > 0)
	{
		::InterlockedIncrement(&pSocketObj->sndCount);

		TItemPtr itPtr(m_itPool, pSocketObj->sndBuff.PopFront());

		int iBufferSize = itPtr->Size();
		ASSERT(iBufferSize > 0 && iBufferSize <= (int)m_dwMaxDatagramSize);

		pSocketObj->pending		 -= iBufferSize;
		TUdpBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);

		memcpy(&pBufferObj->remoteAddr, &pSocketObj->remoteAddr, sizeof(SOCKADDR_IN));
		memcpy(pBufferObj->buff.buf, itPtr->Ptr(), iBufferSize);

		int result = ::PostSendToNotCheck(m_soListen, pBufferObj);

		if(result != NO_ERROR)
		{
			if(result != WSA_IO_PENDING)
				AddFreeBufferObj(pBufferObj);;

			break;
		}
	}

	return result;
}
Пример #5
0
int CTcpServer::DoSend(CONNID dwConnID, TSocketObj* pSocketObj, const BYTE* pBuffer, int iLen)
{
	int result	= NO_ERROR;
	int iRemain	= iLen;

	CCriSecLock locallock(pSocketObj->crisec);

	while(iRemain > 0)
	{
		int iBufferSize = min(iRemain, (int)m_dwSocketBufferSize);
		TBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);
		memcpy(pBufferObj->buff.buf, pBuffer, iBufferSize);

		result = ::PostSend(pSocketObj, pBufferObj);

		if(result != NO_ERROR)
		{
			AddFreeBufferObj(pBufferObj);
			break;
		}

		iRemain -= iBufferSize;
		pBuffer += iBufferSize;
	}

	return result;
}
Пример #6
0
int CTcpServer::SendDirect(TSocketObj* pSocketObj, const BYTE* pBuffer, int iLength)
{
	int result	= NO_ERROR;
	int iRemain	= iLength;

	while(iRemain > 0)
	{
		int iBufferSize = min(iRemain, (int)m_dwSocketBufferSize);
		TBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);
		memcpy(pBufferObj->buff.buf, pBuffer, iBufferSize);

		::InterlockedExchangeAdd(&pSocketObj->pending, iBufferSize);

		result			= ::PostSend(pSocketObj, pBufferObj);
		LONG sndCounter	= pBufferObj->ReleaseSendCounter();

		if(sndCounter == 0 || result != NO_ERROR)
		{
			AddFreeBufferObj(pBufferObj);
			
			if(result != NO_ERROR)
			{
				::InterlockedExchangeAdd(&pSocketObj->pending, -iBufferSize);

				break;
			}
		}

		iRemain -= iBufferSize;
		pBuffer += iBufferSize;
	}

	return result;
}
Пример #7
0
DWORD CTcpAgent::ConnectToServer(CONNID dwConnID, SOCKET& soClient, LPCTSTR pszRemoteAddress, USHORT usPort)
{
	TCHAR szAddress[40];
	int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);

	if(!::GetIPAddress(pszRemoteAddress, szAddress, iAddressLen))
		return ERROR_INVALID_ADDRESS;

	SOCKADDR_IN addr;
	if(!::sockaddr_A_2_IN(AF_INET, szAddress, usPort, addr))
		return ERROR_INVALID_ADDRESS;

	TBufferObj* pBufferObj = GetFreeBufferObj();
	TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, soClient);

	memcpy(&pSocketObj->remoteAddr, &addr, sizeof(SOCKADDR_IN));
	AddClientSocketObj(dwConnID, pSocketObj);

	DWORD result = NO_ERROR;

	if(m_bAsyncConnect)
	{
		if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0))
			result = ::PostConnect(m_pfnConnectEx, soClient, addr, pBufferObj);
		else
			result = ::GetLastError();
	}
	else
	{
		if(::connect(soClient, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
		{
			if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0))
			{
				BOOL bOnOff	= (m_dwKeepAliveTime > 0 && m_dwKeepAliveInterval > 0);
				VERIFY(::SSO_KeepAliveVals(pSocketObj->socket, bOnOff, m_dwKeepAliveTime, m_dwKeepAliveInterval) == NO_ERROR);

				if(FireConnect(dwConnID) != HR_ERROR)
					result = DoReceive(dwConnID, pSocketObj, pBufferObj);
				else
					result = ERROR_FUNCTION_FAILED;
			}
			else
				result = ::GetLastError();
		}
		else
			result = ::WSAGetLastError();
	}

	if(result != NO_ERROR)
	{
		AddFreeSocketObj(dwConnID, SCF_NONE);
		AddFreeBufferObj(pBufferObj);

		soClient = INVALID_SOCKET;
	}

	return result;
}
Пример #8
0
int CUdpServer::SendDirect(TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLength)
{
	TUdpBufferObj* pBufferObj = GetFreeBufferObj(iLength);

	memcpy(&pBufferObj->remoteAddr, &pSocketObj->remoteAddr, sizeof(SOCKADDR_IN));
	memcpy(pBufferObj->buff.buf, pBuffer, iLength);

	int result = ::PostSendTo(m_soListen, pBufferObj);

	if(result != NO_ERROR)
	{
		VERIFY(!HasStarted());
		AddFreeBufferObj(pBufferObj);
	}

	return result;
}
Пример #9
0
int CTcpServer::DoUnpause(CONNID dwConnID)
{
	TSocketObj* pSocketObj = FindSocketObj(dwConnID);

	if(!TSocketObj::IsValid(pSocketObj))
		return ERROR_OBJECT_NOT_FOUND;

	EnHandleResult hr = BeforeUnpause(pSocketObj);

	if(hr != HR_OK && hr != HR_IGNORE)
	{
		if(hr == HR_ERROR)
			AddFreeSocketObj(pSocketObj, SCF_ERROR, SO_RECEIVE, ENSURE_ERROR_CANCELLED);

		return ERROR_CANCELLED;
	}

	return DoReceive(pSocketObj, GetFreeBufferObj());
}
Пример #10
0
BOOL CUdpServer::DoSend(TUdpSocketObj* pSocketObj)
{
	BOOL isOK	= TRUE;
	int result	= NO_ERROR;

	if(TUdpSocketObj::IsPending(pSocketObj))
	{
		CCriSecLock locallock(pSocketObj->crisec);

		TItemList& sndBuff = pSocketObj->sndBuff;

		while(sndBuff.Size() > 0)
		{
			TItemPtr itPtr(m_itPool, sndBuff.PopFront());

			int iBufferSize = itPtr->Size();
			ASSERT(iBufferSize > 0 && iBufferSize <= (int)m_dwMaxDatagramSize);

			TUdpBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);

			memcpy(&pBufferObj->remoteAddr, &pSocketObj->remoteAddr, sizeof(SOCKADDR_IN));
			memcpy(pBufferObj->buff.buf, itPtr->Ptr(), iBufferSize);

			::InterlockedIncrement(&pSocketObj->sndCount);
			pSocketObj->pending -= iBufferSize;

			result = ::PostSendToNotCheck(m_soListen, pBufferObj);

			if(result != NO_ERROR)
			{
				if(result != WSA_IO_PENDING)
				{
					AddFreeBufferObj(pBufferObj);
					isOK = FALSE;
				}

				break;
			}
		}
	}
	
	return isOK;
}
Пример #11
0
BOOL CUdpServer::DoSend(TUdpSocketObj* pSocketObj, const BYTE* pBuffer, int iLen)
{
	BOOL isOK = FALSE;

	if(HasStarted())
	{
		TUdpBufferObj* pBufferObj	= GetFreeBufferObj(iLen);
		pBufferObj->addrLen			= sizeof(SOCKADDR_IN);

		memcpy(&pBufferObj->clientAddr, &pSocketObj->clientAddr, pBufferObj->addrLen);
		memcpy(pBufferObj->buff.buf, pBuffer, iLen);

		isOK = (::PostSendTo(m_soListen, pBufferObj) == NO_ERROR);

		if(!isOK)
		{
			VERIFY(!HasStarted());
			AddFreeBufferObj(pBufferObj);
		}
	}

	return isOK;
}
Пример #12
0
BOOL CIocpServer::Send(DWORD dwConnID, const BYTE* pBuffer, int iLen)
{
	ASSERT(iLen > 0);

	TSocketObj* pSocketObj	= NULL;

	{
		CCriSecLock locallock1(m_csClientSocket);

		TSocketObjPtrMapCI it = m_mpClientSocket.find(dwConnID);
		if(it != m_mpClientSocket.end())
			pSocketObj = it->second;
	}

	if(pSocketObj == NULL)
		return FALSE;

	CCriSecLock2 locallock2(pSocketObj->crisec);

	int iRemain	= iLen;

	while(iRemain > 0)
	{
		int iBufferSize = min(iRemain, (int)m_dwIocpBufferSize);
		TBufferObj* pBufferObj = GetFreeBufferObj(iBufferSize);
		memcpy(pBufferObj->buff.buf, pBuffer, iBufferSize);

		if(DoSend(pSocketObj, pBufferObj) != NO_ERROR)
			return FALSE;

		iRemain -= iBufferSize;
		pBuffer += iBufferSize;
	}

	return TRUE;
}
Пример #13
0
DWORD CTcpAgent::ConnectToServer(CONNID dwConnID, SOCKET& soClient, LPCTSTR lpszRemoteAddress, USHORT usPort)
{
	TCHAR szAddress[40];
	int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);

	if(!::GetIPAddress(lpszRemoteAddress, szAddress, iAddressLen))
		return WSAEADDRNOTAVAIL;

	SOCKADDR_IN addr;
	if(!::sockaddr_A_2_IN(AF_INET, szAddress, usPort, addr))
		return WSAEADDRNOTAVAIL;

	TBufferObj* pBufferObj = GetFreeBufferObj();
	TSocketObj* pSocketObj = GetFreeSocketObj(dwConnID, soClient);

	memcpy(&pSocketObj->remoteAddr, &addr, sizeof(SOCKADDR_IN));
	AddClientSocketObj(dwConnID, pSocketObj);

	DWORD result	= NO_ERROR;
	BOOL bNeedFree	= TRUE;

	if(m_bAsyncConnect)
	{
		if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0))
		{
			result		= DoConnect(dwConnID, pSocketObj, pBufferObj);
			bNeedFree	= FALSE;
		}
		else
			result = ::GetLastError();
	}
	else
	{
		if(::connect(soClient, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
		{
			if(::CreateIoCompletionPort((HANDLE)soClient, m_hCompletePort, (ULONG_PTR)pSocketObj, 0))
			{
				if(TriggerFireConnect(pSocketObj) != HR_ERROR)
				{
					result		= DoReceive(dwConnID, pSocketObj, pBufferObj);
					bNeedFree	= FALSE;
				}
				else
					result = ERROR_CANCELLED;
			}
			else
				result = ::GetLastError();
		}
		else
			result = ::WSAGetLastError();
	}

	if(result != NO_ERROR)
	{
		if(bNeedFree)
		{
			AddFreeSocketObj(pSocketObj, SCF_NONE);
			AddFreeBufferObj(pBufferObj);
		}

		soClient = INVALID_SOCKET;
	}

	return result;
}
Пример #14
0
BOOL CUdpServer::DoAccept()
{
	TUdpBufferObj* pBufferObj = GetFreeBufferObj();
	return DoReceive(pBufferObj);
}
Пример #15
0
BOOL CUdpServer::DoAccept()
{
	TUdpBufferObj* pBufferObj = GetFreeBufferObj();
	return (DoReceive(pBufferObj) == NO_ERROR);
}