net_test.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "stdafx.h"
  2. #include "net_test.h"
  3. #include "udp_address.h"
  4. #ifndef _win_
  5. #include <errno.h>
  6. #endif
  7. namespace NNetliba {
  8. TAtomic ActivePortTestersCount;
  9. const float PING_DELAY = 0.5f;
  10. TPortUnreachableTester::TPortUnreachableTester()
  11. : TimePassed(0)
  12. , ConnectOk(false)
  13. {
  14. s.Open(0);
  15. if (s.IsValid()) {
  16. AtomicAdd(ActivePortTestersCount, 1);
  17. }
  18. }
  19. void TPortUnreachableTester::Connect(const TUdpAddress& addr) {
  20. Y_ASSERT(IsValid());
  21. sockaddr_in6 toAddress;
  22. GetWinsockAddr(&toAddress, addr);
  23. ConnectOk = s.Connect(toAddress);
  24. TimePassed = 0;
  25. }
  26. TPortUnreachableTester::~TPortUnreachableTester() {
  27. if (s.IsValid())
  28. AtomicAdd(ActivePortTestersCount, -1);
  29. }
  30. bool TPortUnreachableTester::Test(float deltaT) {
  31. if (!ConnectOk)
  32. return false;
  33. if (s.IsHostUnreachable())
  34. return false;
  35. TimePassed += deltaT;
  36. if (TimePassed > PING_DELAY) {
  37. TimePassed = 0;
  38. s.SendEmptyPacket();
  39. }
  40. return true;
  41. }
  42. }