From cdade74acf90ca8ae3f66cbb5f16e720698525ad Mon Sep 17 00:00:00 2001 From: Tanguy Date: Wed, 28 Sep 2022 18:22:49 +0200 Subject: [PATCH] Revert "Switch to results.Opt (#38)" This reverts commit 9dd9b528cfda50f6482f800175d7350cb03911ff. --- .github/workflows/test.yml | 2 +- quic/basics.nim | 10 ++++- quic/connection.nim | 10 ++--- .../ngtcp2/connection/closingstate.nim | 2 +- .../ngtcp2/connection/disconnectingstate.nim | 12 +++--- .../ngtcp2/connection/drainingstate.nim | 10 ++--- .../transport/ngtcp2/connection/openstate.nim | 22 ++++------ quic/transport/ngtcp2/native/client.nim | 2 +- quic/transport/ngtcp2/native/connection.nim | 42 +++++++++---------- quic/transport/ngtcp2/native/ids.nim | 4 +- quic/transport/ngtcp2/native/server.nim | 2 +- .../transport/ngtcp2/stream/drainingstate.nim | 10 ++--- quic/transport/ngtcp2/stream/openstate.nim | 14 +++---- quic/transport/quicconnection.nim | 2 +- quic/transport/timeout.nim | 4 +- tests/quic/testNgtcp2TransportParameters.nim | 4 +- 16 files changed, 76 insertions(+), 76 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f878cc5..c13e2b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ on: jobs: test: runs-on: ${{ matrix.os }} - timeout-minutes: 20 + timeout-minutes: 10 strategy: matrix: os: [ubuntu-latest, macOS-latest, windows-latest] diff --git a/quic/basics.nim b/quic/basics.nim index 4ded8fb..63c9e0c 100644 --- a/quic/basics.nim +++ b/quic/basics.nim @@ -1,11 +1,17 @@ import pkg/chronos import pkg/upraises -import pkg/stew/results +import std/options import ./udp/datagram import ./errors export chronos +export options export datagram -export results export errors export upraises + +template getOr*[T](o: Option[T], otherwise: untyped): T = + if o.isSome(): + o.unsafeGet() + else: + otherwise diff --git a/quic/connection.nim b/quic/connection.nim index 7c431ba..b3fbef1 100644 --- a/quic/connection.nim +++ b/quic/connection.nim @@ -12,7 +12,7 @@ type udp: DatagramTransport quic: QuicConnection loop: Future[void] - onClose: Opt[proc() {.gcsafe, upraises: [].}] + onClose: Option[proc() {.gcsafe, upraises: [].}] closed: AsyncEvent IncomingConnection = ref object of Connection OutgoingConnection = ref object of Connection @@ -27,7 +27,7 @@ proc `onRemoveId=`*(connection: Connection, callback: IdCallback) = connection.quic.onRemoveId = callback proc `onClose=`*(connection: Connection, callback: proc() {.gcsafe, upraises: [].}) = - connection.onClose = Opt.some(callback) + connection.onClose = some callback proc drop*(connection: Connection) {.async.} = await connection.quic.drop() @@ -69,9 +69,8 @@ proc newIncomingConnection*(udp: DatagramTransport, let quic = newQuicServerConnection(udp.localAddress, remote, datagram) let closed = newAsyncEvent() let connection = IncomingConnection(udp: udp, quic: quic, closed: closed) - proc onDisconnect {.async.} = + quic.disconnect = some proc {.async.} = await connection.disconnect() - quic.disconnect = Opt.some(onDisconnect) connection.startSending(remote) connection @@ -80,9 +79,8 @@ proc newOutgoingConnection*(udp: DatagramTransport, let quic = newQuicClientConnection(udp.localAddress, remote) let closed = newAsyncEvent() let connection = OutgoingConnection(udp: udp, quic: quic, closed: closed) - proc onDisconnect {.async.} = + quic.disconnect = some proc {.async.} = await connection.disconnect() - quic.disconnect = Opt.some(onDisconnect) connection.startSending(remote) connection diff --git a/quic/transport/ngtcp2/connection/closingstate.nim b/quic/transport/ngtcp2/connection/closingstate.nim index 7f68caf..8ffd8e0 100644 --- a/quic/transport/ngtcp2/connection/closingstate.nim +++ b/quic/transport/ngtcp2/connection/closingstate.nim @@ -14,7 +14,7 @@ proc newClosingConnection*(finalDatagram: Datagram, ids: seq[ConnectionId], state proc sendFinalDatagram(state: ClosingConnection) = - let connection = state.connection.valueOr: return + let connection = state.connection.getOr: return try: connection.outgoing.putNoWait(state.finalDatagram) except AsyncQueueFullError: diff --git a/quic/transport/ngtcp2/connection/disconnectingstate.nim b/quic/transport/ngtcp2/connection/disconnectingstate.nim index 87935c5..3f58519 100644 --- a/quic/transport/ngtcp2/connection/disconnectingstate.nim +++ b/quic/transport/ngtcp2/connection/disconnectingstate.nim @@ -6,7 +6,7 @@ import ./closedstate type DisconnectingConnection* = ref object of ConnectionState - connection: Opt[QuicConnection] + connection: Option[QuicConnection] disconnect: Future[void] ids: seq[ConnectionId] @@ -15,7 +15,7 @@ proc newDisconnectingConnection*(ids: seq[ConnectionId]): DisconnectingConnection(ids: ids) proc callDisconnect(connection: QuicConnection) {.async.} = - let disconnect = connection.disconnect.valueOr: return + let disconnect = connection.disconnect.getOr: return await disconnect() {.push locks: "unknown".} @@ -25,12 +25,12 @@ method ids*(state: DisconnectingConnection): seq[ConnectionId] = method enter(state: DisconnectingConnection, connection: QuicConnection) = procCall enter(ConnectionState(state), connection) - state.connection = Opt.some(connection) + state.connection = some connection state.disconnect = callDisconnect(connection) method leave(state: DisconnectingConnection) = procCall leave(ConnectionState(state)) - state.connection = Opt.none(QuicConnection) + state.connection = QuicConnection.none method send(state: DisconnectingConnection) = raise newException(ClosedConnectionError, "connection is disconnecting") @@ -44,12 +44,12 @@ method openStream(state: DisconnectingConnection, method close(state: DisconnectingConnection) {.async.} = await state.disconnect - let connection = state.connection.valueOr: return + let connection = state.connection.getOr: return connection.switch(newClosedConnection()) method drop(state: DisconnectingConnection) {.async.} = await state.disconnect - let connection = state.connection.valueOr: return + let connection = state.connection.getOr: return connection.switch(newClosedConnection()) {.pop.} diff --git a/quic/transport/ngtcp2/connection/drainingstate.nim b/quic/transport/ngtcp2/connection/drainingstate.nim index 078339c..fd1a8db 100644 --- a/quic/transport/ngtcp2/connection/drainingstate.nim +++ b/quic/transport/ngtcp2/connection/drainingstate.nim @@ -8,7 +8,7 @@ import ./closedstate type DrainingConnection* = ref object of ConnectionState - connection*: Opt[QuicConnection] + connection*: Option[QuicConnection] ids: seq[ConnectionId] timeout: Timeout duration: Duration @@ -33,14 +33,14 @@ push: {.locks: "unknown", upraises: [QuicError].} method enter*(state: DrainingConnection, connection: QuicConnection) = procCall enter(ConnectionState(state), connection) - state.connection = Opt.some(connection) + state.connection = some connection state.timeout = newTimeout(proc {.upraises: [].} = state.onTimeout()) state.timeout.set(state.duration) method leave(state: DrainingConnection) = procCall leave(ConnectionState(state)) state.timeout.stop() - state.connection = Opt.none(QuicConnection) + state.connection = QuicConnection.none method ids(state: DrainingConnection): seq[ConnectionId] {.upraises: [].} = state.ids @@ -57,13 +57,13 @@ method openStream(state: DrainingConnection, method close(state: DrainingConnection) {.async.} = await state.done.wait() - let connection = state.connection.valueOr: return + let connection = state.connection.getOr: return let disconnecting = newDisconnectingConnection(state.ids) connection.switch(disconnecting) await disconnecting.close() method drop(state: DrainingConnection) {.async.} = - let connection = state.connection.valueOr: return + let connection = state.connection.getOr: return let disconnecting = newDisconnectingConnection(state.ids) connection.switch(disconnecting) await disconnecting.drop() diff --git a/quic/transport/ngtcp2/connection/openstate.nim b/quic/transport/ngtcp2/connection/openstate.nim index 7cd9906..808bbf9 100644 --- a/quic/transport/ngtcp2/connection/openstate.nim +++ b/quic/transport/ngtcp2/connection/openstate.nim @@ -13,7 +13,7 @@ import ./openstreams type OpenConnection* = ref object of ConnectionState - quicConnection: Opt[QuicConnection] + quicConnection: Option[QuicConnection] ngtcp2Connection: Ngtcp2Connection streams: OpenStreams @@ -31,17 +31,13 @@ proc openServerConnection*(local, remote: TransportAddress, method enter(state: OpenConnection, connection: QuicConnection) = procCall enter(ConnectionState(state), connection) - state.quicConnection = Opt.some(connection) - # Workaround weird bug - proc onNewId(id: ConnectionId) = + state.quicConnection = some connection + state.ngtcp2Connection.onNewId = some proc(id: ConnectionId) = if isNil(connection.onNewId): return connection.onNewId(id) - state.ngtcp2Connection.onNewId = Opt.some(onNewId) - - proc onRemoveId(id: ConnectionId) = + state.ngtcp2Connection.onRemoveId = some proc(id: ConnectionId) = if isNil(connection.onRemoveId): return connection.onRemoveId(id) - state.ngtcp2Connection.onRemoveId = Opt.some(onRemoveId) state.ngtcp2Connection.onSend = proc(datagram: Datagram) = errorAsDefect: connection.outgoing.putNoWait(datagram) @@ -55,7 +51,7 @@ method leave(state: OpenConnection) = procCall leave(ConnectionState(state)) state.streams.closeAll() state.ngtcp2Connection.destroy() - state.quicConnection = Opt.none(QuicConnection) + state.quicConnection = QuicConnection.none method ids(state: OpenConnection): seq[ConnectionId] {.upraises: [].} = state.ngtcp2Connection.ids @@ -65,7 +61,7 @@ method send(state: OpenConnection) = method receive(state: OpenConnection, datagram: Datagram) = state.ngtcp2Connection.receive(datagram) - let quicConnection = state.quicConnection.valueOr: return + let quicConnection = state.quicConnection.getOr: return if state.ngtcp2Connection.isDraining: let duration = state.ngtcp2Connection.closingDuration() let ids = state.ids @@ -75,14 +71,14 @@ method receive(state: OpenConnection, datagram: Datagram) = method openStream(state: OpenConnection, unidirectional: bool): Future[Stream] {.async.} = - let quicConnection = state.quicConnection.valueOr: + let quicConnection = state.quicConnection.getOr: raise newException(QuicError, "connection is closed") await quicConnection.handshake.wait() result = state.ngtcp2Connection.openStream(unidirectional = unidirectional) state.streams.add(result) method close(state: OpenConnection) {.async.} = - let quicConnection = state.quicConnection.valueOr: return + let quicConnection = state.quicConnection.getOr: return let finalDatagram = state.ngtcp2Connection.close() let duration = state.ngtcp2Connection.closingDuration() let ids = state.ids @@ -91,7 +87,7 @@ method close(state: OpenConnection) {.async.} = await closing.close() method drop(state: OpenConnection) {.async.} = - let quicConnection = state.quicConnection.valueOr: return + let quicConnection = state.quicConnection.getOr: return let disconnecting = newDisconnectingConnection(state.ids) quicConnection.switch(disconnecting) await disconnecting.drop() diff --git a/quic/transport/ngtcp2/native/client.nim b/quic/transport/ngtcp2/native/client.nim index e61474c..8f53400 100644 --- a/quic/transport/ngtcp2/native/client.nim +++ b/quic/transport/ngtcp2/native/client.nim @@ -63,4 +63,4 @@ proc newNgtcp2Client*(local, remote: TransportAddress): Ngtcp2Connection = addr result[] ) - result.conn = Opt.some(conn) + result.conn = conn.some diff --git a/quic/transport/ngtcp2/native/connection.nim b/quic/transport/ngtcp2/native/connection.nim index ce7d14e..40ec76b 100644 --- a/quic/transport/ngtcp2/native/connection.nim +++ b/quic/transport/ngtcp2/native/connection.nim @@ -13,7 +13,7 @@ import ./pointers type Ngtcp2Connection* = ref object - conn*: Opt[ptr ngtcp2_conn] + conn*: Option[ptr ngtcp2_conn] path*: Path buffer*: array[4096, byte] flowing*: AsyncEvent @@ -21,20 +21,20 @@ type onSend*: proc(datagram: Datagram) {.gcsafe, upraises:[].} onIncomingStream*: proc(stream: Stream) onHandshakeDone*: proc() - onNewId*: Opt[proc(id: ConnectionId)] - onRemoveId*: Opt[proc(id: ConnectionId)] + onNewId*: Option[proc(id: ConnectionId)] + onRemoveId*: Option[proc(id: ConnectionId)] Ngtcp2ConnectionClosed* = object of QuicError proc destroy*(connection: Ngtcp2Connection) = - let conn = connection.conn.valueOr: return + let conn = connection.conn.getOr: return connection.timeout.stop() ngtcp2_conn_del(conn) - connection.conn = Opt.none(ptr ngtcp2_conn) + connection.conn = none(ptr ngtcp2_conn) connection.onSend = nil connection.onIncomingStream = nil connection.onHandshakeDone = nil - connection.onNewId = Opt.none(proc(id: ConnectionId)) - connection.onRemoveId = Opt.none(proc(id: ConnectionId)) + connection.onNewId = none proc(id: ConnectionId) + connection.onRemoveId = none proc(id: ConnectionId) proc handleTimeout(connection: Ngtcp2Connection) {.gcsafe, upraises:[].} @@ -48,14 +48,14 @@ proc newConnection*(path: Path): Ngtcp2Connection = proc ids*(connection: Ngtcp2Connection): seq[ConnectionId] = let - conn = connection.conn.valueOr: return + conn = connection.conn.getOr: return amount = ngtcp2_conn_get_num_scid(conn) var scids = newSeq[ngtcp2_cid](amount) discard ngtcp2_conn_get_scid(conn, scids.toPtr) scids.mapIt(ConnectionId(it.data[0..