tls_client_connection.h 2.8 KB

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