Skip to content

Commit b84178f

Browse files
authored
Update DebugServer to be non Copyable (#159)
1 parent b4dbb0d commit b84178f

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

Sources/OpenGraphCxx/DebugServer/DebugServer.mm

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// Audited for 6.5.1
66
// Status: Blocked by profile command
77

8+
// TODO:
9+
// 1. select will both fail on AG and OG with different error.
10+
// 2. run(timeout)'s purpose? And update documentation for hpp and h
11+
// 3. Implement profile commands.
12+
813
#include <OpenGraphCxx/DebugServer/DebugServer.hpp>
914
#if OG_TARGET_OS_DARWIN
1015

@@ -107,10 +112,48 @@
107112
char address[32];
108113
uint32_t converted_ip = htonl(ip);
109114
inet_ntop(AF_INET, &converted_ip, address, sizeof(address));
110-
os_log_info(misc_log(), "debug server graph://%s:%d/?token=%u", address, port, token);
115+
os_log(misc_log(), "debug server graph://%s:%d/?token=%u", address, port, token);
111116
fprintf(stderr, "debug server graph://%s:%d/?token=%u\n", address, port, token);
112117
}
113118

119+
OG::DebugServer::DebugServer(DebugServer&& other) OG_NOEXCEPT
120+
: sockfd(other.sockfd)
121+
, ip(other.ip)
122+
, port(other.port)
123+
, token(other.token)
124+
, source(other.source)
125+
, connections(std::move(other.connections))
126+
{
127+
other.sockfd = -1;
128+
other.ip = 0;
129+
other.port = 0;
130+
other.token = 0;
131+
other.source = nullptr;
132+
}
133+
134+
OG::DebugServer& OG::DebugServer::operator=(DebugServer&& other) OG_NOEXCEPT {
135+
if (this != &other) {
136+
shutdown();
137+
for (auto &connection : connections) {
138+
connection.reset();
139+
}
140+
141+
sockfd = other.sockfd;
142+
ip = other.ip;
143+
port = other.port;
144+
token = other.token;
145+
source = other.source;
146+
connections = std::move(other.connections);
147+
148+
other.sockfd = -1;
149+
other.ip = 0;
150+
other.port = 0;
151+
other.token = 0;
152+
other.source = nullptr;
153+
}
154+
return *this;
155+
}
156+
114157
OG::DebugServer::~DebugServer() {
115158
shutdown();
116159
for (auto &connection : connections) {

Sources/OpenGraphCxx/include/OpenGraphCxx/DebugServer/DebugServer.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <OpenGraphCxx/Vector/vector.hpp>
1515
#include <dispatch/dispatch.h>
1616
#include <memory>
17+
#include <swift/bridging>
1718

1819
OG_ASSUME_NONNULL_BEGIN
1920

@@ -49,6 +50,23 @@ class DebugServer {
4950
/// @param mode The operating mode for the debug server
5051
DebugServer(OGDebugServerMode mode);
5152

53+
/// Move constructor for transferring ownership of server resources.
54+
///
55+
/// @param other The DebugServer instance to move from
56+
DebugServer(DebugServer&& other) OG_NOEXCEPT;
57+
58+
/// Move assignment operator for transferring ownership of server resources.
59+
///
60+
/// @param other The DebugServer instance to move from
61+
/// @return Reference to this instance after the move
62+
DebugServer& operator=(DebugServer&& other) OG_NOEXCEPT;
63+
64+
/// Deleted copy constructor to prevent accidental copying.
65+
DebugServer(const DebugServer&) = delete;
66+
67+
/// Deleted copy assignment operator to prevent accidental copying.
68+
DebugServer& operator=(const DebugServer&) = delete;
69+
5270
/// Destroys the debug server and cleans up all resources.
5371
/// Automatically closes all active connections and stops the server.
5472
~DebugServer();
@@ -57,7 +75,7 @@ class DebugServer {
5775
///
5876
/// @return A CFURLRef containing the server URL, or nullptr if not running.
5977
/// The caller is responsible for releasing the returned URL.
60-
CFURLRef _Nullable copy_url() const;
78+
CFURLRef _Nullable copy_url() const SWIFT_RETURNS_INDEPENDENT_VALUE;
6179

6280
/// Shuts down the debug server and closes all connections.
6381
/// Called internally during destruction or explicit stop.

Tests/OpenGraphCxxTests/DebugServer/DebugServerTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct DebugServerTests {
2525
@Test
2626
func commandTest() async throws {
2727
let debugServer = OG.DebugServer([.valid])
28-
let url = try #require(debugServer.copy_url()) as URL
28+
let cfURL = debugServer.copy_url()
29+
let url = try #require(cfURL) as URL
2930
let components = try #require(URLComponents(url: url, resolvingAgainstBaseURL: false))
3031
let token = try #require(components.queryItems?.first { $0.name == "token" }?.value.flatMap { UInt32($0) })
3132
debugServer.run(1)

0 commit comments

Comments
 (0)