forked from ClickHouse/clickhouse-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.h
More file actions
158 lines (113 loc) · 3.51 KB
/
socket.h
File metadata and controls
158 lines (113 loc) · 3.51 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#pragma once
#include "platform.h"
#include "input.h"
#include "output.h"
#include "endpoint.h"
#include <cstddef>
#include <string>
#include <chrono>
#if defined(_win_)
# include <winsock2.h>
# include <ws2tcpip.h>
#else
# include <arpa/inet.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <poll.h>
# if !defined(SOCKET)
# define SOCKET int
# endif
#endif
#include <memory>
#include <system_error>
struct addrinfo;
namespace clickhouse {
struct ClientOptions;
/** Address of a host to establish connection to.
*
*/
class NetworkAddress {
public:
explicit NetworkAddress(const std::string& host,
const std::string& port = "0");
~NetworkAddress();
const struct addrinfo* Info() const;
const std::string & Host() const;
private:
const std::string host_;
struct addrinfo* info_;
};
#if defined(_win_)
class windowsErrorCategory : public std::error_category {
public:
char const* name() const noexcept override final;
std::string message(int c) const override final;
static windowsErrorCategory const& category();
};
#endif
class SocketBase {
public:
virtual ~SocketBase();
virtual std::unique_ptr<InputStream> makeInputStream() const = 0;
virtual std::unique_ptr<OutputStream> makeOutputStream() const = 0;
};
class SocketFactory {
public:
virtual ~SocketFactory();
// TODO: move connection-related options to ConnectionOptions structure.
virtual std::unique_ptr<SocketBase> connect(const ClientOptions& opts, EndpointConnector& endpointConnector) = 0;
virtual void sleepFor(const std::chrono::milliseconds& duration);
};
class Socket : public SocketBase {
public:
Socket(EndpointConnector& endpointConnector);
Socket(Socket&& other) noexcept;
Socket& operator=(Socket&& other) noexcept;
~Socket() override;
/// @params idle the time (in seconds) the connection needs to remain
/// idle before TCP starts sending keepalive probes.
/// @params intvl the time (in seconds) between individual keepalive probes.
/// @params cnt the maximum number of keepalive probes TCP should send
/// before dropping the connection.
void SetTcpKeepAlive(int idle, int intvl, int cnt) noexcept;
/// @params nodelay whether to enable TCP_NODELAY
void SetTcpNoDelay(bool nodelay) noexcept;
std::unique_ptr<InputStream> makeInputStream() const override;
std::unique_ptr<OutputStream> makeOutputStream() const override;
protected:
Socket(const Socket&) = delete;
Socket& operator = (const Socket&) = delete;
void Close();
SOCKET handle_;
};
class NonSecureSocketFactory : public SocketFactory {
public:
~NonSecureSocketFactory() override;
std::unique_ptr<SocketBase> connect(const ClientOptions& opts, EndpointConnector& endpointConnector) override;
protected:
virtual std::unique_ptr<Socket> doConnect(EndpointConnector& endpointConnector);
void setSocketOptions(Socket& socket, const ClientOptions& opts);
};
class SocketInput : public InputStream {
public:
explicit SocketInput(SOCKET s);
~SocketInput();
protected:
bool Skip(size_t bytes) override;
size_t DoRead(void* buf, size_t len) override;
private:
SOCKET s_;
};
class SocketOutput : public OutputStream {
public:
explicit SocketOutput(SOCKET s);
~SocketOutput();
protected:
size_t DoWrite(const void* data, size_t len) override;
private:
SOCKET s_;
};
static struct NetrworkInitializer {
NetrworkInitializer();
} gNetrworkInitializer;
}