udp_server_socket.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "net/socket/udp_server_socket.h"
  5. #include <utility>
  6. #include "build/build_config.h"
  7. #include "net/base/net_errors.h"
  8. namespace net {
  9. UDPServerSocket::UDPServerSocket(net::NetLog* net_log,
  10. const net::NetLogSource& source)
  11. : socket_(DatagramSocket::DEFAULT_BIND, net_log, source) {}
  12. UDPServerSocket::~UDPServerSocket() = default;
  13. int UDPServerSocket::Listen(const IPEndPoint& address) {
  14. int rv = socket_.Open(address.GetFamily());
  15. if (rv != OK)
  16. return rv;
  17. if (allow_address_reuse_) {
  18. rv = socket_.AllowAddressReuse();
  19. if (rv != OK) {
  20. socket_.Close();
  21. return rv;
  22. }
  23. }
  24. if (allow_broadcast_) {
  25. rv = socket_.SetBroadcast(true);
  26. if (rv != OK) {
  27. socket_.Close();
  28. return rv;
  29. }
  30. }
  31. if (allow_address_sharing_for_multicast_) {
  32. rv = socket_.AllowAddressSharingForMulticast();
  33. if (rv != OK) {
  34. socket_.Close();
  35. return rv;
  36. }
  37. }
  38. return socket_.Bind(address);
  39. }
  40. int UDPServerSocket::RecvFrom(IOBuffer* buf,
  41. int buf_len,
  42. IPEndPoint* address,
  43. CompletionOnceCallback callback) {
  44. return socket_.RecvFrom(buf, buf_len, address, std::move(callback));
  45. }
  46. int UDPServerSocket::SendTo(IOBuffer* buf,
  47. int buf_len,
  48. const IPEndPoint& address,
  49. CompletionOnceCallback callback) {
  50. return socket_.SendTo(buf, buf_len, address, std::move(callback));
  51. }
  52. int UDPServerSocket::SetReceiveBufferSize(int32_t size) {
  53. return socket_.SetReceiveBufferSize(size);
  54. }
  55. int UDPServerSocket::SetSendBufferSize(int32_t size) {
  56. return socket_.SetSendBufferSize(size);
  57. }
  58. int UDPServerSocket::SetDoNotFragment() {
  59. return socket_.SetDoNotFragment();
  60. }
  61. void UDPServerSocket::SetMsgConfirm(bool confirm) {
  62. return socket_.SetMsgConfirm(confirm);
  63. }
  64. void UDPServerSocket::Close() {
  65. socket_.Close();
  66. }
  67. int UDPServerSocket::GetPeerAddress(IPEndPoint* address) const {
  68. return socket_.GetPeerAddress(address);
  69. }
  70. int UDPServerSocket::GetLocalAddress(IPEndPoint* address) const {
  71. return socket_.GetLocalAddress(address);
  72. }
  73. const NetLogWithSource& UDPServerSocket::NetLog() const {
  74. return socket_.NetLog();
  75. }
  76. void UDPServerSocket::AllowAddressReuse() {
  77. allow_address_reuse_ = true;
  78. }
  79. void UDPServerSocket::AllowBroadcast() {
  80. allow_broadcast_ = true;
  81. }
  82. void UDPServerSocket::AllowAddressSharingForMulticast() {
  83. allow_address_sharing_for_multicast_ = true;
  84. }
  85. int UDPServerSocket::JoinGroup(const IPAddress& group_address) const {
  86. return socket_.JoinGroup(group_address);
  87. }
  88. int UDPServerSocket::LeaveGroup(const IPAddress& group_address) const {
  89. return socket_.LeaveGroup(group_address);
  90. }
  91. int UDPServerSocket::SetMulticastInterface(uint32_t interface_index) {
  92. return socket_.SetMulticastInterface(interface_index);
  93. }
  94. int UDPServerSocket::SetMulticastTimeToLive(int time_to_live) {
  95. return socket_.SetMulticastTimeToLive(time_to_live);
  96. }
  97. int UDPServerSocket::SetMulticastLoopbackMode(bool loopback) {
  98. return socket_.SetMulticastLoopbackMode(loopback);
  99. }
  100. int UDPServerSocket::SetDiffServCodePoint(DiffServCodePoint dscp) {
  101. return socket_.SetDiffServCodePoint(dscp);
  102. }
  103. void UDPServerSocket::DetachFromThread() {
  104. socket_.DetachFromThread();
  105. }
  106. void UDPServerSocket::UseNonBlockingIO() {
  107. #if BUILDFLAG(IS_WIN)
  108. socket_.UseNonBlockingIO();
  109. #endif
  110. }
  111. } // namespace net