realvod.cpp

来自「本人买的<<VC++项目开发实例>>源代码配套光盘.」· C++ 代码 · 共 1,090 行 · 第 1/3 页

CPP
1,090
字号
    pBuffer->Set((const UCHAR*)pNewURL, strlen(pNewURL) + 1);
    m_pPlayerController->Redirect(pBuffer);
    PN_RELEASE(pBuffer);

    return PNR_OK;
}

void
CExampleAllowance::DecrementLimitedURLCount(void)
{
    INT32 nClientCount = 0;

    // Obtain the count of players currently watching the "limited.rm" URL
    if (PNR_OK == m_pRegistry->GetIntByName("server.LimitedURLCount", 
					    nClientCount))
    {
	// Decrement the count and update its registry value
	if (nClientCount > 0)
	{
	    nClientCount--;
	}
	m_pRegistry->SetIntByName("server.LimitedURLCount", nClientCount);
    }
}

PN_RESULT CExampleAllowance::HandleProtectedURLRequest()
{
	PN_RESULT hResult = PNR_FAILED;

	//初始化通讯变量	
	PN_RELEASE(m_pUDPDataBuffer);
	do
	{		
		//创建数据Buffer
		hResult = m_pClassFactory->CreateInstance(IID_IRMABuffer, (void **)&m_pUDPDataBuffer);
		CHECK_VALUE_AND_BREAK(m_pUDPDataBuffer);

		AUTH_STRUCT authInfo;
		memset(&authInfo, 0, sizeof(authInfo));
		strncpy(authInfo.szUserName, m_szUserName, sizeof(authInfo.szUserName));
		strncpy(authInfo.szPassword, m_szPassword, sizeof(authInfo.szPassword));
		hResult = m_pUDPDataBuffer->Set((const unsigned char *)&authInfo, sizeof(authInfo) / sizeof(char));
		CHECK_VALUE_AND_BREAK(hResult);

		hResult = m_pUDPSocket->WriteTo(/*IPv4(127, 0, 0, 1)*/IPv4(172, 18, 1, 112)/*0xAC120170*/, 9999,m_pUDPDataBuffer);
		//释放数据接口
		PN_RELEASE(m_pUDPDataBuffer);
				
		if (hResult)
		{
			fprintf(stdout, "Write FAIL\n");
			break;
		}
		else
		{
			fprintf(stdout, "Write OK!!\n");			
		}

		AUTH_RESP_STRUCT authResp;
		memset(&authResp, 0, sizeof(authResp));
		hResult = m_pUDPSocket->Read(sizeof(authResp));
		if (hResult)
		{
			fprintf(stdout, "Read fail.\n");
			break;
		}
		else
		{			
			fprintf(stdout, "Read run into pending...\n");
			//设置成正在认证进行中状态
			m_stsStatus = statusAuthenticating;
			hResult = PNR_READ_PENDING;			
		}
	}while(false);
			
	return hResult;
}
PN_RESULT CExampleAllowance::ReadDone(THIS_
			PN_RESULT		status,
			IRMABuffer*		pBuffer,
			ULONG32			ulAddr,
			UINT16			nPort)
{
	PN_RESULT hResult = status;

	fprintf(stdout, "CExampleAllowance::ReadDone\n");
	if (statusAuthenticating == m_stsStatus)
	{
		union
		{
			AUTH_RESP_STRUCT *m_pAuthResp;
			UCHAR *pDataBuf;
		}Buf;

		ULONG32 ulLength = 0;   
		do
		{
			if (hResult)
			{
				break;
			}
			pBuffer->Get(Buf.pDataBuf, ulLength);
			if (Buf.m_pAuthResp)
			{
				if (0 == Buf.m_pAuthResp->uResp)
				{
					hResult = PNR_OK;               
				}
			}
		}while (false);
		//响应OnURL
		m_stsStatus = statusNormal;	//状态恢复成正常
		m_pPCResponse->OnURLDone(hResult);

		//移除CallBack
		if (m_hCallback)
		{
			fprintf(stdout, "移除授权Scheduler\n");
			m_pScheduler->Remove(m_hCallback);
			m_hCallback = NULL;
		}
	}
	else if (statusHeartbeating == m_stsStatus)
	{
		m_bWaitingBillingResp = false;
		union
		{
			BILLING_RESP_STRUCT *m_pBillingResp;
			UCHAR *pDataBuf;
		}Buf;

		ULONG32 ulLength = 0;   
		do
		{
			if (hResult)
			{
				break;
			}
			pBuffer->Get(Buf.pDataBuf, ulLength);
			if (Buf.m_pBillingResp)
			{
				if (0 == Buf.m_pBillingResp->uResp)
				{
					hResult = PNR_OK;               
				}				
			}
			//如果收费不成功
			if (!hResult)
			{
					// 计费失败
				IRMABuffer *pTempBuffer = NULL;
				m_pClassFactory->CreateInstance(CLSID_IRMABuffer, (void**)&pTempBuffer);
				pTempBuffer->Set((const UCHAR*)"计费失败!", 
					strlen("计费失败!") + 1);
				m_pPlayerController->AlertAndDisconnect(pTempBuffer);
				PN_RELEASE(pTempBuffer);				
			}
		}while (false);
		//响应OnURL
		m_stsStatus = statusNormal;	//状态恢复成正常

		//如果计费失败,移除CallBack
		if (hResult && m_hCallback)
		{
			fprintf(stdout, "移除计费Scheduler\n");
			m_pScheduler->Remove(m_hCallback);
			m_hCallback = NULL;
		}
		
	}
	return status;
}
STDMETHODIMP 
CExampleAllowance::Func()
{
	// We just got our callback, so reinitialize our handle to null
    m_hCallback = NULL;

	switch(m_stsStatus)
	{
	case statusNormal:
		fprintf(stdout, "Normal status...\n");
		break;
	case statusAuthenticating:
		fprintf(stdout, "Authenticating...\n");
		OnAuthenticationTimeout();
		break;
	case statusHeartbeating:
		{
			PN_RESULT hResult = PNR_OK;
			fprintf(stdout, "HeartBeating...\n");
			hResult = OnBilling();
			if (hResult != PNR_READ_BILLING_PENDING)
			{
				IRMABuffer *pAlert = NULL;
				m_pClassFactory->CreateInstance(IID_IRMABuffer, (void **)&pAlert);
				char *szMsg = "计费失败, 停止播放!";
				pAlert->Set((const unsigned char *)szMsg, strlen(szMsg) + 1);
				m_pPlayerController->AlertAndDisconnect(pAlert);
				PN_RELEASE(pAlert);
			}
			ScheduleCallback();
		}		
		break;
	default:
		fprintf(stdout, "Unknown status...\n");
		break;
	}
	    
    return PNR_OK;
}


void CExampleAllowance::ScheduleCallback()
{
    if (m_hCallback)
    {
		// we already have a callback scheduled
		return;
    }
	
    // 设置计划
	m_hCallback = m_pScheduler->RelativeEnter((IRMACallback*)this, 
		m_uCallbackInterval);
	m_stsStatus = statusHeartbeating;
}

void CExampleAllowance::OnAuthenticationTimeout()
{
	if (m_hCallback)
    {
		// we already have a callback scheduled
		return;
    }
	fprintf(stdout, "Entering OnAuthenticationTimeout");
	m_pError->Report(PNLOG_ERR, PNR_PPV_AUTHORIZATION_FAILED
					 , 0, "PNR_PPV_AUTHORIZATION_FAILED", NULL);
	m_stsStatus = statusNormal;
	m_pPCResponse->OnURLDone(PNR_PPV_AUTHORIZATION_FAILED);
}

PN_RESULT CExampleAllowance::OnBilling()
{	
	if (m_stsStatus != statusHeartbeating)
	{
		fprintf(stdout, "状态发生混乱.\n");
		return PNR_UNEXPECTED;
	}

	fprintf(stdout, "Entering OnBilling()\n");
	if (m_hCallback)
    {
		// we already have a callback scheduled
		return PNR_OK;
    }
	PN_RESULT hResult = PNR_FAILED;
	do
	{
		if (m_bWaitingBillingResp)
		{
			fprintf(stdout, "等待计费响应超时错误\n");
			break;
		}
		
		PN_RELEASE(m_pUDPDataBuffer);
		
		BILLING_STRUCT billingInfo;
		strncpy(billingInfo.szUserName, m_szUserName, sizeof(billingInfo.szUserName));
		billingInfo.ulTimeInterval = m_uCallbackInterval;

	
        //创建数据Buffer
		hResult = m_pClassFactory->CreateInstance(IID_IRMABuffer, (void **)&m_pUDPDataBuffer);
		CHECK_VALUE_AND_BREAK(m_pUDPDataBuffer);

		m_pUDPDataBuffer->Set((const UCHAR *)&billingInfo, sizeof(billingInfo));
		hResult = m_pUDPSocket->WriteTo(IPv4(127, 0, 0, 1), 9090, m_pUDPDataBuffer);

		//释放数据接口
		PN_RELEASE(m_pUDPDataBuffer);
        if (hResult)
		{
			fprintf(stdout, "Send billing data FAIL\n");
			break;
		}
		else
		{
			fprintf(stdout, "Send billing data  OK!!\n");			
		}

		BILLING_RESP_STRUCT billingResp;
		memset(&billingResp, 0, sizeof(billingResp));
		hResult = m_pUDPSocket->Read(sizeof(billingResp));
		if (hResult)
		{
			fprintf(stdout, "Read billingResp fail.\n");
			break;
		}
		else
		{			
			fprintf(stdout, "Read billingResp run into pending...\n");
			//设置成正在认证进行中状态
			m_stsStatus = statusHeartbeating;
			hResult = PNR_READ_BILLING_PENDING;
			//设置正在等待计费结果的状态
			m_bWaitingBillingResp = true;
		}

	}while(false);
	//进行等待状态
	if (hResult != PNR_READ_BILLING_PENDING)
	{
		m_stsStatus = statusNormal;
	}
	return hResult;
}
/**
 * 按照分隔符来查找指定字符串为前缀的字符串。
 * 返回的结果为除去前缀的内容。如果没有找到
 * 那么返回为内容为空的字符串。
 */
char * CExampleAllowance::GetStringByDelimitor(char *strDist, const char *strSource, const char *strSearchFor, const char chDelimiter)
{
	*strDist = '\0'; // empty string
	char *strStart = strstr(strSource, strSearchFor);
	do
	{
		if (NULL == strStart)
		{
			break;
			//not found
		}
		strStart  += strlen(strSearchFor); //move the pointer to the content by bypassing the start token.
		char *strEnd = strchr(strStart, chDelimiter); //bypass the current delimiter, and find the next delimiter
		if ( NULL == strEnd)  //copy till to end
		{
			strcpy(strDist, strStart);
		}
		else
		{
			//strEnd - strStart is exact the content's length!!!
			strncpy(strDist, strStart, strEnd - strStart);
		}
	}while(false);

	return strDist;
}

void CExampleAllowance::GetUserInfoFromCookie(const char *szCookies)
{
	char szBuf[1024] = {0};
	if (NULL != szCookies)
	{
		GetStringByDelimitor(szBuf, szCookies, "UserName=", ';');
		strncpy(m_szUserName, szBuf, sizeof(m_szUserName));
		
		memset(szBuf, 0, sizeof(szBuf));
		GetStringByDelimitor(szBuf, szCookies, "Password=", ';');
		strncpy(m_szPassword, szBuf, sizeof(m_szPassword));
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?