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 "base/timer/timer.h" | |
| 20 #include "google/protobuf/io/zero_copy_stream.h" | |
| 21 #include "google_apis/gcm/base/gcm_export.h" | |
| 22 | |
| 23 namespace net { | |
| 24 class DrainableIOBuffer; | |
| 25 class IOBufferWithSize; | |
| 26 class StreamSocket; | |
| 27 } | |
| 28 | |
| 29 namespace gcm { | |
| 30 | |
| 31 class GCM_EXPORT SocketInputStream | |
| 32 : public google::protobuf::io::ZeroCopyInputStream { | |
| 33 public: | |
| 34 enum State { | |
| 35 // No valid data to read. This means the buffer is either empty or all data | |
| 36 // in the buffer has already been consumed. | |
| 37 EMPTY, | |
| 38 // Valid data to read. | |
| 39 READY, | |
| 40 // In the process of reading new data from the socket. | |
| 41 READING, | |
| 42 // An permanent error occurred and the stream is now closed. | |
| 43 CLOSED, | |
| 44 }; | |
| 45 | |
| 46 // |socket| should already be connected. | |
| 47 SocketInputStream(base::TimeDelta read_timeout, | |
| 48 net::StreamSocket* socket); | |
| 49 virtual ~SocketInputStream(); | |
| 50 | |
| 51 // ZeroCopyInputStream implementation. | |
| 52 // Note: the stream must be READY before attempting to read, although it's | |
| 53 // valid to read from an EMPTY stream if it's the last read of a message | |
| 54 // (see GetNextMessage below). | |
| 55 virtual bool Next(const void** data, int* size) OVERRIDE; | |
| 56 virtual void BackUp(int count) OVERRIDE; | |
| 57 virtual bool Skip(int count) OVERRIDE; | |
| 58 virtual int64 ByteCount() const OVERRIDE; | |
| 59 | |
| 60 // Reads and appends the next set of data from the socket into the existing | |
| 61 // buffer (max read size is dependent on available buffer space). | |
| 62 // This is useful to check if there is any pending socket data to be read, | |
| 63 // as the read does not time out, so the callback will only be invoked on | |
| 64 // an error or when data is received. | |
| 65 void Refresh(const base::Closure& callback); | |
| 66 | |
| 67 // Rebuilds the buffer state by copying over any unread data to the beginning | |
| 68 // of the buffer and zeroing the buffer read position. | |
| 69 // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream | |
| 70 // must be recreated from scratch in such a scenario. | |
| 71 void Rebuild(); | |
| 72 | |
| 73 // Asynchronously retrieves the |msg_size| bytes comprising a message, | |
| 74 // invoking the callback when complete or upon encountering an error. After a | |
| 75 // message is successfully received, Next(..) can be repeatedly invoked to | |
| 76 // retrieve the bytes of the message, after which it will return false | |
| 77 // (denoting that the message has been fully consumed). | |
| 78 // Note: The stream will remain valid after the message is consumed, but | |
| 79 // Rebuild() or another GetNextMessage(..) call must be made before data can | |
| 80 // be consumed again. | |
| 81 // Also note: GetNextMessage, unlike Refresh, does have a timeout if no | |
| 82 // data is received for too long. The timeout will close the stream and | |
|
Ryan Sleevi
2013/09/17 19:43:30
grammar nit: does have a timeout if data is not re
Nicolas Zea
2013/09/25 01:21:27
This functionality has been refactored out now
| |
| 83 // trigger the callback. | |
| 84 void GetNextMessage(int msg_size, const base::Closure& callback); | |
| 85 | |
| 86 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 87 int last_error() const; | |
| 88 | |
| 89 // Returns the current state. | |
| 90 State state() const; | |
| 91 | |
| 92 private: | |
| 93 // Callback for Socket::Read calls. | |
| 94 void RefreshCompletionCallback(const base::Closure& callback, int result); | |
| 95 | |
| 96 // Clears the local state. | |
| 97 void ResetInternal(); | |
| 98 | |
| 99 // Permanently closes the stream. | |
| 100 void CloseStream(int error, const base::Closure& callback); | |
| 101 | |
| 102 // Internal net components. | |
| 103 net::StreamSocket* socket_; | |
| 104 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
| 105 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
| 106 | |
| 107 // The amount of data within |io_buffer_| that has already been read. | |
| 108 int buffer_read_pos_; | |
| 109 | |
| 110 // The amount of data written within |io_buffer_|. | |
| 111 int buffer_write_pos_; | |
| 112 | |
| 113 // The |buffer_pos_| limit at which Next(..) will no longer trigger Refresh | |
| 114 // calls. | |
| 115 int limit_; | |
| 116 | |
| 117 // The number of bytes the stream has been backed up by. These bytes need to | |
| 118 // be returned on the subsequent Next(..) call. | |
| 119 // 0 <= backup_bytes_ <= buffer_pos_ | |
| 120 int backup_bytes_; | |
| 121 | |
| 122 // The number of bytes to skip from the next successful refresh. | |
| 123 int skipped_bytes_; | |
| 124 | |
| 125 // If < net::OK, the last net error received. | |
| 126 int last_error_; | |
| 127 | |
| 128 // The current state. | |
| 129 State state_; | |
| 130 | |
| 131 // Timeout when performing synchronous reads. | |
| 132 base::TimeDelta read_timeout_; | |
| 133 | |
| 134 base::OneShotTimer<SocketInputStream> read_timeout_timer_; | |
| 135 | |
| 136 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(SocketInputStream); | |
| 139 }; | |
| 140 | |
| 141 class GCM_EXPORT SocketOutputStream | |
| 142 : public google::protobuf::io::ZeroCopyOutputStream { | |
| 143 public: | |
| 144 enum State { | |
| 145 // No valid data yet. | |
| 146 EMPTY, | |
| 147 // Ready for flushing (some data is present). | |
| 148 READY, | |
| 149 // In the process of flushing into the socket. | |
| 150 FLUSHING, | |
| 151 // A permanent error occurred, and the stream is now closed. | |
| 152 CLOSED, | |
| 153 }; | |
| 154 | |
| 155 // |socket| should already be connected. | |
| 156 SocketOutputStream(net::StreamSocket* socket); | |
|
Ryan Sleevi
2013/09/17 19:43:30
style nit: explicit
Nicolas Zea
2013/09/25 01:21:27
Done.
| |
| 157 virtual ~SocketOutputStream(); | |
| 158 | |
| 159 // ZeroCopyOutputStream implementation. | |
| 160 virtual bool Next(void** data, int* size) OVERRIDE; | |
| 161 virtual void BackUp(int count) OVERRIDE; | |
| 162 virtual int64 ByteCount() const OVERRIDE; | |
| 163 | |
| 164 // Writes the buffer into the Socket. | |
| 165 void Flush(const base::Closure& callback); | |
| 166 | |
| 167 // Returns the current state. | |
| 168 State state() const; | |
| 169 | |
| 170 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
| 171 int last_error() const; | |
| 172 | |
| 173 private: | |
| 174 void FlushCompletionCallback(const base::Closure& callback, int result); | |
| 175 | |
| 176 net::StreamSocket* socket_; | |
| 177 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
| 178 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
| 179 | |
| 180 // The amount of valid buffer data. This is the amount of data written on the | |
| 181 // next call to Flush(). | |
| 182 int buffer_used_; | |
| 183 | |
| 184 // If < net::OK, the last net error received. | |
| 185 int last_error_; | |
| 186 | |
| 187 // The current state. | |
| 188 State state_; | |
| 189 | |
| 190 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_; | |
| 191 | |
| 192 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream); | |
| 193 }; | |
| 194 | |
| 195 } // namespace gcm | |
| 196 | |
| 197 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
| OLD | NEW |