hs-libp2p-0.2.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Yamux.Session

Description

Yamux session management: create, openStream, acceptStream, ping, goaway.

Implements the session-level Yamux protocol per HashiCorp yamux spec.md. The session manages a collection of multiplexed streams over a single underlying transport connection.

Two background loops run per session: recvLoop: reads 12-byte headers from transport, dispatches to streams sendLoop: dequeues from ysessSendCh, writes to transport

Synopsis

Documentation

newSession :: SessionRole -> (ByteString -> IO ()) -> (Int -> IO ByteString) -> IO YamuxSession Source #

Create a new Yamux session over a transport connection. Client uses odd stream IDs starting at 1, server uses even starting at 2.

newSessionWithConfig :: YamuxConfig -> SessionRole -> (ByteString -> IO ()) -> (Int -> IO ByteString) -> IO YamuxSession Source #

Like newSession, with an explicit configuration.

newManagedSession :: SessionRole -> MemoryAccount -> STM (Either ResourceError MemoryAccount) -> STM (Either ResourceError MemoryAccount) -> (ByteString -> IO ()) -> (Int -> IO ByteString) -> IO YamuxSession Source #

Create a session whose connection and inbound streams own memory accounts.

closeSession :: YamuxSession -> IO () Source #

Gracefully close the session by sending GoAway Normal.

openStream :: YamuxSession -> IO (Either YamuxError YamuxStream) Source #

Open a new outbound stream. Allocates the next stream ID and sends SYN. Returns YamuxSessionShutdown after a local GoAway, or YamuxGoAway with the received code after a remote GoAway.

acceptStream :: YamuxSession -> IO (Either YamuxError YamuxStream) Source #

Accept an inbound stream. Blocks until a remote SYN arrives. Returns YamuxSessionShutdown if the session is shut down.

ping :: YamuxSession -> IO (Either YamuxError ()) Source #

Send a Ping and wait for the ACK response. Ping uses StreamID 0 and the Length field carries an opaque value.

Fails with YamuxPingTimeout when the SYN is not written within ycPingTimeout, or when its ACK does not arrive within another ycPingTimeout after the write. The former is just a fallback to ensure that the operation doesn't hang indefinitely.

sendGoAway :: YamuxSession -> GoAwayCode -> IO () Source #

Send a GoAway frame with the specified error code. Sets ysessShutdown to True so no new streams can be opened.

keepaliveLoop :: YamuxSession -> IO (Either YamuxError ()) Source #

Keepalive loop: once the ycKeepAlive interval passes without a frame from the peer, send a Ping (spec.md, Type Field: a Ping "can also be used to heart-beat and do keep-alives over TCP").

Returns Right () without pinging when keepalive is disabled, and as soon as a local or remote GoAway is in effect (closeSession, sendGoAway, failSession, or a GoAway from the peer), including while a Ping is waiting for its ACK.

Returns Left when a Ping fails. The session is left as it is: the caller must stop recvLoop, which tears the session down.

recvLoop :: YamuxSession -> IO () Source #

Receive loop: reads 12-byte headers from transport and dispatches frames. This loop runs until the transport connection is closed or an error occurs.

Whenever the loop terminates -- transport EOF or error (ysessRead throws), a fatal protocol error, or cancellation -- the session is torn down via failSession so that no reader, writer or ping waiter is left blocked forever on a session that can no longer make progress.

sendLoop :: YamuxSession -> IO () Source #

Send loop: dequeues frames from ysessSendCh and writes to transport.

acceptBacklog :: Natural Source #

Maximum number of inbound streams buffered while the application is not accepting (go-yamux AcceptBacklog). The spec requires this buffer to be bounded to mitigate DoS; excess SYNs are reset.