aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/netliba/v6/net_test.cpp
blob: 63fd7c4067c1841df0e753ca57c03b7d28b3eccc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"
#include "net_test.h"
#include "udp_address.h"

#ifndef _win_
#include <errno.h>
#endif

namespace NNetliba {
    TAtomic ActivePortTestersCount;

    const float PING_DELAY = 0.5f;

    TPortUnreachableTester::TPortUnreachableTester()
        : TimePassed(0)
        , ConnectOk(false)

    {
        s.Open(0);
        if (s.IsValid()) {
            AtomicAdd(ActivePortTestersCount, 1);
        }
    }

    void TPortUnreachableTester::Connect(const TUdpAddress& addr) {
        Y_ASSERT(IsValid());
        sockaddr_in6 toAddress;
        GetWinsockAddr(&toAddress, addr);
        ConnectOk = s.Connect(toAddress);
        TimePassed = 0;
    }

    TPortUnreachableTester::~TPortUnreachableTester() {
        if (s.IsValid())
            AtomicAdd(ActivePortTestersCount, -1);
    }

    bool TPortUnreachableTester::Test(float deltaT) {
        if (!ConnectOk)
            return false;
        if (s.IsHostUnreachable())
            return false;
        TimePassed += deltaT;
        if (TimePassed > PING_DELAY) {
            TimePassed = 0;
            s.SendEmptyPacket();
        }
        return true;
    }
}