Skip to content

Commit

Permalink
Update webtransport example (GoogleChrome#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
bashi authored Nov 1, 2021
1 parent 4927e98 commit 4c8316c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
8 changes: 4 additions & 4 deletions webtransport/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function connect() {
currentTransport = transport;
streamNumber = 1;
try {
currentTransportDatagramWriter = transport.datagramWritable.getWriter();
currentTransportDatagramWriter = transport.datagrams.writable.getWriter();
addToEventLog('Datagram writer ready.');
} catch (e) {
addToEventLog('Sending datagrams not supported: ' + e, 'error');
Expand All @@ -60,7 +60,7 @@ async function sendData() {
break;
case 'unidi': {
let stream = await transport.createUnidirectionalStream();
let writer = stream.writable.getWriter();
let writer = stream.getWriter();
await writer.write(data);
await writer.close();
addToEventLog('Sent a unidirectional stream with data: ' + rawData);
Expand Down Expand Up @@ -88,7 +88,7 @@ async function sendData() {
// Reads datagrams from |transport| into the event log until EOF is reached.
async function readDatagrams(transport) {
try {
var reader = transport.datagramReadable.getReader();
var reader = transport.datagrams.readable.getReader();
addToEventLog('Datagram reader ready.');
} catch (e) {
addToEventLog('Receiving datagrams not supported: ' + e, 'error');
Expand Down Expand Up @@ -131,7 +131,7 @@ async function acceptUnidirectionalStreams(transport) {

async function readFromIncomingStream(stream, number) {
let decoder = new TextDecoderStream('utf-8');
let reader = stream.readable.pipeThrough(decoder).getReader();
let reader = stream.pipeThrough(decoder).getReader();
try {
while (true) {
const { value, done } = await reader.read();
Expand Down
27 changes: 23 additions & 4 deletions webtransport/webtransport_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
from typing import Dict, Optional

from aioquic.asyncio import QuicConnectionProtocol, serve
from aioquic.h3.connection import H3_ALPN, H3Connection
from aioquic.h3.connection import H3_ALPN, H3Connection, Setting
from aioquic.h3.events import H3Event, HeadersReceived, WebTransportStreamDataReceived, DatagramReceived
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.connection import stream_is_unidirectional
Expand All @@ -92,6 +92,24 @@

logger = logging.getLogger(__name__)

# https://datatracker.ietf.org/doc/html/draft-ietf-masque-h3-datagram-05#section-9.1
H3_DATAGRAM_05 = 0xffd277

class H3ConnectionWithDatagram(H3Connection):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)

# Overrides H3Connection._validate_settings() to enable HTTP Datagram
def _validate_settings(self, settings: Dict[int, int]) -> None:
settings[Setting.H3_DATAGRAM] = 1
return super()._validate_settings(settings)

# Overrides H3Connection._get_local_settings() to enable HTTP Datagram
def _get_local_settings(self) -> Dict[int, int]:
settings = super()._get_local_settings()
settings[H3_DATAGRAM_05] = 1
return settings


# CounterHandler implements a really simple protocol:
# - For every incoming bidirectional stream, it counts bytes it receives on
Expand All @@ -104,7 +122,7 @@
# datagram that was just received.
class CounterHandler:

def __init__(self, session_id, http: H3Connection) -> None:
def __init__(self, session_id, http: H3ConnectionWithDatagram) -> None:
self._session_id = session_id
self._http = http
self._counters = defaultdict(int)
Expand Down Expand Up @@ -141,12 +159,13 @@ class WebTransportProtocol(QuicConnectionProtocol):

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._http: Optional[H3Connection] = None
self._http: Optional[H3ConnectionWithDatagram] = None
self._handler: Optional[CounterHandler] = None

def quic_event_received(self, event: QuicEvent) -> None:
if isinstance(event, ProtocolNegotiated):
self._http = H3Connection(self._quic, enable_webtransport=True)
self._http = H3ConnectionWithDatagram(
self._quic, enable_webtransport=True)
elif isinstance(event, StreamReset) and self._handler is not None:
# Streams in QUIC can be closed in two ways: normal (FIN) and
# abnormal (resets). FIN is handled by the handler; the code
Expand Down

0 comments on commit 4c8316c

Please sign in to comment.