Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 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> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 11 | #include <weave/error.h> |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 12 | |
| 13 | namespace weave { |
| 14 | |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 15 | // Interface for async input streaming. |
| 16 | class InputStream { |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 17 | public: |
Vitaly Buka | 3bfb13d | 2015-11-24 14:46:13 -0800 | [diff] [blame] | 18 | virtual ~InputStream() {} |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 19 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 20 | // Callback type for Read. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 21 | using ReadCallback = base::Callback<void(size_t size, ErrorPtr error)>; |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 22 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 23 | // Implementation should return immediately and post callback after |
| 24 | // completing operation. Caller guarantees that buffet is alive until callback |
| 25 | // is called. |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 26 | virtual void Read(void* buffer, |
| 27 | size_t size_to_read, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 28 | const ReadCallback& callback) = 0; |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 29 | }; |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 30 | |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 31 | // Interface for async input streaming. |
| 32 | class OutputStream { |
| 33 | public: |
Vitaly Buka | 3bfb13d | 2015-11-24 14:46:13 -0800 | [diff] [blame] | 34 | virtual ~OutputStream() {} |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 35 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 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. |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 41 | // Success callback must be called only after all data is written. |
Vitaly Buka | 1fd619a | 2015-09-24 11:46:05 -0700 | [diff] [blame] | 42 | virtual void Write(const void* buffer, |
| 43 | size_t size_to_write, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 44 | const WriteCallback& callback) = 0; |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 45 | }; |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 46 | |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 47 | // Interface for async bi-directional streaming. |
| 48 | class Stream : public InputStream, public OutputStream { |
| 49 | public: |
Vitaly Buka | 3bfb13d | 2015-11-24 14:46:13 -0800 | [diff] [blame] | 50 | ~Stream() override {} |
Vitaly Buka | da98728 | 2015-09-23 16:50:40 -0700 | [diff] [blame] | 51 | |
| 52 | // Cancels all pending read or write requests. Canceled operations must not |
| 53 | // call any callbacks. |
| 54 | virtual void CancelPendingOperations() = 0; |
Vitaly Buka | 10c69ec | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } // namespace weave |
| 58 | |
| 59 | #endif // LIBWEAVE_INCLUDE_WEAVE_STREAM_H_ |