Ejemplo n.º 1
0
void CImConn::OnRead()
{
	for (;;)
	{
		uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset();
		if (free_buf_len < READ_BUF_SIZE)
			m_in_buf.Extend(READ_BUF_SIZE);

		int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE);
		if (ret == 0) {
			log("close on netlib_recv=0");
			OnClose();
			return;
		} else if (ret < 0) {
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				break;
			} else {
				log("close on error=%d", errno);
				OnClose();
				return;
			}
		}

		m_recv_bytes += ret;
		m_in_buf.IncWriteOffset(ret);

		m_last_recv_tick = get_tick_count();
	}

    CImPdu* pPdu = NULL;
	try
    {
		while ( ( pPdu = CImPdu::ReadPdu(m_in_buf.GetBuffer(), m_in_buf.GetWriteOffset()) ) )
		{
            uint32_t pdu_len = pPdu->GetLength();
            
			HandlePdu(pPdu);

			m_in_buf.Read(NULL, pdu_len);
			delete pPdu;
            pPdu = NULL;
//			++g_recv_pkt_cnt;
		}
	} catch (CPduException& ex) {
		log("!!!catch exception, sid=%u, cid=%u, err_code=%u, err_msg=%s, close the connection ",
				ex.GetServiceId(), ex.GetCommandId(), ex.GetErrorCode(), ex.GetErrorMsg());
        if (pPdu) {
            delete pPdu;
            pPdu = NULL;
        }
        OnClose();
	}
}
Ejemplo n.º 2
0
void CImConn::OnRead()
{
	for (;;)
	{
		uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset();
		if (free_buf_len < READ_BUF_SIZE)
			m_in_buf.Extend(READ_BUF_SIZE);

		int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE);
		if (ret <= 0)
			break;

		m_recv_bytes += ret;
		m_in_buf.IncWriteOffset(ret);

		m_last_recv_tick = get_tick_count();
	}

	if (m_policy_conn) {
		return;
	}

	// no received data is read by ReadPdu(), check if this is a flash security policy request
	if (m_recv_bytes == m_in_buf.GetWriteOffset()) {
		if ( (m_in_buf.GetBuffer()[0] == '<') && (g_policy_content != NULL) ) {
			log("policy request, handle=%d\n", m_handle);
			m_policy_conn = true;
			Send(g_policy_content, g_policy_len);
			return;
		}
	}

	try {
		CImPdu* pPdu = NULL;
		while ( ( pPdu = CImPdu::ReadPdu(m_in_buf.GetBuffer(), m_in_buf.GetWriteOffset()) ) )
		{
			uint32_t pdu_len = pPdu->GetLength();

			HandlePdu(pPdu);

			m_in_buf.Read(NULL, pdu_len);
			delete pPdu;

			++g_recv_pkt_cnt;
		}
	} catch (CPduException& ex) {
		log("!!!catch exception, sid=%u, cid=%u, err_code=%u, err_msg=%s, close the connection\n",
				ex.GetModuleId(), ex.GetCommandId(), ex.GetErrorCode(), ex.GetErrorMsg());
		OnClose();
	}
}
Ejemplo n.º 3
0
void CImConn::OnRead() {
	while (true) {
		uint32_t free_buf_len = m_in_buff.GetAllocSize()
				- m_in_buff.GetWriteOffset();
		if (free_buf_len < READ_BUF_SIZE) {
			m_in_buff.Extend(READ_BUF_SIZE);
		}

		int ret = netlib_recv(m_handle,
				m_in_buff.GetBuffer() + m_in_buff.GetWriteOffset(),
				READ_BUF_SIZE);
		if (ret <= 0) {
			break;
		}

		m_recv_bytes += ret;
		m_in_buff.IncWriteOffset(ret);

		m_last_recv_tick = get_tick_count();
	}

	if (m_policy_conn) {
		return;
	}

	if (m_recv_bytes == m_in_buff.GetWriteOffset()) {
		if ((m_in_buff.GetBuffer()[0] == '<') && (g_policy_content != NULL)) {
			m_policy_conn = true;
			Send(g_policy_content, g_policy_len);
			return;
		}
	}

	CImPdu *pPdu = NULL;
	while ((pPdu = CImPdu::ReadPdu(m_in_buff.GetBuffer(), m_in_buff.GetWriteOffset()))) {
		uint32_t pdu_len = pPdu->GetLength();
		HandlePdu(pPdu);
		m_in_buff.Read(NULL, pdu_len);
		delete pPdu;

		++g_recv_pkt_cnt;
	}
}