Skip to content

Commit d54c973

Browse files
committed
Socket abstraction.
1 parent 2ca2763 commit d54c973

2 files changed

Lines changed: 19 additions & 22 deletions

File tree

libs/networking/include/launchdarkly/network/curl_multi_manager.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44

55
#include <curl/curl.h>
66
#include <boost/asio/any_io_executor.hpp>
7-
#ifdef _WIN32
8-
#include <boost/asio/windows/stream_handle.hpp>
9-
#else
10-
#include <boost/asio/posix/stream_descriptor.hpp>
11-
#endif
7+
#include <boost/asio/ip/tcp.hpp>
128
#include <boost/asio/steady_timer.hpp>
139

1410
#include <functional>
@@ -18,12 +14,8 @@
1814

1915
namespace launchdarkly::network {
2016

21-
// Platform-specific socket handle type
22-
#ifdef _WIN32
23-
using SocketHandle = boost::asio::windows::stream_handle;
24-
#else
25-
using SocketHandle = boost::asio::posix::stream_descriptor;
26-
#endif
17+
// Use tcp::socket for cross-platform socket operations
18+
using SocketHandle = boost::asio::ip::tcp::socket;
2719

2820
/**
2921
* Manages CURL multi interface integrated with ASIO event loop.
@@ -35,7 +27,7 @@ using SocketHandle = boost::asio::posix::stream_descriptor;
3527
*
3628
* Key features:
3729
* - Non-blocking I/O using curl_multi_socket_action
38-
* - Socket monitoring via ASIO (posix::stream_descriptor on POSIX, windows::stream_handle on Windows)
30+
* - Cross-platform socket monitoring via ASIO tcp::socket
3931
* - Timer integration with ASIO steady_timer
4032
* - Thread-safe operation on ASIO executor
4133
*/

libs/networking/src/curl_multi_manager.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,20 @@ void CurlMultiManager::check_multi_info() {
217217
void CurlMultiManager::start_socket_monitor(SocketInfo* socket_info,
218218
const int action) {
219219
if (!socket_info->handle) {
220-
// Create handle for this socket
220+
// Create tcp::socket and assign the native socket handle
221+
// This works cross-platform (Windows and POSIX)
221222
socket_info->handle = std::make_shared<SocketHandle>(executor_);
222-
#ifdef _WIN32
223-
// On Windows, we need to cast the socket to HANDLE for stream_handle
224-
socket_info->handle->assign(reinterpret_cast<HANDLE>(socket_info->sockfd));
225-
#else
226-
// On POSIX, we can assign the socket descriptor directly
227-
socket_info->handle->assign(socket_info->sockfd);
228-
#endif
223+
224+
// Assign the CURL socket to the ASIO socket
225+
// tcp::socket::assign works with native socket handles on both platforms
226+
boost::system::error_code ec;
227+
socket_info->handle->assign(boost::asio::ip::tcp::v4(), socket_info->sockfd, ec);
228+
229+
if (ec) {
230+
std::cerr << "Failed to assign socket: " << ec.message() << std::endl;
231+
socket_info->handle.reset();
232+
return;
233+
}
229234
}
230235

231236
// Check if action has changed
@@ -255,7 +260,7 @@ void CurlMultiManager::start_socket_monitor(SocketInfo* socket_info,
255260
}
256261

257262
handle->async_wait(
258-
SocketHandle::wait_read,
263+
boost::asio::ip::tcp::socket::wait_read,
259264
[weak_self, sockfd, weak_handle, weak_read_handler](
260265
const boost::system::error_code& ec) {
261266
// If operation was canceled or had an error, don't re-register
@@ -298,7 +303,7 @@ void CurlMultiManager::start_socket_monitor(SocketInfo* socket_info,
298303
}
299304

300305
handle->async_wait(
301-
SocketHandle::wait_write,
306+
boost::asio::ip::tcp::socket::wait_write,
302307
[weak_self, sockfd, weak_handle, weak_write_handler](
303308
const boost::system::error_code& ec) {
304309
// If operation was canceled or had an error, don't re-register

0 commit comments

Comments
 (0)