aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/iovec.h
blob: 46b753cee8c2b28753d749e26f1a73f29bceebff (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
59
60
61
62
63
64
65
#pragma once

#include <util/stream/output.h>
#include <util/system/types.h>
#include <util/system/yassert.h>

class TContIOVector {
    using TPart = IOutputStream::TPart;

public:
    inline TContIOVector(TPart* parts, size_t count)
        : Parts_(parts)
        , Count_(count)
    {
    }

    inline void Proceed(size_t len) noexcept { 
        while (Count_) {
            if (len < Parts_->len) {
                Parts_->len -= len;
                Parts_->buf = (const char*)Parts_->buf + len;

                return;
            } else {
                len -= Parts_->len;
                --Count_;
                ++Parts_;
            }
        }

        if (len) {
            Y_ASSERT(0 && "non zero length left");
        }
    }

    inline const TPart* Parts() const noexcept { 
        return Parts_;
    }

    inline size_t Count() const noexcept { 
        return Count_;
    }

    static inline size_t Bytes(const TPart* parts, size_t count) noexcept { 
        size_t ret = 0;

        for (size_t i = 0; i < count; ++i) {
            ret += parts[i].len;
        }

        return ret;
    }

    inline size_t Bytes() const noexcept { 
        return Bytes(Parts_, Count_);
    }

    inline bool Complete() const noexcept { 
        return !Count();
    }

private:
    TPart* Parts_;
    size_t Count_;
};