Index: google_apis/gcm/base/socket_stream.h |
diff --git a/google_apis/gcm/base/socket_stream.h b/google_apis/gcm/base/socket_stream.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..266a626a9bbbb2537239fbd1394f562e221f615f |
--- /dev/null |
+++ b/google_apis/gcm/base/socket_stream.h |
@@ -0,0 +1,204 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// 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 a |
+// net::StreamSocket. Built to work with Protobuf CodedStreams. |
+ |
+#ifndef GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ |
+#define GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ |
+ |
+#include "base/basictypes.h" |
+#include "base/callback_forward.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "google/protobuf/io/zero_copy_stream.h" |
+#include "google_apis/gcm/base/gcm_export.h" |
+ |
+namespace net { |
+class DrainableIOBuffer; |
+class IOBufferWithSize; |
+class StreamSocket; |
+} |
akalin
2013/10/04 18:37:04
// namespace net
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ |
+namespace gcm { |
+ |
+// A helper class for interacting with a net::StreamSocket that is receiving |
+// protobuf encoded messages. A SocketInputStream does not take ownership of |
+// the socket itself, and it is expected that the life of the input stream |
+// should match the life of the socket itself (while the socket remains |
+// connected). If an error is encounters, the input stream will store the error |
+// input last_error_, and state will be set to CLOSED. |
akalin
2013/10/04 18:37:04
input last_error_ -> in |last_error_|
state -> the
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+// Typical usage: |
+// 1. Check the state() 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 state() is EMPTY, call Refresh(..), passing the maximum byte size for |
+// a message, and wait until the callback is invoked. It is invalid to |
+// attempt to Refresh or read data while another Refresh is pending. |
+// 3. Check state() 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 { |
+ public: |
+ enum State { |
+ // No valid data to read. This means the buffer is either empty or all data |
+ // in the buffer has already been consumed. |
+ EMPTY, |
+ // Valid data to read. |
+ READY, |
+ // In the process of reading new data from the socket. |
+ READING, |
+ // An permanent error occurred and the stream is now closed. |
+ CLOSED, |
+ }; |
+ |
+ // |socket| should already be connected. |
+ explicit SocketInputStream(net::StreamSocket* socket); |
+ virtual ~SocketInputStream(); |
+ |
+ // ZeroCopyInputStream implementation. |
+ virtual bool Next(const void** data, int* size) OVERRIDE; |
+ virtual void BackUp(int count) OVERRIDE; |
+ virtual bool Skip(int count) OVERRIDE; |
+ virtual int64 ByteCount() const OVERRIDE; |
+ |
+ // Reads and appends a max of |byte_limit| bytes onto the read buffer. |
+ // |callback| is invoked upon read completion, even if an error was |
+ // encountered (state() and ByteCount() must be checked to see if forward |
+ // progress was made). |
+ void Refresh(const base::Closure& callback, int byte_limit); |
akalin
2013/10/04 18:37:04
so |callback| can be invoked directly by Refresh,
Nicolas Zea
2013/10/04 20:55:28
I've gone ahead and made it so synchronous callbac
|
+ |
+ // Rebuilds the buffer state by copying over any unread data to the beginning |
+ // of the buffer and resetting the buffer read/write positions. |
+ // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream |
+ // must be recreated from scratch in such a scenario. |
+ void RebuildBuffer(); |
+ |
+ // Returns the last fatal error encountered. Only valid if state() == CLOSED. |
+ int last_error() const; |
akalin
2013/10/04 18:37:04
net::Error?
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ |
+ // Returns the current state. |
+ State state() const; |
+ |
+ private: |
+ // Clears the local state. |
+ void ResetInternal(); |
+ |
+ // Callback for Socket::Read calls. |
+ void RefreshCompletionCallback(const base::Closure& callback, int result); |
+ |
+ // Permanently closes the stream. |
+ void CloseStream(int error, const base::Closure& callback); |
+ |
+ // Internal net components. |
+ net::StreamSocket* socket_; |
akalin
2013/10/04 18:37:04
* const socket_?
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ scoped_refptr<net::IOBufferWithSize> io_buffer_; |
akalin
2013/10/04 18:37:04
since drainable_io_buffer_ stores the size already
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; |
akalin
2013/10/04 18:37:04
do we need this to be a member variable? Seems to
Nicolas Zea
2013/10/04 20:55:28
The offset is actually buffer_write_pos_. You're r
|
+ |
+ // The amount of data within |io_buffer_| that has already been read. |
+ int buffer_read_pos_; |
+ |
+ // The amount of data written within |io_buffer_|. |
+ int buffer_write_pos_; |
akalin
2013/10/04 18:37:04
the name suggests that it's analogous to buffer_re
Nicolas Zea
2013/10/04 20:55:28
Removed in favor of reusing DrainableIOBuffer::Byt
|
+ |
+ // The number of bytes the stream has been backed up by. These bytes need to |
+ // be returned on the subsequent Next(..) call. |
+ // 0 <= backup_bytes_ <= buffer_pos_ |
+ int backup_bytes_; |
akalin
2013/10/04 18:37:04
https://developers.google.com/protocol-buffers/doc
akalin
2013/10/04 18:37:04
is it possible to omit backup_bytes_ entirely and
Nicolas Zea
2013/10/04 20:55:28
Good point. It seemed easier at dev time to track
|
+ |
+ // The number of bytes to skip from the next successful refresh. |
+ int skipped_bytes_; |
akalin
2013/10/04 18:37:04
i suppose we're assuming it's not true that both B
Nicolas Zea
2013/10/04 20:55:28
Fair enough. I had originally written this to supp
|
+ |
+ // If < net::OK, the last net error received. |
+ int last_error_; |
akalin
2013/10/04 18:37:04
net::Error?
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ |
+ // The current state. |
+ State state_; |
+ |
+ base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SocketInputStream); |
+}; |
+ |
+// A helper class for writing to a SocketStream with protobuf encoded data. |
+// A SocketOutputStream does not take ownership of the socket itself, and it is |
+// expected that the life of the output stream should match the life of the |
+// socket itself (while the socket remains connected). |
+// Typical usage: |
+// 1. Check the state() 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, state() should be READY, so call Flush(..) to write |
+// the buffer into the StreamSocket. Wait for the callback to be invoked |
+// (it's invalid to write to an output stream while it's flushing). |
+// 4. Check the state() again to ensure the Flush was successful. state() should |
+// be EMPTY again. |
+// 5. Repeat. |
+class GCM_EXPORT SocketOutputStream |
+ : public google::protobuf::io::ZeroCopyOutputStream { |
+ public: |
+ enum State { |
+ // No valid data yet. |
+ EMPTY, |
+ // Ready for flushing (some data is present). |
+ READY, |
+ // In the process of flushing into the socket. |
+ FLUSHING, |
+ // A permanent error occurred, and the stream is now closed. |
+ CLOSED, |
+ }; |
+ |
+ // |socket| should already be connected. |
+ explicit SocketOutputStream(net::StreamSocket* socket); |
+ virtual ~SocketOutputStream(); |
+ |
+ // ZeroCopyOutputStream implementation. |
+ virtual bool Next(void** data, int* size) OVERRIDE; |
+ virtual void BackUp(int count) OVERRIDE; |
+ virtual int64 ByteCount() const OVERRIDE; |
+ |
+ // Writes the buffer into the Socket. |
+ void Flush(const base::Closure& callback); |
+ |
+ // Returns the current state. |
+ State state() const; |
+ |
+ // Returns the last fatal error encountered. Only valid if state() == CLOSED. |
+ int last_error() const; |
akalin
2013/10/04 18:37:04
net::Error
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ |
+ private: |
+ void FlushCompletionCallback(const base::Closure& callback, int result); |
+ |
+ // Internal net components. |
+ net::StreamSocket* socket_; |
+ scoped_refptr<net::IOBufferWithSize> io_buffer_; |
akalin
2013/10/04 18:37:04
IOBufferWithSize -> IOBuffer
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; |
+ |
+ // The amount of valid buffer data. This is the amount of data written on the |
+ // next call to Flush(). |
+ int buffer_used_; |
+ |
+ // If < net::OK, the last net error received. |
+ int last_error_; |
akalin
2013/10/04 18:37:04
net::Error?
Nicolas Zea
2013/10/04 20:55:28
Done.
|
+ |
+ // The current state. |
+ State state_; |
+ |
+ base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SocketOutputStream); |
+}; |
+ |
+} // namespace gcm |
+ |
+#endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ |