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