Install this package:
$ jget get rotnetz
rotnetz Networking Architecture
This document provides an overview of the rotnetz networking architecture, explaining how remote procedure calls (RPC) work and how responses are delivered.
Overview
rotnetz is a Lua library for ComputerCraft that enables remote procedure calls between computers over the rednet network. It provides a client-server architecture where:
- Hosts expose methods that can be called remotely
- Clients make requests to hosts and receive responses
Protocol Structure
rotnetz uses multiple protocols to manage the request-response cycle:
Main Protocol
- Format:
{networkMask}{protocol} - Purpose: Used by clients to send requests to hosts
- Example: If networkMask is "myNetwork" and protocol is "myProtocol", the full protocol name is "myNetworkmyProtocol"
Direct Protocol
- Format:
{networkMask}direct - Purpose: Used for acknowledgments and direct synchronous responses
- Example: "myNetworkdirect"
Receive Protocols
- Synchronous:
{networkMask}callReceive - Asynchronous:
{networkMask}callReceiveAsync - Purpose: Used by hosts to send responses back to clients
- Example: "myNetworkcallReceive"
Message Flow
Synchronous RPC
-
Client sends request:
- Client sends a message to the host via the main protocol
- Message contains: methodName, parameters, and the receive protocol for the response
-
Host acknowledges:
- Host immediately sends "ack" back to the client via the direct protocol
- This confirms the request was received
-
Client waits for response:
- Client blocks and listens on the receive protocol (synchronous mode)
-
Host processes request:
- Host looks up the requested method
- Executes the method with provided parameters
- Catches any errors using pcall
-
Host sends response:
- Host sends response message via the receive protocol specified in the original request
- Response contains: state (true/false), result (data or error message)
-
Client receives response:
- Client unblocks and receives the response message
- Returns the state and result to the caller
Asynchronous RPC
-
Client sends request:
- Client sends a message with
async = trueflag - Client does not wait for acknowledgment
- Client sends a message with
-
Host processes request:
- Host executes the method asynchronously
- Host sends response to the async receive protocol
-
Client receives response later:
- Client must set up a receiver thread that listens on the async receive protocol
- Responses are placed in a queue
- Client polls the queue to get responses when ready
Threading Model
Host Architecture
Hosts use a multi-threaded architecture for better performance:
-
Receiver Thread(s): Listens on the main protocol and receives incoming requests
- Puts messages into a shared queue
- Sends "ack" for synchronous requests
-
Handler Thread(s): Processes messages from the queue
- Looks up and executes the requested method
- Constructs and sends responses
-
Separation of concerns: Receiving and processing happen in parallel for better throughput
Client Architecture
Clients can operate in two modes:
- Synchronous: Blocks while waiting for response
- Asynchronous: Sets up a receiver thread to handle responses in the background
Response Handling
Response Message Structure
All responses from hosts contain:
Synchronous Response Flow
Asynchronous Response Flow
Best Practices
For Clients
- Use synchronous calls when you need immediate results and can afford to block
- Use asynchronous calls when you need to make multiple requests or continue processing while waiting
- Always handle errors - check the
statefield of responses - For async operations, set up a receiver thread before making async calls
For Hosts
- Keep methods fast - handler threads are blocking during method execution
- Handle errors gracefully - use try/catch or pcall to prevent crashes
- Provide good descriptions when registering methods
- Consider threading - if methods are CPU-intensive, the host's performance may degrade
Example Workflow
Synchronous Call
Asynchronous Call
Common Pitfalls
- Missing receiver thread: Forgetting to set up a receiver thread for async operations
- Wrong protocol: Using the wrong protocol for responses
- Timeout issues: Not setting appropriate timeouts for synchronous calls
- Blocking calls: Making synchronous calls in handler threads can cause deadlocks
- Error handling: Not checking the
statefield of responses
See Also
- Client API Documentation - Detailed client API reference
- Host API Documentation - Detailed host API reference