network.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <util/generic/vector.h>
  4. namespace NTesting {
  5. //@brief network port holder interface
  6. class IPort {
  7. public:
  8. virtual ~IPort() {}
  9. virtual ui16 Get() = 0;
  10. };
  11. class TPortHolder : private THolder<IPort> {
  12. using TBase = THolder<IPort>;
  13. public:
  14. using TBase::TBase;
  15. using TBase::Release;
  16. using TBase::Reset;
  17. operator ui16() const& {
  18. return (*this)->Get();
  19. }
  20. operator ui16() const&& = delete;
  21. };
  22. IOutputStream& operator<<(IOutputStream& out, const TPortHolder& port);
  23. //@brief Get first free port.
  24. [[nodiscard]] TPortHolder GetFreePort();
  25. namespace NLegacy {
  26. // Do not use these methods made for Unittest TPortManager backward compatibility.
  27. // Returns continuous sequence of the specified number of ports.
  28. [[nodiscard]] TVector<TPortHolder> GetFreePortsRange(size_t count);
  29. //@brief Returns port from parameter if NO_RANDOM_PORTS env var is set, otherwise first free port
  30. [[nodiscard]] TPortHolder GetPort(ui16 port);
  31. }
  32. //@brief Reinitialize singleton from environment vars for tests
  33. void InitPortManagerFromEnv();
  34. //@brief helper class for inheritance
  35. struct TFreePortOwner {
  36. TFreePortOwner() : Port_(GetFreePort()) {}
  37. ui16 GetPort() const {
  38. return Port_;
  39. }
  40. private:
  41. TPortHolder Port_;
  42. };
  43. }