BufferRef COBSEncode(const BufferRef& buffer) {
    size_t maxEncodedSize = buffer->getSize() + ceil(static_cast<float>(buffer->getSize()) / 254) + 1;
    BufferRef encodeBuffer = Buffer::create(maxEncodedSize);
    size_t encodedSize = COBSEncode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(encodeBuffer->getData()));
    encodeBuffer->resize(encodedSize);
    return encodeBuffer;
}
Esempio n. 2
0
void TcpSession::write( const BufferRef& buffer )
{
	ostream stream( &mRequest );
	if ( buffer && buffer->getSize() > 0 ) {
		stream.write( (const char*)buffer->getData(), buffer->getSize() );
	}
	asio::async_write( *mSocket, mRequest, 
		mStrand.wrap( boost::bind( &TcpSession::onWrite, shared_from_this(), 
			asio::placeholders::error, 
			asio::placeholders::bytes_transferred ) ) );
	mRequest.consume( mRequest.size() );
}
Esempio n. 3
0
void UdpSession::write( const BufferRef& buffer )
{
	ostream stream( &mRequest );
	if ( buffer && buffer->getSize() > 0 ) {
		stream.write( (const char*)buffer->getData(), buffer->getSize() );
	}
	mSocket->async_send( mRequest.data(), 
		mStrand.wrap( boost::bind( &UdpSession::onWrite, shared_from_this(), 
			asio::placeholders::error, 
			asio::placeholders::bytes_transferred ) ) );
	mSocket->set_option( asio::socket_base::broadcast( true ) );
	mEndpointLocal = mSocket->local_endpoint();
	mRequest.consume( mRequest.size() );
}
Esempio n. 4
0
ImFont* Renderer::addFont( ci::DataSourceRef font, float size, const ImWchar* glyph_ranges )
{
	ImFontAtlas* fontAtlas  = ImGui::GetIO().Fonts;
	
	Font ciFont( font, size );
	
	BufferRef buffer		= font->getBuffer();
	void* bufferCopy        = (void*) malloc( buffer->getSize() );
	memcpy( bufferCopy, buffer->getData(), buffer->getSize() );
	
	ImFontConfig config;
	ImFont* newFont         = fontAtlas->AddFontFromMemoryTTF( bufferCopy, buffer->getSize(), size, &config, glyph_ranges );
	
	mFonts.insert( make_pair( font->getFilePath().stem().string(), newFont ) );
	return newFont;
}
BufferRef SLIPEncode(const BufferRef& buffer) {
    size_t maxEncodedSize = 2 * buffer->getSize() + 2;
    BufferRef encodeBuffer = Buffer::create(maxEncodedSize);
    size_t encodedSize = SLIPEncode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(encodeBuffer->getData()));
    encodeBuffer->resize(encodedSize);
    return encodeBuffer;
}
BufferRef COBSDecode(const BufferRef& buffer) {
    size_t maxDecodedSize = buffer->getSize() - 1;
    BufferRef decodeBuffer = Buffer::create(maxDecodedSize);
    size_t decodedSize = COBSDecode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(decodeBuffer->getData()));
    decodeBuffer->resize(decodedSize);
    return decodeBuffer;
}
BufferRef SLIPDecode(const BufferRef& buffer) {
    // should not assume double-ENDed variant
    size_t maxDecodedSize = buffer->getSize() - 1;
    BufferRef decodeBuffer = Buffer::create(maxDecodedSize);
    size_t decodedSize = SLIPDecode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(decodeBuffer->getData()));
    decodeBuffer->resize(decodedSize);
    return decodeBuffer;
}
BufferRef HeaderInterface::removeHeader( const BufferRef& buffer )
{
	string msg		= bufferToString( buffer );
	size_t offset	= msg.find( sCrLf + sCrLf );
	size_t sz		= buffer->getSize();
	if ( offset < sz ) {
		size_t len = ( sz - offset ) - 4;
		BufferRef body = Buffer::create( len );
		char_traits<char>::copy( (char*)body->getData(), (char*)buffer->getData() + ( offset + 4 ), len );
		return body;
	} else {
		return Buffer::create( 0 );
	}
}
void PusherDiscoveryService::onRead( BufferRef buffer )
{
//    mPushersMutex.lock();
    
    uint8_t         *data   =(uint8_t*)buffer->getData();
    DeviceHeader    header  = DeviceHeader( data, buffer->getSize() );
    string          macAddr = header.getMacAddressString();
    PixelPusherRef  thisDevice, incomingDevice;
    
    // ignore non-PixelPusher devices
    if ( header.getDeviceType() != DeviceType::PIXELPUSHER )
        return;
    
    incomingDevice = PixelPusher::create( header );
    
    for( size_t k=0; k < mPushersInternal.size(); k++ )
    {
        if ( !mPushersInternal[k] )
            continue;

        if ( mPushersInternal[k]->getMacAddress() == incomingDevice->getMacAddress() )
        {
            thisDevice = mPushersInternal[k];
            break;
        }
    }
    
    if ( !thisDevice )
    {
        thisDevice = incomingDevice;
        addNewPusher( thisDevice );
    }
    else
    {
        if ( !thisDevice->isEqual( incomingDevice ) )
        {
            thisDevice->copyHeader( incomingDevice );
        }
        else
        {
            // The device is identical, nothing important has changed
            thisDevice->updateVariables( incomingDevice );
            
            // if we dropped more than occasional packets, slow down a little
            if ( incomingDevice->getDeltaSequence() > 3 )
                thisDevice->increaseExtraDelay(5);
            
            if ( incomingDevice->getDeltaSequence() < 1 )
                thisDevice->decreaseExtraDelay(1);
        }
    }
    
    // Set the timestamp for the last time this device checked in
    thisDevice->setLastPing( getElapsedSeconds() );
    
    // update the power limit variables
    if ( TotalPowerLimit > 0 )
    {
        TotalPower = 0;
        
        for( size_t k=0; k < mPushersInternal.size(); k++ )
            TotalPower += mPushersInternal[k]->getPowerTotal();

        PowerScale = ( TotalPower > TotalPowerLimit ) ? ( TotalPowerLimit / TotalPower ) : 1.0;
    }
   
//    mPushersMutex.unlock();
    
    // continue reading
	mSession->read();
}