Interface MyraServerHandler
Implementations of this interface receive notifications for the three main connection lifecycle events: connect, data received, and disconnect. The handler is invoked on the server's event loop thread, so implementations should be non-blocking to maintain high throughput.
Lifecycle
For each client connection, callbacks are invoked in this order:
onConnect(TransportBackend)- called once when connection is acceptedonDataReceived(TransportBackend, RegisteredBuffer, int)- called for each data chunkonDisconnect(TransportBackend)- called once when connection closes
Buffer Ownership
The buffer passed to onDataReceived(TransportBackend, RegisteredBuffer, int) is owned by the server's buffer pool. The handler
may read from the buffer and optionally use it for a response, but must not hold references
beyond the callback scope unless explicitly acquired from the pool.
Thread Safety
All callbacks for a given connection are invoked on the same thread (the server's event loop). Callbacks for different connections may occur concurrently if the server uses multiple event loops.
Example Implementation
public class EchoHandler implements MyraServerHandler {
@Override
public void onConnect(TransportBackend connection) {
System.out.println("Client connected");
}
@Override
public void onDataReceived(TransportBackend connection,
RegisteredBuffer buffer, int length) {
// Echo the data back to the client
connection.send(buffer, 0);
}
@Override
public void onDisconnect(TransportBackend connection) {
System.out.println("Client disconnected");
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidonConnect(express.mvp.myra.transport.TransportBackend connection) Called when a new client connection is accepted.voidonDataReceived(express.mvp.myra.transport.TransportBackend connection, express.mvp.myra.transport.RegisteredBuffer buffer, int length) Called when data is received from a client.voidonDisconnect(express.mvp.myra.transport.TransportBackend connection) Called when a client connection is closed.
-
Method Details
-
onConnect
void onConnect(express.mvp.myra.transport.TransportBackend connection) Called when a new client connection is accepted.This callback provides an opportunity to initialize per-connection state or perform handshake operations. The connection can be used to send data or configure read operations.
- Parameters:
connection- the transport backend for the new connection; can be used to send data or configure I/O
-
onDataReceived
void onDataReceived(express.mvp.myra.transport.TransportBackend connection, express.mvp.myra.transport.RegisteredBuffer buffer, int length) Called when data is received from a client.The buffer contains the received data from position 0 to
length. The handler may:- Read the data directly from the buffer
- Use the buffer to send a response (for zero-copy echo patterns)
- Copy the data if it needs to be retained after the callback returns
Important: The buffer is returned to the pool after this callback unless it is passed to
TransportBackend.send(RegisteredBuffer, long). Do not hold references to the buffer after the callback returns.- Parameters:
connection- the transport backend for the connectionbuffer- the registered buffer containing received datalength- the number of bytes received (valid data from 0 to length-1)
-
onDisconnect
void onDisconnect(express.mvp.myra.transport.TransportBackend connection) Called when a client connection is closed.This callback is invoked when the connection is closed, either by the client, due to an error, or when the server explicitly closes it. Use this to clean up any per-connection resources.
After this callback, the connection should not be used for any I/O operations.
- Parameters:
connection- the transport backend for the closed connection
-