Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium 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 // Protobuf ZeroCopy[Input/Output]Stream implementations capable of using a | |
| 6 // net::StreamSocket. Built to work with Protobuf CodedStreams. | |
| 7 | |
| 8 #ifndef GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| 9 #define GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/compiler_specific.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "google/protobuf/io/zero_copy_stream.h" | |
| 18 #include "google_apis/gcm/base/gcm_export.h" | |
| 19 #include "net/base/net_errors.h" | |
| 20 | |
| 21 namespace net { | |
| 22 class DrainableIOBuffer; | |
| 23 class IOBuffer; | |
| 24 class StreamSocket; | |
| 25 } // namespace net | |
| 26 | |
| 27 namespace gcm { | |
| 28 | |
| 29 // A helper class for interacting with a net::StreamSocket that is receiving | |
| 30 // protobuf encoded messages. A SocketInputStream does not take ownership of | |
| 31 // the socket itself, and it is expected that the life of the input stream | |
| 32 // should match the life of the socket itself (while the socket remains | |
| 33 // connected). If an error is encounters, the input stream will store the error | |
| 34 // in |last_error_|, and state() will be set to CLOSED. | |
| 35 // Typical usage: | |
| 36 // 1. Check the state() of the input stream before using it. If CLOSED, the | |
| 37 // input stream must be rebuilt (and the socket likely needs to be | |
| 38 // reconnected as an error was encountered). | |
| 39 // 2. If state() is EMPTY, call Refresh(..), passing the maximum byte size for | |
| 40 // a message, and wait until completion. It is invalid to attempt to Refresh | |
| 41 // an input stream or read data from the stream while a Refresh is pending. | |
| 42 // 3. Check state() again to ensure the Refresh was successful. | |
| 43 // 4. Use a CodedInputStream to read from the ZeroCopyInputStream interface of | |
| 44 // the SocketInputStream. Next(..) will return true until there is no data | |
| 45 // remaining. | |
| 46 // 5. Call RebuildBuffer when done reading, to shift any unread data to the | |
| 47 // start of the buffer. | |
| 48 // 6. Repeat as necessary. | |
| 49 class GCM_EXPORT SocketInputStream | |
| 50 : public google::protobuf::io::ZeroCopyInputStream { | |
| 51 public: | |
| 52 enum State { | |
| 53 // No valid data to read. This means the buffer is either empty or all data | |
| 54 // in the buffer has already been consumed. | |
| 55 EMPTY, | |
|
akalin
2013/10/11 16:47:22
I'm a fan of having as few states in a state machi
Nicolas Zea
2013/10/11 18:28:32
Given that the states are used as a method of expo
akalin
2013/10/11 18:47:09
Okay, I see. My main concern is the possibility of
Nicolas Zea
2013/10/11 23:41:29
Done.
| |
| 56 // Valid data to read. | |
| 57 READY, | |
| 58 // In the process of reading new data from the socket. | |
| 59 READING, | |
| 60 // An permanent error occurred and the stream is now closed. | |
| 61 CLOSED, | |
| 62 }; | |
| 63 | |
| 64 // |socket| should already be connected. | |
| 65 explicit SocketInputStream(net::StreamSocket* socket); | |
| 66 virtual ~SocketInputStream(); | |
| 67 | |
| 68 // ZeroCopyInputStream implementation. | |
| 69 virtual bool Next(const void** data, int* size) OVERRIDE; | |
| 70 virtual void BackUp(int count) OVERRIDE; | |
| 71 virtual bool Skip(int count) OVERRIDE; // Not implemented. | |
| 72 virtual int64 ByteCount() const OVERRIDE; | |
| 73 | |
| 74 // Reads from the socket, appending a max of |byte_limit| bytes onto the read | |
| 75 // buffer. net::ERR_IO_PENDING is returned if the refresh can't complete | |
| 76 // synchronously, in which case the callback is invoked upon completion. If | |
| 77 // the refresh can complete synchronously returns net::OK (without invoking | |
| 78 // callback). | |
| 79 // Note: state() (and possibly last_error()) should be checked upon completion | |
| 80 // to determine whether the Refresh encountered an error. | |
| 81 net::Error Refresh(const base::Closure& callback, int byte_limit); | |
| 82 | |
| 83 // Rebuilds the buffer state by copying over any unread data to the beginning | |
| 84 // of the buffer and resetting the buffer read/write positions. | |
| 85 // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream | |
| 86 // must be recreated from scratch in such a scenario. | |
| 87 void RebuildBuffer(); | |
| 88 | |
| 89 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 90 net::Error last_error() const; | |
| 91 | |
| 92 // Returns the current state. | |
| 93 State state() const; | |
| 94 | |
| 95 private: | |
| 96 // Clears the local state. | |
| 97 void ResetInternal(); | |
| 98 | |
| 99 // Callback for Socket::Read calls. | |
| 100 void RefreshCompletionCallback(const base::Closure& callback, int result); | |
| 101 | |
| 102 // Permanently closes the stream. | |
| 103 void CloseStream(net::Error error, const base::Closure& callback); | |
| 104 | |
| 105 // Internal net components. | |
| 106 net::StreamSocket* const socket_; | |
| 107 const scoped_refptr<net::IOBuffer> io_buffer_; | |
| 108 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't | |
| 109 // been written to yet by Socket::Read calls. | |
| 110 const scoped_refptr<net::DrainableIOBuffer> read_buffer_; | |
| 111 | |
| 112 // Starting position of the data within |io_buffer_| to consume on subsequent | |
| 113 // Next(..) call. 0 <= next_pos_ <= read_buffer_.BytesConsumed() | |
| 114 // Note: next_pos == read_buffer_.BytesConsumed() implies state_ == EMPTY. | |
| 115 int next_pos_; | |
| 116 | |
| 117 // If < net::OK, the last net error received. | |
| 118 net::Error last_error_; | |
| 119 | |
| 120 // The current state. | |
| 121 State state_; | |
| 122 | |
| 123 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(SocketInputStream); | |
| 126 }; | |
| 127 | |
| 128 // A helper class for writing to a SocketStream with protobuf encoded data. | |
| 129 // A SocketOutputStream does not take ownership of the socket itself, and it is | |
| 130 // expected that the life of the output stream should match the life of the | |
| 131 // socket itself (while the socket remains connected). | |
| 132 // Typical usage: | |
| 133 // 1. Check the state() of the output stream before using it. If CLOSED, the | |
| 134 // output stream must be rebuilt (and the socket likely needs to be | |
| 135 // reconnected, as an error was encountered). | |
| 136 // 2. If EMPTY, the output stream can be written via a CodedOutputStream using | |
| 137 // the ZeroCopyOutputStream interface. | |
| 138 // 3. Once done writing, state() should be READY, so call Flush(..) to write | |
| 139 // the buffer into the StreamSocket. Wait for the callback to be invoked | |
| 140 // (it's invalid to write to an output stream while it's flushing). | |
| 141 // 4. Check the state() again to ensure the Flush was successful. state() should | |
| 142 // be EMPTY again. | |
| 143 // 5. Repeat. | |
| 144 class GCM_EXPORT SocketOutputStream | |
| 145 : public google::protobuf::io::ZeroCopyOutputStream { | |
| 146 public: | |
| 147 enum State { | |
| 148 // No valid data yet. | |
| 149 EMPTY, | |
| 150 // Ready for flushing (some data is present). | |
| 151 READY, | |
| 152 // In the process of flushing into the socket. | |
| 153 FLUSHING, | |
| 154 // A permanent error occurred, and the stream is now closed. | |
| 155 CLOSED, | |
| 156 }; | |
| 157 | |
| 158 // |socket| should already be connected. | |
| 159 explicit SocketOutputStream(net::StreamSocket* socket); | |
| 160 virtual ~SocketOutputStream(); | |
| 161 | |
| 162 // ZeroCopyOutputStream implementation. | |
| 163 virtual bool Next(void** data, int* size) OVERRIDE; | |
| 164 virtual void BackUp(int count) OVERRIDE; | |
| 165 virtual int64 ByteCount() const OVERRIDE; | |
| 166 | |
| 167 // Writes the buffer into the Socket. | |
| 168 void Flush(const base::Closure& callback); | |
| 169 | |
| 170 // Returns the current state. | |
| 171 State state() const; | |
| 172 | |
| 173 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 174 net::Error last_error() const; | |
| 175 | |
| 176 private: | |
| 177 void FlushCompletionCallback(const base::Closure& callback, int result); | |
| 178 | |
| 179 // Internal net components. | |
| 180 net::StreamSocket* const socket_; | |
| 181 const scoped_refptr<net::IOBuffer> io_buffer_; | |
| 182 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't | |
| 183 // been written to the socket yet. | |
| 184 const scoped_refptr<net::DrainableIOBuffer> write_buffer_; | |
| 185 | |
| 186 // The amount of valid buffer data. This is the amount of data written on the | |
| 187 // next call to Flush(). | |
| 188 int buffer_used_; | |
| 189 | |
| 190 // If < net::OK, the last net error received. | |
| 191 net::Error last_error_; | |
| 192 | |
| 193 // The current state. | |
| 194 State state_; | |
| 195 | |
| 196 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_; | |
| 197 | |
| 198 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream); | |
| 199 }; | |
| 200 | |
| 201 } // namespace gcm | |
| 202 | |
| 203 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| OLD | NEW |