Exemplo n.º 1
0
int CSocketBase::RecvV(iovec aIov[], DWORD aCount) const 
{
	int nRet;
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	CM_ASSERTE(aIov);
	
#ifdef WIN32
	DWORD dwBytesReceived = 0;
	DWORD dwFlags = 0;
	nRet = ::WSARecv((CM_SOCKET)m_Handle,
                      (WSABUF *)aIov,
                      aCount,
                      &dwBytesReceived,
                      &dwFlags,
                      0,
                      0);
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
	else {
		nRet = (int)dwBytesReceived;
	}
#else // !WIN32
	nRet = ::readv(m_Handle, aIov, aCount);
#endif // WIN32
	return nRet;
}
Exemplo n.º 2
0
int CSocketBase::SendV(const iovec aIov[], DWORD aCount) const 
{
	int nRet;
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	CM_ASSERTE(aIov);
	
#ifdef WIN32
	DWORD dwBytesSend = 0;
       //TODO:Field WSASend?
	nRet = ::WSARecv((CM_SOCKET)m_Handle,
                      (WSABUF *)aIov,
                      aCount,
                      &dwBytesSend,
                      0,
                      0,
                      0);
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
	else {
		nRet = (int)dwBytesSend;
	}
#else // !WIN32
	nRet = ::writev(m_Handle, aIov, aCount);
#endif // WIN32
	return nRet;
}
Exemplo n.º 3
0
int CTimerQueueOrderedList::PushNode_l(const CNode &aPushNode)
{
	BOOL bFoundEqual = FALSE;
	BOOL bInserted = FALSE;
	NodesType::iterator iter = m_Nodes.begin();
	while (iter != m_Nodes.end()) {
		if ((*iter).m_pEh == aPushNode.m_pEh) {
			CM_ASSERTE(!bFoundEqual);
			iter = m_Nodes.erase(iter);
			bFoundEqual = TRUE;
			if (bInserted || iter == m_Nodes.end())
				break;
		}

		if (!bInserted && (*iter).m_tvExpired >= aPushNode.m_tvExpired) {
			iter = m_Nodes.insert(iter, aPushNode);
			++iter;
			bInserted = TRUE;
			if (bFoundEqual)
				break;
		}
		++iter;
	}

	if (iter != m_Nodes.end())
		CM_ASSERTE(bInserted && bFoundEqual);
	if (!bInserted)
		m_Nodes.push_back(aPushNode);

	EnsureSorted();
	return bFoundEqual ? 1 : 0;
}
Exemplo n.º 4
0
int CTransportTcp::Recv_t(LPSTR aBuf, DWORD aLen)
{
	// the recv len must be as large as possible
	// due to avoid lossing real-time signal
	CM_ASSERTE(aLen > 0);
	int nRecv = m_SockTcp.Recv(aBuf, aLen);

#ifdef CM_DEBUG_SEND_RECV
	m_RecordDebug.push_back(RecEleType('R', nRecv < 0 ? -errno : nRecv));
#endif // CM_DEBUG_SEND_RECV

	if (nRecv < 0) {
		if (errno == EWOULDBLOCK)
			return 0;
		else {
			CErrnoGuard egTmp;
			VP_TRACE_ERROR("CTransportTcp::Recv_t, recv() failed! err=%d", errno);
			return -1;
		}
	}
	if (nRecv == 0) {
		// it is a graceful disconnect
		return -1;
	}
	return nRecv;
}
Exemplo n.º 5
0
TEST_F(CmMessageBlockTest, ChainedReserveTest)
{
    char szData[]       = "0123456789";
    const DWORD dwLen   = static_cast<DWORD>(strlen(szData));
    CCmMessageBlock mb1(dwLen, szData, 0, dwLen);
    CCmMessageBlock mb2(dwLen, szData, 0, dwLen);
    mb1.Append(mb2.DuplicateChained());
    
    CmResult rv = mb1.ReserveCapacity(dwLen + 4);
    ASSERT_TRUE(CM_SUCCEEDED(rv));
    rv = mb1.Write("abcd", 4);
    ASSERT_TRUE(CM_SUCCEEDED(rv));
    
    CCmMessageBlock* mbNext = mb1.GetNext();
    rv = mbNext->ReserveCapacity(dwLen + 6);
    ASSERT_TRUE(CM_SUCCEEDED(rv));
    rv = mbNext->Write("abcdef", 6);
    ASSERT_TRUE(CM_SUCCEEDED(rv));
    
    char szBuffer[31] = {0};
    rv = mb1.Read(szBuffer, 30);
    CM_ASSERTE(CM_SUCCEEDED(rv));
    ASSERT_EQ(0, memcmp(szBuffer, "0123456789abcd0123456789abcdef", 30));
    
}
Exemplo n.º 6
0
int CIPCBase::Disable(int aValue) const 
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	switch(aValue) {
	case NON_BLOCK:
		{
#ifdef WIN32
		u_long nonblock = 0;
		int nRet = ::ioctlsocket((CM_SOCKET)m_Handle, FIONBIO, &nonblock);
		if (nRet == SOCKET_ERROR) {
			errno = ::WSAGetLastError();
			nRet = -1;
		}
		return nRet;

#else // !WIN32
		int nVal = ::fcntl(m_Handle, F_GETFL, 0);
		if (nVal == -1)
			return -1;
		nVal &= ~O_NONBLOCK;
		if (::fcntl(m_Handle, F_SETFL, nVal) == -1)
			return -1;
		return 0;
#endif // WIN32
		}

	default:
		CM_ERROR_LOG(("CIPCBase::Disable, aValue=%d.", aValue));
		return -1;
	}
}
Exemplo n.º 7
0
int CSocketUdp::
RecvFrom(char *aBuf, DWORD aLen, CInetAddr &aAddr, int aFlag) const 
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);

	int nSize = (int)aAddr.GetSize();
	int nRet = ::recvfrom((CM_SOCKET)m_Handle,
						  aBuf,
						  aLen,
						  aFlag,
						  reinterpret_cast<sockaddr *>(const_cast<sockaddr_in *>(aAddr.GetPtr())),
#ifdef WIN32
						  &nSize
#else // !WIN32
						  reinterpret_cast<socklen_t*>(&nSize)
#endif // WIN32
						   );

#ifdef WIN32
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
#endif // WIN32

	return nRet;
}
Exemplo n.º 8
0
int CSocketUdp::
SendTo(const char *aBuf, DWORD aLen, const CInetAddr &aAddr, int aFlag) const 
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);

	int nRet = ::sendto((CM_SOCKET)m_Handle,
						  aBuf,
						  aLen,
						  aFlag,
						  reinterpret_cast<const sockaddr *>(aAddr.GetPtr()),
#ifdef WIN32
						  aAddr.GetSize()
#else // !WIN32
						  static_cast<socklen_t>(aAddr.GetSize())
#endif // WIN32
						  );

#ifdef WIN32
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
#endif // WIN32
	
	return nRet;
}
Exemplo n.º 9
0
int CTransportTcp::OnOutput(CM_HANDLE )
{
	if (m_pSink == NULL)
	{
		VP_TRACE_ERROR("CTransportTcp::OnOutput m_pSink==NULL");
		return 0;
	}
		
	CM_ASSERTE(m_pSink);
//	CM_INFO_LOG(("CTransportTcp::OnOutput"));
	if (m_mbBufferOne.GetLength() == 0)
	{
		m_pReactor->ModifyHandleSignal(this,false);
		return 0;
	}
		
	int nRet = Send_t(m_mbBufferOne.GetReadPtr(), m_mbBufferOne.GetLength());
	if (nRet <= 0)
	{
		VP_TRACE_ERROR("CTransportTcp::OnOutput Send_t nRet=%d GetLength=%d",nRet,m_mbBufferOne.GetLength());
		return nRet;
	}

	if ((DWORD)nRet < m_mbBufferOne.GetLength()) 
	{
		m_mbBufferOne.AdvanceReadPtr((DWORD)nRet);
	}
	else 
	{
		m_pReactor->ModifyHandleSignal(this,false);
		m_mbBufferOne.Resize(0);
		m_pSink->OnSend();
	}
	return 0;
}
Exemplo n.º 10
0
int CSocketBase::Send (const char *aBuf, DWORD aLen, int aFlag) const 
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	CM_ASSERTE(aBuf);

	int nRet = ::send((CM_SOCKET)m_Handle, aBuf, aLen, aFlag);
#ifndef WIN32
	if (nRet == -1 && errno == EAGAIN)
		errno = EWOULDBLOCK;
#else // !WIN32
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
#endif // WIN32
	return nRet;
}
Exemplo n.º 11
0
int CSocketTcp::CloseReader()
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	int nRet = ::shutdown((CM_SOCKET)m_Handle, CM_SD_RECEIVE);

#ifdef WIN32
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
#endif // WIN32
	return nRet;
}
Exemplo n.º 12
0
int CSocketBase::SetOption(int aLevel, int aOption, const void *aOptval, int aOptlen) const 
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);
	int nRet = ::setsockopt((CM_SOCKET)m_Handle, aLevel, aOption, 
#ifdef WIN32
		static_cast<const char*>(aOptval), 
#else // !WIN32
		aOptval,
#endif // WIN32
		aOptlen);

#ifdef WIN32
	if (nRet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nRet = -1;
	}
#endif // WIN32
	return nRet;
}
Exemplo n.º 13
0
int CTransportTcp::Send_t(LPCSTR aBuf, DWORD aLen)
{
	CM_ASSERTE(aLen > 0);
	int nRet = m_SockTcp.Send(aBuf, aLen);
	
#ifdef CM_DEBUG_SEND_RECV
	m_RecordDebug.push_back(RecEleType('S', nRet < 0 ? -errno : nRet));
#endif // CM_DEBUG_SEND_RECV

	if (nRet < 0) {
		if (errno == EWOULDBLOCK)
			return 0;
		else {
			CErrnoGuard egTmp;
			VP_TRACE_ERROR("CTransportTcp::Send_t, send() failed! err=%d", errno);
			return -1;
		}
	}
	return nRet;
}
Exemplo n.º 14
0
int CTransportTcp::OnInput(CM_HANDLE )
{
	if (!m_bCanRecv)
	{
		return 0;
	}
	
	if (m_pSink == NULL)
		return 0;
	CM_ASSERTE(m_pSink);

	int nRecv = Recv_t(s_bwRecvMax.GetRawPtr(), s_bwRecvMax.GetSize());
	if (nRecv <= 0)
		return nRecv;

	CDataBlock *pdbIn = new CDataBlock((DWORD)nRecv, 0);
	::memcpy(pdbIn->GetBuf(), s_bwRecvMax.GetRawPtr(), nRecv);
	pdbIn->Expand(nRecv);

	m_pSink->OnReceive(*pdbIn);
	pdbIn->Release();
	return 0;
}
Exemplo n.º 15
0
int CSocketBase::GetLocalAddr(CInetAddr &aAddr) const
{
	CM_ASSERTE(m_Handle != CM_INVALID_HANDLE);

	int nSize = (int)aAddr.GetSize();
	int nGet = ::getsockname((CM_SOCKET)m_Handle,
					reinterpret_cast<sockaddr *>(const_cast<sockaddr_in *>(aAddr.GetPtr())),
#ifdef WIN32
					&nSize
#else // !WIN32
					reinterpret_cast<socklen_t*>(&nSize)
#endif // WIN32
					);

#ifdef WIN32
	if (nGet == SOCKET_ERROR) {
		errno = ::WSAGetLastError();
		nGet = -1;
	}
#endif // WIN32

	return nGet;
}
Exemplo n.º 16
0
void CIPCBase::SetHandle(CM_HANDLE aNew)
{
	CM_ASSERTE(m_Handle == CM_INVALID_HANDLE || aNew == CM_INVALID_HANDLE);
	m_Handle = aNew;
}
Exemplo n.º 17
0
DWORD CMessageBlock::GetSpace() const 
{
	CM_ASSERTE(m_pEndPrt >= m_pWritePtr);
	return m_pEndPrt - m_pWritePtr;
}
Exemplo n.º 18
0
DWORD CMessageBlock::GetLength() const
{
	CM_ASSERTE(m_pWritePtr >= m_pReadPtr);
	return m_pWritePtr - m_pReadPtr;
}