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..25c41dca5b7334757352314ff6c7ce2a0cf8cfc6 |
| --- /dev/null |
| +++ b/remoting/host/websocket_connection.h |
| @@ -0,0 +1,140 @@ |
| +// 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; |
| + }; |
| + |
| + WebsocketConnection(); |
| + virtual ~WebsocketConnection(); |
| + |
| + // Initialize WebSocket connection for the specified |socket|. |
| + // |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); |
| + |
| + // Connection parameters received in the header. Valid only after |
| + // |connected_callback| specified in Start() is called with true. |
|
Wez
2012/11/20 05:44:09
nit: "Valid only after |connected_callback_| has b
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + const std::string& request_path() { return request_path_; } |
| + const std::string& request_host() { return request_host_; } |
| + const std::string& origin() { return origin_; } |
| + |
| + // Accept or Reject new connection. Accept() or Reject() must be called once |
| + // after Start() completes successfully, i.e. after |connected_callback| is |
| + // called with connected=true. |
|
Wez
2012/11/20 05:44:09
nit: If you rephrase the comment on start to menti
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + void Accept(Delegate* delegate); |
| + void Reject(); |
| + |
| + // Maximum size of incoming messages. Connection is terminated when the remote |
| + // end tries to send a message bigger than the specified value. Can be set |
| + // to 0 (default), which indicates that message size is unlimited. |
|
Wez
2012/11/20 05:44:09
nit: "Sets the maximum incoming message size to al
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + void set_maximum_message_size(uint64 size); |
| + |
| + // Send the specified text message. Sending data messages is not supported. |
| + void SendText(const std::string& text); |
| + |
| + // Closes the connection sending Close frame to the client if necessary. |
|
Wez
2012/11/20 05:44:09
nit: "Closes the connection and sends a Close fram
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + void Close(); |
| + |
| + 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, |
| + }; |
| + |
| + // Closes the socket after protocol error and calls |
|
Wez
2012/11/20 05:44:09
nit: Suggest "Closes the socket in response to a p
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + // Delegate::OnWebsocketClosed(). |
| + void CloseOnError(); |
| + |
| + // Result handler for |reader_|. |
| + void OnSocketReadResult(scoped_refptr<net::IOBuffer> data, int size); |
| + |
| + // Parses websocket headers in |header_| and returns false when headers are |
| + // invalid. |
| + bool ParseHeaders(); |
| + |
| + // Parse incoming data in |received_data_| and dispatches delegate calls when |
|
Wez
2012/11/20 05:44:09
nit: Parse -> Parses
Sergey Ulanov
2012/11/21 01:40:24
Done.
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + // a new message is received. |
| + void ProcessData(); |
| + |
| + // Error handler for |writer_|. |
| + void OnSocketWriteError(int error); |
| + |
| + // Sends outgoing frame with the specified |opcode| and |payload|. |
| + void SendFragment(WebsocketOpcode opcode, |
| + const char* payload, int payload_length); |
|
Wez
2012/11/20 05:44:09
nit: Why not just pass payload as std::string or b
Sergey Ulanov
2012/11/21 01:40:24
Done.
|
| + |
| + // Unmasks fragment |payload| using specified |mask| |
| + void UnmaskPayload(const char* mask, char* payload, int payload_length); |
| + |
| + scoped_ptr<net::StreamSocket> socket_; |
| + ConnectedCallback connected_callback_; |
| + Delegate* delegate_; |
| + |
| + uint64 maximum_message_size_; |
| + |
| + SocketReader reader_; |
| + protocol::BufferedSocketWriter writer_; |
| + |
| + State state_; |
| + |
| + std::string headers_; |
| + |
| + // Header fields. Set in ParseHeaders(). |
| + std::string request_path_; |
| + std::string request_host_; |
| + std::string origin_; |
| + std::string websocket_key_; |
| + |
| + // Raw data that has been received but hasn't been parsed. |
| + std::string received_data_; |
| + |
| + // When receiving a fragmented message |receiving_message_| is set to true and |
| + // |current_message_| contains the fragments that we've already received. |
| + 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_ |