OLD | NEW |
---|---|
(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 "google/protobuf/io/zero_copy_stream.h" | |
20 #include "google_apis/gcm/base/gcm_export.h" | |
21 | |
22 namespace base { | |
23 class RunLoop; | |
24 } | |
25 | |
26 namespace net { | |
27 class DrainableIOBuffer; | |
28 class IOBufferWithSize; | |
29 class StreamSocket; | |
30 } | |
31 | |
32 namespace gcm { | |
33 | |
34 class GCM_EXPORT_PRIVATE SocketInputStream | |
35 : public google::protobuf::io::ZeroCopyInputStream { | |
Ryan Sleevi
2013/09/03 23:04:08
So, as I mentioned over Chat, I think we need to b
Nicolas Zea
2013/09/04 00:43:33
How does Chromoting make use of the message length
Nicolas Zea
2013/09/07 01:07:52
Went ahead and made SocketStream completely asynch
| |
36 public: | |
37 enum State { | |
38 // No valid data to read. This means the buffer is either empty or all data | |
39 // in the buffer has already been consumed. | |
40 EMPTY, | |
41 // Valid data to read. | |
42 READY, | |
43 // In the process of reading new data from the socket. | |
44 READING, | |
45 // An permanent error occurred and the stream is now closed. | |
46 CLOSED, | |
47 }; | |
48 | |
49 // |socket| should already be connected. | |
50 SocketInputStream(base::TimeDelta read_timeout, | |
51 net::StreamSocket* socket); | |
52 virtual ~SocketInputStream(); | |
53 | |
54 // ZeroCopyInputStream implementation. | |
55 // Note: it is okay to poll on Next(..) calls while it returns true if more | |
56 // data is expected. The input stream will internally time out if no data | |
57 // arrives, at which point Next(..) calls will return false. The socket must | |
58 // then be reconnected. | |
59 virtual bool Next(const void** data, int* size) OVERRIDE; | |
60 virtual void BackUp(int count) OVERRIDE; | |
61 virtual bool Skip(int count) OVERRIDE; | |
62 virtual int64 ByteCount() const OVERRIDE; | |
63 | |
64 // Reads and appends the next set of data from the socket into the existing | |
65 // buffer (max read size is dependent on the current limit and available | |
66 // buffer space). | |
67 void Refresh(const base::Closure& callback); | |
68 | |
69 // Resets the buffer state by copying over any unread data to the beginning | |
70 // of the buffer and resetting the buffer position. | |
71 // Note: it is not valid to call Reset() if state() == CLOSED. The stream | |
72 // must be recreated in such a scenario. | |
73 void Reset(); | |
74 | |
75 // Sets the read limit which upon having read |limit| bytes Next(..) will | |
76 // return false. This is useful when the size of a message is known ahead of | |
77 // time. The stream will remain valid after the limit has been reached, but | |
78 // Reset() must be called or a new limit must be set before more data can | |
79 // be consumed. | |
80 void SetLimit(int limit); | |
81 | |
82 // Returns the current buffer position (in bytes) within the stream. | |
83 int GetCurrentPosition() const; | |
84 | |
85 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
86 int last_error() const; | |
87 | |
88 // Returns the current state. | |
89 State state() const; | |
90 | |
91 private: | |
92 // Callback for Socket::Read calls. | |
93 void RefreshCompletionCallback(const base::Closure& callback, int result); | |
94 | |
95 // Clears the local state. | |
96 void ResetInternal(); | |
97 | |
98 // Permanently closes the stream. | |
99 void CloseStream(int error, const base::Closure& callback); | |
100 | |
101 // Internal net components. | |
102 net::StreamSocket* socket_; | |
103 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
104 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
105 | |
106 // The amount of data within |io_buffer_| that has already been read. | |
107 int buffer_pos_; | |
108 | |
109 // The amount of valid data within |io_buffer_|. | |
110 int buffer_size_; | |
111 | |
112 // The |buffer_pos_| limit at which Next(..) will no longer trigger Refresh | |
113 // calls. | |
114 int limit_; | |
115 | |
116 // The number of bytes we've backed up by. These bytes need to be returned | |
117 // on the subsequent Next(..) call. 0 <= backup_bytes_ <= buffer_pos_ | |
118 int backup_bytes_; | |
119 | |
120 // The number of bytes to skip from the next successful refresh. | |
121 int skipped_bytes_; | |
122 | |
123 // If < net::OK, the last net error received. | |
124 int last_error_; | |
125 | |
126 // The current state. | |
127 State state_; | |
128 | |
129 // Run loop for handling callers who would otherwise continuously poll | |
130 // Next(..). | |
131 scoped_ptr<base::RunLoop> run_loop_; | |
132 | |
133 // Timeout when performing synchronous reads. | |
134 base::TimeDelta read_timeout_; | |
135 | |
136 base::WeakPtrFactory<SocketInputStream> weak_ptr_factory_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(SocketInputStream); | |
139 }; | |
140 | |
141 class GCM_EXPORT_PRIVATE 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 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 void Flush(const base::Closure& callback); | |
166 | |
167 // Returns the current state. | |
168 State state() const; | |
169 | |
170 // Returns the last fatal error encountered. Only valid if state() == CLOSED. | |
171 int last_error() const; | |
172 | |
173 private: | |
174 void FlushCompletionCallback(const base::Closure& callback, int result); | |
175 | |
176 net::StreamSocket* socket_; | |
177 scoped_refptr<net::IOBufferWithSize> io_buffer_; | |
178 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer_; | |
179 | |
180 // The amount of valid buffer data. This is the amount of data written on the | |
181 // next call to Flush(). | |
182 int buffer_used_; | |
183 | |
184 // If < net::OK, the last net error received. | |
185 int last_error_; | |
186 | |
187 // The current state. | |
188 State state_; | |
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_ | |
OLD | NEW |