aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/poller.h
blob: 28476bffc97187fe696153805ed7513d2fe49ead (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
51
52
53
54
55
56
57
58
#pragma once

#include "socket.h"

#include <util/generic/ptr.h>
#include <util/datetime/base.h>

class TSocketPoller {
public:
    TSocketPoller();
    ~TSocketPoller(); 

    void WaitRead(SOCKET sock, void* cookie);
    void WaitWrite(SOCKET sock, void* cookie);
    void WaitReadWrite(SOCKET sock, void* cookie);
    void WaitRdhup(SOCKET sock, void* cookie);

    void WaitReadOneShot(SOCKET sock, void* cookie);
    void WaitWriteOneShot(SOCKET sock, void* cookie);
    void WaitReadWriteOneShot(SOCKET sock, void* cookie);

    void WaitReadWriteEdgeTriggered(SOCKET sock, void* cookie);
    void RestartReadWriteEdgeTriggered(SOCKET sock, void* cookie, bool empty = true);

    void Unwait(SOCKET sock);

    size_t WaitD(void** events, size_t len, const TInstant& deadLine);

    inline size_t WaitT(void** events, size_t len, const TDuration& timeOut) {
        return WaitD(events, len, timeOut.ToDeadLine());
    }

    inline size_t WaitI(void** events, size_t len) {
        return WaitD(events, len, TInstant::Max());
    }

    inline void* WaitD(const TInstant& deadLine) {
        void* ret;

        if (WaitD(&ret, 1, deadLine)) {
            return ret;
        }

        return nullptr;
    }

    inline void* WaitT(const TDuration& timeOut) {
        return WaitD(timeOut.ToDeadLine());
    }

    inline void* WaitI() {
        return WaitD(TInstant::Max());
    }

private:
    class TImpl;
    THolder<TImpl> Impl_;
};