Skip to content

Rust — Server API

The server API binds a transport, accepts sessions, receives submits and control messages, and emits results, progress, flow-control feedback, object/cache events, and close acknowledgements.

Workflow

  1. Build NnrpServerConfig.
  2. Bind with NnrpServer::bind_tcp or a transport provider.
  3. Accept a session with NnrpServer::accept.
  4. Receive work with receive_submit.
  5. Send output with send_result, send_partial_result, or send_progress.
  6. Receive runtime-control frames with receive_runtime_control.
  7. Close the session explicitly.

NnrpServer::bind_tcp

ParameterTypeRequiredValues / RangeDescription
addrimpl tokio::net::ToSocketAddrsYesSocket addressLocal TCP bind address.
configNnrpServerConfigYestransport = TcpServer runtime configuration.
ReturnsErrors
Result<NnrpServer, RuntimeError>Bind, listener, transport, or configuration errors.
rust
let config = NnrpServerConfig::default().with_transport(RuntimeTransportKind::Tcp);
let server = NnrpServer::bind_tcp("127.0.0.1:4433", config).await?;

Provider Bind

ProviderPackageTypical methodDescription
TcpProvidernnrp-transport-tcpbind(addr, config)TCP listener.
QuicProvidernnrp-transport-quicbind(endpoint_config, config)QUIC listener with certificate and ALPN config.
IpcProvidernnrp-transport-ipcbind(endpoint, config)Unix socket or Windows named pipe listener.
WebSocketProvidernnrp-transport-websocketbind(endpoint, config)Native WebSocket listener for binary frames.

NnrpServer::from_listener

ParameterTypeRequiredValues / RangeDescription
listenerL: FramedListener + 'staticYesAny framed listenerCustom or provider-created listener.
configNnrpServerConfigYesMust match listener kindRuntime configuration.
ReturnsErrors
Result<NnrpServer, RuntimeError>Listener-kind mismatch or invalid configuration.

NnrpServer::accept

ParameterTypeRequiredValues / RangeDescription
None---Accepts one peer and opens one runtime session.
ReturnsErrors
Result<NnrpServerSession, RuntimeError>Accept, session-open rejection, or transport errors.

NnrpServerSession::receive_submit

ParameterTypeRequiredValues / RangeDescription
None---Reads the next submit frame.
ReturnsErrors
Result<NnrpSubmit, RuntimeError>Transport, parse, lifecycle, or unexpected-message errors.
rust
let submit = session.receive_submit().await?;

NnrpServerSession::send_result

ParameterTypeRequiredValues / RangeDescription
frame_idu32YesSubmitted frame idCorrelates the result.
metadataResultPushMetadataYesValid result metadataResult status and timing metadata.
bodyVec<u8>YesMay be emptySerialized result body.
ReturnsErrors
Result<(), RuntimeError>Lifecycle, serialization, or transport errors.
rust
session
    .send_result(submit.frame_id, ResultPushMetadata::default(), output)
    .await?;

Streaming Result Methods

MethodParametersReturnsDescription
send_partial_resultframe id, metadata, bodyResult<(), RuntimeError>Sends incremental result bytes.
send_progressmetadata, bodyResult<(), RuntimeError>Sends progress or status updates.
send_result_drop_reasonframe id, metadata, bodyResult<(), RuntimeError>Sends a structured drop reason.
send_result_drop_reason_with_diagnosticsframe id, metadata, diagnosticsResult<(), RuntimeError>Sends a drop reason with diagnostic context.

Runtime Control Methods

MethodParametersReturnsDescription
receive_cancelnoneResult<CancelMetadata, RuntimeError>Receives cancellation.
receive_runtime_controlnoneResult<NnrpRuntimeControl, RuntimeError>Receives generic Preview4 control frames with metadata and body.
send_backpressuremetadataResult<(), RuntimeError>Tells the client to slow down.
receive_pressure_updatenoneResult<PressureUpdateMetadata, RuntimeError>Receives client-side pressure state.
send_capabilitymetadataResult<(), RuntimeError>Sends supported cost/preference/limit information.
send_route_hintmetadataResult<(), RuntimeError>Sends execution or routing hints.

Object And Cache Methods

MethodParametersReturnsDescription
send_object_declaremetadata, bodyResult<(), RuntimeError>Declares a runtime object.
send_object_refmetadata, bodyResult<(), RuntimeError>References an existing object.
send_object_releasemetadata, bodyResult<(), RuntimeError>Releases an object reference.
send_object_deltametadata, bodyResult<(), RuntimeError>Sends object delta bytes.
send_cache_referencemetadata, bodyResult<(), RuntimeError>Sends a cache hit/reference.
send_cache_missmetadata, bodyResult<(), RuntimeError>Reports a miss.
send_cache_invalidatemetadata, bodyResult<(), RuntimeError>Invalidates a cache entry.

Lifecycle Methods

MethodParametersReturnsDescription
receive_closenoneResult<SessionCloseMetadata, RuntimeError>Waits for client close.
ack_closeclose metadataResult<(), RuntimeError>Acknowledges close.
closenoneResult<(), RuntimeError>Closes the server session.

NnrpServerConfig

FieldTypeDefaultDescription
transportRuntimeTransportKindTcpRuntime transport slot.
supported_profilesVec<u16>Standard token profileAccepted profiles.
supported_cache_objectsVec<CacheObjectKind>EmptyAccepted cache object kinds.
schema_registrySchemaRegistryStandard registryAccepted schemas.
max_in_flight_operationsu164In-flight operation limit.
granted_operation_creditu162Initial operation credit.
lease_ttl_msu3230000Lease TTL.
resume_window_msu32120000Resume window.
application_policyArc<dyn NnrpServerPolicy>Allow-allApplication validation policy.

NnrpSubmit

FieldTypeDescription
frame_idu32Submitted frame id.
metadataFrameSubmitMetadataSubmit metadata.
bodyVec<u8>Submit body bytes.

WARNING

receive_submit is intentionally narrow. If submit, cancel, progress, and close can interleave in your application, build a loop that dispatches runtime packets into the appropriate receive/send methods instead of assuming a single request/result sequence.

NNRP Documentation