ralpha-assets/Plugins/RalphaPlugin/Source/RalphaCore/Public/RalphaMCPServer.h

99 lines
2.2 KiB
C++

// Copyright Ralpha Team. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Sockets.h"
#include "SocketSubsystem.h"
#include "Containers/Ticker.h"
#include "RalphaMCPServer.generated.h"
class FRalphaClientConnection;
class URalphaParameterBridge;
class URalphaScreenCapture;
DECLARE_LOG_CATEGORY_EXTERN(LogRalphaMCP, Log, All);
/**
* TCP Server that receives JSON-RPC commands from the Python MCP server
* and executes them against UE5 systems.
*/
UCLASS(BlueprintType)
class RALPHACORE_API URalphaMCPServer : public UObject
{
GENERATED_BODY()
public:
URalphaMCPServer();
virtual ~URalphaMCPServer();
/** Get the singleton instance */
UFUNCTION(BlueprintCallable, Category = "Ralpha")
static URalphaMCPServer* Get();
/** Start the TCP server */
UFUNCTION(BlueprintCallable, Category = "Ralpha")
bool Start(int32 Port = 30010);
/** Stop the TCP server */
UFUNCTION(BlueprintCallable, Category = "Ralpha")
void Stop();
/** Check if server is running */
UFUNCTION(BlueprintCallable, Category = "Ralpha")
bool IsRunning() const;
/** Get the port number */
UFUNCTION(BlueprintCallable, Category = "Ralpha")
int32 GetPort() const { return ServerPort; }
/** Process a JSON command and return response */
FString ProcessCommand(const FString& JsonCommand);
protected:
/** Tick function to accept new connections */
bool Tick(float DeltaTime);
private:
static URalphaMCPServer* Instance;
FSocket* ListenerSocket;
int32 ServerPort;
bool bIsRunning;
TArray<TSharedPtr<FRalphaClientConnection>> ClientConnections;
UPROPERTY()
URalphaParameterBridge* ParameterBridge;
UPROPERTY()
URalphaScreenCapture* ScreenCapture;
FTSTicker::FDelegateHandle TickDelegateHandle;
};
/**
* Handles a single client connection
*/
class RALPHACORE_API FRalphaClientConnection : public TSharedFromThis<FRalphaClientConnection>
{
public:
FRalphaClientConnection(FSocket* InSocket, URalphaMCPServer* InServer);
~FRalphaClientConnection();
/** Process incoming data */
void Tick();
/** Check if connection is valid */
bool IsValid() const;
/** Send response to client */
void SendResponse(const FString& Response);
private:
FSocket* Socket;
URalphaMCPServer* Server;
FString ReceiveBuffer;
};