void Send_IRLenData(USART_TypeDef *USARTx, INT8U CMD,u8 *IRlen_buf) { u8 i; PROTOCOL_t IRCtrl; IRCtrl.header = 0x16; IRCtrl.command = CMD; IRCtrl.length = 208; for (i=0;i<IRCtrl.length;i++) //4位ID { IRCtrl.data[i] =*(IRlen_buf+i); } IRCtrl.checksum = IRCtrl.header; IRCtrl.checksum += IRCtrl.command; IRCtrl.checksum += IRCtrl.length; for (i=0;i<IRCtrl.length;i++) //4位ID { IRCtrl.checksum+= IRCtrl.data[i]; } IRCtrl.checksum = IRCtrl.checksum%256; IRCtrl.end = 0x08; SendProtocol(USARTx, &IRCtrl); }
Bool HsPlayer::OnInnerProtocol(SID iSid, Protocol* pProto) { //╗п¤ни■╬ы if (pProto->GetType() == ProtocolId::MSG_NOTIFY && ((MsgNotify*)pProto)->m_iMode == HAWK_ERROR) { SendProtocol(pProto); return true; } return false; }
/******************************************************************************** * FunctionName: Send_LRC * * Description : Sending the LRC to study protocol. * * Parameters : USARTx-Sending Port. irSignal-infrad signal in byte. * * Returns : None. *******************************************************************************/ void Send_IRcmd(USART_TypeDef *USARTx, INT8U CMD) { PROTOCOL_t IRCtrl; IRCtrl.header = 0x16; IRCtrl.command = CMD; IRCtrl.length = 0; IRCtrl.checksum = IRCtrl.header; IRCtrl.checksum += IRCtrl.command; IRCtrl.checksum += IRCtrl.length; IRCtrl.checksum = IRCtrl.checksum%256; IRCtrl.end = 0x08; SendProtocol(USARTx, &IRCtrl); }
/******************************************************************************** * FunctionName: SendAck * * Description : 发送应答信号 * * Parameters : USARTx-发送通道,pProto-欲应答的协议. * * Returns : None. *******************************************************************************/ void SendAck(USART_TypeDef *USARTx, PROTOCOL_t *pProto) { PROTOCOL_t proto; proto.header = 0xAA; proto.direction = pProto->direction >> 4; proto.direction &= 0x0F; proto.direction |= (pProto->direction << 4); proto.msgref = pProto->msgref & 0x3F; proto.command = pProto->command; proto.length = 0; proto.checksum = proto.header; proto.checksum += proto.direction; proto.checksum += proto.msgref; proto.checksum += proto.command; proto.checksum += proto.length; SendProtocol(USARTx, &proto); }
Bool HawkGateProxy::SendProtocol(SID iSid, HawkProtocol* pProto) { SvrMsgHeader sHeader; sHeader.Sid = iSid; return SendProtocol(sHeader, pProto); }
Bool HawkGateThread::OnSessionRead(Session* pSession) { if (!pSession || pSession->Socket == INVALID_SOCKET) return false; //检测输入缓冲区 struct evbuffer* pBuf = bufferevent_get_input((bufferevent*)pSession->Event); if (!pBuf || !evbuffer_get_length(pBuf)) { OnSessionError(pSession); return false; } //循环操作避免事件边缘触发引起的未读取问题 while (evbuffer_get_length(pBuf)) { m_pOctets->Clear(); //读取数据 Int32 iReadSize = evbuffer_remove(pBuf, m_pOctets->Begin(), (Size_t)m_pOctets->Capacity()); if (iReadSize <= 0) { OnSessionError(pSession); return false; } m_pOctets->Resize(iReadSize); //调用性能监视器(只记流量, 解密前才是真实流量数据) if (m_pGateway->GetProfiler()) m_pGateway->GetProfiler()->RegRecvProto(0, iReadSize); //解密 if (pSession->ISecurity) pSession->ISecurity->Update(*m_pOctets); //填充到输入缓冲区 UInt32 iFillPos = 0; UInt32 iBufSize = m_pOctets->Size(); while(iBufSize) { //尽可能填充解码缓冲区 UInt32 iFillSize = HawkMath::Min<UInt32>(pSession->IBuffer->EmptyCap(), iBufSize); if (iFillSize) { void* pData = (Char*)m_pOctets->Begin() + iFillPos; pSession->IBuffer->Insert(pSession->IBuffer->End(), pData, iFillSize); iFillPos += iFillSize; iBufSize -= iFillSize; } //检测是否可以解码输入缓冲区 if (OnSessionDecode(pSession, pSession->IBuffer)) { UInt32 iProtoSize = 0; while (P_ProtocolManager->CheckDecodeProtocol(*pSession->IBuffer, &iProtoSize)) { Char* pProtoData = (Char*)pSession->IBuffer->AvailableData(); ProtoType iProtoType = *((ProtoType*)pProtoData); //检测协议合法性 /* if (!P_ProtocolManager->CheckProtocolLegal(iProtoType)) { HawkFmtError("UnknownProtocol: %d", iProtoType); OnSessionError(pSession); return false; } */ //响应连接的Ping协议 if (iProtoType == SysProtocol::SYS_CLT_PING) { //回复Pong响应 SysProtocol::Sys_SvrPong sProto((UInt32)HawkOSOperator::GetSysTime()); if (!SendProtocol(pSession, &sProto)) return false; } //传递给网关, 再发往后端服务器 else { //给网关发协议消息 SendGateMsg(pSession->Sid, pProtoData, iProtoSize); } //移动数据游标 pSession->IBuffer->MoveNonius(iProtoSize); //调用性能监视器(只记协议数, 避免流量统计不准确) if (m_pGateway->GetProfiler()) m_pGateway->GetProfiler()->RegRecvProto(iProtoType, 0); } } //移除输入缓冲的前段空白 pSession->IBuffer->RemoveBlank(); } } return true; }