コード例 #1
0
ファイル: ChunkedEncoder.cpp プロジェクト: hiwang123/x0
Buffer ChunkedEncoder::process(const BufferRef& input)
{
#if 0
	if (input.empty())
		ERROR("proc: EOF");
	else
		TRACE("proc: inputLen=%ld", input.size());
#endif

	if (finished_)
		return Buffer();

	if (input.empty())
		finished_ = true;

	Buffer output;

	if (input.size())
	{
		char buf[12];
		int size = snprintf(buf, sizeof(buf), "%zx\r\n", input.size());

		output.push_back(buf, size);
		output.push_back(input);
		output.push_back("\r\n");
	}
	else
		output.push_back("0\r\n\r\n");

	//! \todo a filter might create multiple output-chunks, though, we could improve process() to not return the output but append them directly.
	// virtual void process(const BufferRef& input, composite_source& output) = 0;

	return output;
}
コード例 #2
0
ファイル: HttpFileMgr.cpp プロジェクト: hiwang123/x0
bool HttpFileMgr::Settings::openMimeTypes(const std::string& path)
{
	Buffer input;
	if (!readFile(path, &input))
		return false;

	auto lines = Tokenizer<BufferRef, Buffer>::tokenize(input, "\n");

	mimetypes.clear();

	for (auto line: lines) {
		line = line.trim();
		auto columns = Tokenizer<BufferRef, BufferRef>::tokenize(line, " \t");

		auto ci = columns.begin(), ce = columns.end();
		BufferRef mime = ci != ce ? *ci++ : BufferRef();

		if (!mime.empty() && mime[0] != '#') {
			for (; ci != ce; ++ci) {
				mimetypes[ci->str()] = mime.str();
			}
		}
	}

	return true;
}
コード例 #3
0
ファイル: HttpConnection.cpp プロジェクト: crnt/x0
bool HttpConnection::onMessageBegin(const BufferRef& method, const BufferRef& uri, int versionMajor, int versionMinor)
{
	TRACE("onMessageBegin: '%s', '%s', HTTP/%d.%d", method.str().c_str(), uri.str().c_str(), versionMajor, versionMinor);

	request_->method = method;
	request_->uri = uri;
	url_decode(input_, request_->uri);

	std::size_t n = request_->uri.find("?");
	if (n != std::string::npos) {
		request_->path = request_->uri.ref(0, n);
		request_->query = request_->uri.ref(n + 1);
	} else {
		request_->path = request_->uri;
	}

	request_->httpVersionMajor = versionMajor;
	request_->httpVersionMinor = versionMinor;

	if (request_->supportsProtocol(1, 1))
		// FIXME? HTTP/1.1 is keeping alive by default. pass "Connection: close" to close explicitely
		setShouldKeepAlive(true);
	else
		setShouldKeepAlive(false);

	// limit request uri length
	if (request_->uri.size() > worker().server().maxRequestUriSize()) {
		request_->status = HttpError::RequestUriTooLong;
		request_->finish();
		return false;
	}

	return true;
}
コード例 #4
0
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;
}
コード例 #5
0
bool HttpConnection::onMessageBegin(const BufferRef& method, const BufferRef& uri, int versionMajor, int versionMinor)
{
	TRACE(1, "onMessageBegin: '%s', '%s', HTTP/%d.%d", method.str().c_str(), uri.str().c_str(), versionMajor, versionMinor);

	request_->method = method;

	if (!request_->setUri(uri)) {
		abort(HttpStatus::BadRequest);
		return false;
	}

	request_->httpVersionMajor = versionMajor;
	request_->httpVersionMinor = versionMinor;

	if (request_->supportsProtocol(1, 1))
		// FIXME? HTTP/1.1 is keeping alive by default. pass "Connection: close" to close explicitely
		setShouldKeepAlive(true);
	else
		setShouldKeepAlive(false);

	// limit request uri length
	if (request_->unparsedUri.size() > worker().server().maxRequestUriSize()) {
		request_->status = HttpStatus::RequestUriTooLong;
		request_->finish();
		return false;
	}

	return true;
}
コード例 #6
0
void CinderVideoStreamServerApp::update()
{

    if( mCapture && mCapture->checkNewFrame() ) {
        Surface8uRef surf = mCapture->getSurface();
#ifdef USE_JPEG_COMPRESSION
        OStreamMemRef os = OStreamMem::create();
        DataTargetRef target = DataTargetStream::createRef( os );
        writeImage( target, *surf, ImageTarget::Options().quality(mQuality), "jpeg" );
        const void *data = os->getBuffer();
        size_t dataSize = os->tell();
        totalStreamSize += dataSize;

        BufferRef bufRef = Buffer::create(dataSize);
        memcpy(bufRef->getData(), data, dataSize);
        SurfaceRef jpeg = Surface::create(loadImage( DataSourceBuffer::create(bufRef)), SurfaceConstraintsDefault(), false );
        queueToServer->push(jpeg->getData());
        mTexture = gl::Texture::create( *jpeg );
        
        mStatus.assign("Streaming JPG (")
               .append(std::to_string((int)(mQuality*100.0f)))
               .append("%) ")
               .append(std::to_string((int)(totalStreamSize*0.001/getElapsedSeconds())))
               .append(" kB/sec ")
               .append(std::to_string((int)getFrameRate()))
               .append(" fps ");
#else
        queueToServer->push(surf->getData());
        mTexture = gl::Texture::create( *surf );
        mStatus.assign("Streaming ").append(std::to_string((int)getFrameRate())).append(" fps");
#endif
        
    }
}
コード例 #7
0
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;
}
コード例 #8
0
int main()
{
	/*------------------------------------------------------------------------
	 * Instantiate the global processing graph.
	 *-----------------------------------------------------------------------*/
	AudioGraphRef graph = new AudioGraph();

	/*------------------------------------------------------------------------
	 * Take a two-channel input from the default input device.
	 *-----------------------------------------------------------------------*/
	NodeRef input = new AudioIn();

	/*------------------------------------------------------------------------
	 * Create a five-second stereo buffer.
	 *-----------------------------------------------------------------------*/
	BufferRef buffer = new Buffer(2, graph->sample_rate * 1.0);

	/*------------------------------------------------------------------------
	 * Create a Recorder to perform a one-shot recording of the input.
	 * It must be connected to the graph's output to trigger processing.
	 *-----------------------------------------------------------------------*/
	NodeRef recorder = new Recorder(buffer, input);
	graph->add_output(recorder);
	graph->start();

	/*------------------------------------------------------------------------
	 * Wait until our recording is complete (happening in the audio I/O
	 * thread), then save to disk.
	 *-----------------------------------------------------------------------*/
	usleep(1e6);
	buffer->save("out.wav");
}
コード例 #9
0
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;
}
コード例 #10
0
ファイル: CompressFilter.cpp プロジェクト: illdefined/x0
Buffer DeflateFilter::process(const BufferRef& input)
{
	//FIXME
	bool eof = input.empty();
/*	if (input.empty())
	{
		TRACE("process received empty input");
		return Buffer();
	}
*/
	z_.total_out = 0;
	z_.next_in = reinterpret_cast<Bytef *>(input.begin());
	z_.avail_in = input.size();

	Buffer output(input.size() * 1.1 + 12 + 18);
	z_.next_out = reinterpret_cast<Bytef *>(output.end());
	z_.avail_out = output.capacity();

	do
	{
		if (output.size() > output.capacity() / 2)
		{
			z_.avail_out = Buffer::CHUNK_SIZE;
			output.reserve(output.capacity() + z_.avail_out);
		}

		int flushMethod;
		int expected;

		if (!eof)
		{
			flushMethod = Z_SYNC_FLUSH; // Z_NO_FLUSH
			expected = Z_OK;
		}
		else
		{
			flushMethod = Z_FINISH;
			expected = Z_STREAM_END;
		}

		int rv = deflate(&z_, flushMethod);

		//TRACE("deflate(): rv=%d, avail_in=%d, avail_out=%d, total_out=%ld", rv, z_.avail_in, z_.avail_out, z_.total_out);

		if (rv != expected)
		{
			TRACE("process: deflate() error (%d)", rv);
			return Buffer();
		}
	}
	while (z_.avail_out == 0);

	assert(z_.avail_in == 0);

	output.resize(z_.total_out);
	//TRACE("process(%ld bytes, eof=%d) -> %ld", input.size(), eof, z_.total_out);

	return output;
}
コード例 #11
0
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;
}
コード例 #12
0
ファイル: SampleRecorderNode.cpp プロジェクト: Ahbee/Cinder
void BufferRecorderNode::writeToFile( const fs::path &filePath, SampleType sampleType )
{
	size_t currentWritePos = mWritePos;
	BufferRef copiedBuffer = getRecordedCopy();

	audio::TargetFileRef target = audio::TargetFile::create( filePath, getSampleRate(), getNumChannels(), sampleType );
	target->write( copiedBuffer.get(), currentWritePos );
}
コード例 #13
0
	bool Buffer::CopyContent(const BufferRef& buffer)
	{
		NazaraAssert(m_impl, "Invalid buffer");
		NazaraAssert(!buffer && !buffer->IsValid(), "Invalid source buffer");

		BufferMapper<Buffer> mapper(*buffer, BufferAccess_ReadOnly);
		return Fill(mapper.GetPointer(), 0, buffer->GetSize());
	}
コード例 #14
0
ファイル: HttpRequest.cpp プロジェクト: liveck/x0
/** Passes the request body chunk to the applications registered callback.
 *
 * \see setBodyCallback()
 */
void HttpRequest::onRequestContent(const BufferRef& chunk)
{
	if (bodyCallback_) {
		TRACE("onRequestContent(chunkSize=%ld) pass to callback", chunk.size());
		bodyCallback_(chunk, bodyCallbackData_);
	} else {
		TRACE("onRequestContent(chunkSize=%ld) discard", chunk.size());
	}
}
コード例 #15
0
BufferRef HeaderInterface::toBuffer() const
{
	string header	= headerToString();
	size_t sz		= header.size();
	
	BufferRef buffer = Buffer::create( sz );
	char_traits<char>::copy( (char*)buffer->getData(), (char*)&header[ 0 ], sz );
	
	return buffer;
}
コード例 #16
0
ファイル: ObjectCache.cpp プロジェクト: saeschdivara/x0
Buffer ObjectCache::Builder::process(const BufferRef& chunk)
{
	if (object_) {
		TRACE(object_->request_, 3, "ObjectCache.Builder.process(): %zu bytes", chunk.size());
		if (!chunk.empty()) {
			object_->backBuffer().body.push_back(chunk);
		}
	}

	return Buffer(chunk);
}
コード例 #17
0
ファイル: HealthMonitor.cpp プロジェクト: hiwang123/x0
/**
 * Callback, invoked on each successfully parsed response header key/value pair.
 */
bool HealthMonitor::onMessageHeader(const BufferRef& name, const BufferRef& value)
{
	TRACE("onResponseHeader(name:%s, value:%s)", name.str().c_str(), value.str().c_str());

	if (x0::iequals(name, "Status")) {
		int status = value.ref(0, value.find(' ')).toInt();
		responseCode_ = static_cast<x0::HttpStatus>(status);
	}

	return true;
}
コード例 #18
0
ファイル: TcpSession.cpp プロジェクト: naychrist/Cinder-Asio
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() );
}
コード例 #19
0
ファイル: Base64-test.cpp プロジェクト: sharpglasses/x0
void print(const BufferRef& v, const char *msg = 0)
{
	char prefix[64];
	if (msg && *msg)
		snprintf(prefix, sizeof(prefix), "\nbuffer.view(%s)", msg);
	else
		snprintf(prefix, sizeof(prefix), "\nbuffer.view");

	if (v)
		printf("\n%s: '%s' (size=%zu)\n", prefix, v.str().c_str(), v.size());
	else
		printf("\n%s: NULL\n", prefix);
}
コード例 #20
0
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 );
	}
}
コード例 #21
0
ファイル: PortImpl.cpp プロジェクト: ventosus/ingen
void
PortImpl::update_set_state(Context& context, uint32_t v)
{
	Voice&    voice = _voices->at(v);
	SetState& state = voice.set_state;
	BufferRef buf   = voice.buffer;
	switch (state.state) {
	case SetState::State::SET:
		break;
	case SetState::State::SET_CYCLE_1:
		if (state.time < context.start() &&
		    buf->is_sequence() &&
		    buf->value_type() == _bufs.uris().atom_Float &&
		    !_parent->path().is_root()) {
			buf->clear();
			state.time = context.start();
		}
		state.state = SetState::State::SET;
		break;
	case SetState::State::HALF_SET_CYCLE_1:
		state.state = SetState::State::HALF_SET_CYCLE_2;
		break;
	case SetState::State::HALF_SET_CYCLE_2:
		if (buf->is_sequence()) {
			buf->clear();
			buf->append_event(
				0, sizeof(float), _bufs.uris().atom_Float,
				(const uint8_t*)&state.value);
		} else {
			buf->set_block(state.value, 0, context.nframes());
		}
		state.state = SetState::State::SET_CYCLE_1;
		break;
	}
}
コード例 #22
0
ファイル: UdpSession.cpp プロジェクト: naychrist/Cinder-Asio
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() );
}
コード例 #23
0
ファイル: CinderImGui.cpp プロジェクト: Craigson/Choreo3D
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;
}
コード例 #24
0
ファイル: HealthMonitor.cpp プロジェクト: hiwang123/x0
/**
 * Callback, invoked on successfully parsed response status line.
 */
bool HealthMonitor::onMessageBegin(int versionMajor, int versionMinor, int code, const BufferRef& text)
{
	TRACE("onMessageBegin: (HTTP/%d.%d, %d, '%s')", versionMajor, versionMinor, code, text.str().c_str());

	responseCode_ = static_cast<HttpStatus>(code);

	return true;
}
コード例 #25
0
ファイル: HttpConnection.cpp プロジェクト: crnt/x0
bool HttpConnection::onMessageContent(const BufferRef& chunk)
{
	TRACE("onMessageContent(#%ld)", chunk.size());

	request_->onRequestContent(chunk);

	return true;
}
コード例 #26
0
ファイル: HttpConnection.cpp プロジェクト: crnt/x0
bool HttpConnection::onMessageHeader(const BufferRef& name, const BufferRef& value)
{
	if (request_->isFinished()) {
		// this can happen when the request has failed some checks and thus,
		// a client error message has been sent already.
		// we need to "parse" the remaining content anyways.
		TRACE("onMessageHeader() skip \"%s\": \"%s\"", name.str().c_str(), value.str().c_str());
		return true;
	}

	TRACE("onMessageHeader() \"%s\": \"%s\"", name.str().c_str(), value.str().c_str());

	if (iequals(name, "Host")) {
		auto i = value.find(':');
		if (i != BufferRef::npos)
			request_->hostname = value.ref(0, i);
		else
			request_->hostname = value;
		TRACE(" -- hostname set to \"%s\"", request_->hostname.str().c_str());
	} else if (iequals(name, "Connection")) {
		if (iequals(value, "close"))
			setShouldKeepAlive(false);
		else if (iequals(value, "keep-alive"))
			setShouldKeepAlive(true);
	}

	// limit the size of a single request header
	if (name.size() + value.size() > worker().server().maxRequestHeaderSize()) {
		TRACE("header too long. got %ld / %ld", name.size() + value.size(), worker().server().maxRequestHeaderSize());
		request_->status = HttpError::RequestEntityTooLarge;
		request_->finish();
		return false;
	}

	// limit the number of request headers
	if (request_->requestHeaders.size() > worker().server().maxRequestHeaderCount()) {
		TRACE("header count exceeded. got %ld / %ld", request_->requestHeaders.size(), worker().server().maxRequestHeaderCount());
		request_->status = HttpError::RequestEntityTooLarge;
		request_->finish();
		return false;
	}

	request_->requestHeaders.push_back(HttpRequestHeader(name, value));
	return true;
}
コード例 #27
0
ファイル: HttpProxy.cpp プロジェクト: neeravkumar/x0
/** transferres a request body chunk to the origin server.  */
void HttpProxy::ProxyConnection::onRequestChunk(const BufferRef& chunk)
{
	TRACE("onRequestChunk(nb:%ld)", chunk.size());
	writeBuffer_.push_back(chunk);

	if (backend_->state() == Socket::Operational) {
		backend_->setMode(Socket::ReadWrite);
	}
}
コード例 #28
0
ファイル: filter_example.cpp プロジェクト: tempbottle/x0
Buffer ExampleFilter::process(const BufferRef& input) {
  Buffer result;

  switch (mode_) {
    case ExampleFilter::LOWER:
      for (auto i = input.cbegin(), e = input.cend(); i != e; ++i)
        result.push_back(static_cast<char>(std::tolower(*i)));
      break;
    case ExampleFilter::UPPER:
      for (auto i = input.cbegin(), e = input.cend(); i != e; ++i)
        result.push_back(static_cast<char>(std::toupper(*i)));
      break;
    case ExampleFilter::IDENTITY:
    default:
      result.push_back(input);
  }

  return result;
}
コード例 #29
0
ファイル: CompressFilter.cpp プロジェクト: illdefined/x0
Buffer BZip2Filter::process(const BufferRef& input)
{
	if (input.empty())
		return Buffer();

	int rv = BZ2_bzCompressInit(&bz_,
		level(),						// compression level
		0,								// no output
		0								// work factor
	);

	if (rv != BZ_OK)
		return Buffer();

	bz_.next_in = input.begin();
	bz_.avail_in = input.size();
	bz_.total_in_lo32 = 0;
	bz_.total_in_hi32 = 0;

	Buffer output(input.size() * 1.1 + 12);
	bz_.next_out = output.end();
	bz_.avail_out = output.capacity();
	bz_.total_out_lo32 = 0;
	bz_.total_out_hi32 = 0;

	rv = BZ2_bzCompress(&bz_, BZ_FINISH);
	if (rv != BZ_STREAM_END)
	{
		BZ2_bzCompressEnd(&bz_);
		return Buffer();
	}

	if (bz_.total_out_hi32)
		return Buffer(); // file too large

	output.resize(bz_.total_out_lo32);

	rv = BZ2_bzCompressEnd(&bz_);
	if (rv != BZ_OK)
		return Buffer();

	return output;
}
コード例 #30
0
int main()
{
	/*------------------------------------------------------------------------
	 * Create the global signal processing graph.
	 *-----------------------------------------------------------------------*/
	AudioGraphRef graph = new AudioGraph();

	/*------------------------------------------------------------------------
	 * Create a stereo pair of oscillating sine waves, an octave apart.
	 *-----------------------------------------------------------------------*/
	NodeRef sine = new Sine({ 40, 80 });

	/*------------------------------------------------------------------------
	 * Create a WaveShaperBuffer and populate it with a cubic function.
	 * WaveShaperBuffer is a subclass of Buffer that is optimised for
	 * mapping a sample value [-1, 1] to a new sample value [-1, 1].
	 *-----------------------------------------------------------------------*/
	BufferRef buffer = new WaveShaperBuffer();
	buffer->fill([](float input) { return input * input * input; });

	/*------------------------------------------------------------------------
	 * The WaveShaper node takes an input audio node and a WaveShaperBuffer.
	 * Distort the input by multiplying it to exceed [-1, 1].
	 * WaveShaper will clip this to [-1, 1] (as well as mapping the samples
	 * based on the buffer's cubic function).
	 *-----------------------------------------------------------------------*/
	NodeRef overdriven = sine * new Noise(0.2, true, 1, 3);
	NodeRef shaper = new WaveShaper(overdriven, buffer);

	/*------------------------------------------------------------------------
	 * Add some delay and stereo width modulation.
	 *-----------------------------------------------------------------------*/
	NodeRef delay = new Delay(shaper, 0.5, 0.5, 0.5);
	NodeRef width = new Sine(0.2);
	NodeRef throb = new Width(delay, width->scale(0.5, 1));

	/*------------------------------------------------------------------------
	 * Send to output and start processing.
	 *-----------------------------------------------------------------------*/
	graph->add_output(throb);
	graph->start();
	graph->wait();
}