ObjectDeliverer

repository·master·Indexed 41 days ago

https://github.com/ayumaxsoft/objectdeliverer

A flexible data communication library for Unreal Engine supporting C++ and Blueprints. It provides a modular architecture to switch between protocols including TCP/IP, UDP, WebSocket, Shared Memory, and file-based communication, with configurable data division rules and serialization methods for byte arrays, UTF-8 strings, and JSON objects.

Tokens
2.3K
Snippets
7
Records
12
Agent score
52%

What's inside ObjectDeliverer

  1. WebSocket Echo Server specifications and features

    master

    The WebSocket Echo Server is a simple utility that echoes back any received text or binary messages to the client.

    Technical Specifications:

    • Host: 0.0.0.0 (listens on all network interfaces)
    • Port: 8765
    • Protocol: WebSocket (ws://)
    • Message Formats: Supports both text and binary data

    Key Features:

    • Accepts multiple simultaneous client connections.
    • Logs all connections and incoming messages to the console.
    • Supports safe shutdown via Ctrl+C.
  2. How ObjectDeliverer works: Manager, Protocols, and Rules

    master

    ObjectDeliverer uses a modular architecture to handle data communication. To establish a connection, you follow a three-step pattern:

    1. Create a Manager: Instantiate an ObjectDelivererManager to act as the central controller.
    2. Set up Event Handlers: Bind logic to events such as Connected, Disconnected, and ReceiveData to react to network state changes and incoming information.
    3. Configure and Start: Pass a Protocol (defining how to connect, e.g., TCP/IP, UDP, WebSocket) and a Packet Rule (defining how to split/reconstruct data, e.g., FixedSize, Header+Body) to the Start() method.

    This separation allows you to switch from a TCP/IP server to a WebSocket client or change your data division logic without rewriting your core application logic.

  3. Install ObjectDeliverer from GitHub

    master

    To use the open-source version of ObjectDeliverer, follow these steps:

    1. Clone the repository:
      git clone https://github.com/ayumax/ObjectDeliverer.git
    2. Select the correct branch: Choose the branch matching your Unreal Engine version (e.g., UE5.4).
      git checkout UE5.4
    3. Copy the plugin: Move the Plugins directory from the cloned repository into your Unreal Engine project folder.
    4. Build the plugin: Ensure you have a C++ build environment configured for your project.
    5. Enable the plugin: Open your project in Unreal Engine, navigate to Edit → Plugins, and enable ObjectDeliverer.
    git clone https://github.com/ayumax/ObjectDeliverer.git
    git checkout UE5.4
  4. Run ObjectDeliverer WebSocket Tests

    master

    You can run automated WebSocket tests using the Unreal Automation tool.

    Using a Mock Server (Default)

    By default, tests use ws://localhost:8080 as a mock server:

    /path/to/UnrealEditor-Cmd YourProject.uproject -ExecCmds="Automation RunTests ObjectDeliverer.ProtocolWebSocket; quit"

    Using a Real WebSocket Server

    To test against a specific server, set the following environment variables:

    • OBJECTDELIVERER_TEST_WS_URL: The WebSocket server URL (e.g., wss://your-server.com).
    • OBJECTDELIVERER_TEST_WS_TOKEN: The authentication token.
    export OBJECTDELIVERER_TEST_WS_URL="wss://your-server.com"
    export OBJECTDELIVERER_TEST_WS_TOKEN="your-token"
    /path/to/UnrealEditor-Cmd YourProject.uproject -ExecCmds="Automation RunTests ObjectDeliverer.ProtocolWebSocket; quit"
  5. Implement ObjectDeliverer in C++

    master

    To use ObjectDeliverer in C++, create an ObjectDelivererManager, bind to its dynamic delegates, and call Start with a protocol and packet rule.

    void UMyClass::Start()
    {
        auto deliverer = UObjectDelivererManager::CreateObjectDelivererManager();
    
        // Set up event handlers
        deliverer->Connected.AddDynamic(this, &UMyClass::OnConnect);
        deliverer->Disconnected.AddDynamic(this, &UMyClass::OnDisConnect);
        deliverer->ReceiveData.AddDynamic(this, &UMyClass::OnReceive);
    
        // Start communication
        // Protocol: TCP/IP Server
        // Data division rule: Header(Size) + Body
        // Serialization method: Byte array
        deliverer->Start(UProtocolFactory::CreateProtocolTcpIpServer(9099),
                         UPacketRuleFactory::CreatePacketRuleSizeBody());
    }
    
    void UMyClass::OnConnect(UObjectDelivererProtocol* ClientSocket)
    {
        // Send data
        TArray<uint8> buffer;
        deliverer->Send(buffer);
    }
    
    void UMyClass::OnDisConnect(UObjectDelivererProtocol* ClientSocket)
    {
        // Handle disconnection
        UE_LOG(LogTemp, Log, TEXT("closed"));
    }
    
    void UMyClass::OnReceive(UObjectDelivererProtocol* ClientSocket, const TArray<uint8>& Buffer)
    {
        // Handle received data
    }
  6. Available Communication Protocols

    master

    ObjectDeliverer supports several protocols for different communication needs. You can use the built-in factories to create these protocols:

    • TCP/IP Server: Connects to multiple clients.
    • TCP/IP Client: Connects to a specific server.
    • WebSocket Client: Connects to a WebSocket server.
    • UDP (Sender): Sends UDP data.
    • UDP (Receiver): Receives UDP data.
    • Shared Memory: Windows-specific process-to-process communication.
    • File Writer: Saves data to a file.
    • File Reader: Reads data from a file
  7. Available Data Division Rules

    master

    Data division rules determine how the library reconstructs packets from a raw byte stream. Supported rules include:

    • FixedSize: Uses a constant number of bytes for every packet.
    • Header(BodySize) + Body: Reads a specific number of bytes (e.g., 4 bytes) from the header to determine the length of the subsequent body.
    • Split by terminal symbol: Uses a specific byte (e.g., 0x00) as a delimiter to mark the end of a packet.
    • No division: Uses the received buffer as-is without any splitting or combining operations.
  8. Run the WebSocket Echo Server

    master

    The server.py script implements a basic WebSocket echo server. When a client connects, the server logs the connection, receives messages, and immediately echoes the same message back to the client. The server runs on 0.0.0.0:8765 by default and handles client disconnections gracefully.

    # To run the server, execute the script directly
    python Tools/WebSocketServer/server.py
  9. Implement a custom client handler with handle_client

    master

    The handle_client(websocket) function is the core asynchronous handler for incoming WebSocket connections. It manages the lifecycle of a single connection:

    1. Connection: Logs the remote_address of the new client.
    2. Message Loop: Uses async for message in websocket to listen for incoming data.
    3. Echo Logic: Sends the received message back to the client using await websocket.send(message).
    4. Error Handling: Catches websockets.exceptions.ConnectionClosed for clean disconnects and general Exception for other errors.
    async def handle_client(websocket):
        client_address = websocket.remote_address
        logger.info(f"New client connected: {client_address}")
    
        try:
            async for message in websocket:
                logger.info(f"Received from {client_address}: {message}")
                await websocket.send(message)
                logger.info(f"Echoed back to {client_address}: {message}")
        except websockets.exceptions.ConnectionClosed:
            logger.info(f"Client disconnected: {client_address}")
        except Exception as e:
            logger.error(f"Error handling client {client_address}: {e}")