blob: 14cc7f02a0dca94dd526701515bbacbe076fdee6 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Vitaly Buka10c69ec2015-08-12 16:17:16 -07002// 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 Buka0801a1f2015-08-14 10:03:46 -070011#include <weave/error.h>
Vitaly Buka10c69ec2015-08-12 16:17:16 -070012
13namespace weave {
14
Vitaly Bukada987282015-09-23 16:50:40 -070015// Interface for async input streaming.
16class InputStream {
Vitaly Buka10c69ec2015-08-12 16:17:16 -070017 public:
Vitaly Buka3bfb13d2015-11-24 14:46:13 -080018 virtual ~InputStream() {}
Vitaly Buka10c69ec2015-08-12 16:17:16 -070019
Vitaly Bukac3c6dab2015-10-01 19:41:02 -070020 // Callback type for Read.
Vitaly Buka74763422015-10-11 00:39:52 -070021 using ReadCallback = base::Callback<void(size_t size, ErrorPtr error)>;
Vitaly Buka1fd619a2015-09-24 11:46:05 -070022
Vitaly Buka74763422015-10-11 00:39:52 -070023 // Implementation should return immediately and post callback after
24 // completing operation. Caller guarantees that buffet is alive until callback
25 // is called.
Vitaly Buka1fd619a2015-09-24 11:46:05 -070026 virtual void Read(void* buffer,
27 size_t size_to_read,
Vitaly Buka74763422015-10-11 00:39:52 -070028 const ReadCallback& callback) = 0;
Vitaly Bukada987282015-09-23 16:50:40 -070029};
Vitaly Buka10c69ec2015-08-12 16:17:16 -070030
Vitaly Bukada987282015-09-23 16:50:40 -070031// Interface for async input streaming.
32class OutputStream {
33 public:
Vitaly Buka3bfb13d2015-11-24 14:46:13 -080034 virtual ~OutputStream() {}
Vitaly Bukada987282015-09-23 16:50:40 -070035
Vitaly Buka74763422015-10-11 00:39:52 -070036 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 Bukada987282015-09-23 16:50:40 -070041 // Success callback must be called only after all data is written.
Vitaly Buka1fd619a2015-09-24 11:46:05 -070042 virtual void Write(const void* buffer,
43 size_t size_to_write,
Vitaly Buka74763422015-10-11 00:39:52 -070044 const WriteCallback& callback) = 0;
Vitaly Bukada987282015-09-23 16:50:40 -070045};
Vitaly Buka10c69ec2015-08-12 16:17:16 -070046
Vitaly Bukada987282015-09-23 16:50:40 -070047// Interface for async bi-directional streaming.
48class Stream : public InputStream, public OutputStream {
49 public:
Vitaly Buka3bfb13d2015-11-24 14:46:13 -080050 ~Stream() override {}
Vitaly Bukada987282015-09-23 16:50:40 -070051
52 // Cancels all pending read or write requests. Canceled operations must not
53 // call any callbacks.
54 virtual void CancelPendingOperations() = 0;
Vitaly Buka10c69ec2015-08-12 16:17:16 -070055};
56
57} // namespace weave
58
59#endif // LIBWEAVE_INCLUDE_WEAVE_STREAM_H_