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 the callback is invoked. It is invalid to | |
41 // attempt to Refresh or read data while another 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, | |
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 and appends a max of |byte_limit| bytes onto the read buffer. | |
75 // |callback| is invoked upon read completion, even if an error was | |
76 // encountered (state() and ByteCount() must be checked to see if forward | |
77 // progress was made). | |
78 void Refresh(const base::Closure& callback, int byte_limit); | |
79 | |
80 // Rebuilds the buffer state by copying over any unread data to the beginning | |
81 // of the buffer and resetting the buffer read/write positions. | |
82 // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream | |
83 // must be recreated from scratch in such a scenario. | |
84 void RebuildBuffer(); | |
85 | |
86 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
87 net::Error last_error() const; | |
88 | |
89 // Returns the current state. | |
90 State state() const; | |
91 | |
92 private: | |
93 // Clears the local state. | |
94 void ResetInternal(); | |
95 | |
96 // Callback for Socket::Read calls. | |
97 void RefreshCompletionCallback(const base::Closure& callback, int result); | |
98 | |
99 // Permanently closes the stream. | |
100 void CloseStream(net::Error error, const base::Closure& callback); | |
101 | |
102 // Internal net components. | |
103 net::StreamSocket* const socket_; | |
104 scoped_refptr<net::IOBuffer> io_buffer_; | |
akalin
2013/10/07 23:00:08
const scoped_refptr
Nicolas Zea
2013/10/09 00:44:22
Done.
| |
105 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
akalin
2013/10/07 23:00:08
const scoped_refptr
akalin
2013/10/07 23:00:08
can this be given a better name? read_buffer_ perh
Nicolas Zea
2013/10/09 00:44:22
Can't due to having to rebuild this periodically (
Nicolas Zea
2013/10/09 00:44:22
Done (and write_buffer_ in the output stream imple
| |
106 | |
107 // The amount of data within |io_buffer_| that has already been read. | |
108 int buffer_read_pos_; | |
akalin
2013/10/07 23:00:08
a bit confusing since this is separate from the of
Nicolas Zea
2013/10/09 00:44:22
Done.
| |
109 | |
110 // If < net::OK, the last net error received. | |
111 net::Error last_error_; | |
112 | |
113 // The current state. | |
114 State state_; | |
115 | |
116 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(SocketInputStream); | |
119 }; | |
120 | |
121 // A helper class for writing to a SocketStream with protobuf encoded data. | |
122 // A SocketOutputStream does not take ownership of the socket itself, and it is | |
123 // expected that the life of the output stream should match the life of the | |
124 // socket itself (while the socket remains connected). | |
125 // Typical usage: | |
126 // 1. Check the state() of the output stream before using it. If CLOSED, the | |
127 // output stream must be rebuilt (and the socket likely needs to be | |
128 // reconnected, as an error was encountered). | |
129 // 2. If EMPTY, the output stream can be written via a CodedOutputStream using | |
130 // the ZeroCopyOutputStream interface. | |
131 // 3. Once done writing, state() should be READY, so call Flush(..) to write | |
132 // the buffer into the StreamSocket. Wait for the callback to be invoked | |
133 // (it's invalid to write to an output stream while it's flushing). | |
134 // 4. Check the state() again to ensure the Flush was successful. state() should | |
135 // be EMPTY again. | |
136 // 5. Repeat. | |
137 class GCM_EXPORT SocketOutputStream | |
138 : public google::protobuf::io::ZeroCopyOutputStream { | |
139 public: | |
140 enum State { | |
141 // No valid data yet. | |
142 EMPTY, | |
143 // Ready for flushing (some data is present). | |
144 READY, | |
145 // In the process of flushing into the socket. | |
146 FLUSHING, | |
147 // A permanent error occurred, and the stream is now closed. | |
148 CLOSED, | |
149 }; | |
150 | |
151 // |socket| should already be connected. | |
152 explicit SocketOutputStream(net::StreamSocket* socket); | |
153 virtual ~SocketOutputStream(); | |
154 | |
155 // ZeroCopyOutputStream implementation. | |
156 virtual bool Next(void** data, int* size) OVERRIDE; | |
157 virtual void BackUp(int count) OVERRIDE; | |
158 virtual int64 ByteCount() const OVERRIDE; | |
159 | |
160 // Writes the buffer into the Socket. | |
161 void Flush(const base::Closure& callback); | |
162 | |
163 // Returns the current state. | |
164 State state() const; | |
165 | |
166 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
167 net::Error last_error() const; | |
168 | |
169 private: | |
170 void FlushCompletionCallback(const base::Closure& callback, int result); | |
171 | |
172 // Internal net components. | |
173 net::StreamSocket* const socket_; | |
174 scoped_refptr<net::IOBuffer> io_buffer_; | |
175 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
176 | |
177 // The amount of valid buffer data. This is the amount of data written on the | |
178 // next call to Flush(). | |
179 int buffer_used_; | |
180 | |
181 // If < net::OK, the last net error received. | |
182 net::Error last_error_; | |
183 | |
184 // The current state. | |
185 State state_; | |
186 | |
187 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream); | |
190 }; | |
191 | |
192 } // namespace gcm | |
193 | |
194 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_ | |
OLD | NEW |