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

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

Powered by Google App Engine
This is Rietveld 408576698