Exemplo n.º 1
0
BOOL KUVSocket::Send(H_CONNECTION handle, LPSTR pData, UINT nDataLen)
{
	BOOL bRet = false;
	g_ClearError();

	// check
	if (handle < 1 || handle > m_nIdGen)
	{
		sprintf_s(g_ErrorMsg, "KUVSocket::Send Fail, the handle is Error : %d\n%s", handle,g_ErrorMsg);
		return bRet;
	}

	if (!pData)
	{
		sprintf_s(g_ErrorMsg, "KUVSocket::Send Fail, the send data is emtpy\n%s", g_ErrorMsg);
		return bRet;
	}

	uv_tcp_t* pTcp = getUVHandle(handle);
	if (!pTcp)
	{
		sprintf_s(g_ErrorMsg, "KUVSocket::Send Fail, can't get uv_tcp_t with handle : %d\n%s", handle, g_ErrorMsg);
		return bRet;
	}
	if (!uv_is_writable((uv_stream_t*)pTcp))
	{
		sprintf_s(g_ErrorMsg, "KUVSocket::Send Fail,this pTcp is unable for writing: %d\n%s", handle, g_ErrorMsg);
		return bRet;
	}

	Header *pHeader = new Header();
	pHeader->SetDataLen(nDataLen);

	SendReq* pSendReq = new SendReq();
	pSendReq->pHeader = pHeader;

	uv_buf_t buff[2];
	buff[0] = uv_buf_init((LPSTR)pHeader->GetHeader(), pHeader->GetHeaderSize());
	buff[1] = uv_buf_init(pData, nDataLen);

	m_nError = uv_write(pSendReq, (uv_stream_t*)pTcp, buff, 2, OnConnectionWriteCompleted);

	if (m_nError != 0)
	{
		delete pHeader;
		delete pSendReq;

		sprintf_s(g_ErrorMsg, "KUVSocket::Send Fail, uv_write get UV Error Code: %d\n%s", m_nError, g_ErrorMsg);
		return bRet;
	}

	bRet = TRUE;
	return bRet;
}