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

Side by Side Diff: remoting/host/websocket_connection.h

Issue 11358190: Add simple WebSocket server implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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
« no previous file with comments | « no previous file | remoting/host/websocket_connection.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 REMOTING_HOST_WEBSOCKET_CONNECTION_H_
6 #define REMOTING_HOST_WEBSOCKET_CONNECTION_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/weak_ptr.h"
14 #include "remoting/base/socket_reader.h"
15 #include "remoting/protocol/buffered_socket_writer.h"
16
17 namespace net {
18 class StreamSocket;
19 } // namespace net
20
21 namespace remoting {
22
23 class WebSocketConnection {
24 public:
25 typedef base::Callback<void (bool connected)> ConnectedCallback;
26 class Delegate {
27 public:
28 virtual void OnWebSocketMessage(const std::string& message) = 0;
29 virtual void OnWebSocketClosed() = 0;
30 };
31
32 WebSocketConnection();
33 virtual ~WebSocketConnection();
34
35 // Initialize WebSocket connection for the specified |socket|.
36 // |connected_callback| is called with |connected|=true after handshake
37 // headers have been received from the client and parsed or with
38 // |connected|=false if the connection failed (e.g. headers were invalid).
39 void Start(scoped_ptr<net::StreamSocket> socket,
40 ConnectedCallback connected_callback);
41
42 // Connection parameters received in the header. Valid only once Start() has
43 // successfully completed.
44 const std::string& request_path() { return request_path_; }
45 const std::string& request_host() { return request_host_; }
46 const std::string& origin() { return origin_; }
47
48 // Accept or Reject new connection. Accept() or Reject() must be called once
49 // after Start() completes successfully.
50 void Accept(Delegate* delegate);
51 void Reject();
52
53 // Sets the maximum incoming message size to allow. The connection will be
54 // closed if a message exceeding this size is received. |size| may be 0 to
55 // allow any message size. By default size of incoming messages is unlimited.
56 void set_maximum_message_size(uint64 size);
57
58 // Send the specified text message. Sending data messages is not supported.
59 void SendText(const std::string& text);
60
61 // Closes the connection and sends Close frame to the client if necessary.
62 void Close();
63
64 private:
65 enum State {
66 READING_HEADERS,
67 HEADERS_READ,
68 ACCEPTED,
69 CLOSED,
70 };
71
72 enum WebsocketOpcode {
73 OPCODE_CONTINUATION = 0,
74 OPCODE_TEXT_FRAME = 1,
75 OPCODE_BINARY_FRAME = 2,
76 OPCODE_CLOSE = 8,
77 OPCODE_PING = 9,
78 OPCODE_PONG = 10,
79 };
80
81 // Closes the socket in response to a protocol error, and notifies
82 // Delegate::OnWebSocketClosed().
83 void CloseOnError();
84
85 // Result handler for |reader_|.
86 void OnSocketReadResult(scoped_refptr<net::IOBuffer> data, int size);
87
88 // Parses websocket headers in |header_| and returns false when headers are
89 // invalid.
90 bool ParseHeaders();
91
92 // Parses incoming data in |received_data_| and dispatches delegate calls when
93 // a new message is received.
94 void ProcessData();
95
96 // Error handler for |writer_|.
97 void OnSocketWriteError(int error);
98
99 // Sends outgoing frame with the specified |opcode| and |payload|.
100 void SendFragment(WebsocketOpcode opcode, const std::string& payload);
101
102 // Unmasks fragment |payload| using specified |mask|
103 void UnmaskPayload(const char* mask, char* payload, int payload_length);
104
105 scoped_ptr<net::StreamSocket> socket_;
106 ConnectedCallback connected_callback_;
107 Delegate* delegate_;
108
109 uint64 maximum_message_size_;
110
111 SocketReader reader_;
112 protocol::BufferedSocketWriter writer_;
113
114 State state_;
115
116 std::string headers_;
117
118 // Header fields. Set in ParseHeaders().
119 std::string request_path_;
120 std::string request_host_;
121 std::string origin_;
122 std::string websocket_key_;
123
124 // Raw data that has been received but hasn't been parsed.
125 std::string received_data_;
126
127 // When receiving a fragmented message |receiving_message_| is set to true and
128 // |current_message_| contains the fragments that we've already received.
129 bool receiving_message_;
130 std::string current_message_;
131
132 base::WeakPtrFactory<WebSocketConnection> weak_factory_;
133
134 DISALLOW_COPY_AND_ASSIGN(WebSocketConnection);
135 };
136
137 } // namespace remoting
138
139 #endif // REMOTING_HOST_WEBSOCKET_CONNECTION_H_
OLDNEW
« no previous file with comments | « no previous file | remoting/host/websocket_connection.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698