@@ -19,63 +19,143 @@ OG_ASSUME_NONNULL_BEGIN
1919
2020OG_IMPLICIT_BRIDGING_ENABLED
2121
22+ // / Message header structure for debug server communication protocol.
23+ // / Contains metadata for messages exchanged between the debug server and clients.
2224typedef struct OG_SWIFT_NAME (DebugServerMessageHeader) OGDebugServerMessageHeader {
25+ // / Authentication token for the message
2326 uint32_t token;
27+ // / Reserved field for future use
2428 uint32_t unknown;
29+ // / Length of the message payload in bytes
2530 uint32_t length;
31+ // / Additional reserved field for protocol extensions
2632 uint32_t unknown2;
2733} OGDebugServerMessageHeader; /* OGDebugServerMessageHeader */
2834
2935namespace OG {
36+
37+ // / Debug server for OpenGraph runtime inspection and debugging.
38+ // /
39+ // / The DebugServer provides a network interface for external tools to connect
40+ // / and inspect the internal state of OpenGraph. It supports both listening for
41+ // / incoming connections and handling client requests for graph data.
42+ // /
43+ // / The server operates in different modes as specified by OGDebugServerMode
44+ // / and manages multiple concurrent client connections using GCD dispatch sources.
3045class DebugServer {
3146public:
47+ // / Creates a new debug server instance with the specified mode.
48+ // /
49+ // / @param mode The operating mode for the debug server
3250 DebugServer (OGDebugServerMode mode);
51+
52+ // / Destroys the debug server and cleans up all resources.
53+ // / Automatically closes all active connections and stops the server.
3354 ~DebugServer ();
3455
56+ // / Returns a copy of the URL where the debug server is accessible.
57+ // /
58+ // / @return A CFURLRef containing the server URL, or nullptr if not running.
59+ // / The caller is responsible for releasing the returned URL.
3560 CFURLRef _Nullable copy_url () const ;
61+
62+ // / Shuts down the debug server and closes all connections.
63+ // / Called internally during destruction or explicit stop.
64+ void shutdown ();
65+
66+ // / Runs the debug server for the specified timeout duration.
67+ // /
68+ // / @param timeout Maximum time in seconds to run the server.
69+ // / Use -1 for indefinite operation.
3670 void run (int timeout) const ;
3771
72+ // / Returns the shared debug server instance.
73+ // /
74+ // / @return Pointer to the shared server, or nullptr if none exists
3875 static OG_INLINE DebugServer *shared_server () { return _shared_server; }
76+
77+ // / Checks if a shared debug server instance exists.
78+ // /
79+ // / @return true if a shared server is active, false otherwise
3980 static OG_INLINE bool has_shared_server () { return _shared_server != nullptr ; }
4081
82+ // / Starts a new shared debug server on the specified port.
83+ // /
84+ // / @param port The TCP port number to bind the server to
85+ // / @return Pointer to the started server, or nullptr if startup failed
4186 static DebugServer *_Nullable start (unsigned int port);
87+
88+ // / Stops the shared debug server and releases all resources.
89+ // / This will close all client connections and shut down the server.
4290 static void stop ();
4391
4492private:
93+ // / Represents a single client connection to the debug server.
94+ // /
95+ // / Each Connection manages its own socket descriptor and dispatch source
96+ // / for handling incoming data from a connected client. Connections are
97+ // / automatically cleaned up when the client disconnects.
4598 class Connection {
4699 public:
100+ // / Creates a new connection for the specified server and socket.
101+ // /
102+ // / @param server The debug server that owns this connection
103+ // / @param descriptor The socket file descriptor for the client
47104 Connection (DebugServer *server,int descriptor);
105+
106+ // / Destroys the connection and cleans up resources.
107+ // / Automatically closes the socket and cancels the dispatch source.
48108 ~Connection ();
49109
110+ // / Static handler function for processing incoming connection data.
111+ // /
112+ // / @param context Pointer to the Connection instance
50113 static void handler (void *_Nullable context);
51114
52115 friend class DebugServer ;
53116 private:
54- DebugServer *server;
55- int sockfd;
56- dispatch_source_t source;
117+ DebugServer *server; // /< Owning debug server
118+ int sockfd; // /< Client socket file descriptor
119+ dispatch_source_t source; // /< GCD source for socket events
57120 }; /* Connection */
58121
122+ // / Receives and processes a message from the specified connection.
123+ // /
124+ // / @param connection The client connection to receive from
125+ // / @param header Reference to store the received message header
126+ // / @param data Existing data buffer to append to, or nullptr for new data
127+ // / @return CFDataRef containing the complete message, or nullptr on error
59128 CFDataRef _Nullable receive (Connection *connection, OGDebugServerMessageHeader &header, CFDataRef data);
129+
130+ // / Closes and removes the specified client connection.
131+ // /
132+ // / @param connection The connection to close and clean up
60133 void close_connection (Connection *connection);
61- void shutdown ();
62134
135+ // / Static handler for accepting new client connections.
136+ // /
137+ // / @param context Pointer to the DebugServer instance
63138 static void accept_handler (void *_Nullable context);
64139
65- int32_t sockfd;
66- uint32_t ip;
67- uint32_t port;
68- uint32_t token;
69- _Nullable dispatch_source_t source;
70- OG::vector<std::unique_ptr<Connection>, 0 , u_long> connections;
140+ int32_t sockfd; // /< Server socket file descriptor
141+ uint32_t ip; // /< Server IP address
142+ uint32_t port; // /< Server port number
143+ uint32_t token; // /< Authentication token
144+ _Nullable dispatch_source_t source; // /< GCD source for accepting connections
145+ OG::vector<std::unique_ptr<Connection>, 0 , u_long> connections; // /< Active client connections
71146
72- static DebugServer *_Nullable _shared_server;
147+ static DebugServer *_Nullable _shared_server; // /< Global shared server instance
73148}; /* DebugServer */
74149
75150} /* OG */
76151
152+ // / C-compatible storage wrapper for DebugServer instances.
153+ // /
154+ // / This structure provides a C-compatible interface for storing
155+ // / DebugServer objects in contexts where C++ objects cannot be
156+ // / used directly.
77157typedef struct OGDebugServerStorage {
78- OG::DebugServer debugServer;
158+ OG::DebugServer debugServer; // /< The wrapped DebugServer instance
79159} OGDebugServerStorage;
80160
81161OG_IMPLICIT_BRIDGING_DISABLED
0 commit comments