Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: google_apis/gcm/base/socket_stream.h

Issue 23684017: [GCM] Initial work to set up directory structure and introduce socket integration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
20 namespace net {
21 class DrainableIOBuffer;
22 class IOBufferWithSize;
23 class StreamSocket;
24 }
akalin 2013/10/04 18:37:04 // namespace net
Nicolas Zea 2013/10/04 20:55:28 Done.
25
26 namespace gcm {
27
28 // A helper class for interacting with a net::StreamSocket that is receiving
29 // protobuf encoded messages. A SocketInputStream does not take ownership of
30 // the socket itself, and it is expected that the life of the input stream
31 // should match the life of the socket itself (while the socket remains
32 // connected). If an error is encounters, the input stream will store the error
33 // input last_error_, and state will be set to CLOSED.
akalin 2013/10/04 18:37:04 input last_error_ -> in |last_error_| state -> the
Nicolas Zea 2013/10/04 20:55:28 Done.
34 // Typical usage:
35 // 1. Check the state() of the input stream before using it. If CLOSED, the
36 // input stream must be rebuilt (and the socket likely needs to be
37 // reconnected as an error was encountered).
38 // 2. If state() is EMPTY, call Refresh(..), passing the maximum byte size for
39 // a message, and wait until the callback is invoked. It is invalid to
40 // attempt to Refresh or read data while another Refresh is pending.
41 // 3. Check state() again to ensure the Refresh was successful.
42 // 4. Use a CodedInputStream to read from the ZeroCopyInputStream interface of
43 // the SocketInputStream. Next(..) will return true until there is no data
44 // remaining.
45 // 5. Call RebuildBuffer when done reading, to shift any unread data to the
46 // start of the buffer.
47 // 6. Repeat as necessary.
48 class GCM_EXPORT SocketInputStream
49 : public google::protobuf::io::ZeroCopyInputStream {
50 public:
51 enum State {
52 // No valid data to read. This means the buffer is either empty or all data
53 // in the buffer has already been consumed.
54 EMPTY,
55 // Valid data to read.
56 READY,
57 // In the process of reading new data from the socket.
58 READING,
59 // An permanent error occurred and the stream is now closed.
60 CLOSED,
61 };
62
63 // |socket| should already be connected.
64 explicit SocketInputStream(net::StreamSocket* socket);
65 virtual ~SocketInputStream();
66
67 // ZeroCopyInputStream implementation.
68 virtual bool Next(const void** data, int* size) OVERRIDE;
69 virtual void BackUp(int count) OVERRIDE;
70 virtual bool Skip(int count) OVERRIDE;
71 virtual int64 ByteCount() const OVERRIDE;
72
73 // Reads and appends a max of |byte_limit| bytes onto the read buffer.
74 // |callback| is invoked upon read completion, even if an error was
75 // encountered (state() and ByteCount() must be checked to see if forward
76 // progress was made).
77 void Refresh(const base::Closure& callback, int byte_limit);
akalin 2013/10/04 18:37:04 so |callback| can be invoked directly by Refresh,
Nicolas Zea 2013/10/04 20:55:28 I've gone ahead and made it so synchronous callbac
78
79 // Rebuilds the buffer state by copying over any unread data to the beginning
80 // of the buffer and resetting the buffer read/write positions.
81 // Note: it is not valid to call Rebuild() if state() == CLOSED. The stream
82 // must be recreated from scratch in such a scenario.
83 void RebuildBuffer();
84
85 // Returns the last fatal error encountered. Only valid if state() == CLOSED.
86 int last_error() const;
akalin 2013/10/04 18:37:04 net::Error?
Nicolas Zea 2013/10/04 20:55:28 Done.
87
88 // Returns the current state.
89 State state() const;
90
91 private:
92 // Clears the local state.
93 void ResetInternal();
94
95 // Callback for Socket::Read calls.
96 void RefreshCompletionCallback(const base::Closure& callback, int result);
97
98 // Permanently closes the stream.
99 void CloseStream(int error, const base::Closure& callback);
100
101 // Internal net components.
102 net::StreamSocket* socket_;
akalin 2013/10/04 18:37:04 * const socket_?
Nicolas Zea 2013/10/04 20:55:28 Done.
103 scoped_refptr<net::IOBufferWithSize> io_buffer_;
akalin 2013/10/04 18:37:04 since drainable_io_buffer_ stores the size already
Nicolas Zea 2013/10/04 20:55:28 Done.
104 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_;
akalin 2013/10/04 18:37:04 do we need this to be a member variable? Seems to
Nicolas Zea 2013/10/04 20:55:28 The offset is actually buffer_write_pos_. You're r
105
106 // The amount of data within |io_buffer_| that has already been read.
107 int buffer_read_pos_;
108
109 // The amount of data written within |io_buffer_|.
110 int buffer_write_pos_;
akalin 2013/10/04 18:37:04 the name suggests that it's analogous to buffer_re
Nicolas Zea 2013/10/04 20:55:28 Removed in favor of reusing DrainableIOBuffer::Byt
111
112 // The number of bytes the stream has been backed up by. These bytes need to
113 // be returned on the subsequent Next(..) call.
114 // 0 <= backup_bytes_ <= buffer_pos_
115 int backup_bytes_;
akalin 2013/10/04 18:37:04 https://developers.google.com/protocol-buffers/doc
akalin 2013/10/04 18:37:04 is it possible to omit backup_bytes_ entirely and
Nicolas Zea 2013/10/04 20:55:28 Good point. It seemed easier at dev time to track
116
117 // The number of bytes to skip from the next successful refresh.
118 int skipped_bytes_;
akalin 2013/10/04 18:37:04 i suppose we're assuming it's not true that both B
Nicolas Zea 2013/10/04 20:55:28 Fair enough. I had originally written this to supp
119
120 // If < net::OK, the last net error received.
121 int last_error_;
akalin 2013/10/04 18:37:04 net::Error?
Nicolas Zea 2013/10/04 20:55:28 Done.
122
123 // The current state.
124 State state_;
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 state() 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, state() 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 state() again to ensure the Flush was successful. state() should
145 // 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 void Flush(const base::Closure& callback);
172
173 // Returns the current state.
174 State state() const;
175
176 // Returns the last fatal error encountered. Only valid if state() == CLOSED.
177 int last_error() const;
akalin 2013/10/04 18:37:04 net::Error
Nicolas Zea 2013/10/04 20:55:28 Done.
178
179 private:
180 void FlushCompletionCallback(const base::Closure& callback, int result);
181
182 // Internal net components.
183 net::StreamSocket* socket_;
184 scoped_refptr<net::IOBufferWithSize> io_buffer_;
akalin 2013/10/04 18:37:04 IOBufferWithSize -> IOBuffer
Nicolas Zea 2013/10/04 20:55:28 Done.
185 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_;
186
187 // The amount of valid buffer data. This is the amount of data written on the
188 // next call to Flush().
189 int buffer_used_;
190
191 // If < net::OK, the last net error received.
192 int last_error_;
akalin 2013/10/04 18:37:04 net::Error?
Nicolas Zea 2013/10/04 20:55:28 Done.
193
194 // The current state.
195 State state_;
196
197 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_;
198
199 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream);
200 };
201
202 } // namespace gcm
203
204 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698