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 // Note that any caller must first check the state() of the stream before | |
| 8 // using it. It is not valid to use a CLOSED stream. | |
| 9 | |
| 10 #ifndef GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| 11 #define GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/callback_forward.h" | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/weak_ptr.h" | |
| 19 #include "google/protobuf/io/zero_copy_stream.h" | |
| 20 #include "google_apis/gcm/base/gcm_export.h" | |
| 21 | |
| 22 namespace net { | |
| 23 class DrainableIOBuffer; | |
| 24 class IOBufferWithSize; | |
| 25 class StreamSocket; | |
| 26 } | |
| 27 | |
| 28 namespace gcm { | |
| 29 | |
| 30 class GCM_EXPORT SocketInputStream | |
|
Ryan Sleevi
2013/09/25 23:30:24
style nit: Can you provide a brief description of
Nicolas Zea
2013/10/02 22:56:03
Added better comments to both input and output str
| |
| 31 : public google::protobuf::io::ZeroCopyInputStream { | |
| 32 public: | |
| 33 enum State { | |
| 34 // No valid data to read. This means the buffer is either empty or all data | |
| 35 // in the buffer has already been consumed. | |
| 36 EMPTY, | |
| 37 // Valid data to read. | |
| 38 READY, | |
| 39 // In the process of reading new data from the socket. | |
| 40 READING, | |
| 41 // An permanent error occurred and the stream is now closed. | |
| 42 CLOSED, | |
| 43 }; | |
| 44 | |
| 45 // |socket| should already be connected. | |
| 46 explicit SocketInputStream(net::StreamSocket* socket); | |
| 47 virtual ~SocketInputStream(); | |
| 48 | |
| 49 // ZeroCopyInputStream implementation. | |
| 50 virtual bool Next(const void** data, int* size) OVERRIDE; | |
| 51 virtual void BackUp(int count) OVERRIDE; | |
| 52 virtual bool Skip(int count) OVERRIDE; | |
| 53 virtual int64 ByteCount() const OVERRIDE; | |
| 54 | |
| 55 // Reads and appends a max of |byte_limit| bytes onto the read buffer. | |
| 56 // |callback| is invoked upon read completion, even if an error was | |
| 57 // encountered (state() and ByteCount() must be checked to see if forward | |
| 58 // progress was made). | |
| 59 void Refresh(const base::Closure& callback, int byte_limit); | |
| 60 | |
| 61 // Rebuilds the buffer state by copying over any unread data to the beginning | |
| 62 // of the buffer and resetting the buffer read/write positions. | |
| 63 // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream | |
| 64 // must be recreated from scratch in such a scenario. | |
| 65 void RebuildBuffer(); | |
| 66 | |
| 67 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 68 int last_error() const; | |
| 69 | |
| 70 // Returns the current state. | |
| 71 State state() const; | |
| 72 | |
| 73 private: | |
| 74 // Clears the local state. | |
| 75 void ResetInternal(); | |
| 76 | |
| 77 // Callback for Socket::Read calls. | |
| 78 void RefreshCompletionCallback(const base::Closure& callback, int result); | |
| 79 | |
| 80 // Permanently closes the stream. | |
| 81 void CloseStream(int error, const base::Closure& callback); | |
| 82 | |
| 83 // Internal net components. | |
| 84 net::StreamSocket* socket_; | |
| 85 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
| 86 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
| 87 | |
| 88 // The amount of data within |io_buffer_| that has already been read. | |
| 89 int buffer_read_pos_; | |
| 90 | |
| 91 // The amount of data written within |io_buffer_|. | |
| 92 int buffer_write_pos_; | |
| 93 | |
| 94 // The number of bytes the stream has been backed up by. These bytes need to | |
| 95 // be returned on the subsequent Next(..) call. | |
| 96 // 0 <= backup_bytes_ <= buffer_pos_ | |
| 97 int backup_bytes_; | |
| 98 | |
| 99 // The number of bytes to skip from the next successful refresh. | |
| 100 int skipped_bytes_; | |
| 101 | |
| 102 // If < net::OK, the last net error received. | |
| 103 int last_error_; | |
| 104 | |
| 105 // The current state. | |
| 106 State state_; | |
| 107 | |
| 108 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(SocketInputStream); | |
| 111 }; | |
| 112 | |
| 113 class GCM_EXPORT SocketOutputStream | |
| 114 : public google::protobuf::io::ZeroCopyOutputStream { | |
| 115 public: | |
| 116 enum State { | |
| 117 // No valid data yet. | |
| 118 EMPTY, | |
| 119 // Ready for flushing (some data is present). | |
| 120 READY, | |
| 121 // In the process of flushing into the socket. | |
| 122 FLUSHING, | |
| 123 // A permanent error occurred, and the stream is now closed. | |
| 124 CLOSED, | |
| 125 }; | |
| 126 | |
| 127 // |socket| should already be connected. | |
| 128 explicit SocketOutputStream(net::StreamSocket* socket); | |
| 129 virtual ~SocketOutputStream(); | |
| 130 | |
| 131 // ZeroCopyOutputStream implementation. | |
| 132 virtual bool Next(void** data, int* size) OVERRIDE; | |
| 133 virtual void BackUp(int count) OVERRIDE; | |
| 134 virtual int64 ByteCount() const OVERRIDE; | |
| 135 | |
| 136 // Writes the buffer into the Socket. | |
| 137 void Flush(const base::Closure& callback); | |
| 138 | |
| 139 // Returns the current state. | |
| 140 State state() const; | |
| 141 | |
| 142 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 143 int last_error() const; | |
| 144 | |
| 145 private: | |
| 146 void FlushCompletionCallback(const base::Closure& callback, int result); | |
| 147 | |
| 148 // Internal net components. | |
| 149 net::StreamSocket* socket_; | |
| 150 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
| 151 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
| 152 | |
| 153 // The amount of valid buffer data. This is the amount of data written on the | |
| 154 // next call to Flush(). | |
| 155 int buffer_used_; | |
| 156 | |
| 157 // If < net::OK, the last net error received. | |
| 158 int last_error_; | |
| 159 | |
| 160 // The current state. | |
| 161 State state_; | |
| 162 | |
| 163 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_; | |
| 164 | |
| 165 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream); | |
| 166 }; | |
| 167 | |
| 168 } // namespace gcm | |
| 169 | |
| 170 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| OLD | NEW |