[cdc_rsync] Add support for ServerSocket on Windows (#48)

Makes ServerSocket multi-platform, mainly by working around some small
API differences. The code is largely the same, there should be no
differences on Linux.

Also moves WSAStartup() and WSACleanup() up to the Socket level as
static methods because it's used by both ClientSocket and ServerSocket,
and because it doesn't make sense to do that in the socket class as
that would prevent one from using several sockets.
This commit is contained in:
Lutz Justen
2022-12-19 23:02:36 +01:00
committed by GitHub
parent d8c2b5906e
commit a138fb55c4
13 changed files with 242 additions and 84 deletions

View File

@@ -80,7 +80,15 @@ cc_library(
cc_library(
name = "socket",
srcs = ["socket.cc"],
hdrs = ["socket.h"],
deps = [
"//common:log",
"//common:platform",
"//common:status",
"//common:util",
"@com_google_absl//absl/status",
],
)
filegroup(

65
cdc_rsync/base/socket.cc Normal file
View File

@@ -0,0 +1,65 @@
/*
* Copyright 2022 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
*
* http://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 "cdc_rsync/base/socket.h"
#include "common/log.h"
#include "common/platform.h"
#include "common/status.h"
#include "common/util.h"
#if PLATFORM_WINDOWS
#include <winsock2.h>
#endif
namespace cdc_ft {
// static
absl::Status Socket::Initialize() {
#if PLATFORM_WINDOWS
WSADATA wsaData;
const int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
return MakeStatus("WSAStartup() failed: %s", Util::GetWin32Error(result));
}
return absl::OkStatus();
#elif PLATFORM_LINUX
return absl::OkStatus();
#endif
}
// static
absl::Status Socket::Shutdown() {
#if PLATFORM_WINDOWS
const int result = WSACleanup();
if (result == SOCKET_ERROR) {
return MakeStatus("WSACleanup() failed: %s",
Util::GetWin32Error(WSAGetLastError()));
}
return absl::OkStatus();
#elif PLATFORM_LINUX
return absl::OkStatus();
#endif
}
SocketFinalizer::~SocketFinalizer() {
absl::Status status = Socket::Shutdown();
if (!status.ok()) {
LOG_ERROR("Socket shutdown failed: %s", status.message())
}
};
} // namespace cdc_ft

View File

@@ -26,6 +26,14 @@ class Socket {
Socket() = default;
virtual ~Socket() = default;
// Calls WSAStartup() on Windows, no-op on Linux.
// Must be called before using sockets.
static absl::Status Initialize();
// Calls WSACleanup() on Windows, no-op on Linux.
// Must be called after using sockets.
static absl::Status Shutdown();
// Send data to the socket.
virtual absl::Status Send(const void* buffer, size_t size) = 0;
@@ -40,6 +48,12 @@ class Socket {
size_t* bytes_received) = 0;
};
// Convenience class that calls Shutdown() on destruction. Logs on errors.
class SocketFinalizer {
public:
~SocketFinalizer();
};
} // namespace cdc_ft
#endif // CDC_RSYNC_BASE_SOCKET_H_

View File

@@ -263,6 +263,12 @@ absl::Status CdcRsyncClient::StartServer() {
return SetTag(MakeStatus("Redeploy server"), Tag::kDeployServer);
}
status = Socket::Initialize();
if (!status.ok()) {
return WrapStatus(status, "Failed to initialize sockets");
}
socket_finalizer_ = std::make_unique<SocketFinalizer>();
assert(is_server_listening_);
status = socket_.Connect(port);
if (!status.ok()) {

View File

@@ -123,6 +123,7 @@ class CdcRsyncClient {
WinProcessFactory process_factory_;
RemoteUtil remote_util_;
PortManager port_manager_;
std::unique_ptr<SocketFinalizer> socket_finalizer_;
ClientSocket socket_;
MessagePump message_pump_{&socket_, MessagePump::PacketReceivedDelegate()};
ConsoleProgressPrinter printer_;

View File

@@ -39,10 +39,10 @@ absl::Status MakeSocketStatus(const char* message) {
} // namespace
struct SocketInfo {
struct ClientSocketInfo {
SOCKET socket;
SocketInfo() : socket(INVALID_SOCKET) {}
ClientSocketInfo() : socket(INVALID_SOCKET) {}
};
ClientSocket::ClientSocket() = default;
@@ -50,12 +50,6 @@ ClientSocket::ClientSocket() = default;
ClientSocket::~ClientSocket() { Disconnect(); }
absl::Status ClientSocket::Connect(int port) {
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
return MakeStatus("WSAStartup() failed: %i", result);
}
addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
@@ -64,14 +58,13 @@ absl::Status ClientSocket::Connect(int port) {
// Resolve the server address and port.
addrinfo* addr_infos = nullptr;
result = getaddrinfo("localhost", std::to_string(port).c_str(), &hints,
&addr_infos);
int result = getaddrinfo("localhost", std::to_string(port).c_str(), &hints,
&addr_infos);
if (result != 0) {
WSACleanup();
return MakeStatus("getaddrinfo() failed: %i", result);
}
socket_info_ = std::make_unique<SocketInfo>();
socket_info_ = std::make_unique<ClientSocketInfo>();
int count = 0;
for (addrinfo* curr = addr_infos; curr; curr = curr->ai_next, count++) {
socket_info_->socket =
@@ -101,7 +94,6 @@ absl::Status ClientSocket::Connect(int port) {
if (socket_info_->socket == INVALID_SOCKET) {
socket_info_.reset();
WSACleanup();
return MakeStatus("Unable to connect to port %i", port);
}
@@ -120,7 +112,6 @@ void ClientSocket::Disconnect() {
}
socket_info_.reset();
WSACleanup();
}
absl::Status ClientSocket::Send(const void* buffer, size_t size) {

View File

@@ -45,7 +45,7 @@ class ClientSocket : public Socket {
size_t* bytes_received) override;
private:
std::unique_ptr<struct SocketInfo> socket_info_;
std::unique_ptr<struct ClientSocketInfo> socket_info_;
};
} // namespace cdc_ft