-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathsocket.cpp
More file actions
206 lines (174 loc) · 4.42 KB
/
Copy pathsocket.cpp
File metadata and controls
206 lines (174 loc) · 4.42 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "socket.h"
#if defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
#if defined(_WIN32)
#include <atomic>
namespace {
std::atomic<int> wsaInitCount = {0};
} // anonymous namespace
#else
namespace {
using SOCKET = int;
} // anonymous namespace
#endif
namespace {
constexpr SOCKET InvalidSocket = static_cast<SOCKET>(-1);
} // anonymous namespace
class dap::Socket::Shared : public dap::ReaderWriter {
public:
static void init() {
#if defined(_WIN32)
if (wsaInitCount++ == 0) {
WSADATA winsockData;
(void)WSAStartup(MAKEWORD(2, 2), &winsockData);
}
#endif
}
static void term() {
#if defined(_WIN32)
if (--wsaInitCount == 0) {
WSACleanup();
}
#endif
}
static std::shared_ptr<Shared> create(const char* address, const char* port) {
init();
addrinfo hints = {};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
addrinfo* info = nullptr;
getaddrinfo(address, port, &hints, &info);
if (info) {
auto socket =
::socket(info->ai_family, info->ai_socktype, info->ai_protocol);
return std::make_shared<Shared>(info, socket);
}
freeaddrinfo(info);
term();
return nullptr;
}
Shared(SOCKET socket) : info(nullptr), sock(socket) {}
Shared(addrinfo* info, SOCKET socket) : info(info), sock(socket) {}
~Shared() {
freeaddrinfo(info);
close();
term();
}
SOCKET socket() { return sock.load(); }
// dap::ReaderWriter compliance
bool isOpen() {
SOCKET s = socket();
if (s == InvalidSocket) {
return false;
}
char error = 0;
socklen_t len = sizeof(error);
getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &len);
if (error != 0) {
sock.compare_exchange_weak(s, InvalidSocket);
return false;
}
return true;
}
void close() {
SOCKET s = sock.exchange(InvalidSocket);
if (s != InvalidSocket) {
#if defined(_WIN32)
closesocket(s);
#else
::shutdown(s, SHUT_RDWR);
::close(s);
#endif
}
}
size_t read(void* buffer, size_t bytes) {
SOCKET s = socket();
if (s == InvalidSocket) {
return 0;
}
return recv(s, reinterpret_cast<char*>(buffer), static_cast<int>(bytes), 0);
}
bool write(const void* buffer, size_t bytes) {
SOCKET s = socket();
if (s == InvalidSocket) {
return false;
}
if (bytes == 0) {
return true;
}
return ::send(s, reinterpret_cast<const char*>(buffer),
static_cast<int>(bytes), 0) > 0;
}
addrinfo* const info;
private:
std::atomic<SOCKET> sock = {InvalidSocket};
};
namespace dap {
Socket::Socket(const char* address, const char* port)
: shared(Shared::create(address, port)) {
if (!shared) {
return;
}
auto socket = shared->socket();
if (bind(socket, shared->info->ai_addr, (int)shared->info->ai_addrlen) != 0) {
shared.reset();
return;
}
if (listen(socket, 1) != 0) {
shared.reset();
return;
}
}
std::shared_ptr<ReaderWriter> Socket::accept() const {
if (shared) {
SOCKET socket = shared->socket();
if (socket != InvalidSocket) {
return std::make_shared<Shared>(::accept(socket, 0, 0));
}
}
return {};
}
bool Socket::isOpen() const {
if (shared) {
return shared->isOpen();
}
return false;
}
void Socket::close() const {
if (shared) {
shared->close();
}
}
std::shared_ptr<ReaderWriter> Socket::connect(const char* address,
const char* port) {
auto shared = Shared::create(address, port);
if (::connect(shared->socket(), shared->info->ai_addr,
(int)shared->info->ai_addrlen) == 0) {
return shared;
}
return {};
}
} // namespace dap