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

Side by Side Diff: net/websockets/websocket_basic_stream.h

Issue 18792002: WebSocketBasicStream framing logic (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Amend comment to http_read_buffer_ to explain the type. 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
OLDNEW
(Empty)
1 // Copyright 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 #ifndef NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_
6 #define NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_
7
8 #include <string>
9
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "net/websockets/websocket_frame_parser.h"
15 #include "net/websockets/websocket_stream.h"
16
17 namespace net {
18
19 class ClientSocketHandle;
20 class DrainableIOBuffer;
21 class GrowableIOBuffer;
22 class HttpRequestHeaders;
23 class HttpResponseInfo;
24 class IOBufferWithSize;
25 struct WebSocketFrameChunk;
26
27 // Implementation of WebSocketStream for non-multiplexed ws:// connections (or
28 // the physical side of a multiplexed ws:// connection).
29 class NET_EXPORT_PRIVATE WebSocketBasicStream : public WebSocketStream {
30 public:
31 typedef WebSocketMaskingKey (*WebSocketMaskingKeyGeneratorFunction)();
32
33 // This class should not normally be constructed directly; see
34 // WebSocketStream::CreateAndConnectStream.
35 explicit WebSocketBasicStream(scoped_ptr<ClientSocketHandle> connection);
36
37 // The destructor has to make sure the connection is closed when we finish so
38 // that it does not get returned to the pool.
39 virtual ~WebSocketBasicStream();
40
41 virtual int ReadFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks,
szym 2013/08/29 16:42:01 nit: // WebSocketStream implementation:
Adam Rice 2013/08/30 10:05:35 Done.
42 const CompletionCallback& callback) OVERRIDE;
43
44 virtual int WriteFrames(ScopedVector<WebSocketFrameChunk>* frame_chunks,
45 const CompletionCallback& callback) OVERRIDE;
46
47 virtual void Close() OVERRIDE;
48
49 virtual std::string GetSubProtocol() const OVERRIDE;
50
51 virtual std::string GetExtensions() const OVERRIDE;
52
53 // Writes WebSocket handshake request HTTP-style to the connection. Adds
54 // "Sec-WebSocket-Key" header; this should not be included in |headers|.
55 virtual int SendHandshakeRequest(const GURL& url,
56 const HttpRequestHeaders& headers,
57 HttpResponseInfo* response_info,
58 const CompletionCallback& callback) OVERRIDE;
59
60 virtual int ReadHandshakeResponse(
61 const CompletionCallback& callback) OVERRIDE;
62
63 ////////////////////////////////////////////////////////////////////////////
64 // Methods for testing only.
65
66 static scoped_ptr<WebSocketBasicStream> CreateWebSocketBasicStreamForTesting(
67 scoped_ptr<ClientSocketHandle> connection,
68 const scoped_refptr<GrowableIOBuffer>& http_read_buffer,
69 const std::string& sub_protocol,
70 const std::string& extensions,
71 WebSocketMaskingKeyGeneratorFunction key_generator_function);
72
73 private:
74 // Returns OK or calls |callback| when the |buffer| is fully drained or
75 // something has failed.
76 int WriteEverything(const scoped_refptr<DrainableIOBuffer>& buffer,
77 const CompletionCallback& callback);
78
79 // Wraps the |callback| to continue writing until everything has been written.
80 void OnWriteComplete(const scoped_refptr<DrainableIOBuffer>& buffer,
81 const CompletionCallback& callback,
82 int result);
83
84 // Attempts to parse the output of a read as WebSocket frames. On success,
85 // returns OK and places the frame(s) in frame_chunks.
86 int HandleReadResult(int result,
87 ScopedVector<WebSocketFrameChunk>* frame_chunks);
88
89 // Called when a read completes. Parses the result and (unless no complete
90 // header has been received) calls |callback|.
91 void OnReadComplete(ScopedVector<WebSocketFrameChunk>* frame_chunks,
92 const CompletionCallback& callback,
93 int result);
94
95 // Storage for pending reads. All active WebSockets spend all the time with a
96 // call to ReadFrames() pending, so there is no benefit in trying to share
97 // this between sockets.
98 scoped_refptr<IOBufferWithSize> read_buffer_;
99
100 // The connection, wrapped in a ClientSocketHandle so that we can prevent it
101 // from being returned to the pool.
102 scoped_ptr<ClientSocketHandle> connection_;
103
104 // Only used during handshake. Some data may be left in this buffer after the
105 // handshake, in which case it will be picked up during the first call to
106 // ReadFrames(). The type is GrowableIOBuffer for compatibility with
107 // net::HttpStreamParser, which is used to parse the handshake.
108 scoped_refptr<GrowableIOBuffer> http_read_buffer_;
109
110 // This keeps the current parse state (including any incomplete headers) and
111 // parses frames.
112 WebSocketFrameParser parser_;
113
114 // The negotated sub-protocol, or empty for none.
115 std::string sub_protocol_;
116
117 // The extensions negotiated with the remote server.
118 std::string extensions_;
119
120 // This can be overridden in tests to make the output deterministic. We don't
121 // use a Callback here because a function pointer is faster and good enough
122 // for our purposes.
123 WebSocketMaskingKeyGeneratorFunction generate_websocket_masking_key_;
124 };
125
126 } // namespace net
127
128 #endif // NET_WEBSOCKETS_WEBSOCKET_BASIC_STREAM_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698