btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 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 | |
Jordan Bayles | ad2d2cd | 2019-05-22 14:49:40 -0700 | [diff] [blame] | 5 | #ifndef PLATFORM_API_UDP_SOCKET_H_ |
| 6 | #define PLATFORM_API_UDP_SOCKET_H_ |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 7 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 8 | #include <cstdint> |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 9 | #include <functional> |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Yuri Wiitala | 82edd20 | 2018-10-31 18:42:58 -0700 | [diff] [blame] | 12 | #include "platform/api/network_interface.h" |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 13 | #include "platform/base/error.h" |
| 14 | #include "platform/base/ip_address.h" |
| 15 | #include "platform/base/macros.h" |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 16 | |
| 17 | namespace openscreen { |
| 18 | namespace platform { |
| 19 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 20 | class UdpSocket; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 21 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 22 | using UdpSocketUniquePtr = std::unique_ptr<UdpSocket>; |
Yuri Wiitala | b94f12e | 2018-10-31 18:28:53 -0700 | [diff] [blame] | 23 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 24 | // An open UDP socket for sending/receiving datagrams to/from either specific |
| 25 | // endpoints or over IP multicast. |
| 26 | // |
| 27 | // Usage: The socket is created and opened by calling the Create() method. This |
| 28 | // returns a unique pointer that auto-closes/destroys the socket when it goes |
| 29 | // out-of-scope. |
| 30 | // |
| 31 | // Platform implementation note: There must only be one platform-specific |
| 32 | // implementation of UdpSocket linked into the library/application. For that |
| 33 | // reason, none of the methods here are declared virtual (i.e., the overhead is |
| 34 | // pure waste). However, UdpSocket can be subclassed to include all extra |
| 35 | // private state, such as OS-specific handles. See UdpSocketPosix for a |
| 36 | // reference implementation. |
| 37 | class UdpSocket { |
| 38 | public: |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 39 | virtual ~UdpSocket(); |
| 40 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 41 | // Constants used to specify how we want packets sent from this socket. |
| 42 | enum class DscpMode : uint8_t { |
| 43 | // Default value set by the system on creation of a new socket. |
| 44 | kUnspecified = 0x0, |
| 45 | |
| 46 | // Mode for Audio only. |
| 47 | kAudioOnly = 0xb8, |
| 48 | |
| 49 | // Mode for Audio + Video. |
| 50 | kAudioVideo = 0x88, |
| 51 | |
| 52 | // Mode for low priority operations such as trace log data. |
| 53 | kLowPriority = 0x20 |
| 54 | }; |
| 55 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 56 | using Version = IPAddress::Version; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 57 | |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 58 | // Creates a new, scoped UdpSocket within the IPv4 or IPv6 family. This method |
| 59 | // must be defined in the platform-level implementation. |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 60 | static ErrorOr<UdpSocketUniquePtr> Create(Version version); |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 61 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 62 | // Returns true if |socket| belongs to the IPv4/IPv6 address family. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 63 | virtual bool IsIPv4() const = 0; |
| 64 | virtual bool IsIPv6() const = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 65 | |
| 66 | // Sets the socket for address reuse, binds to the address/port. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 67 | virtual Error Bind(const IPEndpoint& local_endpoint) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 68 | |
| 69 | // Sets the device to use for outgoing multicast packets on the socket. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 70 | virtual Error SetMulticastOutboundInterface( |
| 71 | NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 72 | |
| 73 | // Joins to the multicast group at the given address, using the specified |
| 74 | // interface. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 75 | virtual Error JoinMulticastGroup(const IPAddress& address, |
| 76 | NetworkInterfaceIndex ifindex) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 77 | |
| 78 | // Performs a non-blocking read on the socket, returning the number of bytes |
| 79 | // received. Note that a non-Error return value of 0 is a valid result, |
| 80 | // indicating an empty message has been received. Also note that |
| 81 | // Error::Code::kAgain might be returned if there is no message currently |
| 82 | // ready for receive, which can be expected during normal operation. |src| and |
| 83 | // |original_destination| are optional output arguments that provide the |
| 84 | // source of the message and its intended destination, respectively. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 85 | virtual ErrorOr<size_t> ReceiveMessage(void* data, |
| 86 | size_t length, |
| 87 | IPEndpoint* src, |
| 88 | IPEndpoint* original_destination) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 89 | |
| 90 | // Sends a message and returns the number of bytes sent, on success. |
| 91 | // Error::Code::kAgain might be returned to indicate the operation would |
| 92 | // block, which can be expected during normal operation. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 93 | virtual Error SendMessage(const void* data, |
| 94 | size_t length, |
| 95 | const IPEndpoint& dest) = 0; |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 96 | |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 97 | // Sets the DSCP value to use for all messages sent from this socket. |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 98 | virtual Error SetDscp(DscpMode state) = 0; |
| 99 | |
| 100 | // Sets the callback that should be called upon deletion of this socket. This |
| 101 | // allows other objects to observe the socket's destructor and act when it is |
| 102 | // called. |
| 103 | void SetDeletionCallback(std::function<void(UdpSocket*)> callback); |
Ryan Keane | 8f1c925 | 2019-05-06 11:03:38 -0700 | [diff] [blame] | 104 | |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 105 | protected: |
| 106 | UdpSocket(); |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 107 | |
| 108 | private: |
Ryan Keane | deb48b3 | 2019-06-28 16:24:40 -0700 | [diff] [blame] | 109 | // This callback allows other objects to observe the socket's destructor and |
| 110 | // act when it is called. |
| 111 | std::function<void(UdpSocket*)> deletion_callback_; |
| 112 | |
Jordan Bayles | f1e4bb7 | 2019-05-01 12:38:39 -0700 | [diff] [blame] | 113 | OSP_DISALLOW_COPY_AND_ASSIGN(UdpSocket); |
Yuri Wiitala | 10dea9f | 2019-02-04 20:19:17 -0800 | [diff] [blame] | 114 | }; |
btolsch | 9ccfa78 | 2018-07-26 00:16:08 -0700 | [diff] [blame] | 115 | |
| 116 | } // namespace platform |
| 117 | } // namespace openscreen |
| 118 | |
Jordan Bayles | ad2d2cd | 2019-05-22 14:49:40 -0700 | [diff] [blame] | 119 | #endif // PLATFORM_API_UDP_SOCKET_H_ |