aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/net/socket.h
blob: 0ed6c14cde958967395eee96609baf2b3f0ce439 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "socket_handle.h"
#include <memory>

namespace vespalib {

class SocketSpec;

/**
 * Abstract stream-based socket interface.
 **/
struct Socket {
    using UP = std::unique_ptr<Socket>;
    virtual ssize_t read(char *buf, size_t len) = 0;
    virtual ssize_t write(const char *buf, size_t len) = 0;
    virtual ~Socket() {}
};

struct SimpleSocket : public Socket {
    SocketHandle handle;
    explicit SimpleSocket(SocketHandle handle_in) : handle(std::move(handle_in)) {}
    ssize_t read(char *buf, size_t len) final override { return handle.read(buf, len); }
    ssize_t write(const char *buf, size_t len) final override { return handle.write(buf, len); }
    static std::unique_ptr<SimpleSocket> connect(const SocketSpec &spec);
};

} // namespace vespalib