Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix building ExportedConnection on Windows #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SeverinLeonhardt
Copy link

The <unistd.h> header isn't available on Windows. The dup function is deprecated1 and apparently also not the correct function here.

On Windows uv_fileno currently returns a SOCKET2 cast into uv_os_fd_t3. Contrary to many other handles this handle type can't be duplicated using DuplicateHandle4. WSADuplicateSocket has to be used instead. As documented an actual SOCKET has to be constructed from WSAPROTOCOL_INFOW. The parameters are taken from the struct, except for g and dwFlags which were taken from libuv's implementation5.

Error handling was omitted like with uv_fileno and dup already.

Also changed the type of fd to what uv_tcp_open expects which is SOCKET instead of int on Windows.

Footnotes

  1. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-dup-dup2

  2. https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/include/uv/win.h#L456

  3. https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/src/win/core.c#L689

  4. https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle#remarks

  5. https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/src/win/tcp.c#L1281-L1282

The `<unistd.h>` header isn't available on Windows. The `dup` function
is deprecated[^1] and apparently also not the correct function here.

On Windows `uv_fileno` currently returns a `SOCKET`[^2] cast into
`uv_os_fd_t`[^3]. Contrary to many other handles this handle type can't
be duplicated using `DuplicateHandle`[^4]. `WSADuplicateSocket` has to
be used instead. As documented an actual `SOCKET` has to be constructed
from `WSAPROTOCOL_INFOW`.  The parameters are taken from the struct,
except for `g` and `dwFlags` which were taken from libuv's
implementation[^5].

Error handling was omitted like with `uv_fileno` and `dup` already.

Also changed the type of `fd` to what `uv_tcp_open` expects which is
`SOCKET` instead of `int` on Windows.

[^1]: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-dup-dup2
[^2]: https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/include/uv/win.h#L456
[^3]: https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/src/win/core.c#L689
[^4]: https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle#remarks
[^5]: https://github.com/libuv/libuv/blob/2f1614b1286f53b3b2ab96a15a525cb25462ba9a/src/win/tcp.c#L1281-L1282
@SeverinLeonhardt
Copy link
Author

To test whether these changes work I've applied the following patch:

diff --git a/src/connection_pool.cpp b/src/connection_pool.cpp
index 21464b18..891226ec 100644
--- a/src/connection_pool.cpp
+++ b/src/connection_pool.cpp
@@ -95,11 +95,7 @@ ConnectionPool::ConnectionPool(const Connection::Vec& connections, ConnectionPoo
        ++it) {
     const Connection::Ptr& connection(*it);
     if (!connection->is_closing()) {
-      if (connections_by_shard_[connection->shard_id()].size() < num_connections_per_shard_) {
-        add_connection(PooledConnection::Ptr(new PooledConnection(this, connection)));
-      } else {
         host_->add_unpooled_connection(std::move(connection));
-      }
     }
   }

This lead to ExportConnection's constructor and import_connection method being called.
The test application then successfully ran various CQL queries against a single-node Scylla cluster.

Copy link

@Gor027 Gor027 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants