System UnrealPlugin TCP SingleTcpConnection - kcccr123/ue-reinforcement-learning GitHub Wiki
SingleTcpConnection
USingleTcpConnection
is a subclass of UBaseTcpConnection
specifically designed for use with single-agent, single-environment training scenarios. It accepts exactly two client sockets: one admin and one environment. This class manages both sockets and includes a threaded background process to asynchronously accept connections.
Overview
This connection type supports one control (admin) connection and one simulation (environment) connection. It is primarily used in conjunction with SingleEnvBridge
. Once both sockets are connected, the system is considered fully ready for communication. Appends \n
delimiter at the end of each outbound message.
Connection Lifecycle
bool StartListening(const FString& IPAddress, int32 Port)
Creates a TCP listening socket bound to the given address and port, configured to accept up to two clients. Spawns the acceptance thread internally. Logs success or failure.
virtual bool AcceptEnvConnection(FSocket* InNewSocket) override
Handles environment socket acceptance. If an EnvSocket
already exists, the new connection is closed and rejected. If accepted, the accept thread is stopped via FAcceptRunnable::Stop()
.
virtual bool CloseConnection() override
Shuts down and deletes the listening socket, admin socket, and environment socket. Also stops and destroys the accept thread. This should be called when tearing down the connection cleanly.
bool IsConnected() const override
Returns true
only if both AdminSocket
and EnvSocket
are valid and connected.
Messaging API
bool SendMessageEnv(const FString& Data)
Sends a UTF-8 message to the environment socket. Returns true
if the message was sent successfully. Logs failures.
FString ReceiveMessageEnv(int32 BufSize = 1024)
Reads available UTF-8 data from the environment socket and returns it as an FString
. Returns empty if nothing is available or socket is disconnected.
bool SendMessageAdmin(const FString& Data)
Inherited from base. Sends a UTF-8 message to the admin socket.
FString ReceiveMessageAdmin(int32 BufSize = 1024)
Inherited from base. Receives a UTF-8 string from the admin socket.
Acceptance Thread
USingleTcpConnection
launches a background thread via FAcceptRunnable
to accept connections asynchronously. The thread logic is defined and managed separately from the connection logic.
void StartAcceptThread()
Creates and starts the SingleEnvAcceptThread
using an FAcceptRunnable
. Logs thread start and failure messages.
AcceptRunnableRef
is assigned a new instance ofFAcceptRunnable
.AcceptThreadRef
is created with that runnable.- If
AcceptThreadRef
is null, logs an error.
FAcceptRunnable (Threaded Worker)
Implements FRunnable
and runs in its own thread during StartListening()
.
Constructor
FAcceptRunnable::FAcceptRunnable(USingleTcpConnection* InOwner)
Stores a reference to the USingleTcpConnection
that owns the thread.
uint32 Run()
Main thread loop:
- While not stopped and the listening socket is valid:
- Check for pending connections.
- Call
Owner->AcceptConnection()
if one is available. - Sleep for a short interval to reduce CPU usage.
void Stop()
Sets a thread-safe boolean flag to exit the loop cleanly.
Thread is stopped either when an environment connection is accepted, or explicitly by CloseConnection()
.
Internal State & Members
FSocket* EnvSocket
— Dedicated socket for communication with the Python environment.AdminSocket
andListeningSocket
— inherited from base.FRunnableThread* AcceptThreadRef
— Background thread for connection acceptance.TSharedPtr<FAcceptRunnable> AcceptRunnableRef
— Runnable object polled in the background.bool bStopAcceptThreadRef
— Control flag to exit the thread loop.
Logging
Extensive logging is included at all key operations:
- Socket creation and teardown
- Data transmission success and failure
- Thread lifecycle
- Connection acceptance and rejection
This provides transparent debugging during setup and runtime.