Exemplo n.º 1
0
	//! 重载接收事件
	void OnReceive()
	{
		for (BlockBuffer::Iterator iter = I_.Begin(); iter; iter += 1)
		{
			if (iPacketBegin_ != -1)
			{
				if (*iter == '\x3')
				{
					OnPacket(iPacketBegin_ + 1, iter.O_);
					iPacketBegin_ = -1;
				}
				else
				{
					if (iter.O_ > iPacketBegin_+254)
						OnOverflow(iter);
				}
			}
			else
			{
				if (*iter == '\x2')
					iPacketBegin_ = iter.O_;
			}
		}

		if (iPacketBegin_ == -1)
			I_.Clear();
	}
Exemplo n.º 2
0
Arquivo: uart.cpp Projeto: HclX/MSP430
static void OnUartData()
{
    // UART receiving buffer: PACKET structure
    static byte s_uartData[sizeof(PACKET)];

    // -1 indicates magic byte not received yet
    static int s_cbUartData = -1;

    byte b = UART_RECV();
    if (s_cbUartData < 0)
    {
        // Not started yet, looking for the magic byte
        if (b == UART_PACKET_MAGIC)
        {
            // Found magic byte, start receiving from next incoming byte
            s_cbUartData = 0;
        }
    }
    else
    {
        // The logic will make sure s_cbUartData is less than UART_PACKET_SIZE here
        s_uartData[s_cbUartData] = b;
        s_cbUartData ++;

        if (s_cbUartData == sizeof(PACKET))
        {
            // Reset packet byte counter for next packet
            s_cbUartData = -1;

            OnPacket((const PACKET*)s_uartData);
        }
    }
}
Exemplo n.º 3
0
BOOL CHostBrowser::StreamPacketsG2()
{
	ASSUME_LOCK( Transfers.m_pSection );
	ASSERT( m_nProtocol == PROTOCOL_G2 );

	while ( CG2Packet* pPacket = CG2Packet::ReadBuffer( m_pBuffer ) )
	{
		BOOL bSuccess = FALSE;

		try
		{
			bSuccess = OnPacket( pPacket );
		}
		catch ( CException* pException )
		{
			pException->Delete();
		}

		pPacket->Release();

		if ( ! bSuccess )
		{
			Stop();
			return FALSE;
		}
	}

	return TRUE;
}
Exemplo n.º 4
0
BOOL CHostBrowser::StreamPacketsG2()
{
	CG2Packet* pPacket;

	while ( pPacket = CG2Packet::ReadBuffer( m_pBuffer ) )
	{
		BOOL bSuccess = FALSE;

		try
		{
			bSuccess = OnPacket( pPacket );
		}
		catch ( CException* pException )
		{
			pException->Delete();
		}

		pPacket->Release();

		if ( ! bSuccess )
		{
			Stop();
			return FALSE;
		}
	}

	return TRUE;
}
Exemplo n.º 5
0
BOOL CDCNeighbour::ProcessPackets(CBuffer* pInput)
{
	if ( ! pInput )
		return FALSE;

	BOOL bSuccess = TRUE;

	while ( CDCPacket* pPacket = CDCPacket::ReadBuffer( pInput ) )
	{
		try
		{
			bSuccess = OnPacket( pPacket );
		}
		catch ( CException* pException )
		{
			pException->Delete();
		}

		pPacket->Release();
		if ( ! bSuccess )
			break;

		if ( m_pZInput && m_pZInput != pInput )
			// Compression just turned on
			break;
	}

	return bSuccess;
}
Exemplo n.º 6
0
void CG2Node::OnRead()
{
	if( !Network.m_pSection.tryLock(50) )
	{
		emit readyRead(); // it is a queued connection, lets requeue the missed signal
		return;
	}

    //qDebug() << "CG2Node::OnRead";
    if( m_nState == nsHandshaking )
    {
        if( peek(bytesAvailable()).indexOf("\r\n\r\n") != -1 )
        {
            if( m_bInitiated )
            {
                ParseOutgoingHandshake();
            }
            else
            {
                ParseIncomingHandshake();
            }
        }
    }
    else if ( m_nState == nsConnected )
    {

		G2Packet* pPacket = 0;
        try
        {
			while( (pPacket = G2Packet::ReadBuffer(GetInputBuffer())) )
            {
				m_tLastPacketIn = time(0);
				m_nPacketsIn++;

				OnPacket(pPacket);

				pPacket->Release();
            }
        }
		catch(...)
        {
			if( pPacket )
			{
				qDebug() << pPacket->ToHex() << "\n" << pPacket->ToASCII();
				pPacket->Release();
			}

            qDebug() << "Packet error - " << m_oAddress.toString().toAscii();
            m_nState = nsClosing;
            emit NodeStateChanged();
            deleteLater();
        }
    }

	Network.m_pSection.unlock();
}
Exemplo n.º 7
0
BOOL CHostBrowser::StreamPacketsG1()
{
	ASSUME_LOCK( Transfers.m_pSection );
	ASSERT( m_nProtocol == PROTOCOL_G1 );

	BOOL bSuccess = TRUE;
	for ( ; bSuccess; )
	{
		GNUTELLAPACKET* pPacket = (GNUTELLAPACKET*)m_pBuffer->m_pBuffer;
		if ( m_pBuffer->m_nLength < sizeof( *pPacket ) ) break;

		DWORD nLength = sizeof( *pPacket ) + pPacket->m_nLength;

		if ( pPacket->m_nLength < 0 || nLength >= (DWORD)Settings.Gnutella.MaximumPacket * 8 )
		{
			theApp.Message( MSG_ERROR, IDS_BROWSE_PACKET_ERROR, (LPCTSTR)m_sAddress );
			Stop();
			return FALSE;
		}

		if ( m_pBuffer->m_nLength < nLength ) break;

		CG1Packet* pPacketObject = CG1Packet::New( pPacket );

		try
		{
			bSuccess = OnPacket( pPacketObject );
		}
		catch ( CException* pException )
		{
			pException->Delete();
		}

		pPacketObject->Release();

		m_pBuffer->Remove( nLength );
	}

	if ( ! bSuccess )
		Stop();

	return bSuccess;
}
Exemplo n.º 8
0
void CChatSessionG2::OnRead()
{
	if( m_nState == csHandshaking )
	{
		if(Peek(bytesAvailable()).indexOf("\r\n\r\n") != -1)
		{
			if(m_bInitiated)
			{
				ParseOutgoingHandshake();
			}
			else
			{
				//ParseIncomingHandshake();
			}
		}
	}
	else if( m_nState == csConnected || m_nState == csActive )
	{
		G2Packet* pPacket = 0;
		try
		{
			while((pPacket = G2Packet::ReadBuffer(GetInputBuffer())))
			{
				OnPacket(pPacket);

				pPacket->Release();
			}
		}
		catch(...)
		{
			if(pPacket)
			{
				pPacket->Release();
			}

			emit systemMessage("Received corrupted G2 packet, connection lost.");

			CNetworkConnection::Close();
		}
		m_bReadyReadSent = false;
	}
}
Exemplo n.º 9
0
bool PeerClient::OnRead()
{
    bool bSuccess = true;

    if(!TcpConnection::OnRead()) return false;

    PPPacket* pPacket = NULL;

    while(true)
    {
        pPacket = PPPacket::FromBuffer(m_pInput);
        if(pPacket == NULL)
        {
            break;
        }
        
        OnPacket(pPacket);
        pPacket->Release();
    }

    return bSuccess;
}
Exemplo n.º 10
0
    void CPacketizer::operator() (char* buffer, size_t bufSize)
    {
        // copy over the input buffer into existing buffer.
        std::copy( (const boost::uint8_t *)buffer, 
                (const boost::uint8_t *)buffer + bufSize, 
                std::back_inserter(buffer_) );

        int32_t bytesProcessed=0;
        int32_t bytesRemaining=buffer_.size();

        // process the block
        while ( bytesRemaining > 0 )
        {
            if (bytesRemaining < hdrLength_)
            {
                // get more data
                return;
            }

            char hdr[hdrLength_+1];
            memset(hdr, 0, sizeof(hdr));

            // copy first headerlength from buffer_ to character array
            for (int32_t i=0; i<hdrLength_; i++)
            {
                hdr[i]=buffer_[i];
            }

            int32_t bodyLen=0;
            int32_t n=sscanf(hdr, "%d", &bodyLen);
            std::ostringstream tmp;
            tmp << "Header is - " <<hdr << " " << n << std::endl;
            LOG_ERROR(tmp.str());
            if ( n != 1 )
            {
                // something is wrong
                buffer_.clear(); // drop the buffer

                std::ostringstream errorStr;
                errorStr << "Error - " << strerror(errno) << 
                    " in scanning header buffer " << hdr << 
                    " . for body length - " << std::endl;
                LOG_ERROR(errorStr.str());
                return;
            }

            bytesProcessed += hdrLength_;
            bytesRemaining -= hdrLength_;

            if (bytesRemaining < bodyLen )
            {
                return; // get more data to complete body
            }

            char body[bodyLen + 1];
            memset(body, 0, sizeof(body));
            for(int i=0; i<bodyLen; i++)
            {
                body[i]=buffer_[i+bytesProcessed];
            }

            // just print out what you received
            std::ostringstream debugStr;
            debugStr<< "Body - " << body << std::endl;
            LOG_DEBUG(debugStr.str());

            // raise signal for passing data to client
            // TODO - its fine now since we are using a single thread, going
            // forward we need to ensure that the buffers are unique enough !!

            OnPacket(body, bodyLen );

            bytesProcessed += bodyLen;

            // clean up buffer_ by erasing the values already processed
            // note that we are erasing elements only after the body is
            // processed. This ensures that incomplete body is processed in the
            // same loop
            buffer_.erase(buffer_.begin(), 
                          buffer_.begin() + bytesProcessed);

            bytesRemaining -= bodyLen;
        }
    }