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; }
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; }
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; }
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; }
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; }
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 } }
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; }
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"); }
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; }
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; }
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; }
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 ); }
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()); }
/** 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()); } }
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; }
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); }
/** * 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; }
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() ); }
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); }
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 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; } }
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() ); }
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; }
/** * 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; }
bool HttpConnection::onMessageContent(const BufferRef& chunk) { TRACE("onMessageContent(#%ld)", chunk.size()); request_->onRequestContent(chunk); return true; }
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; }
/** 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); } }
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; }
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; }
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(); }