2024-06-11 11:56:37 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-12-10 10:12:24 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-05-10 13:50:04 +00:00
|
|
|
{.push raises: [].}
|
2021-12-10 10:12:24 +00:00
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
import
|
2024-06-18 16:09:27 +00:00
|
|
|
std/tables,
|
2022-06-17 20:45:37 +00:00
|
|
|
chronos, chronicles, metrics,
|
2024-06-18 16:09:27 +00:00
|
|
|
results,
|
2024-09-29 08:52:19 +00:00
|
|
|
../common/keys,
|
2021-10-28 09:41:43 +00:00
|
|
|
./utp_socket,
|
|
|
|
./packets
|
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
export utp_socket
|
|
|
|
|
|
|
|
logScope:
|
2022-12-06 13:54:03 +00:00
|
|
|
topics = "eth utp utp_router"
|
2022-04-12 19:11:01 +00:00
|
|
|
|
2022-03-17 09:53:46 +00:00
|
|
|
declareCounter utp_received_packets,
|
|
|
|
"All correct received uTP packets"
|
|
|
|
declareCounter utp_failed_packets,
|
|
|
|
"All received uTP packets which failed decoding"
|
|
|
|
declareGauge utp_established_connections,
|
|
|
|
"Current number of established uTP sockets"
|
|
|
|
declareCounter utp_allowed_incoming,
|
|
|
|
"Total number of allowed incoming connections"
|
|
|
|
declareCounter utp_declined_incoming,
|
|
|
|
"Total number of declined incoming connections"
|
|
|
|
declareCounter utp_success_outgoing,
|
2022-11-16 16:44:00 +00:00
|
|
|
"Total number of successful outgoing connections"
|
2022-03-17 09:53:46 +00:00
|
|
|
declareCounter utp_failed_outgoing,
|
|
|
|
"Total number of failed outgoing connections"
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
type
|
|
|
|
# New remote client connection callback
|
|
|
|
# ``server`` - UtpProtocol object.
|
|
|
|
# ``client`` - accepted client utp socket.
|
|
|
|
AcceptConnectionCallback*[A] = proc(server: UtpRouter[A],
|
2024-06-12 16:01:23 +00:00
|
|
|
client: UtpSocket[A]): Future[void] {.gcsafe, async: (raw: true, raises: []).}
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
# Callback to act as firewall for incoming peers. Should return true if peer
|
|
|
|
# is allowed to connect.
|
|
|
|
AllowConnectionCallback*[A] = proc(r: UtpRouter[A], remoteAddress: A,
|
2023-05-10 13:50:04 +00:00
|
|
|
connectionId: uint16): bool {.gcsafe, raises: [], noSideEffect.}
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
# Object responsible for creating and maintaining table of utp sockets.
|
|
|
|
# Caller should use `processIncomingBytes` proc to feed it with incoming byte
|
|
|
|
# packets. Based on this input, proper utp sockets will be created, closed,
|
|
|
|
# or will receive data.
|
2021-10-28 09:41:43 +00:00
|
|
|
UtpRouter*[A] = ref object
|
|
|
|
sockets: Table[UtpSocketKey[A], UtpSocket[A]]
|
|
|
|
socketConfig: SocketConfig
|
|
|
|
acceptConnection: AcceptConnectionCallback[A]
|
2021-11-09 14:29:59 +00:00
|
|
|
closed: bool
|
2021-10-28 09:41:43 +00:00
|
|
|
sendCb*: SendCallback[A]
|
2021-11-18 09:05:56 +00:00
|
|
|
allowConnection*: AllowConnectionCallback[A]
|
2022-08-12 13:09:59 +00:00
|
|
|
udata: pointer
|
2022-06-17 20:45:37 +00:00
|
|
|
rng*: ref HmacDrbgContext
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
const
|
2021-12-10 10:12:24 +00:00
|
|
|
# Maximal number of tries to generate unique socket while establishing
|
|
|
|
# outgoing connection.
|
2021-11-18 09:05:56 +00:00
|
|
|
maxSocketGenerationTries = 1000
|
|
|
|
|
2021-12-10 10:12:24 +00:00
|
|
|
# This should probably be in standard lib, it allows lazy composition of options
|
|
|
|
# i.e one can write: O1 orElse O2 orElse O3, and chain will be evaluated to
|
|
|
|
# first option which isSome()
|
2024-06-18 16:09:27 +00:00
|
|
|
template orElse[A](a: Opt[A], b: Opt[A]): Opt[A] =
|
2021-11-05 08:41:41 +00:00
|
|
|
if (a.isSome()):
|
|
|
|
a
|
|
|
|
else:
|
|
|
|
b
|
|
|
|
|
2024-06-18 16:09:27 +00:00
|
|
|
proc getUtpSocket[A](s: UtpRouter[A], k: UtpSocketKey[A]): Opt[UtpSocket[A]] =
|
2021-10-28 09:41:43 +00:00
|
|
|
let s = s.sockets.getOrDefault(k)
|
|
|
|
if s == nil:
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.none(UtpSocket[A])
|
2021-10-28 09:41:43 +00:00
|
|
|
else:
|
2024-06-18 16:09:27 +00:00
|
|
|
Opt.some(s)
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
proc deRegisterUtpSocket[A](s: UtpRouter[A], socket: UtpSocket[A]) =
|
|
|
|
s.sockets.del(socket.socketKey)
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_established_connections.set(int64(len(s.sockets)))
|
2022-08-04 13:55:39 +00:00
|
|
|
debug "Removed utp socket", dst = socket.socketKey, lenSockets = len(s.sockets)
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
iterator allSockets[A](s: UtpRouter[A]): UtpSocket[A] =
|
|
|
|
for socket in s.sockets.values():
|
|
|
|
yield socket
|
|
|
|
|
|
|
|
proc len*[A](s: UtpRouter[A]): int =
|
2021-11-05 08:41:41 +00:00
|
|
|
## returns number of active sockets
|
2021-10-28 09:41:43 +00:00
|
|
|
len(s.sockets)
|
|
|
|
|
|
|
|
proc registerUtpSocket[A](p: UtpRouter, s: UtpSocket[A]) =
|
2021-11-18 09:05:56 +00:00
|
|
|
## Register socket, overwriting already existing one
|
2021-10-28 09:41:43 +00:00
|
|
|
p.sockets[s.socketKey] = s
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_established_connections.set(int64(len(p.sockets)))
|
2023-04-04 15:11:36 +00:00
|
|
|
debug "Registered new uTP socket",
|
|
|
|
dst = s.socketKey, totalSockets = len(p.sockets)
|
|
|
|
# Install deregister handler so that when the socket gets closed, it gets
|
|
|
|
# removed from open sockets table.
|
2021-10-28 09:41:43 +00:00
|
|
|
s.registerCloseCallback(proc () = p.deRegisterUtpSocket(s))
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
proc registerIfAbsent[A](p: UtpRouter, s: UtpSocket[A]): bool =
|
2021-12-10 10:12:24 +00:00
|
|
|
## Registers socket only if it's not already existing in the active sockets
|
2022-11-16 16:44:00 +00:00
|
|
|
## table. Returns true if socket has been successfully registered.
|
2021-11-18 09:05:56 +00:00
|
|
|
if p.sockets.hasKey(s.socketKey):
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
p.registerUtpSocket(s)
|
|
|
|
true
|
|
|
|
|
2021-10-28 09:41:43 +00:00
|
|
|
proc new*[A](
|
2021-12-10 10:12:24 +00:00
|
|
|
T: type UtpRouter[A],
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[A],
|
|
|
|
allowConnectionCb: AllowConnectionCallback[A],
|
2022-08-12 13:09:59 +00:00
|
|
|
udata: pointer,
|
2021-12-10 10:12:24 +00:00
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
rng = newRng()): UtpRouter[A] =
|
2021-10-28 09:41:43 +00:00
|
|
|
doAssert(not(isNil(acceptConnectionCb)))
|
|
|
|
UtpRouter[A](
|
2024-06-10 19:19:25 +00:00
|
|
|
sockets: Table[UtpSocketKey[A], UtpSocket[A]](),
|
2021-10-28 09:41:43 +00:00
|
|
|
acceptConnection: acceptConnectionCb,
|
2021-11-18 09:05:56 +00:00
|
|
|
allowConnection: allowConnectionCb,
|
2021-10-28 09:41:43 +00:00
|
|
|
socketConfig: socketConfig,
|
2022-08-12 13:09:59 +00:00
|
|
|
udata: udata,
|
2021-10-28 09:41:43 +00:00
|
|
|
rng: rng
|
|
|
|
)
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
proc new*[A](
|
2021-12-10 10:12:24 +00:00
|
|
|
T: type UtpRouter[A],
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[A],
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
rng = newRng()): UtpRouter[A] =
|
2022-08-12 13:09:59 +00:00
|
|
|
UtpRouter[A].new(acceptConnectionCb, nil, nil, socketConfig, rng)
|
|
|
|
|
|
|
|
proc new*[A](
|
|
|
|
T: type UtpRouter[A],
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[A],
|
|
|
|
allowConnectionCb: AllowConnectionCallback[A],
|
|
|
|
udata: ref,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
rng = newRng()): UtpRouter[A] =
|
|
|
|
doAssert(not(isNil(acceptConnectionCb)))
|
|
|
|
GC_ref(udata)
|
2023-04-04 15:11:36 +00:00
|
|
|
UtpRouter[A].new(
|
|
|
|
acceptConnectionCb, allowConnectionCb,
|
|
|
|
cast[pointer](udata), socketConfig, rng)
|
2022-08-12 13:09:59 +00:00
|
|
|
|
|
|
|
proc new*[A](
|
|
|
|
T: type UtpRouter[A],
|
|
|
|
acceptConnectionCb: AcceptConnectionCallback[A],
|
|
|
|
udata: ref,
|
|
|
|
socketConfig: SocketConfig = SocketConfig.init(),
|
|
|
|
rng = newRng()): UtpRouter[A] =
|
|
|
|
UtpRouter[A].new(acceptConnectionCb, nil, udata, socketConfig, rng)
|
|
|
|
|
|
|
|
proc getUserData*[A, T](router: UtpRouter[A]): T =
|
|
|
|
## Obtain user data stored in ``router`` object.
|
|
|
|
cast[T](router.udata)
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2022-01-10 12:49:36 +00:00
|
|
|
# There are different possibilities on how the connection got established, need
|
|
|
|
# to check every case.
|
2022-04-12 19:11:01 +00:00
|
|
|
proc getSocketOnReset[A](
|
2024-06-18 16:09:27 +00:00
|
|
|
r: UtpRouter[A], sender: A, id: uint16): Opt[UtpSocket[A]] =
|
2021-11-05 08:41:41 +00:00
|
|
|
# id is our recv id
|
|
|
|
let recvKey = UtpSocketKey[A].init(sender, id)
|
|
|
|
|
2022-11-16 16:44:00 +00:00
|
|
|
# id is our send id, and we did initiate the connection, our recv id is id - 1
|
2021-11-05 08:41:41 +00:00
|
|
|
let sendInitKey = UtpSocketKey[A].init(sender, id - 1)
|
|
|
|
|
2023-04-04 15:11:36 +00:00
|
|
|
# id is our send id, and we did not initiate the connection,
|
|
|
|
# our recv id is id + 1
|
2021-11-05 08:41:41 +00:00
|
|
|
let sendNoInitKey = UtpSocketKey[A].init(sender, id + 1)
|
|
|
|
|
|
|
|
r.getUtpSocket(recvKey)
|
2024-06-18 16:09:27 +00:00
|
|
|
.orElse(r.getUtpSocket(sendInitKey).filter(proc(s: UtpSocket[A]): bool = s.connectionIdSnd == id))
|
|
|
|
.orElse(r.getUtpSocket(sendNoInitKey).filter(proc(s: UtpSocket[A]): bool = s.connectionIdSnd == id))
|
2021-11-05 08:41:41 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc shouldAllowConnection[A](
|
|
|
|
r: UtpRouter[A], remoteAddress: A, connectionId: uint16): bool =
|
2021-11-18 09:05:56 +00:00
|
|
|
if r.allowConnection == nil:
|
2023-04-04 15:11:36 +00:00
|
|
|
# if the callback is not configured all incoming connections are allowed
|
2021-11-18 09:05:56 +00:00
|
|
|
true
|
|
|
|
else:
|
|
|
|
r.allowConnection(r, remoteAddress, connectionId)
|
|
|
|
|
2024-01-23 17:41:38 +00:00
|
|
|
proc processPacket[A](
|
|
|
|
r: UtpRouter[A], p: Packet, sender: A
|
|
|
|
) {.async: (raises: [CancelledError]).}=
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Received packet ",
|
|
|
|
sender = sender,
|
|
|
|
packetType = p.header.pType
|
2021-10-28 09:41:43 +00:00
|
|
|
|
|
|
|
case p.header.pType
|
|
|
|
of ST_RESET:
|
2021-11-05 08:41:41 +00:00
|
|
|
let maybeSocket = r.getSocketOnReset(sender, p.header.connectionId)
|
|
|
|
if maybeSocket.isSome():
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Received RST packet on known connection, closing socket"
|
2021-11-05 08:41:41 +00:00
|
|
|
let socket = maybeSocket.unsafeGet()
|
2023-04-04 15:11:36 +00:00
|
|
|
# The reference implementation actually changes the socket state to reset
|
|
|
|
# state unless the user explicitly closed the socket before. The only
|
|
|
|
# difference between the reset and the destroy state is that a socket in
|
|
|
|
# the destroy state is ultimately deleted from active connection list but
|
|
|
|
# a socket in reset state lingers there until the user of library closes
|
|
|
|
# it explicitly.
|
2021-11-09 14:29:59 +00:00
|
|
|
socket.destroy()
|
2021-11-05 08:41:41 +00:00
|
|
|
else:
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Received RST packet for unknown connection, ignoring"
|
2021-10-28 09:41:43 +00:00
|
|
|
of ST_SYN:
|
2023-04-04 15:11:36 +00:00
|
|
|
# SYN packets are special and need an addition of 1 to header connectionId
|
2021-10-28 09:41:43 +00:00
|
|
|
let socketKey = UtpSocketKey[A].init(sender, p.header.connectionId + 1)
|
|
|
|
let maybeSocket = r.getUtpSocket(socketKey)
|
|
|
|
if (maybeSocket.isSome()):
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Ignoring SYN for already existing connection"
|
2021-10-28 09:41:43 +00:00
|
|
|
else:
|
2022-08-04 13:55:39 +00:00
|
|
|
if (len(r.sockets) >= r.socketConfig.maxNumberOfOpenConnections):
|
|
|
|
debug "New incoming connection not allowed due to connection limit",
|
|
|
|
lenConnections = len(r.sockets),
|
|
|
|
limit = r.socketConfig.maxNumberOfOpenConnections
|
|
|
|
|
|
|
|
utp_declined_incoming.inc()
|
|
|
|
return
|
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
if (r.shouldAllowConnection(sender, p.header.connectionId)):
|
2022-01-20 08:22:53 +00:00
|
|
|
debug "Received SYN for new connection. Initiating incoming connection",
|
|
|
|
synSeqNr = p.header.seqNr
|
2023-04-04 15:11:36 +00:00
|
|
|
# Initial ackNr is set to incoming packet seqNr
|
2022-04-12 19:11:01 +00:00
|
|
|
let incomingSocket = newIncomingSocket[A](
|
|
|
|
sender, r.sendCb, r.socketConfig,
|
|
|
|
p.header.connectionId, p.header.seqNr, r.rng[])
|
2021-11-18 09:05:56 +00:00
|
|
|
r.registerUtpSocket(incomingSocket)
|
2022-02-24 17:22:44 +00:00
|
|
|
incomingSocket.startIncomingSocket()
|
2022-04-12 19:11:01 +00:00
|
|
|
# Based on configuration, socket is passed to upper layer either in
|
|
|
|
# SynRecv or Connected state
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_allowed_incoming.inc()
|
2022-04-12 19:11:01 +00:00
|
|
|
debug "Accepting incoming connection", src = incomingSocket.socketKey
|
2021-11-18 09:05:56 +00:00
|
|
|
asyncSpawn r.acceptConnection(r, incomingSocket)
|
|
|
|
else:
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_declined_incoming.inc()
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Connection declined"
|
2021-10-28 09:41:43 +00:00
|
|
|
else:
|
|
|
|
let socketKey = UtpSocketKey[A].init(sender, p.header.connectionId)
|
|
|
|
let maybeSocket = r.getUtpSocket(socketKey)
|
|
|
|
if (maybeSocket.isSome()):
|
2022-01-07 09:38:19 +00:00
|
|
|
debug "Received FIN/DATA/ACK packet on existing socket"
|
2021-10-28 09:41:43 +00:00
|
|
|
let socket = maybeSocket.unsafeGet()
|
|
|
|
await socket.processPacket(p)
|
|
|
|
else:
|
2023-04-04 15:11:36 +00:00
|
|
|
# TODO: add keeping track of recently send reset packets and do not send
|
2022-04-12 19:11:01 +00:00
|
|
|
# reset to peers which we recently send reset to.
|
2023-06-22 11:31:30 +00:00
|
|
|
debug "Received FIN/DATA/ACK on unknown socket, sending reset"
|
2022-04-12 19:11:01 +00:00
|
|
|
let rstPacket = resetPacket(
|
|
|
|
randUint16(r.rng[]), p.header.connectionId, p.header.seqNr)
|
2024-01-23 17:41:38 +00:00
|
|
|
r.sendCb(sender, encodePacket(rstPacket))
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc processIncomingBytes*[A](
|
2024-01-23 17:41:38 +00:00
|
|
|
r: UtpRouter[A], bytes: seq[byte], sender: A
|
|
|
|
) {.async: (raises:[CancelledError]).} =
|
2021-11-09 14:29:59 +00:00
|
|
|
if (not r.closed):
|
2022-08-04 13:55:39 +00:00
|
|
|
let decoded = decodePacket(bytes)
|
|
|
|
if (decoded.isOk()):
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_received_packets.inc()
|
2022-08-04 13:55:39 +00:00
|
|
|
await processPacket[A](r, decoded.get(), sender)
|
2021-11-09 14:29:59 +00:00
|
|
|
else:
|
2022-03-17 09:53:46 +00:00
|
|
|
utp_failed_packets.inc()
|
2022-08-04 13:55:39 +00:00
|
|
|
let err = decoded.error()
|
2022-04-12 19:11:01 +00:00
|
|
|
warn "Failed to decode packet from address", address = sender, msg = err
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc generateNewUniqueSocket[A](
|
2024-06-11 11:56:37 +00:00
|
|
|
r: UtpRouter[A], address: A): Opt[UtpSocket[A]] =
|
2022-04-12 19:11:01 +00:00
|
|
|
## Try to generate unique socket, give up after maxSocketGenerationTries tries
|
2021-11-18 09:05:56 +00:00
|
|
|
var tryCount = 0
|
|
|
|
|
|
|
|
while tryCount < maxSocketGenerationTries:
|
|
|
|
let rcvId = randUint16(r.rng[])
|
2022-04-12 19:11:01 +00:00
|
|
|
let socket = newOutgoingSocket[A](
|
|
|
|
address, r.sendCb, r.socketConfig, rcvId, r.rng[])
|
2021-11-18 09:05:56 +00:00
|
|
|
|
|
|
|
if r.registerIfAbsent(socket):
|
2024-06-11 11:56:37 +00:00
|
|
|
return Opt.some(socket)
|
2021-12-20 12:14:50 +00:00
|
|
|
|
2021-11-18 09:05:56 +00:00
|
|
|
inc tryCount
|
|
|
|
|
2024-06-11 11:56:37 +00:00
|
|
|
return Opt.none(UtpSocket[A])
|
2022-05-30 08:28:10 +00:00
|
|
|
|
2024-06-11 11:56:37 +00:00
|
|
|
proc connect[A](s: UtpSocket[A]): Future[ConnectionResult[A]] {.async: (raises: [CancelledError]).} =
|
|
|
|
debug "Initiating connection", dst = s.socketKey
|
|
|
|
try:
|
|
|
|
await s.startOutgoingSocket()
|
|
|
|
utp_success_outgoing.inc()
|
|
|
|
debug "Outgoing connection successful", dst = s.socketKey
|
|
|
|
return ok(s)
|
|
|
|
except ConnectionError:
|
|
|
|
utp_failed_outgoing.inc()
|
|
|
|
debug "Outgoing connection timed-out", dst = s.socketKey
|
|
|
|
s.destroy()
|
|
|
|
return err(ConnectionTimedOut)
|
|
|
|
except CancelledError as exc:
|
|
|
|
s.destroy()
|
|
|
|
debug "Connection cancelled", dst = s.socketKey
|
|
|
|
raise exc
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc connectTo*[A](
|
2024-06-11 11:56:37 +00:00
|
|
|
r: UtpRouter[A], address: A
|
|
|
|
): Future[ConnectionResult[A]] {.async: (raises: [CancelledError]).} =
|
|
|
|
## Connect to the provided address
|
|
|
|
## Reference implementation:
|
|
|
|
## https://github.com/bittorrent/libutp/blob/master/utp_internal.cpp#L2732
|
|
|
|
let socket = (r.generateNewUniqueSocket(address)).valueOr:
|
|
|
|
return err(SocketAlreadyExists)
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2024-06-11 11:56:37 +00:00
|
|
|
await socket.connect()
|
2021-11-18 09:05:56 +00:00
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
proc connectTo*[A](
|
2024-06-11 11:56:37 +00:00
|
|
|
r: UtpRouter[A], address: A, connectionId: uint16
|
|
|
|
): Future[ConnectionResult[A]] {.async: (raises: [CancelledError]).} =
|
|
|
|
## Connect to address with provided connection id. If a socket with this id
|
|
|
|
## id and address already exists, return SocketAlreadyExists error.
|
2022-04-12 19:11:01 +00:00
|
|
|
let socket = newOutgoingSocket[A](
|
|
|
|
address, r.sendCb, r.socketConfig, connectionId, r.rng[])
|
2021-11-18 09:05:56 +00:00
|
|
|
|
|
|
|
if (r.registerIfAbsent(socket)):
|
2024-06-11 11:56:37 +00:00
|
|
|
await socket.connect()
|
2021-11-18 09:05:56 +00:00
|
|
|
else:
|
2024-06-11 11:56:37 +00:00
|
|
|
err(SocketAlreadyExists)
|
2021-10-28 09:41:43 +00:00
|
|
|
|
2021-11-09 14:29:59 +00:00
|
|
|
proc shutdown*[A](r: UtpRouter[A]) =
|
2024-06-11 11:56:37 +00:00
|
|
|
## Stop processing any new packets and close all sockets in background without
|
|
|
|
## notifying remote peers.
|
2021-11-09 14:29:59 +00:00
|
|
|
r.closed = true
|
|
|
|
for s in r.allSockets():
|
|
|
|
s.destroy()
|
|
|
|
|
2024-06-11 11:56:37 +00:00
|
|
|
proc shutdownWait*[A](r: UtpRouter[A]) {.async: (raises: []).} =
|
2021-11-09 14:29:59 +00:00
|
|
|
var activeSockets: seq[UtpSocket[A]] = @[]
|
2024-06-11 11:56:37 +00:00
|
|
|
## Stop processing any new packets and close all sockets without notifying
|
|
|
|
## remote peers.
|
2021-11-09 14:29:59 +00:00
|
|
|
r.closed = true
|
|
|
|
|
2022-04-12 19:11:01 +00:00
|
|
|
# Need to make a copy as calling socket.destroyWait() removes the socket from
|
|
|
|
# the table and iterator throws error. Another option would be to wait until
|
|
|
|
# the number of open sockets drops to 0
|
2021-10-28 09:41:43 +00:00
|
|
|
for s in r.allSockets():
|
2021-11-09 14:29:59 +00:00
|
|
|
activeSockets.add(s)
|
|
|
|
|
|
|
|
for s in activeSockets:
|
2024-06-11 11:56:37 +00:00
|
|
|
await noCancel(s.destroyWait())
|