datagram_server_socket.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2012 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 NET_SOCKET_DATAGRAM_SERVER_SOCKET_H_
  5. #define NET_SOCKET_DATAGRAM_SERVER_SOCKET_H_
  6. #include <stdint.h>
  7. #include "net/base/completion_once_callback.h"
  8. #include "net/base/net_export.h"
  9. #include "net/socket/datagram_socket.h"
  10. #include "net/socket/diff_serv_code_point.h"
  11. namespace net {
  12. class IPAddress;
  13. class IPEndPoint;
  14. class IOBuffer;
  15. // A UDP Socket.
  16. class NET_EXPORT DatagramServerSocket : public DatagramSocket {
  17. public:
  18. ~DatagramServerSocket() override = default;
  19. // Initialize this socket as a server socket listening at |address|.
  20. // Returns a network error code.
  21. virtual int Listen(const IPEndPoint& address) = 0;
  22. // Read from a socket and receive sender address information.
  23. // |buf| is the buffer to read data into.
  24. // |buf_len| is the maximum amount of data to read.
  25. // |address| is a buffer provided by the caller for receiving the sender
  26. // address information about the received data. This buffer must be kept
  27. // alive by the caller until the callback is placed.
  28. // |callback| is the callback on completion of the RecvFrom.
  29. // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
  30. // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
  31. // alive until the callback is called.
  32. virtual int RecvFrom(IOBuffer* buf,
  33. int buf_len,
  34. IPEndPoint* address,
  35. CompletionOnceCallback callback) = 0;
  36. // Send to a socket with a particular destination.
  37. // |buf| is the buffer to send.
  38. // |buf_len| is the number of bytes to send.
  39. // |address| is the recipient address.
  40. // |callback| is the user callback function to call on complete.
  41. // Returns a net error code, or ERR_IO_PENDING if the IO is in progress.
  42. // If ERR_IO_PENDING is returned, the caller must keep |buf| and |address|
  43. // alive until the callback is called.
  44. virtual int SendTo(IOBuffer* buf,
  45. int buf_len,
  46. const IPEndPoint& address,
  47. CompletionOnceCallback callback) = 0;
  48. // Set the receive buffer size (in bytes) for the socket.
  49. // Returns a net error code.
  50. virtual int SetReceiveBufferSize(int32_t size) = 0;
  51. // Set the send buffer size (in bytes) for the socket.
  52. // Returns a net error code.
  53. virtual int SetSendBufferSize(int32_t size) = 0;
  54. // Allow the socket to share the local address to which the socket will be
  55. // bound with other processes. If multiple processes are bound to the same
  56. // local address at the same time, behavior is undefined; e.g., it is not
  57. // guaranteed that incoming messages will be sent to all listening sockets.
  58. //
  59. // Should be called before Listen().
  60. virtual void AllowAddressReuse() = 0;
  61. // Allow sending and receiving packets to and from broadcast addresses.
  62. // Should be called before Listen().
  63. virtual void AllowBroadcast() = 0;
  64. // Allow the socket to share the local address to which the socket will be
  65. // bound with other processes and attempt to allow all such sockets to receive
  66. // the same multicast messages.
  67. //
  68. // For best cross-platform results in allowing the messages to be shared, all
  69. // sockets sharing the same address should join the same multicast group and
  70. // interface. Also, the socket should listen to the specific multicast group
  71. // address rather than a wildcard address (e.g. 0.0.0.0) on platforms where
  72. // doing so is allowed.
  73. //
  74. // Should be called before Listen().
  75. virtual void AllowAddressSharingForMulticast() = 0;
  76. // Join the multicast group with address |group_address|.
  77. // Returns a network error code.
  78. virtual int JoinGroup(const IPAddress& group_address) const = 0;
  79. // Leave the multicast group with address |group_address|.
  80. // If the socket hasn't joined the group, it will be ignored.
  81. // It's optional to leave the multicast group before destroying
  82. // the socket. It will be done by the OS.
  83. // Returns a network error code.
  84. virtual int LeaveGroup(const IPAddress& group_address) const = 0;
  85. // Set interface to use for multicast. If |interface_index| set to 0, default
  86. // interface is used.
  87. // Should be called before Bind().
  88. // Returns a network error code.
  89. virtual int SetMulticastInterface(uint32_t interface_index) = 0;
  90. // Set the time-to-live option for UDP packets sent to the multicast
  91. // group address. The default value of this option is 1.
  92. // Cannot be negative or more than 255.
  93. // Should be called before Bind().
  94. // Returns a network error code.
  95. virtual int SetMulticastTimeToLive(int time_to_live) = 0;
  96. // Set the loopback flag for UDP socket. If this flag is true, the host
  97. // will receive packets sent to the joined group from itself.
  98. // The default value of this option is true.
  99. // Should be called before Bind().
  100. // Returns a network error code.
  101. virtual int SetMulticastLoopbackMode(bool loopback) = 0;
  102. // Set the Differentiated Services Code Point. May do nothing on
  103. // some platforms. Returns a network error code.
  104. virtual int SetDiffServCodePoint(DiffServCodePoint dscp) = 0;
  105. // Resets the thread to be used for thread-safety checks.
  106. virtual void DetachFromThread() = 0;
  107. };
  108. } // namespace net
  109. #endif // NET_SOCKET_DATAGRAM_SERVER_SOCKET_H_