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

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 #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 // Reads from the socket, appending a max of |byte_limit| bytes onto the read
76 // buffer. net::ERR_IO_PENDING is returned if the refresh can't complete
77 // synchronously, in which case the callback is invoked upon completion. If
78 // the refresh can complete synchronously, even in case of an error, returns
79 // net::OK without invoking callback.
80 // Note: GetState() (and possibly last_error()) should be checked upon
81 // completion to determine whether the Refresh encountered an error.
82 net::Error Refresh(const base::Closure& callback, int byte_limit);
83
84 // Rebuilds the buffer state by copying over any unread data to the beginning
85 // of the buffer and resetting the buffer read/write positions.
86 // Note: it is not valid to call Rebuild() if GetState() == CLOSED. The stream
87 // must be recreated from scratch in such a scenario.
88 void RebuildBuffer();
89
90 // Returns the last fatal error encountered. Only valid if GetState() ==
91 // CLOSED.
92 net::Error last_error() const;
93
94 // Returns the current state.
95 State GetState() const;
96
97 private:
98 // Clears the local state.
99 void ResetInternal();
100
101 // Callback for Socket::Read calls.
102 void RefreshCompletionCallback(const base::Closure& callback, int result);
103
104 // Permanently closes the stream.
105 void CloseStream(net::Error error, const base::Closure& callback);
106
107 // Internal net components.
108 net::StreamSocket* const socket_;
109 const scoped_refptr<net::IOBuffer> io_buffer_;
110 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't
111 // been written to yet by Socket::Read calls.
112 const scoped_refptr<net::DrainableIOBuffer> read_buffer_;
113
114 // Starting position of the data within |io_buffer_| to consume on subsequent
115 // Next(..) call. 0 <= next_pos_ <= read_buffer_.BytesConsumed()
116 // Note: next_pos == read_buffer_.BytesConsumed() implies GetState() == EMPTY.
117 int next_pos_;
118
119 // If < net::ERR_IO_PENDING, the last net error received.
120 // Note: last_error_ == net::ERR_IO_PENDING implies GetState() == READING.
121 net::Error last_error_;
122
123 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_;
124
125 DISALLOW_COPY_AND_ASSIGN(SocketInputStream);
126 };
127
128 // A helper class for writing to a SocketStream with protobuf encoded data.
129 // A SocketOutputStream does not take ownership of the socket itself, and it is
130 // expected that the life of the output stream should match the life of the
131 // socket itself (while the socket remains connected).
132 // Typical usage:
133 // 1. Check the GetState() of the output stream before using it. If CLOSED, the
134 // output stream must be rebuilt (and the socket likely needs to be
135 // reconnected, as an error was encountered).
136 // 2. If EMPTY, the output stream can be written via a CodedOutputStream using
137 // the ZeroCopyOutputStream interface.
138 // 3. Once done writing, GetState() should be READY, so call Flush(..) to write
139 // the buffer into the StreamSocket. Wait for the callback to be invoked
140 // (it's invalid to write to an output stream while it's flushing).
141 // 4. Check the GetState() again to ensure the Flush was successful. GetState()
142 // should be EMPTY again.
143 // 5. Repeat.
144 class GCM_EXPORT SocketOutputStream
145 : public google::protobuf::io::ZeroCopyOutputStream {
146 public:
147 enum State {
148 // No valid data yet.
149 EMPTY,
150 // Ready for flushing (some data is present).
151 READY,
152 // In the process of flushing into the socket.
153 FLUSHING,
154 // A permanent error occurred, and the stream is now closed.
155 CLOSED,
156 };
157
158 // |socket| should already be connected.
159 explicit SocketOutputStream(net::StreamSocket* socket);
160 virtual ~SocketOutputStream();
161
162 // ZeroCopyOutputStream implementation.
163 virtual bool Next(void** data, int* size) OVERRIDE;
164 virtual void BackUp(int count) OVERRIDE;
165 virtual int64 ByteCount() const OVERRIDE;
166
167 // Writes the buffer into the Socket.
168 net::Error Flush(const base::Closure& callback);
169
170 // Returns the last fatal error encountered. Only valid if GetState() ==
171 // CLOSED.
172 net::Error last_error() const;
173
174 // Returns the current state.
175 State GetState() const;
176
177 private:
178 void FlushCompletionCallback(const base::Closure& callback, int result);
179
180 // Internal net components.
181 net::StreamSocket* const socket_;
182 const scoped_refptr<net::IOBuffer> io_buffer_;
183 // IOBuffer implementation that wraps the data within |io_buffer_| that hasn't
184 // been written to the socket yet.
185 const scoped_refptr<net::DrainableIOBuffer> write_buffer_;
186
187 // Starting position of the data within |io_buffer_| to consume on subsequent
188 // Next(..) call. 0 <= write_buffer_.BytesConsumed() <= next_pos_
189 // Note: next_pos == 0 implies GetState() == EMPTY.
190 int next_pos_;
191
192 // If < net::ERR_IO_PENDING, the last net error received.
193 // Note: last_error_ == net::ERR_IO_PENDING implies GetState() == FLUSHING.
194 net::Error last_error_;
195
196 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_;
197
198 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream);
199 };
200
201 } // namespace gcm
202
203 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698