System UnrealPlugin TCP BaseTcpConnection - kcccr123/ue-reinforcement-learning GitHub Wiki
BaseTcpConnection
UBaseTcpConnection
defines a shared foundation for handling TCP connections between Unreal Engine and external Python clients. It is used as a superclass for specialized implementations like USingleTcpConnection
, and provides support for listening sockets, message transmission, handshake management, and connection delegation.
Overview
This class listens for incoming TCP connections and assigns the first to the "admin" role, with subsequent sockets handled by child implementations. It abstracts message send/receive logic and exposes a common interface for socket-based interaction.
Admin socket acts as a control interface for the python module to manage the training process.
It does not start threads on its own, but is intended to integrate cleanly with threaded accept loops.
Connection Lifecycle
bool StartListening(const FString& IPAddress, int32 Port) (pure virtual)
To be implemented by subclasses. Should bind a listening socket to the specified IP/Port and optionally start an acceptance thread.
bool AcceptConnection()
Accepts a pending connection from the listening socket.
- First connection becomes the admin socket.
- Additional connections are routed to
AcceptEnvConnection()
. - Returns
true
on success; logs and returnsfalse
on failure.
virtual bool AcceptEnvConnection(FSocket* InNewSocket) (pure virtual)
Must be implemented by subclasses. Defines what to do with environment connections.
virtual void CloseConnection() (pure virtual)
Shuts down any listening sockets, threads, and client sockets.
FSocket* GetListeningSocket()
Returns the active listening socket pointer. Used in threaded polling or external socket validation.
virtual void StartAcceptThread() (pure virtual)
To be implemented by subclasses. Launches a background thread that polls ListeningSocket
and calls AcceptConnection()
when a client connects.
Messaging API
bool SendMessageAdmin(const FString& Data)
Sends a UTF-8 message to the admin socket. Logs failure and returns false
if the socket is unavailable.
FString ReceiveMessageAdmin(int32 BufSize = 1024)
Receives a UTF-8 string from the admin socket, if data is available. Returns an empty string otherwise.
virtual bool SendMessageEnv(const FString& Data) (pure virtual)
Subclasses must define how to send data to the environment connection(s).
virtual FString ReceiveMessageEnv(int32 BufSize = 1024) (pure virtual)
Subclasses must define how to receive data from environment socket(s).
Handshake
void SetHandshake(const FString& InHandshakeMsg)
Stores a string to be sent to the admin socket after a successful connection.
void SendHandshake()
Immediately sends the stored handshake string to the admin socket.
Internal State & Members
FSocket* ListeningSocket
— The TCP server socket accepting incoming connections.FSocket* AdminSocket
— The first client that connects; reserved for handshake/config.FString HandshakeMessage
— Stored handshake configuration string.FRunnableThread* AcceptThreadRef
— Optional background thread for socket polling (managed by subclasses).TSharedPtr<FAcceptRunnable>
— Threaded runnable used inUSingleTcpConnection
to poll for connections.bool bStopAcceptThreadRef
— Flag shared with thread to trigger shutdown.
Logging
UBaseTcpConnection
logs all major events, including:
- Socket binding success/failure
- Connection acceptance
- Message transmission
- Read/write errors
This aids in debugging both bridge-side and Python-side communication.