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

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: Make SocketStream asynchronous. Remove custom data provider Created 7 years, 3 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 // Note that any caller must first check the state() of the stream before
8 // using it. It is not valid to use a CLOSED stream.
9
10 #ifndef GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
11 #define GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
12
13 #include "base/basictypes.h"
14 #include "base/callback_forward.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/timer/timer.h"
20 #include "google/protobuf/io/zero_copy_stream.h"
21 #include "google_apis/gcm/base/gcm_export.h"
22
23 namespace net {
24 class DrainableIOBuffer;
25 class IOBufferWithSize;
26 class StreamSocket;
27 }
28
29 namespace gcm {
30
31 class GCM_EXPORT SocketInputStream
32 : public google::protobuf::io::ZeroCopyInputStream {
33 public:
34 enum State {
35 // No valid data to read. This means the buffer is either empty or all data
36 // in the buffer has already been consumed.
37 EMPTY,
38 // Valid data to read.
39 READY,
40 // In the process of reading new data from the socket.
41 READING,
42 // An permanent error occurred and the stream is now closed.
43 CLOSED,
44 };
45
46 // |socket| should already be connected.
47 SocketInputStream(base::TimeDelta read_timeout,
48 net::StreamSocket* socket);
49 virtual ~SocketInputStream();
50
51 // ZeroCopyInputStream implementation.
52 // Note: the stream must be READY before attempting to read, although it's
53 // valid to read from an EMPTY stream if it's the last read of a message
54 // (see GetNextMessage below).
55 virtual bool Next(const void** data, int* size) OVERRIDE;
56 virtual void BackUp(int count) OVERRIDE;
57 virtual bool Skip(int count) OVERRIDE;
58 virtual int64 ByteCount() const OVERRIDE;
Ryan Sleevi 2013/09/10 19:35:27 STYLE: I thought the policy was NOT to OVERRIDE fr
Nicolas Zea 2013/09/12 22:46:58 Chromium style guide just says to use OVERRIDE for
59
60 // Reads and appends the next set of data from the socket into the existing
61 // buffer (max read size is dependent on available buffer space).
62 // This is useful to check if there is any pending socket data to be read,
63 // as the read does not time out, so the callback will only be invoked on
64 // an error or when data is received.
65 void Refresh(const base::Closure& callback);
66
67 // Resets the buffer state by copying over any unread data to the beginning
68 // of the buffer and zeroing the buffer position.
69 // Note: it is not valid to call Reset() if state() == CLOSED. The stream
70 // must be recreated in such a scenario.
71 void Reset();
Ryan Sleevi 2013/09/10 19:35:27 STYLE: Naming: I would expect Reset() to actually
Nicolas Zea 2013/09/12 22:46:58 Renamed to Rebuild
72
73 // Asynchronously retrieves the |msg_size| bytes comprising a message,
74 // invoking the callback when complete or upon encountering an error. After a
75 // message is successfully received, Next(..) can be repeatedly invoked to
76 // retrieve the bytes of the message, after which it will return false
77 // (denoting that the message has been fully consumed).
78 // Note: The stream will remain valid after the message is consumed, but
79 // Reset() or another GetNextMessage(..) call must be made before data can be
80 // consumed again.
81 // Also note: GetNextMessage, unlike Refresh, does have a timeout if no
82 // data is received for too long. The timeout will close the stream and
83 // trigger the callback.
84 void GetNextMessage(int msg_size, const base::Closure& callback);
85
86 // Returns the last fatal error encountered. Only valid if state() == CLOSED.
87 int last_error() const;
88
89 // Returns the current state.
90 State state() const;
91
92 private:
93 // Callback for Socket::Read calls.
94 void RefreshCompletionCallback(const base::Closure& callback, int result);
95
96 // Clears the local state.
97 void ResetInternal();
98
99 // Permanently closes the stream.
100 void CloseStream(int error, const base::Closure& callback);
101
102 // Internal net components.
103 net::StreamSocket* socket_;
104 scoped_refptr<net::IOBufferWithSize> io_buffer_;
105 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_;
106
107 // The amount of data within |io_buffer_| that has already been read.
108 int buffer_pos_;
109
110 // The amount of valid data within |io_buffer_|.
111 int buffer_size_;
Ryan Sleevi 2013/09/10 19:35:27 STYLE: naming: io_buffer_ is an IOBufferWithSize,
Nicolas Zea 2013/09/12 22:46:58 Updated to buffer_write_pos_ (and changed buffer_p
112
113 // The |buffer_pos_| limit at which Next(..) will no longer trigger Refresh
114 // calls.
115 int limit_;
116
117 // The number of bytes we've backed up by. These bytes need to be returned
118 // on the subsequent Next(..) call. 0 <= backup_bytes_ <= buffer_pos_
119 int backup_bytes_;
Ryan Sleevi 2013/09/10 19:35:27 STYLE: Pronouns in comments considered harmful. Or
Nicolas Zea 2013/09/12 22:46:58 Done.
120
121 // The number of bytes to skip from the next successful refresh.
122 int skipped_bytes_;
123
124 // If < net::OK, the last net error received.
125 int last_error_;
126
127 // The current state.
128 State state_;
129
130 // Timeout when performing synchronous reads.
131 base::TimeDelta read_timeout_;
132
133 base::OneShotTimer<SocketInputStream> read_timeout_timer_;
134
135 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_;
136
137 DISALLOW_COPY_AND_ASSIGN(SocketInputStream);
138 };
139
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 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 int last_error() const;
171
172 private:
173 void FlushCompletionCallback(const base::Closure& callback, int result);
174
175 net::StreamSocket* socket_;
176 scoped_refptr<net::IOBufferWithSize> io_buffer_;
177 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_;
178
179 // The amount of valid buffer data. This is the amount of data written on the
180 // next call to Flush().
181 int buffer_used_;
182
183 // If < net::OK, the last net error received.
184 int last_error_;
185
186 // The current state.
187 State state_;
188
189 base::WeakPtrFactory<SocketOutputStream> weak_ptr_factory_;
190
191 DISALLOW_COPY_AND_ASSIGN(SocketOutputStream);
192 };
193
194 } // namespace gcm
195
196 #endif // GOOGLE_APIS_GCM_BASE_SOCKET_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698