Chromium Code Reviews| Index: remoting/host/websocket_connection.h |
| diff --git a/remoting/host/websocket_connection.h b/remoting/host/websocket_connection.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84d33d1536e07c9c3ce87061166f0657cf4b5840 |
| --- /dev/null |
| +++ b/remoting/host/websocket_connection.h |
| @@ -0,0 +1,107 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef REMOTING_HOST_WEBSOCKET_CONNECTION_H_ |
| +#define REMOTING_HOST_WEBSOCKET_CONNECTION_H_ |
| + |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "remoting/base/socket_reader.h" |
| +#include "remoting/protocol/buffered_socket_writer.h" |
| + |
| +namespace net { |
| +class StreamSocket; |
| +} // namespace net |
| + |
| +namespace remoting { |
| + |
| +class WebsocketConnection { |
| + public: |
| + typedef base::Callback<void (bool connected)> ConnectedCallback; |
| + class Delegate { |
| + public: |
| + virtual void OnWebsocketMessage(const std::string& message) = 0; |
| + virtual void OnWebsocketClosed() = 0; |
| + }; |
| + |
| + // Creates new web-socket using. |socket| specifies accepted transport socket. |
| + // |connected_callback| is called when handshake finishes or connection fails. |
|
Wez
2012/11/15 02:28:37
Please reword this comment. :)
Sergey Ulanov
2012/11/15 21:01:03
Just removed it. No, I wasn't drunk when writing i
Wez
2012/11/20 05:44:09
:D
|
| + WebsocketConnection(); |
| + virtual ~WebsocketConnection(); |
| + |
| + // Initialize WebSocket connection for the specified |socket|. |
|
Wez
2012/11/15 02:28:37
nit: You're calling it WebSocket here, but the cla
Sergey Ulanov
2012/11/15 21:01:03
WebSocket is the name of the protocol. For type na
Wez
2012/11/20 05:44:09
I think in this case WebSocket fits the style guid
Sergey Ulanov
2012/11/21 01:40:24
Ok. Renamed everything to WebSocket.
|
| + // |connected_callback| is called after handshake headers have been received |
| + // from the client and parsed or when the connection terminated. |
| + void Start(scoped_ptr<net::StreamSocket> socket, |
| + ConnectedCallback connected_callback); |
| + |
| + const std::string& request_path() { return request_path_; } |
| + const std::string& request_host() { return request_host_; } |
| + const std::string& origin() { return origin_; } |
|
Wez
2012/11/15 02:28:37
nit: Add a short comment to explain these getters.
Sergey Ulanov
2012/11/15 21:01:03
Done.
|
| + |
| + void Accept(Delegate* delegate); |
| + void Reject(); |
| + |
| + void SendText(const std::string& text); |
|
Wez
2012/11/15 02:28:37
If this implementation only supports text frames,
Sergey Ulanov
2012/11/15 21:01:03
Done.
According to RFC the only difference between
|
| + void Close(); |
|
Wez
2012/11/15 02:28:37
nit: Add short comments to explain these methods.
Sergey Ulanov
2012/11/15 21:01:03
Done.
|
| + |
| + private: |
| + enum State { |
| + READING_HEADERS, |
| + HEADERS_READ, |
| + ACCEPTED, |
| + CLOSED, |
| + }; |
| + |
| + enum WebsocketOpcode { |
| + OPCODE_CONTINUATION = 0, |
| + OPCODE_TEXT_FRAME = 1, |
| + OPCODE_BINARY_FRAME = 2, |
| + OPCODE_CLOSE = 8, |
| + OPCODE_PING = 9, |
| + OPCODE_PONG = 10, |
| + }; |
| + |
| + void CloseOnError(); |
| + void OnSocketReadResult(scoped_refptr<net::IOBuffer> data, int size); |
| + bool ParseHeaders(); |
| + void ProcessData(); |
| + |
| + void OnSocketWriteError(int error); |
| + void SendFragment(WebsocketOpcode opcode, |
| + const char* payload, int payload_length); |
| + |
| + void UnmaskPayload(const char* mask, char* payload, int payload_length); |
|
Wez
2012/11/15 02:28:37
Again, short comments for each method, please.
Sergey Ulanov
2012/11/15 21:01:03
Done.
|
| + |
| + scoped_ptr<net::StreamSocket> socket_; |
| + ConnectedCallback connected_callback_; |
| + Delegate* delegate_; |
| + SocketReader reader_; |
| + protocol::BufferedSocketWriter writer_; |
| + State state_; |
| + std::string headers_; |
| + |
| + std::string request_path_; |
| + std::string request_host_; |
| + std::string origin_; |
| + std::string websocket_key_; |
| + |
| + std::string current_packet_; |
|
Wez
2012/11/15 02:28:37
nit: Add a comment to this field, esp given the si
Sergey Ulanov
2012/11/15 21:01:03
Done. Also renamed it to |received_data_|
|
| + |
| + // Used to assemble fragmented message. |
| + bool receiving_message_; |
| + std::string current_message_; |
| + |
| + base::WeakPtrFactory<WebsocketConnection> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebsocketConnection); |
| +}; |
| + |
| +} // namespace remoting |
| + |
| +#endif // REMOTING_HOST_WEBSOCKET_CONNECTION_H_ |