1*1a96fba6SXin Li // Copyright 2015 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_STREAMS_INPUT_STREAM_SET_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_STREAMS_INPUT_STREAM_SET_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <vector> 9*1a96fba6SXin Li 10*1a96fba6SXin Li #include <base/macros.h> 11*1a96fba6SXin Li #include <brillo/brillo_export.h> 12*1a96fba6SXin Li #include <brillo/streams/stream.h> 13*1a96fba6SXin Li 14*1a96fba6SXin Li namespace brillo { 15*1a96fba6SXin Li 16*1a96fba6SXin Li // Multiplexer stream allows to bundle a bunch of secondary streams in one 17*1a96fba6SXin Li // logical stream and simulate a read operation across data concatenated from 18*1a96fba6SXin Li // all those source streams. 19*1a96fba6SXin Li // 20*1a96fba6SXin Li // When created on a set of source streams like stream1, stream2, stream3, etc., 21*1a96fba6SXin Li // reading from the multiplexer stream will read all the data from stream1 until 22*1a96fba6SXin Li // end-of-stream is reached, then keep reading from stream2, stream3 and so on. 23*1a96fba6SXin Li // 24*1a96fba6SXin Li // InputStreamSet has an option of owning the underlying source streams 25*1a96fba6SXin Li // or just referencing them. Owned streams are passed to InputStreamSet 26*1a96fba6SXin Li // with exclusive ownership transfer (using StreamPtr) and those streams will 27*1a96fba6SXin Li // be closed/destroyed when InputStreamSet is closed/destroyed. 28*1a96fba6SXin Li // Referenced source streams' life time is maintained elsewhere and they must 29*1a96fba6SXin Li // be valid for the duration of InputStreamSet's life. Closing the 30*1a96fba6SXin Li // muliplexer stream does not close the referenced streams. 31*1a96fba6SXin Li class BRILLO_EXPORT InputStreamSet : public Stream { 32*1a96fba6SXin Li public: 33*1a96fba6SXin Li // == Construction ========================================================== 34*1a96fba6SXin Li 35*1a96fba6SXin Li // Generic method that constructs a multiplexer stream on a list of source 36*1a96fba6SXin Li // streams. |source_streams| is the list of all source stream references 37*1a96fba6SXin Li // in the order they need to be read from. |owned_source_streams| is a list 38*1a96fba6SXin Li // of source stream instances that the multiplexer stream will own. 39*1a96fba6SXin Li // Note that the streams from |owned_source_streams| should still be 40*1a96fba6SXin Li // referenced in |source_streams| if you need their data to be read from. 41*1a96fba6SXin Li // |owned_source_streams| could be empty (in which case none of the source 42*1a96fba6SXin Li // streams are not owned), or contain fewer items than in |source_streams|. 43*1a96fba6SXin Li static StreamPtr Create(std::vector<Stream*> source_streams, 44*1a96fba6SXin Li std::vector<StreamPtr> owned_source_streams, 45*1a96fba6SXin Li ErrorPtr* error); 46*1a96fba6SXin Li 47*1a96fba6SXin Li // Simple helper method to create a multiplexer stream with a list of 48*1a96fba6SXin Li // referenced streams. None of the streams will be owned. 49*1a96fba6SXin Li // Effectively calls Create(source_streams, {}, error); 50*1a96fba6SXin Li static StreamPtr Create(std::vector<Stream*> source_streams, ErrorPtr* error); 51*1a96fba6SXin Li 52*1a96fba6SXin Li // Simple helper method to create a multiplexer stream with a list of 53*1a96fba6SXin Li // referenced streams. None of the streams will be owned. 54*1a96fba6SXin Li // Effectively calls Create(source_streams, owned_source_streams, error) 55*1a96fba6SXin Li // with |source_streams| containing pointers to the streams from 56*1a96fba6SXin Li // |owned_source_streams| list. 57*1a96fba6SXin Li static StreamPtr Create(std::vector<StreamPtr> owned_source_streams, 58*1a96fba6SXin Li ErrorPtr* error); 59*1a96fba6SXin Li 60*1a96fba6SXin Li // == Stream capabilities =================================================== 61*1a96fba6SXin Li bool IsOpen() const override; CanRead()62*1a96fba6SXin Li bool CanRead() const override { return true; } CanWrite()63*1a96fba6SXin Li bool CanWrite() const override { return false; } CanSeek()64*1a96fba6SXin Li bool CanSeek() const override { return false; } 65*1a96fba6SXin Li bool CanGetSize() const override; 66*1a96fba6SXin Li 67*1a96fba6SXin Li // == Stream size operations ================================================ 68*1a96fba6SXin Li uint64_t GetSize() const override; 69*1a96fba6SXin Li bool SetSizeBlocking(uint64_t size, ErrorPtr* error) override; 70*1a96fba6SXin Li uint64_t GetRemainingSize() const override; 71*1a96fba6SXin Li 72*1a96fba6SXin Li // == Seek operations ======================================================= GetPosition()73*1a96fba6SXin Li uint64_t GetPosition() const override { return 0; } 74*1a96fba6SXin Li bool Seek(int64_t offset, 75*1a96fba6SXin Li Whence whence, 76*1a96fba6SXin Li uint64_t* new_position, 77*1a96fba6SXin Li ErrorPtr* error) override; 78*1a96fba6SXin Li 79*1a96fba6SXin Li // == Read operations ======================================================= 80*1a96fba6SXin Li bool ReadNonBlocking(void* buffer, 81*1a96fba6SXin Li size_t size_to_read, 82*1a96fba6SXin Li size_t* size_read, 83*1a96fba6SXin Li bool* end_of_stream, 84*1a96fba6SXin Li ErrorPtr* error) override; 85*1a96fba6SXin Li 86*1a96fba6SXin Li // == Write operations ====================================================== 87*1a96fba6SXin Li bool WriteNonBlocking(const void* buffer, 88*1a96fba6SXin Li size_t size_to_write, 89*1a96fba6SXin Li size_t* size_written, 90*1a96fba6SXin Li ErrorPtr* error) override; 91*1a96fba6SXin Li 92*1a96fba6SXin Li // == Finalizing/closing streams =========================================== FlushBlocking(ErrorPtr *)93*1a96fba6SXin Li bool FlushBlocking(ErrorPtr* /* error */) override { return true; } 94*1a96fba6SXin Li bool CloseBlocking(ErrorPtr* error) override; 95*1a96fba6SXin Li 96*1a96fba6SXin Li // == Data availability monitoring ========================================== 97*1a96fba6SXin Li bool WaitForData(AccessMode mode, 98*1a96fba6SXin Li const base::Callback<void(AccessMode)>& callback, 99*1a96fba6SXin Li ErrorPtr* error) override; 100*1a96fba6SXin Li 101*1a96fba6SXin Li bool WaitForDataBlocking(AccessMode in_mode, 102*1a96fba6SXin Li base::TimeDelta timeout, 103*1a96fba6SXin Li AccessMode* out_mode, 104*1a96fba6SXin Li ErrorPtr* error) override; 105*1a96fba6SXin Li 106*1a96fba6SXin Li void CancelPendingAsyncOperations() override; 107*1a96fba6SXin Li 108*1a96fba6SXin Li private: 109*1a96fba6SXin Li friend class InputStreamSetTest; 110*1a96fba6SXin Li 111*1a96fba6SXin Li // Internal constructor used by the Create() factory methods. 112*1a96fba6SXin Li InputStreamSet(std::vector<Stream*> source_streams, 113*1a96fba6SXin Li std::vector<StreamPtr> owned_source_streams, 114*1a96fba6SXin Li uint64_t initial_stream_size); 115*1a96fba6SXin Li 116*1a96fba6SXin Li // List of streams to read data from. 117*1a96fba6SXin Li std::vector<Stream*> source_streams_; 118*1a96fba6SXin Li 119*1a96fba6SXin Li // List of source streams this stream owns. Owned source streams will be 120*1a96fba6SXin Li // closed when InputStreamSet::CloseBlocking() is called and will be 121*1a96fba6SXin Li // destroyed when this stream is destroyed. 122*1a96fba6SXin Li std::vector<StreamPtr> owned_source_streams_; 123*1a96fba6SXin Li 124*1a96fba6SXin Li uint64_t initial_stream_size_{0}; 125*1a96fba6SXin Li bool closed_{false}; 126*1a96fba6SXin Li 127*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(InputStreamSet); 128*1a96fba6SXin Li }; 129*1a96fba6SXin Li 130*1a96fba6SXin Li } // namespace brillo 131*1a96fba6SXin Li 132*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_STREAMS_INPUT_STREAM_SET_H_ 133