Connection#
This page covers opening, reading and closing a connection. The messages are described under Connection Handshake and Disconnecting.
Connecting#
connection-tcpip [tcpip] [MUST]The client connects over TCP/IP to an address and a port.
connection-default-address [core] [MUST]Over TCP/IP and websockets, the address defaults to
127.0.0.1. The port defaults to50000over TCP/IP and50001over websockets.
connection-unreachable [core] [MUST]Connecting raises the connection error when no server listens at the address, no socket is at the local socket path, or no serial port is at the path.
connection-unknown-host [core] [MUST]A host name that does not resolve raises the connection error.
connection-localsocket [localsocket] [MUST]The client connects to a local socket path.
connection-localsocket-default-path [localsocket] [MUST]The default local socket path follows the rule under Default Socket Path.
connection-localsocket-fallback-path [localsocket] [MUST]With no
XDG_RUNTIME_DIR, the default local socket path is/tmp/krpc-<user>.sock. The client ignoresTMPDIRfor it.
connection-localsocket-path-too-long [core] [MUST]A local socket path too long for a socket address raises the argument error. The client does not connect to the socket a leading part of the path names.
connection-serialio [serialio] [MUST]The client connects to a serial port.
connection-websockets [websockets] [MUST]The client connects over websockets to
ws://<address>:<port>/, with no subprotocol. EachMessageis one binary websockets message.
Handshake#
connection-handshake-name [core] [MUST]The client sends the name the program gives it in the
connection_request. The name can hold any Unicode character.KRPC.GetClientNamereturns that name.
connection-handshake-status [core] [MUST]KRPC.GetStatusreturns a structure that holds the version of the server, then the protocol version of the connection.
connection-handshake-version [core] [MUST]The client sends protocol version 2.
connection-handshake-wrong-version [core] [MUST]A
connection_responsewith the statusOKand aprotocol_versionother than 2 raises the connection error.
connection-handshake-refused [core] [MUST]A
connection_responsewith a status other thanOKraises the connection error, with the status and the message.
connection-handshake-unknown-status [core] [MUST]A status the protocol does not define raises the connection error, with the message.
connection-handshake-disconnect [core] [MUST]A
disconnectin place of theconnection_responseraises the connection error, with its reason and message. A reason the protocol does not define raises the connection error with the message.
connection-handshake-unexpected-message [core] [MUST]Any other message in place of the
connection_response, including an empty one, raises the connection error.
connection-handshake-lost [core] [MUST]A connection that closes before the
connection_responsearrives raises the connection error.
connection-handshake-failed-write [core] [MUST]A
connection_requestthat fails to be written raises the connection error. Its message carries the message of the write’s error, which is also its cause in a language that chains errors.
connection-handshake-malformed-size [core] [MUST]A
connection_responsewhose size prefix has not ended within five bytes raises the connection error.
connection-handshake-timeout [core] [SHOULD]Connecting raises the connection error when the
connection_responsehas not arrived in full after the handshake timeout. The stall timeout does not apply to it. The default is 5 seconds, and the program can set it.
connection-handshake-timeout-values [core] [SHOULD]An infinite handshake timeout waits with no deadline. A zero, negative or NaN timeout raises the argument error.
Reading Messages#
connection-message-split-across-reads [core] [MUST]The client reads a
Messagehowever the bytes arrive: one byte at a time, including a size prefix of two bytes, or in one read with all or the start of the next message.
connection-websockets-fragments [websockets] [MUST]The client reads a
Messagesplit across several websockets frames.
connection-unknown-message [core] [MUST]Once the connection is established, the client skips a
Messageholding a case the protocol does not define.
connection-wrong-direction [core] [MUST]A
Messageholding a case the client sends, such as arequest, fails the connection. The client sends adisconnectwith the reasonPROTOCOL_ERROR.
connection-malformed-message [core] [MUST]A message that fails to parse fails the connection. The client sends a
disconnectwith the reasonMALFORMED_MESSAGE.
connection-malformed-size [core] [MUST]A size prefix that has not ended within five bytes fails the connection. The client sends a
disconnectwith the reasonMALFORMED_MESSAGE.
connection-size-over-32-bits [core] [MUST]A size prefix whose value does not fit in 32 bits fails the connection. The client sends a
disconnectwith the reasonMALFORMED_MESSAGE.
connection-stall [core] [SHOULD]A message whose bytes stop arriving for the stall timeout, once its first byte has arrived, fails the connection with the reason
TIMEOUT. The default is 30 seconds, and the program can set it.
connection-stall-while-idle [streams] [SHOULD]A message that stalls while no call is waiting for a response, such as a stream update that starts to arrive with the response to a call, fails the connection with the reason
TIMEOUT.
connection-stall-timeout-values [core] [SHOULD]An infinite stall timeout waits with no deadline. A zero, negative or NaN timeout raises the argument error.
Closing#
connection-close [core] [MUST]Closing sends a
disconnectwith the reasonCLOSED. The client then closes its write side, and reads and discards until the server closes.
connection-websockets-close [websockets] [MUST]Over websockets, closing sends a
disconnectwith the reasonCLOSED, then a close frame. The client then waits for the server’s close frame.
connection-close-wait-limit [core] [MUST]Closing waits for the server to close for up to 1 second.
connection-close-interrupted [concurrency] [MUST]Closing from a thread with an interrupt pending sends a
disconnectwith the reasonCLOSEDand waits for the server to close. The interrupt is still pending once the close returns.
connection-close-twice [core] [MUST]Closing a connection that is already closed does nothing.
Losing the Connection#
connection-lost [core] [MUST]The connection fails when the server sends a
disconnect, or the connection ends without one, including partway through a message. The waiting call raises the connection error, with the reason and message of thedisconnect.
connection-lost-pending-calls [concurrency] [MUST]The calls waiting for a response when the connection fails raise the connection error.
connection-failed-write [core] [MUST]A call whose request fails to be written fails the connection. The call raises the connection error. Its message carries the message of the write’s error, which is also its cause in a language that chains errors. A later call raises the connection error at once.
connection-call-after-end [core] [MUST]Once the connection is closed or has failed, each call raises the connection error at once.
Resuming a Serial Line#
These requirements let a client that may be resuming a serial line start a new session. The line can hold what an earlier session left on it. See Resetting Clients.
connection-serialio-leftovers [serialio] [MUST]While it waits for its
connection_response, the client skips aresponseor astream_update. Either can be left over from an earlier session.
connection-serialio-no-answer [serialio] [MUST]The client treats a
disconnect, and the statusesMALFORMED_MESSAGEandTIMEOUT, as no answer. Each can follow a request that landed in a stale partial message. The client stays silent until its handshake timeout, then sends the request again. It sends it no sooner than 3 seconds after the first, the server’s stall timeout for a partial message.
connection-serialio-refusal [serialio] [MUST]The client never sends the request again after
DENIEDorWRONG_PROTOCOL_VERSION.
connection-serialio-repeated-connection-response [serialio] [MUST]The client reads
connection_responsemessages until the firstresponseof the session. Retries queue on the line while the server is not reading, and the server answers each with a new session. The client continues in the last one.