The switch is the core of libp2p, which brings together the transports, the connection manager, the upgrader and other parts to allow programs to use libp2p
Types
Switch {.public.} = ref object of Dial peerInfo*: PeerInfo connManager*: ConnManager transports*: seq[Transport] ms*: MultistreamSelect acceptFuts: seq[Future[void]] dialer*: Dial peerStore*: PeerStore nameResolver*: NameResolver started: bool services*: seq[Service]
- Source Edit
Procs
proc addConnEventHandler(s: Switch; handler: ConnEventHandler; kind: ConnEventKind) {.public, ...raises: [], tags: [RootEffect].}
-
Adds a ConnEventHandler, which will be triggered when a connection to a peer is created or dropped. There may be multiple connections per peer.
The handler should not raise.
Source Edit proc addPeerEventHandler(s: Switch; handler: PeerEventHandler; kind: PeerEventKind) {.public, ...raises: [], tags: [RootEffect].}
-
Adds a PeerEventHandler, which will be triggered when a peer connects or disconnects from us.
The handler should not raise.
Source Edit proc connectedPeers(s: Switch; dir: Direction): seq[PeerId] {....raises: [], tags: [RootEffect].}
- Source Edit
proc dial(s: Switch; peerId: PeerId; addrs: seq[MultiAddress]; proto: string): Future[ Connection] {.public, ...raises: [], tags: [RootEffect].}
- Connected to a peer and open a stream with the specified proto Source Edit
proc dial(s: Switch; peerId: PeerId; proto: string): Future[Connection] {. public, ...raises: [], tags: [RootEffect].}
- Open a stream to a connected peer with the specified proto Source Edit
proc disconnect(s: Switch; peerId: PeerId): Future[void] {....gcsafe, public, ...raises: [], tags: [RootEffect].}
- Disconnect from a peer, waiting for the connection(s) to be dropped Source Edit
proc isConnected(s: Switch; peerId: PeerId): bool {.public, ...raises: [], tags: [].}
- returns true if the peer has one or more associated connections Source Edit
proc mount[T: LPProtocol](s: Switch; proto: T; matcher: Matcher = nil) {....gcsafe, raises: [LPError], public, ...raises: [].}
- mount a protocol to the switch Source Edit
proc newSwitch(peerInfo: PeerInfo; transports: seq[Transport]; secureManagers: openArray[Secure] = []; connManager: ConnManager; ms: MultistreamSelect; peerStore: PeerStore; nameResolver: NameResolver = nil; services = newSeq()): Switch {. ...raises: [LPError], raises: [], tags: [].}
- Source Edit
proc removeConnEventHandler(s: Switch; handler: ConnEventHandler; kind: ConnEventKind) {.public, ...raises: [], tags: [RootEffect].}
- Source Edit
proc removePeerEventHandler(s: Switch; handler: PeerEventHandler; kind: PeerEventKind) {.public, ...raises: [], tags: [RootEffect].}
- Source Edit
Methods
method addTransport(s: Switch; t: Transport) {....raises: [], tags: [].}
- Source Edit
method connect(s: Switch; address: MultiAddress; allowUnknownPeerId = false): Future[ PeerId] {....raises: [], tags: [RootEffect].}
-
Connects to a peer and retrieve its PeerId
If the P2P part is missing from the MA and allowUnknownPeerId is set to true, this will discover the PeerId while connecting. This exposes you to MiTM attacks, so it shouldn't be used without care!
Source Edit method connect(s: Switch; peerId: PeerId; addrs: seq[MultiAddress]; forceDial = false; reuseConnection = true; dir = Direction.Out): Future[ void] {.public, ...raises: [], tags: [RootEffect].}
- Connects to a peer without opening a stream to it Source Edit
method dial(s: Switch; peerId: PeerId; addrs: seq[MultiAddress]; protos: seq[string]; forceDial = false): Future[Connection] {. public, ...raises: [], tags: [RootEffect].}
- Connected to a peer and open a stream with the specified protos Source Edit
method dial(s: Switch; peerId: PeerId; protos: seq[string]): Future[Connection] {. public, ...raises: [], tags: [RootEffect].}
- Open a stream to a connected peer with the specified protos Source Edit
method run(self: Service; switch: Switch): Future[void] {.base, stackTrace: false, ...raises: [], gcsafe, raises: [], raises: [], tags: [RootEffect].}
- Source Edit
Exports
-
ConnManager, getStream, PeerEventKind, getStream, addPeerEventHandler, new, ConnEventKind, PeerEventHandler, selectMuxer, getConnections, contains, removePeerEventHandler, expectConnection, ConnEvent, connCount, ConnEventHandler, addConnEventHandler, triggerConnEvent, removeConnEventHandler, contains, triggerPeerEvents, AlreadyExpectingConnectionError, PeerEvent, TooManyConnectionsError, getStream, trackConnection, close, MaxConnectionsPerPeer, connectedPeers, selectMuxer, trackMuxer, getOutgoingSlot, slotsAvailable, dropPeer, getIncomingSlot, storeMuxer, release, MaxConnections, ConnectionSlot, DNS_OR_IP, LPStreamError, AlreadyExpectingConnectionError, shortLog, init, random, new, UDP_DNS, $, getProtoHeader, MaxConnectionsPerPeer, ephemeral, decode, MaPatternOp, ==, WS_DNS, initVBuffer, <=, decode, MaxSizeError, ProtoFlags, handler=, getLen, handler, ECDHEScheme, shortLog, libp2p_failed_upgrades_outgoing, init, init, closed, addConnEventHandler, [], IdentifyPushCodec, new, macOpenArray, random, new, Secret, peekVarint, random, handshake, MaxConnections, $, validate, mapOr, Matcher, verify, MaError, bytes, UpgradeFailedError, IdentityInvalidMsgError, maxInlineKeyLength, addHandler, TranscoderDNS, QUIC, ProtoVersion, [], init, withValue, getRequiredField, getField, mac, init, supported, removeConnEventHandler, WS_IP, write, public, MAKind, peekSeq, data, init, getField, checkFieldNumber, valueOr, init, codec, getRepeatedField, matchPartial, addObservation, IdentityNoMatchError, push, getBytes, trackConnection, &, init, readLp, TimeoutHandler, initVBuffer, start, atEof, PeerEventKind, getOutgoingSlot, write, toOpt, fullAddrs, payloadType, getProtoHeader, ProtoHeader, $, IP, protoAddress, QUIC_V1_IP, newRng, withValue, init, SecureConn, checkValid, getPublicKey, CircuitRelay, len, libp2p_failed_upgrades_incoming, exceptionToAssert, extractPublicKey, mapAnd, new, maErr, UDP_IP, new, newLPStreamEOFError, selectMuxer, init, connCount, len, write, DNSADDR, LPError, handler, AddressMapper, IdentifyPushHandler, write, ConnManager, WSS, QUIC_V1, getField, TCP_IP, close, selectMuxer, writePacked, readVarint, handle, closeImpl, Identify, readSeq, toBytes, TCP_DNS, shortLog, encode, len, capLen, init, protoName, protoArgument, PeerEventHandler, LPProtoHandler, secure, ==, toRawBytes, IdentifyPush, getStream, TranscoderIP6Zone, $, initProtoBuffer, maxIncomingStreams, ProtoBuffer, WSS_DNS, list, match, <, CryptoResult, parseFullAddress, readMessage, ProtoResult, WebRTCDirect, init, upgrade, UDP, TranscoderOnion, StreamTracker, IdentifyNoPubKeyError, write, init, getConnections, LPStreamResetError, isEmpty, writeArray, getRawBytes, new, toString, LPStreamIncompleteError, PeerId, LPStreamEOFError, readOnce, maxIncomingStreams=, IP6, shortLog, new, release, initVBuffer, PKScheme, key, new, payload, triggerConnEvent, triggerPeerEvents, readVarint, QUIC_V1_DNS, Connection, write, init, TCP, PrivateKey, dropPeer, getBytes, start, write, close, &=, PeerInfoError, hash, newLPStreamClosedError, random, compilesOr, handle, trackMuxer, init, initProtoBuffer, toException, DNS, Upgrade, goffset, HandlerHolder, >, SecureConnTrackerName, KeyPair, init, high, cmp, init, SupportedWireTypes, formatItIMPL, formatItIMPL, initStream, formatItIMPL, Signature, contains, exclIfIt, formatItIMPL, getStream, init, IPFS, shortLog, readOnce, init, init, secure, isEmpty, ConnEventKind, ProtoScalar, shortLog, contains, hex, expandItIMPL, protocols, codec=, DefaultMaxIncomingStreams, HTTPS, UNIX, encode, TooManyConnectionsError, ConnectionSlot, getPtr, getBytes, TLS_WS, sign, init, addHandler, storeMuxer, TranscoderIP4, formatItIMPL, PeerEvent, ObservedAddrManager, MaResult, init, ProtoField, IdentifyCodec, getStream, writeSeq, DigestSheme, ConnEvent, ==, $, DNS6, [], IpTransportProtocol, toOpenArray, init, IP4, TranscoderPort, isEnough, safeConvert, addPeerEventHandler, DNSANY, >=, expectConnection, init, finish, ephemeral, payloadDomain, update, $, withValue, init, init, LPStreamLimitError, removePeerEventHandler, TranscoderOnion3, SignedPayload, closeImpl, toBytes, Secure, newLPStreamLimitError, write, keyOpenArray, init, match, parseFullAddress, $, closeImpl, isEmpty, writeLp, connectedPeers, new, getRequiredRepeatedField, init, newLPStreamIncompleteError, initProtoBuffer, MAProtocol, newLPStreamResetError, getPackedRepeatedField, initStream, init, getField, shortLog, DNS4, getWrapped, readArray, selectBest, write, P2PPattern, getWrapped, Envelope, hasPublicKey, toException, contains, isEnough, init, Reliable, addrs, hex, slotsAvailable, write, WS, AgentVersion, init, MultistreamSelect, encode, LPProtocol, stretchKeys, init, getField, getField, match, getField, writeLPVarint, iv, DefaultConnectionTimeout, new, readLine, Eof, CryptoError, QUIC_DNS, append, MaPattern, init, WebSockets_DNS, WSS_IP, LPStreamConnDownError, PeerRecord, init, SupportedSchemes, getRepeatedField, getField, select, Unreliable, SupportedSchemesInt, random, checkFutures, handler=, guessDialableAddr, shuffle, init, validate, MultiStreamError, mapEq, getField, finish, libp2p_pki_schemes, Onion3, write, LPStreamTrackerName, select, concat, TranscoderP2P, PeerInfo, AddressInfo, ConnectionTrackerName, TranscoderIP6, write, EnvelopeError, hash, IdentifyInfo, IdentifyError, addHandler, encode, WebSockets, newLPStreamRemoteClosedError, MaPatResult, Direction, init, InvalidVarintError, shortLog, ==, write, getBytes, write, UTP, toBytes, peekArray, hash, HTTP, getMostObservedProtosAndPorts, QUIC_IP, PublicKey, ConnEventHandler, WebSockets_IP, decodeMsg, VBuffer, writeLp, writePBVarint, ProtoFieldKind, readExactly, protoArgument, getRepeatedField, decode, ProtoError, newLPStreamConnDownError, init, initStream, MultiAddress, ivOpenArray, closeWithEOF, init, TcpOnion3, orError, vsizeof, getField, LPStreamRemoteClosedError, identify, init, protoCode, stop, MaInvalidAddress, select, LPStream, LPStreamClosedError, RsaDefaultKeySize, writeVarint, getIncomingSlot, items, write, toBytes, getOrder, TranscoderUnix, SignedPeerRecord, init, stop, join, dial, dial, dial, connect, checkFutures, LPError, addTransport, addTransport, connect, toException, new, dial, Dialer, tryDial, DialFailedError, connect, Dial, connect, toException, tryDial, PeerBook, ProtoBook, AddressBook, ProtoVersionBook, KeyBook, LastSeenBook, AgentBook, SPRBook, new, SeqPeerBook, PeerBookChangeHandler, PeerStore