udp_socket_global_limits.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2020 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_UDP_SOCKET_GLOBAL_LIMITS_H_
  5. #define NET_SOCKET_UDP_SOCKET_GLOBAL_LIMITS_H_
  6. #include "net/base/net_errors.h"
  7. #include "net/base/net_export.h"
  8. namespace net {
  9. // Helper class for RAII-style management of the global count of "open UDP
  10. // sockets" [1] in the process.
  11. //
  12. // Keeping OwnedUDPSocketCount alive increases the global socket counter by 1.
  13. // When it goes out of scope - or is explicitly Reset() - the reference is
  14. // returned to the global counter.
  15. class NET_EXPORT OwnedUDPSocketCount {
  16. public:
  17. // The default constructor builds an empty OwnedUDPSocketCount (does not own a
  18. // count).
  19. OwnedUDPSocketCount();
  20. // Any count held by OwnedUDPSocketCount is transferred when moving.
  21. OwnedUDPSocketCount(OwnedUDPSocketCount&&);
  22. OwnedUDPSocketCount& operator=(OwnedUDPSocketCount&&);
  23. // This is a move-only type.
  24. OwnedUDPSocketCount(const OwnedUDPSocketCount&) = delete;
  25. OwnedUDPSocketCount& operator=(const OwnedUDPSocketCount&) = delete;
  26. ~OwnedUDPSocketCount();
  27. // Returns false if this instance "owns" a socket count. In
  28. // other words, when |empty()|, destruction of |this| will
  29. // not change the global socket count.
  30. bool empty() const { return empty_; }
  31. // Resets |this| to an empty state (|empty()| becomes true after
  32. // calling this). If |this| was previously |!empty()|, the global
  33. // socket count will be decremented.
  34. void Reset();
  35. private:
  36. // Only TryAcquireGlobalUDPSocketCount() is allowed to construct a non-empty
  37. // OwnedUDPSocketCount.
  38. friend NET_EXPORT OwnedUDPSocketCount TryAcquireGlobalUDPSocketCount();
  39. explicit OwnedUDPSocketCount(bool empty);
  40. bool empty_;
  41. };
  42. // Attempts to increase the global "open UDP socket" [1] count.
  43. //
  44. // * On failure returns an OwnedUDPSocketCount that is |empty()|. This happens
  45. // if the global socket limit has been reached.
  46. // * On success returns an OwnedUDPSocketCount that is |!empty()|. This
  47. // OwnedUDPSocketCount should be kept alive until the socket resource is
  48. // released.
  49. //
  50. // [1] For simplicity, an "open UDP socket" is defined as a net::UDPSocket that
  51. // successfully called Open(), and has not yet called Close(). This is
  52. // analogous to the number of open platform socket handles, and in practice
  53. // should also be a good proxy for the number of consumed UDP ports.
  54. [[nodiscard]] NET_EXPORT OwnedUDPSocketCount TryAcquireGlobalUDPSocketCount();
  55. // Returns the current count of open UDP sockets (for testing only).
  56. NET_EXPORT int GetGlobalUDPSocketCountForTesting();
  57. } // namespace net
  58. #endif // NET_SOCKET_UDP_SOCKET_GLOBAL_LIMITS_H_