// Copyright 2013 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Protobuf ZeroCopy[Input/Output]Stream implementations capable of using // mojo data pipes. Built to work with Protobuf CodedStreams. #ifndef GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ #define GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ #include <stdint.h> #include "base/compiler_specific.h" #include "base/functional/callback.h" #include "base/functional/callback_forward.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "google/protobuf/io/zero_copy_stream.h" #include "google_apis/gcm/base/gcm_export.h" #include "mojo/public/cpp/system/data_pipe.h" #include "mojo/public/cpp/system/simple_watcher.h" #include "net/base/net_errors.h" namespace net { class DrainableIOBuffer; class IOBuffer; class IOBufferWithSize; } // namespace net namespace gcm { // A helper class for interacting with a mojo consumer pipe that is receiving // protobuf encoded messages. If an error is encounters, the input stream will // store the error in |last_error_|, and GetState() will be set to CLOSED. // Typical usage: // 1. Check the GetState() of the input stream before using it. If CLOSED, the // input stream must be rebuilt (and the socket likely needs to be // reconnected as an error was encountered). // 2. If GetState() is EMPTY, call Refresh(..), passing the maximum byte size // for a message, and wait until completion. It is invalid to attempt to // Refresh an input stream or read data from the stream while a Refresh is // pending. // 3. Check GetState() again to ensure the Refresh was successful. // 4. Use a CodedInputStream to read from the ZeroCopyInputStream interface of // the SocketInputStream. Next(..) will return true until there is no data // remaining. // 5. Call RebuildBuffer when done reading, to shift any unread data to the // start of the buffer. // 6. Repeat as necessary. class GCM_EXPORT SocketInputStream : public google::protobuf::io::ZeroCopyInputStream { … }; // A helper class for writing to a mojo producer handle with protobuf encoded // data. Typical usage: // 1. Check the GetState() of the output stream before using it. If CLOSED, the // output stream must be rebuilt (and the socket likely needs to be // reconnected, as an error was encountered). // 2. If EMPTY, the output stream can be written via a CodedOutputStream using // the ZeroCopyOutputStream interface. // 3. Once done writing, GetState() should be READY, so call Flush(..) to write // the buffer into the mojo producer handle. Wait for the callback to be // invoked (it's invalid to write to an output stream while it's flushing). // 4. Check the GetState() again to ensure the Flush was successful. GetState() // should be EMPTY again. // 5. Repeat. class GCM_EXPORT SocketOutputStream : public google::protobuf::io::ZeroCopyOutputStream { … }; } // namespace gcm #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_