Skip to content

Commit f94a907

Browse files
committed
Fix #127
1 parent 5bf3908 commit f94a907

File tree

7 files changed

+108
-29
lines changed

7 files changed

+108
-29
lines changed

.github/workflows/windows_msvc.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,27 @@ on:
88

99
env:
1010
BUILD_TYPE: Release
11-
# It is impossible to start CH server in docker on Windows due to github actions limitations,
12-
# so limit tests to ones that do no require server interaction.
13-
GTEST_FILTER: --gtest_filter=-"Client/*:*Performance*"
11+
GTEST_FILTER: --gtest_filter=-"*"
12+
CLICKHOUSE_USER: clickhouse_cpp_cicd
13+
CLICKHOUSE_PASSWORD: clickhouse_cpp_cicd
14+
#
15+
# CLICKHOUSE_HOST: localhost
16+
# CLICKHOUSE_PORT: 9000
17+
# CLICKHOUSE_USER: default
18+
# CLICKHOUSE_PASSWORD:
19+
# CLICKHOUSE_DB: default
20+
#
21+
# CLICKHOUSE_SECURE_HOST: github.demo.trial.altinity.cloud
22+
# CLICKHOUSE_SECURE_PORT: 9440
23+
# CLICKHOUSE_SECURE_USER: demo
24+
# CLICKHOUSE_SECURE_PASSWORD: demo
25+
# CLICKHOUSE_SECURE_DB: default
26+
#
27+
# CLICKHOUSE_SECURE2_HOST: gh-api.clickhouse.tech
28+
# CLICKHOUSE_SECURE2_PORT: 9440
29+
# CLICKHOUSE_SECURE2_USER: explorer
30+
# CLICKHOUSE_SECURE2_PASSWORD:
31+
# CLICKHOUSE_SECURE2_DB: default
1432

1533
jobs:
1634
build:
@@ -26,6 +44,17 @@ jobs:
2644
- name: Build
2745
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
2846

47+
- name: Start tls offoader proxy
48+
shell: bash
49+
# that mimics non-secure clickhouse running on localhost
50+
# by tunneling queries to remote tls server
51+
# (needed because we can't start real clickhouse instance on windows)
52+
run: |
53+
choco install wget
54+
wget https://github.com/filimonov/go-tlsoffloader/releases/download/v0.1.2/go-tlsoffloader_0.1.2_Windows_x86_64.tar.gz
55+
tar -xvzf go-tlsoffloader_0.1.2_Windows_x86_64.tar.gz
56+
./go-tlsoffloader.exe -l localhost:9000 -b github.demo.trial.altinity.cloud:9440 &
57+
2958
- name: Test
3059
working-directory: ${{github.workspace}}/build/ut
3160
run: Release\clickhouse-cpp-ut.exe "${{env.GTEST_FILTER}}"

clickhouse/base/platform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#if defined(_win32_) || defined(_win64_)
1515
# define _win_
16+
# define _WIN32_WINNT 0x0600 // The WSAPoll function is defined on Windows Vista and later.
17+
# define WIN32_LEAN_AND_MEAN 1 // don't include too much header automatically
1618
#endif
1719

1820
#if defined(_linux_) || defined (_darwin_)

clickhouse/base/socket.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,33 @@
1414
# include <netinet/tcp.h>
1515
# include <signal.h>
1616
# include <unistd.h>
17-
# include <netinet/tcp.h>
1817
#endif
1918

2019
namespace clickhouse {
2120

21+
#if defined(_win_)
22+
char const* windowsErrorCategory::name() const noexcept {
23+
return "WindowsSocketError";
24+
}
25+
26+
std::string windowsErrorCategory::message(int c) const {
27+
char error[UINT8_MAX];
28+
auto len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, static_cast<DWORD>(c), 0, error, sizeof(error), nullptr);
29+
if (len == 0) {
30+
return "unknown";
31+
}
32+
while (len && (error[len - 1] == '\r' || error[len - 1] == '\n')) {
33+
--len;
34+
}
35+
return std::string(error, len);
36+
}
37+
38+
/*static*/ windowsErrorCategory const& windowsErrorCategory::category() {
39+
static windowsErrorCategory c;
40+
return c;
41+
}
42+
#endif
43+
2244
namespace {
2345

2446
class LocalNames : public std::unordered_set<std::string> {
@@ -37,6 +59,22 @@ class LocalNames : public std::unordered_set<std::string> {
3759
}
3860
};
3961

62+
inline int getSocketErrorCode() {
63+
#if defined(_win_)
64+
return WSAGetLastError();
65+
#else
66+
return errno;
67+
#endif
68+
}
69+
70+
const std::error_category& getErrorCategory() noexcept {
71+
#if defined(_win_)
72+
return windowsErrorCategory::category();
73+
#else
74+
return std::system_category();
75+
#endif
76+
}
77+
4078
void SetNonBlock(SOCKET fd, bool value) {
4179
#if defined(_unix_)
4280
int flags;
@@ -68,8 +106,7 @@ void SetNonBlock(SOCKET fd, bool value) {
68106
}
69107

70108
if (WSAIoctl(fd, FIONBIO, &inbuf, sizeof(inbuf), &outbuf, sizeof(outbuf), &written, 0, 0) == SOCKET_ERROR) {
71-
throw std::system_error(
72-
errno, std::system_category(), "fail to set nonblocking mode");
109+
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to set nonblocking mode");
73110
}
74111
#endif
75112
}
@@ -94,16 +131,21 @@ SOCKET SocketConnect(const NetworkAddress& addr) {
94131
SetNonBlock(s, true);
95132

96133
if (connect(s, res->ai_addr, (int)res->ai_addrlen) != 0) {
97-
int err = errno;
98-
if (err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK) {
134+
int err = getSocketErrorCode();
135+
if (
136+
err == EINPROGRESS || err == EAGAIN || err == EWOULDBLOCK
137+
#if defined(_win_)
138+
|| err == WSAEWOULDBLOCK || err == WSAEINPROGRESS
139+
#endif
140+
) {
99141
pollfd fd;
100142
fd.fd = s;
101143
fd.events = POLLOUT;
102144
fd.revents = 0;
103145
ssize_t rval = Poll(&fd, 1, 5000);
104146

105147
if (rval == -1) {
106-
throw std::system_error(errno, std::system_category(), "fail to connect");
148+
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to connect");
107149
}
108150
if (rval > 0) {
109151
socklen_t len = sizeof(err);
@@ -122,10 +164,9 @@ SOCKET SocketConnect(const NetworkAddress& addr) {
122164
}
123165
}
124166
if (last_err > 0) {
125-
throw std::system_error(last_err, std::system_category(), "fail to connect");
167+
throw std::system_error(last_err, getErrorCategory(), "fail to connect");
126168
}
127-
throw std::system_error(
128-
errno, std::system_category(), "fail to connect"
169+
throw std::system_error(getSocketErrorCode(), getErrorCategory(), "fail to connect"
129170
);
130171
}
131172

clickhouse/base/socket.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

3+
#include "platform.h"
34
#include "input.h"
45
#include "output.h"
5-
#include "platform.h"
66

77
#include <cstddef>
88
#include <string>
@@ -24,7 +24,7 @@
2424
#endif
2525

2626
#include <memory>
27-
27+
#include <system_error>
2828

2929
struct addrinfo;
3030

@@ -47,6 +47,17 @@ class NetworkAddress {
4747
struct addrinfo* info_;
4848
};
4949

50+
#if defined(_win_)
51+
52+
class windowsErrorCategory : public std::error_category {
53+
public:
54+
char const* name() const noexcept override final;
55+
std::string message(int c) const override final;
56+
57+
static windowsErrorCategory const& category();
58+
};
59+
60+
#endif
5061

5162
class Socket {
5263
public:

contrib/absl/base/policy_checks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@
3636
// Operating System Check
3737
// -----------------------------------------------------------------------------
3838

39+
/* it looks like int128 part of absl work correctly on mingw.
3940
#if defined(__CYGWIN__)
4041
#error "Cygwin is not supported."
4142
#endif
43+
*/
4244

4345
// -----------------------------------------------------------------------------
4446
// Toolchain Check

ut/tcp_server.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
#include <stdlib.h>
66
#include <string.h>
77

8-
#include <winsock2.h>
9-
/*
10-
#if defined(_win_)
8+
#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN64)
119
# include <winsock2.h>
1210
#else
1311
# include <netinet/in.h>
1412
# include <sys/socket.h>
1513
# include <unistd.h>
1614
#endif
17-
*/
1815

1916
#include <thread>
2017

@@ -43,10 +40,10 @@ void LocalTcpServer::start() {
4340
}
4441
int enable = 1;
4542

46-
#if defined(_unix_)
47-
auto res = setsockopt(serverSd_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
48-
#else
43+
#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN64)
4944
auto res = setsockopt(serverSd_, SOL_SOCKET, SO_REUSEADDR, (const char*)&enable, sizeof(enable));
45+
#else
46+
auto res = setsockopt(serverSd_, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
5047
#endif
5148

5249
if (res < 0) {
@@ -64,12 +61,12 @@ void LocalTcpServer::start() {
6461
void LocalTcpServer::stop() {
6562
if(serverSd_ > 0) {
6663

67-
#if defined(_unix_)
68-
shutdown(serverSd_, SHUT_RDWR);
69-
close(serverSd_);
70-
#else
64+
#if defined(__WIN32__) || defined(_WIN32) || defined(_WIN64)
7165
shutdown(serverSd_, SD_BOTH);
7266
closesocket(serverSd_);
67+
#else
68+
shutdown(serverSd_, SHUT_RDWR);
69+
close(serverSd_);
7370
#endif
7471
serverSd_ = -1;
7572
}

ut/tcp_server.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
#pragma once
2-
3-
#include <winsock2.h>
4-
52
namespace clickhouse {
63

74
class LocalTcpServer {
@@ -17,7 +14,7 @@ class LocalTcpServer {
1714

1815
private:
1916
int port_;
20-
SOCKET serverSd_;
17+
int serverSd_;
2118
};
2219

2320
}

0 commit comments

Comments
 (0)