udp_socket.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_UDP_SOCKET_H_
  5. #define COMPONENTS_OPENSCREEN_PLATFORM_UDP_SOCKET_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "mojo/public/cpp/bindings/pending_receiver.h"
  9. #include "mojo/public/cpp/bindings/receiver.h"
  10. #include "mojo/public/cpp/bindings/remote.h"
  11. #include "services/network/public/mojom/udp_socket.mojom.h"
  12. #include "third_party/openscreen/src/platform/api/udp_socket.h"
  13. #include "third_party/openscreen/src/platform/base/error.h"
  14. #include "third_party/openscreen/src/platform/base/interface_info.h"
  15. #include "third_party/openscreen/src/platform/base/ip_address.h"
  16. namespace net {
  17. class IPEndPoint;
  18. }
  19. namespace openscreen_platform {
  20. class UdpSocket final : public openscreen::UdpSocket,
  21. network::mojom::UDPSocketListener {
  22. public:
  23. UdpSocket(Client* client,
  24. const openscreen::IPEndpoint& local_endpoint,
  25. mojo::Remote<network::mojom::UDPSocket> udp_socket,
  26. mojo::PendingReceiver<network::mojom::UDPSocketListener>
  27. pending_listener);
  28. ~UdpSocket() final;
  29. // Implementations of openscreen::UdpSocket methods.
  30. bool IsIPv4() const final;
  31. bool IsIPv6() const final;
  32. openscreen::IPEndpoint GetLocalEndpoint() const final;
  33. void Bind() final;
  34. void SetMulticastOutboundInterface(
  35. openscreen::NetworkInterfaceIndex ifindex) final;
  36. void JoinMulticastGroup(const openscreen::IPAddress& address,
  37. openscreen::NetworkInterfaceIndex ifindex) final;
  38. void SendMessage(const void* data,
  39. size_t length,
  40. const openscreen::IPEndpoint& dest) final;
  41. void SetDscp(openscreen::UdpSocket::DscpMode state) final;
  42. // network::mojom::UDPSocketListener overrides:
  43. void OnReceived(int32_t net_result,
  44. const absl::optional<net::IPEndPoint>& source_endpoint,
  45. absl::optional<base::span<const uint8_t>> data) override;
  46. private:
  47. void BindCallback(int32_t result,
  48. const absl::optional<net::IPEndPoint>& address);
  49. void JoinGroupCallback(int32_t result);
  50. void SendCallback(int32_t result);
  51. const raw_ptr<Client> client_ = nullptr;
  52. // The local endpoint can change as a result of bind calls.
  53. openscreen::IPEndpoint local_endpoint_;
  54. mojo::Remote<network::mojom::UDPSocket> udp_socket_;
  55. // The pending listener gets converted to a proper Receiver when this socket
  56. // gets bound.
  57. mojo::PendingReceiver<network::mojom::UDPSocketListener> pending_listener_;
  58. mojo::Receiver<network::mojom::UDPSocketListener> listener_{this};
  59. base::WeakPtrFactory<UdpSocket> weak_ptr_factory_{this};
  60. };
  61. } // namespace openscreen_platform
  62. #endif // COMPONENTS_OPENSCREEN_PLATFORM_UDP_SOCKET_H_