ouroboros-network-framework-0.13.2.0: Ouroboros network framework
Safe HaskellSafe-Inferred
LanguageHaskell2010

Ouroboros.Network.Socket

Description

Module exports interface for running a node over a socket over TCP / IP.

Synopsis

High level socket interface

data ConnectionTableRef Source #

Constructors

ConnectionTableCreate

No connection to peer exists, attempt to create one.

ConnectionTableExist

A connection to the peer existed, either from another subscriber or the peer opened one towards us.

ConnectionTableDuplicate

This subscriber already has counted a connection to this peer. It must try another target.

data ValencyCounter m Source #

ValencyCounter represents how many active connections we have towards a given peer. It starts out with a positive value representing a desired number of connections for a specific subscription worker. It can become negative, for example if a peer opens multiple connections to us. The vcId is unique per ConnectionTable and ensures that we won't count the same connection twice.

data NetworkMutableState addr Source #

Mutable state maintained by the network component.

Constructors

NetworkMutableState 

Fields

data SomeResponderApplication addr bytes m b where Source #

Wrapper for OuroborosResponderApplication and OuroborosInitiatorAndResponderApplication.

Constructors

SomeResponderApplication ∷ ∀ appType addr bytes m a b. HasResponder appType ~ TrueOuroborosApplicationWithMinimalCtx appType addr bytes m a b → SomeResponderApplication addr bytes m b 

data AcceptedConnectionsLimit Source #

Policy which governs how to limit the number of accepted connections.

Constructors

AcceptedConnectionsLimit 

Fields

data ConnectionId addr Source #

Connection is identified by local and remote address.

TODO: the type variable which this data type fills in is called peerid. We should renamed to connectionId.

Constructors

ConnectionId 

Fields

Instances

Instances details
Functor ConnectionId Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Methods

fmap ∷ (a → b) → ConnectionId a → ConnectionId b #

(<$) ∷ a → ConnectionId b → ConnectionId a #

ShowProxy addr ⇒ ShowProxy (ConnectionId addr ∷ Type) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Methods

showProxyProxy (ConnectionId addr) → String Source #

Generic (ConnectionId addr) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Associated Types

type Rep (ConnectionId addr) ∷ TypeType #

Methods

fromConnectionId addr → Rep (ConnectionId addr) x #

toRep (ConnectionId addr) x → ConnectionId addr #

Show addr ⇒ Show (ConnectionId addr) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Methods

showsPrecIntConnectionId addr → ShowS #

showConnectionId addr → String #

showList ∷ [ConnectionId addr] → ShowS #

Eq addr ⇒ Eq (ConnectionId addr) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Methods

(==)ConnectionId addr → ConnectionId addr → Bool #

(/=)ConnectionId addr → ConnectionId addr → Bool #

Ord addr ⇒ Ord (ConnectionId addr) Source #

Order first by remoteAddress then by localAddress.

Note: we relay on the fact that remoteAddress is an order preserving map (which allows us to use mapKeysMonotonic in some cases).

Instance details

Defined in Ouroboros.Network.ConnectionId

Methods

compareConnectionId addr → ConnectionId addr → Ordering #

(<)ConnectionId addr → ConnectionId addr → Bool #

(<=)ConnectionId addr → ConnectionId addr → Bool #

(>)ConnectionId addr → ConnectionId addr → Bool #

(>=)ConnectionId addr → ConnectionId addr → Bool #

maxConnectionId addr → ConnectionId addr → ConnectionId addr #

minConnectionId addr → ConnectionId addr → ConnectionId addr #

Hashable a ⇒ Hashable (ConnectionId a) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

Typeable addr ⇒ NoThunks (ConnectionId addr) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

type Rep (ConnectionId addr) Source # 
Instance details

Defined in Ouroboros.Network.ConnectionId

type Rep (ConnectionId addr) = D1 ('MetaData "ConnectionId" "Ouroboros.Network.ConnectionId" "ouroboros-network-framework-0.13.2.0-inplace" 'False) (C1 ('MetaCons "ConnectionId" 'PrefixI 'True) (S1 ('MetaSel ('Just "localAddress") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 addr) :*: S1 ('MetaSel ('Just "remoteAddress") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 addr)))

withServerNode Source #

Arguments

∷ ∀ vNumber vData t fd addr b. (Ord vNumber, Typeable vNumber, Show vNumber, Ord addr) 
Snocket IO fd addr 
MakeBearer IO fd 
→ (fd → addr → IO ())

callback to configure a socket

NetworkServerTracers addr vNumber 
NetworkMutableState addr 
AcceptedConnectionsLimit 
→ addr 
Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
HandshakeCallbacks vData 
Versions vNumber vData (SomeResponderApplication addr ByteString IO b)

The mux application that will be run on each incoming connection from a given address. Note that if MuxClientAndServerApplication is returned, the connection will run a full duplex set of mini-protocols.

ErrorPolicies 
→ (addr → Async VoidIO t)

callback which takes the Async of the thread that is running the server. Note: the server thread will terminate when the callback returns or throws an exception.

IO t 

Run a server application. It will listen on the given address for incoming connection, otherwise like withServerNode'.

withServerNode' Source #

Arguments

∷ ∀ vNumber vData t fd addr b. (Ord vNumber, Typeable vNumber, Show vNumber, Ord addr) 
Snocket IO fd addr 
MakeBearer IO fd 
NetworkServerTracers addr vNumber 
NetworkMutableState addr 
AcceptedConnectionsLimit 
→ fd

a configured socket to be used be the server. The server will call bind and listen methods but it will not set any socket or tcp options on it.

Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
HandshakeCallbacks vData 
Versions vNumber vData (SomeResponderApplication addr ByteString IO b)

The mux application that will be run on each incoming connection from a given address. Note that if MuxClientAndServerApplication is returned, the connection will run a full duplex set of mini-protocols.

ErrorPolicies 
→ (addr → Async VoidIO t)

callback which takes the Async of the thread that is running the server. Note: the server thread will terminate when the callback returns or throws an exception.

IO t 

Run a server application on the provided socket. The socket must be ready to accept connections. The server thread runs using withAsync function, which means that it will terminate when the callback terminates or throws an exception.

TODO: we should track connections in the state and refuse connections from peers we are already connected to. This is also the right place to ban connection from peers which misbehaved.

The server will run handshake protocol on each incoming connection. We assume that each version negotiation message should fit into maxTransmissionUnit (~5k bytes).

Note: it will open a socket in the current thread and pass it to the spawned thread which runs the server. This makes it useful for testing, where we need to guarantee that a socket is open before we try to connect to it.

connectToNode Source #

Arguments

∷ ∀ appType vNumber vData fd addr a b. (Ord vNumber, Typeable vNumber, Show vNumber, HasInitiator appType ~ True) 
Snocket IO fd addr 
MakeBearer IO fd 
→ (fd → IO ())

configure a socket

Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
NetworkConnectTracers addr vNumber 
HandshakeCallbacks vData 
Versions vNumber vData (OuroborosApplicationWithMinimalCtx appType addr ByteString IO a b)

application to run over the connection

Maybe addr

local address; the created socket will bind to it

→ addr

remote address

IO () 

Connect to a remote node. It is using bracket to enclose the underlying socket acquisition. This implies that when the continuation exits the underlying bearer will get closed.

The connection will start with handshake protocol sending Versions to the remote peer. It must fit into maxTransmissionUnit (~5k bytes).

Exceptions thrown by MuxApplication are rethrown by connectTo.

connectToNodeSocket Source #

Arguments

∷ ∀ appType vNumber vData a b. (Ord vNumber, Typeable vNumber, Show vNumber, HasInitiator appType ~ True) 
IOManager 
Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
NetworkConnectTracers SockAddr vNumber 
HandshakeCallbacks vData 
Versions vNumber vData (OuroborosApplicationWithMinimalCtx appType SockAddr ByteString IO a b)

application to run over the connection

Socket 
IO () 

connectToNode' Source #

Arguments

∷ ∀ appType vNumber vData fd addr a b. (Ord vNumber, Typeable vNumber, Show vNumber, HasInitiator appType ~ True) 
Snocket IO fd addr 
MakeBearer IO fd 
Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
NetworkConnectTracers addr vNumber 
HandshakeCallbacks vData 
Versions vNumber vData (OuroborosApplicationWithMinimalCtx appType addr ByteString IO a b)

application to run over the connection

→ fd

a configured socket to use to connect to a remote service provider

IO () 

Connect to a remote node using an existing socket. It is up to to caller to ensure that the socket is closed in case of an exception.

The connection will start with handshake protocol sending Versions to the remote peer. It must fit into maxTransmissionUnit (~5k bytes).

Exceptions thrown by MuxApplication are rethrown by connectTo.

Socket configuration

configureSocketSocketMaybe SockAddrIO () Source #

Configure a socket. Either AF_INET or AF_INET6 socket is expected.

configureSystemdSocketTracer IO SystemdSocketTracerSocketSockAddrIO () Source #

Configure sockets passed through systemd socket activation. Currently ReuseAddr and Linger options are not configurable with 'systemd.socket', these options are set by this function. For other socket options we only trace if they are not set.

Traces

data NetworkConnectTracers addr vNumber Source #

Tracer used by connectToNode (and derivatives, like connectTo or 'Ouroboros.Network.NodeToClient.connectTo).

Constructors

NetworkConnectTracers 

Fields

data NetworkServerTracers addr vNumber Source #

Tracers required by a server which handles inbound connections.

Constructors

NetworkServerTracers 

Fields

data AcceptConnectionsPolicyTrace Source #

Trace for the AcceptConnectionsLimit policy.

Helper function for creating servers

fromSnocket Source #

Arguments

∷ ∀ fd addr. Ord addr 
ConnectionTable IO addr 
Snocket IO fd addr 
→ fd

socket or handle

IO (Socket addr fd) 

Make a server-compatible socket from a network socket.

beginConnection Source #

Arguments

∷ ∀ vNumber vData addr st fd. (Ord vNumber, Typeable vNumber, Show vNumber) 
MakeBearer IO fd 
Tracer IO (WithMuxBearer (ConnectionId addr) MuxTrace) 
Tracer IO (WithMuxBearer (ConnectionId addr) (TraceSendRecv (Handshake vNumber Term))) 
Codec (Handshake vNumber Term) DeserialiseFailure IO ByteString 
ProtocolTimeLimits (Handshake vNumber Term) 
VersionDataCodec Term vNumber vData 
HandshakeCallbacks vData 
→ (Time → addr → st → STM (AcceptConnection st vNumber vData addr IO ByteString))

either accept or reject a connection.

BeginConnection addr fd st () 

Accept or reject incoming connection based on the current state and address of the incoming connection.

Re-export of HandshakeCallbacks

data HandshakeCallbacks vData Source #

A record that holds handshake callbacks.

Constructors

HandshakeCallbacks 

Fields

Re-export of PeerStates

data PeerStates m addr Source #

Map from addresses to PeerStates; it will be be shared in a StrictTVar.

Abstracting t is useful for tests, the IO version will use Time IO.

Instances

Instances details
Show addr ⇒ Show (PeerStates IO addr) Source # 
Instance details

Defined in Ouroboros.Network.Subscription.PeerState

Methods

showsPrecIntPeerStates IO addr → ShowS #

showPeerStates IO addr → String #

showList ∷ [PeerStates IO addr] → ShowS #

Eq addr ⇒ Eq (PeerStates IO addr) Source # 
Instance details

Defined in Ouroboros.Network.Subscription.PeerState

Methods

(==)PeerStates IO addr → PeerStates IO addr → Bool #

(/=)PeerStates IO addr → PeerStates IO addr → Bool #

Re-export connection table functions

addConnection Source #

Arguments

∷ ∀ m addr. (MonadSTM m, Ord addr) 
ConnectionTable m addr 
→ addr 
→ addr 
ConnectionDirection 
Maybe (ValencyCounter m)

Optional ValencyCounter, used by subscription worker and set to Nothing when called by a local server.

STM m () 

Insert a new connection into the ConnectionTable.

removeConnection ∷ ∀ m addr. (MonadSTM m, Ord addr) ⇒ ConnectionTable m addr → addr → addr → ConnectionDirection → m () Source #

newValencyCounter Source #

Arguments

MonadSTM m 
ConnectionTable m addr 
Int

Desired valency, that is number of connections a subscription worker will attempt to maintain.

STM m (ValencyCounter m) 

Create a new ValencyCounter

addValencyCounterMonadSTM m ⇒ ValencyCounter m → STM m () Source #

Add a connection.

remValencyCounterMonadSTM m ⇒ ValencyCounter m → STM m () Source #

Remove a connection.

waitValencyCounterMonadSTM m ⇒ ValencyCounter m → STM m () Source #

Wait until ValencyCounter becomes positive, used for detecting when we can create new connections.

readValencyCounterMonadSTM m ⇒ ValencyCounter m → STM m Int Source #

Returns current ValencyCounter value, represent the number of additional connections that can be created. May be negative.

Auxiliary functions

Orphan instances

Hashable SockAddr Source # 
Instance details