1 // Copyright 2019 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 COMPONENTS_OPENSCREEN_PLATFORM_TLS_CLIENT_CONNECTION_H_ 6 #define COMPONENTS_OPENSCREEN_PLATFORM_TLS_CLIENT_CONNECTION_H_ 7 8 #include "mojo/public/cpp/bindings/remote.h" 9 #include "mojo/public/cpp/system/data_pipe.h" 10 #include "mojo/public/cpp/system/simple_watcher.h" 11 #include "services/network/public/mojom/tcp_socket.mojom.h" 12 #include "services/network/public/mojom/tls_socket.mojom.h" 13 #include "third_party/openscreen/src/platform/api/task_runner.h" 14 #include "third_party/openscreen/src/platform/api/tls_connection.h" 15 #include "third_party/openscreen/src/platform/base/error.h" 16 #include "third_party/openscreen/src/platform/base/ip_address.h" 17 18 namespace openscreen_platform { 19 20 class TlsClientConnection : public openscreen::TlsConnection { 21 public: 22 TlsClientConnection( 23 openscreen::TaskRunner* task_runner, 24 openscreen::IPEndpoint local_address, 25 openscreen::IPEndpoint remote_address, 26 mojo::ScopedDataPipeConsumerHandle receive_stream, 27 mojo::ScopedDataPipeProducerHandle send_stream, 28 mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket, 29 mojo::Remote<network::mojom::TLSClientSocket> tls_socket); 30 31 ~TlsClientConnection() final; 32 33 // TlsConnection overrides. 34 void SetClient(Client* client) final; 35 bool Send(const void* data, size_t len) final; 36 openscreen::IPEndpoint GetLocalEndpoint() const final; 37 openscreen::IPEndpoint GetRemoteEndpoint() const final; 38 39 // The maximum size of the vector in any single Client::OnRead() callback. 40 static constexpr uint32_t kMaxBytesPerRead = 64 << 10; // 64 KB. 41 42 private: 43 // Invoked by |receive_stream_watcher_| when the |receive_stream_|'s status 44 // has changed. Calls Client::OnRead() if data has become available. 45 void ReceiveMore(MojoResult result, const mojo::HandleSignalsState& state); 46 47 // Classifies MojoResult codes into one of three categories: kOk, kAgain for 48 // transient errors, or |error_code_if_fatal| for fatal errors. If the result 49 // is a fatal error, this also invokes Client::OnError(). 50 openscreen::Error::Code ProcessMojoResult( 51 MojoResult result, 52 openscreen::Error::Code error_code_if_fatal); 53 54 openscreen::TaskRunner* const task_runner_ = nullptr; 55 const openscreen::IPEndpoint local_address_; 56 const openscreen::IPEndpoint remote_address_; 57 const mojo::ScopedDataPipeConsumerHandle receive_stream_; 58 const mojo::ScopedDataPipeProducerHandle send_stream_; 59 const mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket_; 60 const mojo::Remote<network::mojom::TLSClientSocket> tls_socket_; 61 62 mojo::SimpleWatcher receive_stream_watcher_; 63 64 Client* client_ = nullptr; 65 }; 66 67 } // namespace openscreen_platform 68 69 #endif // COMPONENTS_OPENSCREEN_PLATFORM_TLS_CLIENT_CONNECTION_H_ 70