void run_iterator_test() { StreamBuf b; // create a sample data boost::array<char, 20000> sample; std::generate(sample.begin(), sample.end(), make_randgen(boost::as_literal("0123456789"))); std::copy(sample.begin(), sample.begin() + 12000, ybuffers_begin(b.prepare(12000))); b.commit(12000); std::copy(sample.begin() + 12000, sample.begin() + 14000, ybuffers_begin(b.prepare(2000))); b.commit(1000); std::copy(sample.begin() + 13000, sample.begin() + 15000, ybuffers_begin(b.prepare(2000))); b.commit(2000); std::copy(sample.begin() + 15000, sample.begin() + 20000, ybuffers_begin(b.prepare(5000))); b.commit(5000); typename StreamBuf::const_buffers_type bufs = b.data(); assert(boost::equal(boost::make_iterator_range(sample.begin(), sample.end()), boost::make_iterator_range(ybuffers_begin(bufs), ybuffers_end(bufs))) ); assert(ybuffers_end(bufs) - ybuffers_begin(bufs) == 20000); assert(ybuffers_begin(bufs) - ybuffers_end(bufs) == -20000); }
/** * \param error Describes the error that occured, if any did. * \param bytes_transferred The amount of bytes transferred for this operation. */ void handleRead_(ErrorCode const & error, std::size_t bytes_transferred) { if (error) { throw Error(*this, error); // Otherwise check the received data for a whole message and process it. } else { read_buffer_.commit(bytes_transferred); while (auto message = Protocol::template consumeMessage<ReadMessage>(read_buffer_)) { handleMessage_(*message); } // Get more data. asyncRead_(); } }
/// Start an asynchronous read operation. void asyncRead_() { auto connection = this->shared_from_this(); auto handler = std::bind(&Connection<Protocol, IsServer>::handleRead_, this, std::placeholders::_1, std::placeholders::_2); socket_->async_read_some(read_buffer_.prepare(1024), handler); }
int main(int argc, char **argv) try { int idx = gflags::ParseCommandLineFlags(&argc, &argv, true); // cout << "FLAGS_timeout = " << FLAGS_timeout << endl; // for (; idx < argc; ++idx) // cout << argv[idx] << " "; // cout << endl; // return 0; StreamBuf *pBuf = NULL; std::shared_ptr<managed_shared_memory> pShmSegment; try { pShmSegment = std::make_shared<managed_shared_memory>(open_only, SHM_NAME); auto ret = pShmSegment->find<StreamBuf>("StreamBuf"); // return pair<type*, size_t> pBuf = ret.first; if (!pBuf) RETVAL(-1, "Cannot load shared buffer object. " "Please make sure that BigRLab apiserver has benn launched with -b option"); } catch (const std::exception &ex) { RETVAL(-1, "Cannot open shared memory object. " "Please make sure that BigRLab apiserver has benn launched with -b option"); } // try string cmd, resp; if (idx >= argc) { cmd = "hello"; } else { for (; idx < argc; ++idx) cmd.append(argv[idx]).append(" "); cmd.erase(cmd.size()-1); } // if bufferstream stream(pBuf->buf, SHARED_STREAM_BUF_SIZE); auto resetStream = [&] { stream.clear(); stream.seekg(0, std::ios::beg); stream.seekp(0, std::ios::beg); }; // clear state pBuf->respReady = false; // send cmd { resetStream(); pBuf->clear(); scoped_lock<interprocess_mutex> lk(pBuf->mtx); stream << cmd << endl << flush; pBuf->reqReady = true; lk.unlock(); pBuf->condReq.notify_all(); } // waiting for server response { resetStream(); //!! 不能用local_time auto deadline = boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(FLAGS_timeout); scoped_lock<interprocess_mutex> lk(pBuf->mtx); if (!pBuf->condResp.timed_wait(lk, deadline, [&]{ return pBuf->respReady; })) RETVAL(-1, "Wait cmd response timeout. " "Please make sure that BigRLab apiserver has benn launched with -b option"); getline(stream, resp, '\0'); pBuf->respReady = false; lk.unlock(); boost::trim(resp); if (!resp.empty()) cout << resp << endl; } return 0; } catch (const std::exception &ex) { cerr << "Exception caught by main: " << ex.what() << endl; exit(-1); }
void run_streambuf_test() { StreamBuf b; assert(b.size() == 0); std::size_t q = ystreambuf::chunk_size; assert( size(b.prepare(q-2)) == q-2 ); assert( size(b.prepare(1)) == 1 ); assert( size(b.prepare(q)) == q ); assert( size(b.prepare(q+1)) == q+1 ); assert( size(b.data()) == 0 && 0 == b.size()); b.commit(1); assert( size(b.data()) == 1 && 1 == b.size()); b.commit(q-1); assert( size(b.data()) == q && q == b.size()); b.commit(1); assert( size(b.data()) == q+1 && q+1 == b.size()); b.consume(1); assert( size(b.data()) == q && q == b.size()); b.consume(q-1); assert( size(b.data()) == 1 && 1 == b.size()); assert( size(b.prepare(2*q-1)) == 2*q-1 ); b.consume(1); assert( size(b.data()) == 0 && 0 == b.size()); b.commit(q); assert( size(b.data()) == q && q == b.size()); b.commit(q-1); assert( size(b.data()) == 2*q-1 && 2*q-1 == b.size()); b.consume(2*q-1); assert( size(b.data()) == 0 && 0 == b.size()); }