raiha 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/LICENSE +202 -0
- data/README.md +94 -22
- data/docs/configuration.md +227 -0
- data/docs/development.md +99 -0
- data/docs/fiber-scheduler.md +591 -0
- data/docs/getting-started.md +190 -0
- data/docs/http3-client.md +303 -0
- data/docs/http3-server.md +301 -0
- data/docs/platforms.md +98 -0
- data/docs/qlog.md +86 -0
- data/docs/quic.md +268 -0
- data/docs/threads.md +127 -0
- data/examples/_certificate.rb +55 -0
- data/examples/high_level_loopback.rb +59 -0
- data/examples/http3_get_public.rb +68 -0
- data/examples/in_memory_handshake.rb +38 -0
- data/examples/qlog_output.rb +58 -0
- data/examples/qlog_roundtrips.rb +66 -0
- data/examples/quic_gem_loopback.rb +62 -0
- data/examples/stream_echo.rb +68 -0
- data/examples/udp_handshake.rb +76 -0
- data/lib/raiha/http3/action.rb +21 -0
- data/lib/raiha/http3/capsule.rb +86 -0
- data/lib/raiha/http3/connection.rb +983 -0
- data/lib/raiha/http3/error.rb +24 -0
- data/lib/raiha/http3/error_code.rb +27 -0
- data/lib/raiha/http3/event.rb +63 -0
- data/lib/raiha/http3/frame.rb +148 -0
- data/lib/raiha/http3/frame_reader.rb +114 -0
- data/lib/raiha/http3/message.rb +137 -0
- data/lib/raiha/http3/output.rb +61 -0
- data/lib/raiha/http3/priority.rb +52 -0
- data/lib/raiha/http3/qlog.rb +145 -0
- data/lib/raiha/http3/qpack/decoder.rb +302 -0
- data/lib/raiha/http3/qpack/dynamic_table.rb +83 -0
- data/lib/raiha/http3/qpack/encoder.rb +94 -0
- data/lib/raiha/http3/qpack/error.rb +39 -0
- data/lib/raiha/http3/qpack/error_code.rb +13 -0
- data/lib/raiha/http3/qpack/field.rb +15 -0
- data/lib/raiha/http3/qpack/huffman.rb +397 -0
- data/lib/raiha/http3/qpack/reader.rb +82 -0
- data/lib/raiha/http3/qpack/result.rb +26 -0
- data/lib/raiha/http3/qpack/static_table.rb +139 -0
- data/lib/raiha/http3/qpack/writer.rb +44 -0
- data/lib/raiha/http3/qpack.rb +26 -0
- data/lib/raiha/http3/receiving.rb +46 -0
- data/lib/raiha/http3/sending.rb +23 -0
- data/lib/raiha/http3/session.rb +31 -0
- data/lib/raiha/http3/settings.rb +81 -0
- data/lib/raiha/http3/structured_field.rb +120 -0
- data/lib/raiha/http3/tracer.rb +26 -0
- data/lib/raiha/http3/varint.rb +45 -0
- data/lib/raiha/http3.rb +19 -0
- data/lib/raiha/http3_binding.rb +139 -0
- data/lib/raiha/namespace.rb +11 -0
- data/lib/raiha/quic/config.rb +229 -0
- data/lib/raiha/quic/congestion/algorithms.rb +17 -0
- data/lib/raiha/quic/congestion/cubic.rb +258 -0
- data/lib/raiha/quic/congestion/new_reno.rb +161 -0
- data/lib/raiha/quic/congestion/pacing.rb +21 -0
- data/lib/raiha/quic/congestion/startup/rapid_start.rb +249 -0
- data/lib/raiha/quic/congestion/startup.rb +13 -0
- data/lib/raiha/quic/congestion.rb +15 -0
- data/lib/raiha/quic/connection.rb +2022 -0
- data/lib/raiha/quic/connection_ids.rb +62 -0
- data/lib/raiha/quic/crypto/aead.rb +68 -0
- data/lib/raiha/quic/crypto/header_protection.rb +42 -0
- data/lib/raiha/quic/crypto/initial.rb +23 -0
- data/lib/raiha/quic/crypto/keys.rb +32 -0
- data/lib/raiha/quic/crypto/mac.rb +15 -0
- data/lib/raiha/quic/crypto/random.rb +13 -0
- data/lib/raiha/quic/crypto.rb +18 -0
- data/lib/raiha/quic/crypto_stream.rb +161 -0
- data/lib/raiha/quic/datagrams.rb +91 -0
- data/lib/raiha/quic/endpoint.rb +785 -0
- data/lib/raiha/quic/error.rb +61 -0
- data/lib/raiha/quic/error_code.rb +34 -0
- data/lib/raiha/quic/event.rb +59 -0
- data/lib/raiha/quic/issued_connection_ids.rb +137 -0
- data/lib/raiha/quic/kept_packets.rb +41 -0
- data/lib/raiha/quic/mtu_search.rb +75 -0
- data/lib/raiha/quic/negotiation.rb +143 -0
- data/lib/raiha/quic/new_token.rb +55 -0
- data/lib/raiha/quic/pacer.rb +52 -0
- data/lib/raiha/quic/packet_protection.rb +310 -0
- data/lib/raiha/quic/path.rb +473 -0
- data/lib/raiha/quic/peer_connection_ids.rb +160 -0
- data/lib/raiha/quic/qlog.rb +260 -0
- data/lib/raiha/quic/ranges.rb +32 -0
- data/lib/raiha/quic/recovery/duty.rb +102 -0
- data/lib/raiha/quic/recovery/packet_number_space.rb +420 -0
- data/lib/raiha/quic/recovery/rate_meter.rb +95 -0
- data/lib/raiha/quic/recovery/rtt_estimator.rb +57 -0
- data/lib/raiha/quic/recovery/sent_packet.rb +16 -0
- data/lib/raiha/quic/recovery.rb +641 -0
- data/lib/raiha/quic/remembered_transport_parameters.rb +29 -0
- data/lib/raiha/quic/retry_token.rb +54 -0
- data/lib/raiha/quic/route.rb +37 -0
- data/lib/raiha/quic/scheduler.rb +112 -0
- data/lib/raiha/quic/session.rb +47 -0
- data/lib/raiha/quic/stateless_reset.rb +38 -0
- data/lib/raiha/quic/stats.rb +24 -0
- data/lib/raiha/quic/streams/receive_window.rb +119 -0
- data/lib/raiha/quic/streams/receiver.rb +217 -0
- data/lib/raiha/quic/streams/sender.rb +200 -0
- data/lib/raiha/quic/streams.rb +687 -0
- data/lib/raiha/quic/test/bottleneck.rb +55 -0
- data/lib/raiha/quic/test/congestion_contract.rb +106 -0
- data/lib/raiha/quic/test/network.rb +244 -0
- data/lib/raiha/quic/test/random_loss.rb +34 -0
- data/lib/raiha/quic/test/reorder.rb +51 -0
- data/lib/raiha/quic/test/server.rb +54 -0
- data/lib/raiha/quic/test/vectors.rb +337 -0
- data/lib/raiha/quic/test/virtual_clock.rb +21 -0
- data/lib/raiha/quic/test.rb +17 -0
- data/lib/raiha/quic/tls_binding.rb +179 -0
- data/lib/raiha/quic/token.rb +28 -0
- data/lib/raiha/quic/tracer.rb +127 -0
- data/lib/raiha/quic/transmit.rb +14 -0
- data/lib/raiha/quic/transport_parameters.rb +244 -0
- data/lib/raiha/quic/version.rb +63 -0
- data/lib/raiha/quic/wire/codec.rb +136 -0
- data/lib/raiha/quic/wire/frame.rb +521 -0
- data/lib/raiha/quic/wire/header.rb +90 -0
- data/lib/raiha/quic/wire/packet.rb +130 -0
- data/lib/raiha/quic/wire/packet_number.rb +47 -0
- data/lib/raiha/quic/wire/retry.rb +62 -0
- data/lib/raiha/quic/wire/version_negotiation.rb +53 -0
- data/lib/raiha/quic/wire.rb +9 -0
- data/lib/raiha/quic.rb +46 -0
- data/lib/raiha/runtime/addresses.rb +35 -0
- data/lib/raiha/runtime/admission.rb +37 -0
- data/lib/raiha/runtime/client.rb +190 -0
- data/lib/raiha/runtime/driver.rb +165 -0
- data/lib/raiha/runtime/error.rb +16 -0
- data/lib/raiha/runtime/http3.rb +363 -0
- data/lib/raiha/runtime/http3_fields.rb +38 -0
- data/lib/raiha/runtime/http3_server.rb +416 -0
- data/lib/raiha/runtime/kept.rb +36 -0
- data/lib/raiha/runtime/qlog.rb +204 -0
- data/lib/raiha/runtime/rack.rb +180 -0
- data/lib/raiha/runtime/server.rb +334 -0
- data/lib/raiha/runtime/session.rb +447 -0
- data/lib/raiha/runtime/threaded/client.rb +161 -0
- data/lib/raiha/runtime/threaded/driver.rb +230 -0
- data/lib/raiha/runtime/threaded/http3.rb +287 -0
- data/lib/raiha/runtime/threaded/http3_server.rb +432 -0
- data/lib/raiha/runtime/threaded/server.rb +290 -0
- data/lib/raiha/runtime/threaded/session.rb +604 -0
- data/lib/raiha/runtime/threaded.rb +45 -0
- data/lib/raiha/runtime/udp.rb +410 -0
- data/lib/raiha/runtime/worker.rb +138 -0
- data/lib/raiha/runtime/workers.rb +248 -0
- data/lib/raiha/runtime.rb +37 -0
- data/lib/raiha/tls/anti_replay.rb +139 -0
- data/lib/raiha/tls/cipher_suite.rb +62 -0
- data/lib/raiha/tls/codec/error.rb +43 -0
- data/lib/raiha/tls/codec/reader.rb +95 -0
- data/lib/raiha/tls/codec/writer.rb +81 -0
- data/lib/raiha/tls/codec.rb +5 -0
- data/lib/raiha/tls/config.rb +318 -0
- data/lib/raiha/tls/crypto/aead.rb +64 -0
- data/lib/raiha/tls/crypto/certificate.rb +28 -0
- data/lib/raiha/tls/crypto/digest.rb +30 -0
- data/lib/raiha/tls/crypto/ecdh.rb +74 -0
- data/lib/raiha/tls/crypto/hash.rb +22 -0
- data/lib/raiha/tls/crypto/hkdf.rb +28 -0
- data/lib/raiha/tls/crypto/signature.rb +87 -0
- data/lib/raiha/tls/crypto.rb +19 -0
- data/lib/raiha/tls/error.rb +36 -0
- data/lib/raiha/tls/event.rb +45 -0
- data/lib/raiha/tls/extension/alpn.rb +35 -0
- data/lib/raiha/tls/extension/certificate_authorities.rb +35 -0
- data/lib/raiha/tls/extension/compress_certificate.rb +33 -0
- data/lib/raiha/tls/extension/cookie.rb +27 -0
- data/lib/raiha/tls/extension/early_data.rb +54 -0
- data/lib/raiha/tls/extension/key_share.rb +90 -0
- data/lib/raiha/tls/extension/list.rb +49 -0
- data/lib/raiha/tls/extension/opaque.rb +21 -0
- data/lib/raiha/tls/extension/placement.rb +55 -0
- data/lib/raiha/tls/extension/post_handshake_auth.rb +22 -0
- data/lib/raiha/tls/extension/pre_shared_key.rb +81 -0
- data/lib/raiha/tls/extension/psk_key_exchange_modes.rb +32 -0
- data/lib/raiha/tls/extension/record_size_limit.rb +30 -0
- data/lib/raiha/tls/extension/registry.rb +50 -0
- data/lib/raiha/tls/extension/server_name.rb +63 -0
- data/lib/raiha/tls/extension/signature_algorithms.rb +39 -0
- data/lib/raiha/tls/extension/signature_algorithms_cert.rb +21 -0
- data/lib/raiha/tls/extension/supported_groups.rb +32 -0
- data/lib/raiha/tls/extension/supported_versions.rb +53 -0
- data/lib/raiha/tls/extension.rb +32 -0
- data/lib/raiha/tls/handshake/acceptor.rb +164 -0
- data/lib/raiha/tls/handshake/authentication_messages.rb +127 -0
- data/lib/raiha/tls/handshake/certificate.rb +56 -0
- data/lib/raiha/tls/handshake/certificate_request.rb +39 -0
- data/lib/raiha/tls/handshake/certificate_verify.rb +40 -0
- data/lib/raiha/tls/handshake/client_hello.rb +208 -0
- data/lib/raiha/tls/handshake/client_hello_builder.rb +144 -0
- data/lib/raiha/tls/handshake/client_side.rb +578 -0
- data/lib/raiha/tls/handshake/conductor.rb +390 -0
- data/lib/raiha/tls/handshake/encrypted_extensions.rb +33 -0
- data/lib/raiha/tls/handshake/end_of_early_data.rb +28 -0
- data/lib/raiha/tls/handshake/finished.rb +36 -0
- data/lib/raiha/tls/handshake/key_update.rb +38 -0
- data/lib/raiha/tls/handshake/key_update_handling.rb +88 -0
- data/lib/raiha/tls/handshake/machine.rb +238 -0
- data/lib/raiha/tls/handshake/message.rb +25 -0
- data/lib/raiha/tls/handshake/new_session_ticket.rb +48 -0
- data/lib/raiha/tls/handshake/reassembler.rb +88 -0
- data/lib/raiha/tls/handshake/server_hello.rb +98 -0
- data/lib/raiha/tls/handshake/server_side.rb +672 -0
- data/lib/raiha/tls/handshake/violation.rb +12 -0
- data/lib/raiha/tls/handshake.rb +62 -0
- data/lib/raiha/tls/iana.rb +113 -0
- data/lib/raiha/tls/key_exchange.rb +58 -0
- data/lib/raiha/tls/key_schedule.rb +269 -0
- data/lib/raiha/tls/keylog.rb +40 -0
- data/lib/raiha/tls/keyring.rb +180 -0
- data/lib/raiha/tls/masked_inspect.rb +33 -0
- data/lib/raiha/tls/output.rb +32 -0
- data/lib/raiha/tls/profile.rb +82 -0
- data/lib/raiha/tls/record/layer.rb +228 -0
- data/lib/raiha/tls/record/protection.rb +67 -0
- data/lib/raiha/tls/record.rb +11 -0
- data/lib/raiha/tls/server_name.rb +42 -0
- data/lib/raiha/tls/session.rb +106 -0
- data/lib/raiha/tls/signature_scheme.rb +35 -0
- data/lib/raiha/tls/tcp/acceptor.rb +121 -0
- data/lib/raiha/tls/tcp/connection.rb +371 -0
- data/lib/raiha/tls/tcp/event.rb +15 -0
- data/lib/raiha/tls/tcp/socket.rb +161 -0
- data/lib/raiha/tls/tcp.rb +14 -0
- data/lib/raiha/tls/test/fixed_kem.rb +30 -0
- data/lib/raiha/tls/test/fixed_random.rb +18 -0
- data/lib/raiha/tls/test/rfc8448.rb +923 -0
- data/lib/raiha/tls/test/rfc8448_handshakes.rb +118 -0
- data/lib/raiha/tls/test.rb +13 -0
- data/lib/raiha/tls/ticket.rb +184 -0
- data/lib/raiha/tls/ticket_keys.rb +206 -0
- data/lib/raiha/tls/trace.rb +58 -0
- data/lib/raiha/tls/transcript.rb +94 -0
- data/lib/raiha/tls/verifier/default.rb +72 -0
- data/lib/raiha/tls/verifier/host_name.rb +75 -0
- data/lib/raiha/tls/verifier/none.rb +14 -0
- data/lib/raiha/tls/verifier.rb +24 -0
- data/lib/raiha/tls.rb +34 -0
- data/lib/raiha/version.rb +3 -1
- data/lib/raiha.rb +8 -5
- data/sig/patches/array.rbs +9 -0
- data/sig/patches/io_buffer.rbs +13 -0
- data/sig/patches/openssl.rbs +25 -0
- data/sig/patches/socket.rbs +29 -0
- data/sig/patches/string.rbs +26 -0
- data/sig/patches/strscan.rbs +12 -0
- data/sig/raiha/http3/capsule.rbs +32 -0
- data/sig/raiha/http3/connection.rbs +324 -0
- data/sig/raiha/http3/frame.rbs +138 -0
- data/sig/raiha/http3/frame_reader.rbs +39 -0
- data/sig/raiha/http3/message.rbs +23 -0
- data/sig/raiha/http3/priority.rbs +40 -0
- data/sig/raiha/http3/qlog.rbs +54 -0
- data/sig/raiha/http3/qpack.rbs +246 -0
- data/sig/raiha/http3/session.rbs +11 -0
- data/sig/raiha/http3/settings.rbs +26 -0
- data/sig/raiha/http3.rbs +2 -0
- data/sig/raiha/http3_binding.rbs +26 -0
- data/sig/raiha/namespace.rbs +2 -0
- data/sig/raiha/quic/config.rbs +89 -0
- data/sig/raiha/quic/congestion.rbs +213 -0
- data/sig/raiha/quic/connection.rbs +249 -0
- data/sig/raiha/quic/connection_ids.rbs +34 -0
- data/sig/raiha/quic/crypto.rbs +78 -0
- data/sig/raiha/quic/crypto_stream.rbs +34 -0
- data/sig/raiha/quic/datagrams.rbs +32 -0
- data/sig/raiha/quic/endpoint.rbs +179 -0
- data/sig/raiha/quic/frame.rbs +387 -0
- data/sig/raiha/quic/issued_connection_ids.rbs +47 -0
- data/sig/raiha/quic/kept_packets.rbs +25 -0
- data/sig/raiha/quic/mtu_search.rbs +21 -0
- data/sig/raiha/quic/negotiation.rbs +33 -0
- data/sig/raiha/quic/new_token.rbs +14 -0
- data/sig/raiha/quic/pacer.rbs +16 -0
- data/sig/raiha/quic/packet_protection.rbs +63 -0
- data/sig/raiha/quic/path.rbs +106 -0
- data/sig/raiha/quic/peer_connection_ids.rbs +42 -0
- data/sig/raiha/quic/qlog.rbs +74 -0
- data/sig/raiha/quic/ranges.rbs +7 -0
- data/sig/raiha/quic/recovery.rbs +376 -0
- data/sig/raiha/quic/retry_token.rbs +21 -0
- data/sig/raiha/quic/route.rbs +51 -0
- data/sig/raiha/quic/scheduler.rbs +36 -0
- data/sig/raiha/quic/session.rbs +17 -0
- data/sig/raiha/quic/stateless_reset.rbs +10 -0
- data/sig/raiha/quic/streams.rbs +244 -0
- data/sig/raiha/quic/test.rbs +354 -0
- data/sig/raiha/quic/tls_binding.rbs +58 -0
- data/sig/raiha/quic/token.rbs +11 -0
- data/sig/raiha/quic/tracer.rbs +71 -0
- data/sig/raiha/quic/transport_parameters.rbs +113 -0
- data/sig/raiha/quic/values.rbs +200 -0
- data/sig/raiha/quic/version.rbs +30 -0
- data/sig/raiha/quic/wire.rbs +199 -0
- data/sig/raiha/quic.rbs +2 -0
- data/sig/raiha/runtime/threaded.rbs +616 -0
- data/sig/raiha/runtime.rbs +982 -0
- data/sig/raiha/tls/anti_replay.rbs +41 -0
- data/sig/raiha/tls/cipher_suite.rbs +41 -0
- data/sig/raiha/tls/codec.rbs +66 -0
- data/sig/raiha/tls/config.rbs +122 -0
- data/sig/raiha/tls/crypto.rbs +86 -0
- data/sig/raiha/tls/error.rbs +21 -0
- data/sig/raiha/tls/event.rbs +61 -0
- data/sig/raiha/tls/extension.rbs +331 -0
- data/sig/raiha/tls/handshake.rbs +311 -0
- data/sig/raiha/tls/iana.rbs +13 -0
- data/sig/raiha/tls/key_exchange.rbs +33 -0
- data/sig/raiha/tls/key_schedule.rbs +58 -0
- data/sig/raiha/tls/keylog.rbs +20 -0
- data/sig/raiha/tls/keyring.rbs +64 -0
- data/sig/raiha/tls/machine.rbs +429 -0
- data/sig/raiha/tls/masked_inspect.rbs +12 -0
- data/sig/raiha/tls/output.rbs +37 -0
- data/sig/raiha/tls/profile.rbs +26 -0
- data/sig/raiha/tls/record.rbs +79 -0
- data/sig/raiha/tls/server_name.rbs +10 -0
- data/sig/raiha/tls/session.rbs +48 -0
- data/sig/raiha/tls/signature_scheme.rbs +20 -0
- data/sig/raiha/tls/tcp.rbs +134 -0
- data/sig/raiha/tls/test.rbs +143 -0
- data/sig/raiha/tls/ticket.rbs +68 -0
- data/sig/raiha/tls/ticket_keys.rbs +67 -0
- data/sig/raiha/tls/trace.rbs +99 -0
- data/sig/raiha/tls/transcript.rbs +22 -0
- data/sig/raiha/tls/verifier.rbs +54 -0
- data/sig/raiha/tls.rbs +5 -0
- data/sig/raiha.rbs +3 -0
- metadata +355 -57
- data/.gitignore +0 -10
- data/CODE_OF_CONDUCT.md +0 -74
- data/Gemfile +0 -4
- data/Gemfile.lock +0 -22
- data/Rakefile +0 -10
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/raiha.gemspec +0 -40
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raiha::Runtime
|
|
4
|
+
# The peer reset the stream a read waits on, or stopped the one a write
|
|
5
|
+
# goes to, with `error_code`.
|
|
6
|
+
class StreamReset < StandardError
|
|
7
|
+
attr_reader :stream_id, :error_code
|
|
8
|
+
|
|
9
|
+
def initialize(stream_id, error_code)
|
|
10
|
+
@stream_id = stream_id
|
|
11
|
+
@error_code = error_code
|
|
12
|
+
super(format("stream %d was reset with 0x%x", stream_id, error_code))
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# A QUIC connection the fibers of the application use as if it blocked:
|
|
17
|
+
# a read waits for data, a write for room, an open for a stream the peer
|
|
18
|
+
# allows, each on the condition of the connection, which its Driver's
|
|
19
|
+
# turns signal. A call made once the connection has closed, or waiting
|
|
20
|
+
# when it does, raises QUIC::ClosedError, and so does one once the driver
|
|
21
|
+
# has stopped, or what failed in it.
|
|
22
|
+
#
|
|
23
|
+
# The events of the connection are taken before each wait is looked at
|
|
24
|
+
# again, so that a stream the peer reset is not read to an end it never
|
|
25
|
+
# sent. What the Session keeps of a stream goes once the application is
|
|
26
|
+
# done with both its parts, and of the streams the peer opened it keeps
|
|
27
|
+
# the next to come of each type, so a long connection keeps no more than
|
|
28
|
+
# the streams open on it.
|
|
29
|
+
#
|
|
30
|
+
# A protocol over the connection, HTTP/3, is attached to the Session: it
|
|
31
|
+
# takes each event first, keeping those of its own, and takes its turn
|
|
32
|
+
# after them, and the streams it opens are its own to keep.
|
|
33
|
+
#
|
|
34
|
+
# A client's Session keeps the tickets and the NEW_TOKEN tokens the
|
|
35
|
+
# server gives, for later connections; storing them is the
|
|
36
|
+
# application's.
|
|
37
|
+
#
|
|
38
|
+
# When the server takes no 0-RTT, the streams the client opened are gone
|
|
39
|
+
# and their IDs start over (QUIC::Connection#zero_rtt_rejected): each
|
|
40
|
+
# Stream of them raises QUIC::Error::ZeroRttRejected from then on, so
|
|
41
|
+
# that it cannot reach a stream opened since under the same ID, and the
|
|
42
|
+
# Session tells the connection the application knows. What to send
|
|
43
|
+
# again is the application's.
|
|
44
|
+
class Session
|
|
45
|
+
# What the application has yet to hear of a stream: how the peer reset
|
|
46
|
+
# its sending part or stopped ours, and whether each part is still in
|
|
47
|
+
# use.
|
|
48
|
+
Track = Struct.new(:reset, :stop, :reading, :writing)
|
|
49
|
+
# The peer's max_ack_delay when it says none (RFC 9000 "Transport
|
|
50
|
+
# Parameter Definitions"), for a PTO of the connection.
|
|
51
|
+
MAX_ACK_DELAY = 0.025
|
|
52
|
+
# The most tickets and tokens kept (Kept::MAX_HELD).
|
|
53
|
+
MAX_HELD = Kept::MAX_HELD
|
|
54
|
+
# The longest close_when_acknowledged waits in all, so that a peer that
|
|
55
|
+
# takes what was sent a little at a time does not keep it waiting.
|
|
56
|
+
CLOSE_TIMEOUT = 30
|
|
57
|
+
|
|
58
|
+
# The connection, and the qlog tracer of it, if it has one, which a
|
|
59
|
+
# protocol records its own events with.
|
|
60
|
+
attr_reader :connection, :tracer
|
|
61
|
+
|
|
62
|
+
# At a client, the name of the server the connection is to, and what
|
|
63
|
+
# Client.connect was given to resume: a QUIC::Session, or an
|
|
64
|
+
# HTTP3::Session whose SETTINGS a protocol over the connection keeps to
|
|
65
|
+
# while 0-RTT goes. Both are nil at a server.
|
|
66
|
+
attr_reader :server_name, :resumed
|
|
67
|
+
|
|
68
|
+
def initialize(connection, driver, role:, tracer: nil, server_name: nil, resumed: nil)
|
|
69
|
+
@connection = connection
|
|
70
|
+
@tracer = tracer
|
|
71
|
+
@server_name = server_name
|
|
72
|
+
@resumed = resumed
|
|
73
|
+
@driver = driver
|
|
74
|
+
@role = role
|
|
75
|
+
@lock = Thread::Mutex.new
|
|
76
|
+
@changed = Thread::ConditionVariable.new
|
|
77
|
+
@waiting = 0
|
|
78
|
+
@tracks = {} #: Hash[Integer, Track]
|
|
79
|
+
# The streams the peer opened and the application has not taken, and
|
|
80
|
+
# the index of the next to come of each type of stream ID.
|
|
81
|
+
@incoming = [] #: Array[Integer]
|
|
82
|
+
@peer_next = [0, 0, 0, 0]
|
|
83
|
+
@aborted = nil #: StandardError?
|
|
84
|
+
@stopping = false
|
|
85
|
+
@stop_deadline = nil #: Float?
|
|
86
|
+
@connected = nil #: Raiha::QUIC::Event::Connected?
|
|
87
|
+
@protocol = nil #: _Protocol?
|
|
88
|
+
# The application's error code a close with none given closes with.
|
|
89
|
+
@close_code = 0
|
|
90
|
+
@kept = Kept.new(tracer)
|
|
91
|
+
# One more each time the server rejects 0-RTT; the Streams of this
|
|
92
|
+
# client opened before are of an earlier one.
|
|
93
|
+
@generation = 0
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# What the handshake came to (TLS::Event::HandshakeComplete), once it
|
|
97
|
+
# has completed.
|
|
98
|
+
def handshake = @connected&.handshake
|
|
99
|
+
|
|
100
|
+
# The tickets the server gave, each a QUIC::Session for
|
|
101
|
+
# Client.connect(session:), the newest MAX_HELD of them, newest last. A
|
|
102
|
+
# ticket is for one connection: once used, the application drops it,
|
|
103
|
+
# as one used again links the connections for those who watch (RFC 9846
|
|
104
|
+
# "Client and Server Tracking Prevention").
|
|
105
|
+
def tickets = @kept.tickets
|
|
106
|
+
|
|
107
|
+
# The tokens the server gave in NEW_TOKEN frames, for
|
|
108
|
+
# Client.connect(token:), the newest MAX_HELD of them, newest last. A
|
|
109
|
+
# token is for one connection too (RFC 9000 "Address Validation for
|
|
110
|
+
# Future Connections").
|
|
111
|
+
def tokens = @kept.tokens
|
|
112
|
+
|
|
113
|
+
# Puts `protocol` over the connection, which then closes with its
|
|
114
|
+
# `close_code` when no other is given: H3_NO_ERROR for HTTP/3, say. A
|
|
115
|
+
# handshake completed before is told it with the Connected event it
|
|
116
|
+
# missed.
|
|
117
|
+
def attach(protocol, close_code: @close_code)
|
|
118
|
+
@protocol = protocol
|
|
119
|
+
@close_code = close_code
|
|
120
|
+
connected = @connected and protocol.take(connected, Driver.clock)
|
|
121
|
+
changed
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Has what the protocol or the application changed go out.
|
|
125
|
+
def changed = @driver.changed
|
|
126
|
+
|
|
127
|
+
# A new stream of `direction` for a protocol, which keeps it itself: its
|
|
128
|
+
# ID, once the peer allows it.
|
|
129
|
+
def open_stream_id(direction: :bidi)
|
|
130
|
+
stream_id = wait { @connection.open_stream(direction:) }
|
|
131
|
+
changed
|
|
132
|
+
stream_id
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Calls the block until it gives something other than nil or "",
|
|
136
|
+
# waiting for a change in between, as `wait` does: for a protocol's
|
|
137
|
+
# waits. With `deadline`, nil once the clock is past it; with `drain`, a
|
|
138
|
+
# read gives what came before the connection closed.
|
|
139
|
+
# steep picks no overload of `wait` for a block that gives T?.
|
|
140
|
+
def wait_for(deadline: nil, drain: false) # steep:ignore
|
|
141
|
+
deadline ? wait(deadline:, drain:) { yield } : wait(drain:) { yield }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# A new stream of `direction` (:bidi or :uni), once the peer allows it.
|
|
145
|
+
def open_stream(direction: :bidi)
|
|
146
|
+
stream_id = wait { @connection.open_stream(direction:) }
|
|
147
|
+
@tracks[stream_id] = Track.new(nil, nil, direction == :bidi, true)
|
|
148
|
+
@driver.changed
|
|
149
|
+
Stream.new(self, stream_id, @generation)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Waits, for `seconds` at most, until all the streams have sent has been
|
|
153
|
+
# acknowledged; whether it has. A connection closed meanwhile raises
|
|
154
|
+
# QUIC::ClosedError.
|
|
155
|
+
def wait_acknowledged(seconds)
|
|
156
|
+
wait(deadline: Driver.clock + seconds) { @connection.stats.send_buffer_bytes.zero? || nil } || false
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Closes the connection with the application's `error_code` once what the
|
|
160
|
+
# streams sent is acknowledged, or three PTOs have gone with no more of
|
|
161
|
+
# it acknowledged, or `timeout` seconds in all, unless it has closed
|
|
162
|
+
# already: what is still going out when the application is done, a
|
|
163
|
+
# large write last, is not cut short while it goes.
|
|
164
|
+
def close_when_acknowledged(error_code: @close_code, timeout: CLOSE_TIMEOUT)
|
|
165
|
+
return if closed?
|
|
166
|
+
limit = Driver.clock + timeout
|
|
167
|
+
while (left = @connection.stats.send_buffer_bytes).positive?
|
|
168
|
+
stats = @connection.stats
|
|
169
|
+
three_ptos = 3 * (stats.rtt + [4 * stats.rtt_var, 0.001].max + MAX_ACK_DELAY)
|
|
170
|
+
deadline = [Driver.clock + three_ptos, limit].min
|
|
171
|
+
break unless wait(deadline:) { @connection.stats.send_buffer_bytes < left || nil }
|
|
172
|
+
end
|
|
173
|
+
close(error_code:)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Waits until the handshake has completed.
|
|
177
|
+
def wait_handshake
|
|
178
|
+
wait { @connection.handshake_complete? || nil }
|
|
179
|
+
nil
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Waits until the application may send: until the handshake has
|
|
183
|
+
# completed, or not at all while 0-RTT goes.
|
|
184
|
+
def wait_ready
|
|
185
|
+
wait { @connection.handshake_complete? || @connection.zero_rtt_state == :pending || nil }
|
|
186
|
+
nil
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# The next stream the peer opens; nil once the connection has closed, or
|
|
190
|
+
# the server is stopping and none has come.
|
|
191
|
+
def accept_stream
|
|
192
|
+
stream_id = wait(drain: true) { @incoming.shift || (:stopping if @stopping) }
|
|
193
|
+
Stream.new(self, stream_id, @generation) unless stream_id == :stopping
|
|
194
|
+
rescue Raiha::QUIC::ClosedError
|
|
195
|
+
nil
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Each stream the peer opens, in a fiber of its own, until the
|
|
199
|
+
# connection closes or the server stops.
|
|
200
|
+
def each_stream
|
|
201
|
+
while (stream = accept_stream)
|
|
202
|
+
Fiber.schedule { yield stream }
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# Closes the connection with the application's `error_code`.
|
|
207
|
+
def close(error_code: @close_code, reason: "")
|
|
208
|
+
@connection.close(error_code:, reason:, application: true, now: Driver.clock)
|
|
209
|
+
@driver.changed
|
|
210
|
+
notify
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def closed? = !(@aborted || @connection.close_reason).nil?
|
|
214
|
+
|
|
215
|
+
# Whether the server is stopping: the application is to end the
|
|
216
|
+
# connection soon.
|
|
217
|
+
def stopping? = @stopping
|
|
218
|
+
|
|
219
|
+
# When the server will close what is left, while it is stopping.
|
|
220
|
+
attr_reader :stop_deadline
|
|
221
|
+
|
|
222
|
+
# The server is stopping, and closes what is left at `deadline`: what
|
|
223
|
+
# waits for a stream to come wakes.
|
|
224
|
+
def stopping!(deadline = nil)
|
|
225
|
+
@stopping = true
|
|
226
|
+
@stop_deadline = deadline
|
|
227
|
+
notify
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# The server is out of time to stop: the protocol is told, what it
|
|
231
|
+
# sent goes, and the connection closes with `error_code` without
|
|
232
|
+
# waiting for anything to be acknowledged.
|
|
233
|
+
def expire(error_code:)
|
|
234
|
+
return if closed?
|
|
235
|
+
@protocol&.expire
|
|
236
|
+
@driver.flush
|
|
237
|
+
close(error_code:)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# The driver has stopped, after `failure` when something failed in it:
|
|
241
|
+
# nothing more comes or goes, and what waits wakes to raise it, or
|
|
242
|
+
# QUIC::ClosedError.
|
|
243
|
+
def abort(failure = nil)
|
|
244
|
+
@aborted ||= failure || Raiha::QUIC::ClosedError.new(Raiha::QUIC::ConnectionError.new(
|
|
245
|
+
code: Raiha::QUIC::ErrorCode::NO_ERROR, frame_type: nil, reason: "the runtime stopped", application: false))
|
|
246
|
+
notify
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# The runner's turn: the events are taken, and what waits looks again,
|
|
250
|
+
# as a limit the peer raised comes with no event.
|
|
251
|
+
def turn
|
|
252
|
+
take_events
|
|
253
|
+
notify if @waiting.positive?
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Up to `max_bytes` of `stream_id` once there are any, nil at its end,
|
|
257
|
+
# and after it. `generation` is that of the Stream, which tells a stream
|
|
258
|
+
# of rejected 0-RTT from one opened since under its ID.
|
|
259
|
+
def read(stream_id, max_bytes, generation:)
|
|
260
|
+
current!(stream_id, generation)
|
|
261
|
+
track = @tracks[stream_id]
|
|
262
|
+
return nil unless track&.reading
|
|
263
|
+
data = wait(drain: true) do
|
|
264
|
+
current!(stream_id, generation)
|
|
265
|
+
if (reset = track.reset)
|
|
266
|
+
over(stream_id, :reading)
|
|
267
|
+
raise StreamReset.new(stream_id, reset)
|
|
268
|
+
end
|
|
269
|
+
@connection.stream_read(stream_id, max_bytes) || :end
|
|
270
|
+
end
|
|
271
|
+
return data if data.is_a?(String)
|
|
272
|
+
over(stream_id, :reading)
|
|
273
|
+
nil
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Writes all of `data` to `stream_id`, waiting for room as it goes, and
|
|
277
|
+
# ends it with `fin`. `generation` is that of the Stream, as for `read`.
|
|
278
|
+
def write(stream_id, data, fin:, generation:)
|
|
279
|
+
current!(stream_id, generation)
|
|
280
|
+
track = @tracks[stream_id]
|
|
281
|
+
raise Raiha::QUIC::Error::StreamError, "stream #{stream_id} has ended" unless track&.writing
|
|
282
|
+
written = 0
|
|
283
|
+
wait do
|
|
284
|
+
current!(stream_id, generation)
|
|
285
|
+
if (stopped = track.stop)
|
|
286
|
+
over(stream_id, :writing)
|
|
287
|
+
raise StreamReset.new(stream_id, stopped)
|
|
288
|
+
end
|
|
289
|
+
taken = @connection.stream_write(stream_id, data.byteslice(written..) || "".b, fin:)
|
|
290
|
+
written += taken
|
|
291
|
+
# A write that took nothing has nothing to send; the runner woken
|
|
292
|
+
# for it would wake this fiber again, and round they would go.
|
|
293
|
+
@driver.changed if taken.positive? || data.empty?
|
|
294
|
+
written if written == data.bytesize
|
|
295
|
+
end
|
|
296
|
+
over(stream_id, :writing) if fin
|
|
297
|
+
written
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
private def take_events
|
|
301
|
+
protocol = @protocol
|
|
302
|
+
now = Driver.clock
|
|
303
|
+
@connection.each_event do |event|
|
|
304
|
+
# Before the protocol, which opens its streams again, sees it.
|
|
305
|
+
zero_rtt_rejected if event.is_a?(Raiha::QUIC::Event::ZeroRttRejected)
|
|
306
|
+
take(event) unless protocol&.take(event, now)
|
|
307
|
+
end
|
|
308
|
+
protocol&.turn(now)
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# The server took no 0-RTT: the streams this client opened are gone, and
|
|
312
|
+
# their Streams are of an earlier generation from here on.
|
|
313
|
+
private def zero_rtt_rejected
|
|
314
|
+
@generation += 1
|
|
315
|
+
@tracks.delete_if { |stream_id, _| local?(stream_id) }
|
|
316
|
+
@connection.acknowledge_zero_rtt_rejection
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# A stream this client opened before the server rejected its 0-RTT is
|
|
320
|
+
# gone, and its ID may now be another's.
|
|
321
|
+
private def current!(stream_id, generation)
|
|
322
|
+
return if generation == @generation || !local?(stream_id)
|
|
323
|
+
raise Raiha::QUIC::Error::ZeroRttRejected, "stream #{stream_id} went in 0-RTT, which the server rejected"
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
private def take(event)
|
|
327
|
+
case event
|
|
328
|
+
when Raiha::QUIC::Event::Connected then @connected = event
|
|
329
|
+
when Raiha::QUIC::Event::StreamReadable then arrived(event.stream_id)
|
|
330
|
+
when Raiha::QUIC::Event::StreamReset
|
|
331
|
+
arrived(event.stream_id)
|
|
332
|
+
@tracks[event.stream_id]&.reset = event.error_code
|
|
333
|
+
forget_reset(event.stream_id)
|
|
334
|
+
when Raiha::QUIC::Event::StopSending then @tracks[event.stream_id]&.stop = event.error_code
|
|
335
|
+
when Raiha::QUIC::Event::SessionTicket then @kept.ticket(event.session)
|
|
336
|
+
when Raiha::QUIC::Event::NewToken then @kept.token(event.token)
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# A stream the peer opened comes to be known from its first event, and
|
|
341
|
+
# so do those of its type below it, which it opened with it (RFC 9000
|
|
342
|
+
# "Stream Concurrency").
|
|
343
|
+
private def arrived(stream_id)
|
|
344
|
+
return if local?(stream_id)
|
|
345
|
+
type = stream_id & 0x3
|
|
346
|
+
while @peer_next.fetch(type) <= stream_id >> 2
|
|
347
|
+
opened = @peer_next.fetch(type) << 2 | type
|
|
348
|
+
@tracks[opened] = Track.new(nil, nil, true, opened.nobits?(0x2))
|
|
349
|
+
@incoming << opened
|
|
350
|
+
@peer_next[type] = @peer_next.fetch(type) + 1
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
# A stream going one way the peer reset before the application took it
|
|
355
|
+
# has nothing for it: the core has let it go and lets the peer open
|
|
356
|
+
# another, so keeping it would let the peer grow what is kept without
|
|
357
|
+
# end.
|
|
358
|
+
private def forget_reset(stream_id)
|
|
359
|
+
return unless stream_id.anybits?(0x2) && @incoming.delete(stream_id)
|
|
360
|
+
@tracks.delete(stream_id)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
# The application is done with a part of `stream_id`; once with both,
|
|
364
|
+
# the Session forgets it.
|
|
365
|
+
private def over(stream_id, part)
|
|
366
|
+
track = @tracks[stream_id] or return
|
|
367
|
+
track[part] = false
|
|
368
|
+
@tracks.delete(stream_id) unless track.reading || track.writing
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
private def local?(stream_id) = stream_id.anybits?(0x1) == (@role == :server)
|
|
372
|
+
|
|
373
|
+
private def notify = @lock.synchronize { @changed.broadcast }
|
|
374
|
+
|
|
375
|
+
# Calls the block until it gives something other than nil or "", waiting
|
|
376
|
+
# for a change in between; what it gave, or nil once the clock is past
|
|
377
|
+
# `deadline`. Once the connection has closed, raises QUIC::ClosedError, and
|
|
378
|
+
# once the driver has stopped, what `abort` was given; with `drain`, the
|
|
379
|
+
# block is called first all the same, so what came before the close is
|
|
380
|
+
# still given.
|
|
381
|
+
private def wait(deadline: nil, drain: false)
|
|
382
|
+
@lock.synchronize do
|
|
383
|
+
@waiting += 1
|
|
384
|
+
loop do
|
|
385
|
+
take_events
|
|
386
|
+
result = yield if drain
|
|
387
|
+
# T is within T | nil; steep loses it across the two signatures.
|
|
388
|
+
return result unless result.nil? || result == "" # steep:ignore
|
|
389
|
+
aborted = @aborted and raise aborted
|
|
390
|
+
reason = @connection.close_reason and raise Raiha::QUIC::ClosedError.new(reason)
|
|
391
|
+
result = yield unless drain
|
|
392
|
+
return result unless result.nil? || result == "" # steep:ignore
|
|
393
|
+
left = deadline && deadline - Driver.clock
|
|
394
|
+
return nil if left && left <= 0
|
|
395
|
+
@changed.wait(@lock, left)
|
|
396
|
+
end
|
|
397
|
+
ensure
|
|
398
|
+
@waiting -= 1
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
# A stream of a Session, read and written as an IO would be. A stream
|
|
404
|
+
# the client opened in 0-RTT that the server rejected raises
|
|
405
|
+
# QUIC::Error::ZeroRttRejected.
|
|
406
|
+
class Stream
|
|
407
|
+
# The most a read with no length asks for at a time.
|
|
408
|
+
PIECE = 65_536
|
|
409
|
+
|
|
410
|
+
attr_reader :id
|
|
411
|
+
|
|
412
|
+
def initialize(session, id, generation)
|
|
413
|
+
@session = session
|
|
414
|
+
@id = id
|
|
415
|
+
@generation = generation
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# With `length`, up to that many bytes once there are any, nil at the
|
|
419
|
+
# end; without, all until the end, "" when it has ended.
|
|
420
|
+
def read(length = nil)
|
|
421
|
+
return @session.read(@id, length, generation: @generation) if length
|
|
422
|
+
data = "".b
|
|
423
|
+
while (chunk = @session.read(@id, PIECE, generation: @generation))
|
|
424
|
+
data << chunk
|
|
425
|
+
end
|
|
426
|
+
data
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
# Writes all of `data`, waiting while the peer's flow control holds it
|
|
430
|
+
# back; its length.
|
|
431
|
+
def write(data)
|
|
432
|
+
@session.write(@id, data.b, fin: false, generation: @generation)
|
|
433
|
+
data.bytesize
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def <<(data)
|
|
437
|
+
write(data)
|
|
438
|
+
self
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# Ends the sending part.
|
|
442
|
+
def close_write
|
|
443
|
+
@session.write(@id, "".b, fin: true, generation: @generation)
|
|
444
|
+
nil
|
|
445
|
+
end
|
|
446
|
+
end
|
|
447
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require_relative "../../http3/session"
|
|
5
|
+
|
|
6
|
+
module Raiha::Runtime
|
|
7
|
+
module Threaded
|
|
8
|
+
# The client of the runtime on threads, as the Client of fibers is:
|
|
9
|
+
# Client.connect finds the addresses of a host and connects to each in
|
|
10
|
+
# turn, a socket of its own for each, until one completes its handshake
|
|
11
|
+
# within `connect_timeout` seconds, then gives the block a Session of
|
|
12
|
+
# the connection, in the thread that called it; no Fiber scheduler is
|
|
13
|
+
# needed. The arguments are those of the Client of fibers, with
|
|
14
|
+
# `max_threads:`: the threads of the streams of `each_stream`, which
|
|
15
|
+
# wait for a place while that many run. The block's value is returned.
|
|
16
|
+
#
|
|
17
|
+
# Once the block is over, what the streams sent is let be acknowledged,
|
|
18
|
+
# until three PTOs go with no more of it acknowledged or 30 seconds in
|
|
19
|
+
# all, the connection closes, and the threads of its streams are waited
|
|
20
|
+
# for, Server::STOP_TIMEOUT at most; those still
|
|
21
|
+
# running are left to. An attempt that fails has its driver's threads
|
|
22
|
+
# ended before the next address is tried.
|
|
23
|
+
class Client
|
|
24
|
+
CONNECT_TIMEOUT = Raiha::Runtime::Client::CONNECT_TIMEOUT
|
|
25
|
+
MAX_THREADS = 64
|
|
26
|
+
|
|
27
|
+
def self.connect(host, port, config:, tls_config:, keyring:, server_name: host, connect_timeout: CONNECT_TIMEOUT, qlog_dir: nil,
|
|
28
|
+
session: nil, token: nil, early_data: false, max_threads: MAX_THREADS, &)
|
|
29
|
+
new(config:, tls_config:, keyring:, connect_timeout:, qlog_dir:, max_threads:).connect(host, port, server_name:, session:, token:,
|
|
30
|
+
early_data:, &)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def initialize(config:, tls_config:, keyring:, connect_timeout: CONNECT_TIMEOUT, qlog_dir: nil, max_threads: MAX_THREADS)
|
|
34
|
+
@threads = Threads.new(max_threads)
|
|
35
|
+
@config = UDP.ecn(UDP.pmtud(UDP.batching(config)))
|
|
36
|
+
@tls_config = tls_config
|
|
37
|
+
@keyring = keyring
|
|
38
|
+
@connect_timeout = connect_timeout
|
|
39
|
+
@session = nil #: Session?
|
|
40
|
+
@driver = nil #: Driver?
|
|
41
|
+
@deadline = nil #: Float?
|
|
42
|
+
@qlog = qlog_dir && Qlog.new(qlog_dir, budget: Budget.current)
|
|
43
|
+
@tracing = Thread::Mutex.new
|
|
44
|
+
@trace = nil #: Qlog::Pending?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def connect(host, port, server_name: host, session: nil, token: nil, early_data: false)
|
|
48
|
+
errors = [] #: Array[[UDP::address | String, StandardError]]
|
|
49
|
+
addresses = begin
|
|
50
|
+
resolve(host, port)
|
|
51
|
+
rescue SocketError => error
|
|
52
|
+
raise ConnectError.new(host, port, [[host, error]])
|
|
53
|
+
end
|
|
54
|
+
addresses.each do |address|
|
|
55
|
+
begin
|
|
56
|
+
driver, opened = attempt(address, server_name, session:, token:, early_data:)
|
|
57
|
+
rescue SystemCallError => error
|
|
58
|
+
errors << [address, error]
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
session = token = nil
|
|
62
|
+
connected = false
|
|
63
|
+
begin
|
|
64
|
+
driver.start
|
|
65
|
+
opened.wait_ready
|
|
66
|
+
connected = true
|
|
67
|
+
rescue Raiha::QUIC::ClosedError => error
|
|
68
|
+
errors << [address, error]
|
|
69
|
+
next
|
|
70
|
+
ensure
|
|
71
|
+
# However the attempt is left, Interrupt included.
|
|
72
|
+
unless connected
|
|
73
|
+
driver.stop
|
|
74
|
+
driver.join
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
begin
|
|
78
|
+
raise Error::Usage, "the core lock of the driver is held where the application runs" if driver.mon_owned?
|
|
79
|
+
return yield opened
|
|
80
|
+
ensure
|
|
81
|
+
finish(driver, opened)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
raise ConnectError.new(host, port, errors)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The driver's owner: a client has no Route to take.
|
|
88
|
+
def route(route, now) = nil
|
|
89
|
+
|
|
90
|
+
# The driver's turn, under the core lock: the Session's, and the end of
|
|
91
|
+
# the time to connect.
|
|
92
|
+
def turn(now)
|
|
93
|
+
session = @session or return
|
|
94
|
+
session.turn
|
|
95
|
+
deadline = @deadline or return
|
|
96
|
+
connection = session.connection
|
|
97
|
+
return @deadline = nil if connection.handshake_complete?
|
|
98
|
+
return if now < deadline
|
|
99
|
+
@deadline = nil
|
|
100
|
+
connection.close(error_code: Raiha::QUIC::ErrorCode::NO_ERROR, reason: "no handshake in #{@connect_timeout} seconds",
|
|
101
|
+
application: false, now:)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def next_timeout = @deadline
|
|
105
|
+
|
|
106
|
+
def stopped(failure)
|
|
107
|
+
@session&.abort(failure)
|
|
108
|
+
@trace&.over!
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# Writes what the trace has taken to its file, outside the core lock,
|
|
112
|
+
# one writer at a time.
|
|
113
|
+
def flush_traces
|
|
114
|
+
qlog = @qlog or return
|
|
115
|
+
driver = @driver or return
|
|
116
|
+
@tracing.synchronize do
|
|
117
|
+
taken = driver.synchronize { qlog.take }
|
|
118
|
+
done = qlog.put(taken)
|
|
119
|
+
driver.synchronize { qlog.retire(done) }
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Closes the connection once what was sent is acknowledged, or three
|
|
124
|
+
# PTOs have gone with no more of it acknowledged, or 30 seconds in
|
|
125
|
+
# all; sends the CONNECTION_CLOSE, of a close in the block as well;
|
|
126
|
+
# stops the driver; and waits for the threads of the streams.
|
|
127
|
+
private def finish(driver, session)
|
|
128
|
+
session.close_when_acknowledged unless session.closed?
|
|
129
|
+
driver.flush
|
|
130
|
+
rescue Raiha::QUIC::ClosedError
|
|
131
|
+
nil
|
|
132
|
+
ensure
|
|
133
|
+
driver.stop
|
|
134
|
+
driver.join
|
|
135
|
+
@threads.join(Driver.clock + Server::STOP_TIMEOUT)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# The addresses to try, as Addresses.resolve finds them.
|
|
139
|
+
private def resolve(host, port) = Addresses.resolve(host, port)
|
|
140
|
+
|
|
141
|
+
# A connection to `address` on a socket of its own, its handshake to
|
|
142
|
+
# begin once the driver starts: [driver, session].
|
|
143
|
+
private def attempt(address, server_name, session:, token:, early_data:)
|
|
144
|
+
made = false
|
|
145
|
+
udp, config = Addresses.bind(address, @config)
|
|
146
|
+
endpoint = Raiha::QUIC::Endpoint.client(config:, random: SecureRandom, time: Time.now, now: Driver.clock)
|
|
147
|
+
driver = @driver = Driver.new(endpoint, udp, self)
|
|
148
|
+
tracer, @trace = @qlog&.trace(:client, Time.now.utc.strftime("%Y%m%dT%H%M%S.%6N"), tls_config: @tls_config)
|
|
149
|
+
quic = session.is_a?(Raiha::HTTP3::Session) ? session.quic : session
|
|
150
|
+
connection = endpoint.connect(tls_config: @tls_config, keyring: @keyring, server_name:, remote: address, now: Driver.clock, tracer:,
|
|
151
|
+
session: quic, token:, early_data:)
|
|
152
|
+
opened = @session = Session.new(connection, driver, role: :client, threads: @threads, tracer:, server_name:, resumed: session)
|
|
153
|
+
@deadline = Driver.clock + @connect_timeout
|
|
154
|
+
made = true
|
|
155
|
+
[driver, opened] #: [Driver, Session]
|
|
156
|
+
ensure
|
|
157
|
+
udp&.close unless made
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|