1// Copyright 2015 The Weave Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef LIBWEAVE_INCLUDE_WEAVE_STREAM_H_ 6#define LIBWEAVE_INCLUDE_WEAVE_STREAM_H_ 7 8#include <string> 9 10#include <base/callback.h> 11#include <weave/error.h> 12 13namespace weave { 14 15// Interface for async input streaming. 16class InputStream { 17 public: 18 virtual ~InputStream() {} 19 20 // Callback type for Read. 21 using ReadCallback = base::Callback<void(size_t size, ErrorPtr error)>; 22 23 // Implementation should return immediately and post callback after 24 // completing operation. Caller guarantees that buffet is alive until callback 25 // is called. 26 virtual void Read(void* buffer, 27 size_t size_to_read, 28 const ReadCallback& callback) = 0; 29}; 30 31// Interface for async input streaming. 32class OutputStream { 33 public: 34 virtual ~OutputStream() {} 35 36 using WriteCallback = base::Callback<void(ErrorPtr error)>; 37 38 // Implementation should return immediately and post callback after 39 // completing operation. Caller guarantees that buffet is alive until either 40 // of callback is called. 41 // Success callback must be called only after all data is written. 42 virtual void Write(const void* buffer, 43 size_t size_to_write, 44 const WriteCallback& callback) = 0; 45}; 46 47// Interface for async bi-directional streaming. 48class Stream : public InputStream, public OutputStream { 49 public: 50 ~Stream() override {} 51 52 // Cancels all pending read or write requests. Canceled operations must not 53 // call any callbacks. 54 virtual void CancelPendingOperations() = 0; 55}; 56 57} // namespace weave 58 59#endif // LIBWEAVE_INCLUDE_WEAVE_STREAM_H_ 60