udp_socket_client.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2018 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_MIRRORING_SERVICE_UDP_SOCKET_CLIENT_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_UDP_SOCKET_CLIENT_H_
  6. #include "base/callback.h"
  7. #include "base/component_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "media/cast/net/cast_transport_config.h"
  10. #include "mojo/public/cpp/bindings/receiver.h"
  11. #include "mojo/public/cpp/bindings/remote.h"
  12. #include "net/base/ip_endpoint.h"
  13. #include "services/network/public/mojom/network_service.mojom.h"
  14. #include "services/network/public/mojom/udp_socket.mojom.h"
  15. namespace mirroring {
  16. // This class implements a UDP packet transport that can send/receive UDP
  17. // packets to/from |remote_endpoint_| through a UDPSocket that implements
  18. // network::mojom::UDPSocket. The UDPSocket is created and connected to
  19. // |remote_endpoint_| when StartReceiving() is called. Sending/Receiving ends
  20. // when StopReceiving() is called or this class is destructed. |error_callback|
  21. // will be called if the UDPSocket is failed to be created or connected.
  22. class COMPONENT_EXPORT(MIRRORING_SERVICE) UdpSocketClient final
  23. : public media::cast::PacketTransport,
  24. public network::mojom::UDPSocketListener {
  25. public:
  26. UdpSocketClient(const net::IPEndPoint& remote_endpoint,
  27. network::mojom::NetworkContext* context,
  28. base::OnceClosure error_callback);
  29. UdpSocketClient(const UdpSocketClient&) = delete;
  30. UdpSocketClient& operator=(const UdpSocketClient&) = delete;
  31. ~UdpSocketClient() override;
  32. // media::cast::PacketTransport implementations.
  33. bool SendPacket(media::cast::PacketRef packet, base::OnceClosure cb) override;
  34. int64_t GetBytesSent() override;
  35. void StartReceiving(
  36. media::cast::PacketReceiverCallbackWithStatus packet_receiver) override;
  37. void StopReceiving() override;
  38. // network::mojom::UDPSocketListener implementation.
  39. void OnReceived(int32_t result,
  40. const absl::optional<net::IPEndPoint>& src_addr,
  41. absl::optional<base::span<const uint8_t>> data) override;
  42. private:
  43. // The callback of network::mojom::UDPSocket::Send(). Further sending is
  44. // blocked if |result| indicates that pending send requests buffer is full.
  45. void OnPacketSent(int result);
  46. // The callback of network::mojom::UDPSocket::Connect(). Packets are only
  47. // allowed to send after the socket is successfully connected to the
  48. // |remote_endpoint_|.
  49. void OnSocketConnected(int result,
  50. const absl::optional<net::IPEndPoint>& addr);
  51. const net::IPEndPoint remote_endpoint_;
  52. const raw_ptr<network::mojom::NetworkContext> network_context_;
  53. base::OnceClosure error_callback_;
  54. mojo::Receiver<network::mojom::UDPSocketListener> receiver_{this};
  55. // The callback to deliver the received packets to the packet parser. Set
  56. // when StartReceiving() is called.
  57. media::cast::PacketReceiverCallbackWithStatus packet_receiver_callback_;
  58. mojo::Remote<network::mojom::UDPSocket> udp_socket_;
  59. // Set by SendPacket() when the sending is not allowed. Once set, SendPacket()
  60. // can only be called again when a previous sending completes successfully.
  61. base::OnceClosure resume_send_callback_;
  62. // Total numbe of bytes written to the data pipe.
  63. int64_t bytes_sent_;
  64. // Indicates whether further sending is allowed. Set to true when the
  65. // UDPSocket is successfully connected to |remote_endpoint_|, and false when
  66. // too many send requests are pending.
  67. bool allow_sending_;
  68. // The number of packets pending to receive.
  69. int num_packets_pending_receive_;
  70. base::WeakPtrFactory<UdpSocketClient> weak_factory_{this};
  71. };
  72. } // namespace mirroring
  73. #endif // COMPONENTS_MIRRORING_SERVICE_UDP_SOCKET_CLIENT_H_