Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 after handshake headers have been received | |
| 37 // from the client and parsed or when the connection terminated. | |
| 38 void Start(scoped_ptr<net::StreamSocket> socket, | |
| 39 ConnectedCallback connected_callback); | |
| 40 | |
| 41 // Connection parameters received in the header. Valid only after | |
| 42 // |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.
| |
| 43 const std::string& request_path() { return request_path_; } | |
| 44 const std::string& request_host() { return request_host_; } | |
| 45 const std::string& origin() { return origin_; } | |
| 46 | |
| 47 // Accept or Reject new connection. Accept() or Reject() must be called once | |
| 48 // after Start() completes successfully, i.e. after |connected_callback| is | |
| 49 // 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.
| |
| 50 void Accept(Delegate* delegate); | |
| 51 void Reject(); | |
| 52 | |
| 53 // Maximum size of incoming messages. Connection is terminated when the remote | |
| 54 // end tries to send a message bigger than the specified value. Can be set | |
| 55 // 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.
| |
| 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 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.
| |
| 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 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.
| |
| 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 // 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.
| |
| 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, | |
| 101 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.
| |
| 102 | |
| 103 // Unmasks fragment |payload| using specified |mask| | |
| 104 void UnmaskPayload(const char* mask, char* payload, int payload_length); | |
| 105 | |
| 106 scoped_ptr<net::StreamSocket> socket_; | |
| 107 ConnectedCallback connected_callback_; | |
| 108 Delegate* delegate_; | |
| 109 | |
| 110 uint64 maximum_message_size_; | |
| 111 | |
| 112 SocketReader reader_; | |
| 113 protocol::BufferedSocketWriter writer_; | |
| 114 | |
| 115 State state_; | |
| 116 | |
| 117 std::string headers_; | |
| 118 | |
| 119 // Header fields. Set in ParseHeaders(). | |
| 120 std::string request_path_; | |
| 121 std::string request_host_; | |
| 122 std::string origin_; | |
| 123 std::string websocket_key_; | |
| 124 | |
| 125 // Raw data that has been received but hasn't been parsed. | |
| 126 std::string received_data_; | |
| 127 | |
| 128 // When receiving a fragmented message |receiving_message_| is set to true and | |
| 129 // |current_message_| contains the fragments that we've already received. | |
| 130 bool receiving_message_; | |
| 131 std::string current_message_; | |
| 132 | |
| 133 base::WeakPtrFactory<WebsocketConnection> weak_factory_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(WebsocketConnection); | |
| 136 }; | |
| 137 | |
| 138 } // namespace remoting | |
| 139 | |
| 140 #endif // REMOTING_HOST_WEBSOCKET_CONNECTION_H_ | |
| OLD | NEW |