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