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

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: Use SetOffset instead of recreating DrainableIOBuffer 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 #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;
akalin 2013/10/10 08:47:40 just checking, you can't also just make BackUp not
Nicolas Zea 2013/10/11 01:14:30 No, it's necessary. CodedInputStream will consume
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 const scoped_refptr<net::IOBuffer> io_buffer_;
105 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't
106 // been written to yet by Socket::Read calls.
107 const scoped_refptr<net::DrainableIOBuffer> read_buffer_;
108
109 // Starting position of the data within |io_buffer_| to consume on subsequent
110 // Next(..) call.
akalin 2013/10/10 08:47:40 i'd also comment that 0 <= next_pos_ < read_buffer
Nicolas Zea 2013/10/11 01:14:30 Technically it's <= BytesConsumed (when there's no
111 int next_pos_;
112
113 // If < net::OK, the last net error received.
114 net::Error last_error_;
115
116 // The current state.
117 State state_;
118
119 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_;
120
121 DISALLOW_COPY_AND_ASSIGN(SocketInputStream);
122 };
123
124 // A helper class for writing to a SocketStream with protobuf encoded data.
125 // A SocketOutputStream does not take ownership of the socket itself, and it is
126 // expected that the life of the output stream should match the life of the
127 // socket itself (while the socket remains connected).
128 // Typical usage:
129 // 1. Check the state() of the output stream before using it. If CLOSED, the
130 // output stream must be rebuilt (and the socket likely needs to be
131 // reconnected, as an error was encountered).
132 // 2. If EMPTY, the output stream can be written via a CodedOutputStream using
133 // the ZeroCopyOutputStream interface.
134 // 3. Once done writing, state() should be READY, so call Flush(..) to write
135 // the buffer into the StreamSocket. Wait for the callback to be invoked
136 // (it's invalid to write to an output stream while it's flushing).
137 // 4. Check the state() again to ensure the Flush was successful. state() should
138 // be EMPTY again.
139 // 5. Repeat.
140 class GCM_EXPORT SocketOutputStream
141 : public google::protobuf::io::ZeroCopyOutputStream {
142 public:
143 enum State {
144 // No valid data yet.
145 EMPTY,
146 // Ready for flushing (some data is present).
147 READY,
148 // In the process of flushing into the socket.
149 FLUSHING,
150 // A permanent error occurred, and the stream is now closed.
151 CLOSED,
152 };
153
154 // |socket| should already be connected.
155 explicit SocketOutputStream(net::StreamSocket* socket);
156 virtual ~SocketOutputStream();
157
158 // ZeroCopyOutputStream implementation.
159 virtual bool Next(void** data, int* size) OVERRIDE;
160 virtual void BackUp(int count) OVERRIDE;
161 virtual int64 ByteCount() const OVERRIDE;
162
163 // Writes the buffer into the Socket.
164 void Flush(const base::Closure& callback);
165
166 // Returns the current state.
167 State state() const;
168
169 // Returns the last fatal error encountered. Only valid if state() == CLOSED.
170 net::Error last_error() const;
171
172 private:
173 void FlushCompletionCallback(const base::Closure& callback, int result);
174
175 // Internal net components.
176 net::StreamSocket* const socket_;
177 const scoped_refptr<net::IOBuffer> io_buffer_;
178 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't
179 // been written to the socket yet.
180 const scoped_refptr<net::DrainableIOBuffer> write_buffer_;
181
182 // The amount of valid buffer data. This is the amount of data written on the
183 // next call to Flush().
184 int buffer_used_;
185
186 // If < net::OK, the last net error received.
187 net::Error last_error_;
188
189 // The current state.
190 State state_;
191
192 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_;
193
194 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream);
195 };
196
197 } // namespace gcm
198
199 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698