summaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/socket.cpp
blob: 8c016df3d6d1732368791e6e80449d7e067eb2a8 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "socket.h"
#include "socket_spec.h"

namespace vespalib {

SocketAddress
Socket::address() const
{
    return SocketAddress::address_of(_handle.get());
}

SocketAddress
Socket::peer_address() const
{
    return SocketAddress::peer_address(_handle.get());
}

void
Socket::shutdown()
{
    if (valid()) {
        ::shutdown(_handle.get(), SHUT_RDWR);
    }
}

ssize_t
Socket::read(char *buf, size_t len)
{
    for (;;) {
        ssize_t result = ::read(_handle.get(), buf, len);
        if ((result >= 0) || (errno != EINTR)) {
            return result;
        }
    }
}

ssize_t
Socket::write(const char *buf, size_t len)
{
    for (;;) {
        ssize_t result = ::write(_handle.get(), buf, len);
        if ((result >= 0) || (errno != EINTR)) {
            return result;
        }
    }
}

Socket::UP
Socket::connect(const SocketSpec &spec)
{
    SocketHandle handle = spec.client_address().connect();
    return std::make_unique<Socket>(std::move(handle));
}

} // namespace vespalib