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

#pragma once

#include <memory>
#include "crypto_socket.h"
#include "crypto_engine.h"
#include <vespa/vespalib/data/smart_buffer.h>

namespace vespalib {

/**
 * A synchronous wrapper around a CryptoSocket. The create function
 * will perform connection handshaking. If handshaking fails, an empty
 * unique pointer is returned. The read function blocks until at least
 * 1 byte of data can be read, EOF is reached or an error occurs. The
 * write function blocks until all data can be written or an error
 * occurs. Note that this class is not thread-safe.
 **/
class SyncCryptoSocket
{
public:
    using UP = std::unique_ptr<SyncCryptoSocket>;
private:
    CryptoSocket::UP _socket;
    SmartBuffer _buffer;
    SyncCryptoSocket(CryptoSocket::UP socket) : _socket(std::move(socket)), _buffer(0) {}
    static UP create(CryptoSocket::UP socket);
public:
    ~SyncCryptoSocket();
    ssize_t read(char *buf, size_t len);
    ssize_t write(const char *buf, size_t len);
    ssize_t half_close();
    static UP create_client(CryptoEngine &engine, SocketHandle socket, const SocketSpec &spec);
    static UP create_server(CryptoEngine &engine, SocketHandle socket);
};

} // namespace vespalib