-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathquic-echo-server.py
More file actions
executable file
·197 lines (160 loc) · 5.61 KB
/
quic-echo-server.py
File metadata and controls
executable file
·197 lines (160 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python3
#
# Copyright (c) 2026 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0
#
import argparse
import asyncio
import logging
import random
import time
from dataclasses import dataclass, field
from typing import Dict
from aioquic.asyncio import serve
from aioquic.asyncio.protocol import QuicConnectionProtocol
from aioquic.quic.configuration import QuicConfiguration
from aioquic.quic.events import StreamDataReceived, ConnectionTerminated
# -------------------------
# Per-stream statistics
# -------------------------
@dataclass
class StreamStats:
stream_id: int
bytes_received: int = 0
bytes_sent: int = 0
start_time: float = field(default_factory=time.time)
end_time: float | None = None
def finish(self):
self.end_time = time.time()
@property
def duration(self):
if self.end_time is None:
return None
return self.end_time - self.start_time
# -------------------------
# Echo protocol
# -------------------------
class EchoServerProtocol(QuicConnectionProtocol):
def __init__(self, *args, loss_rate=0.0, delay_ms=0, jitter_ms=0, **kwargs):
super().__init__(*args, **kwargs)
self.loss_rate = loss_rate
self.delay_ms = delay_ms
self.jitter_ms = jitter_ms
self.stream_stats: Dict[int, StreamStats] = {}
async def _echo_with_impairment(self, stream_id: int, data: bytes, end_stream: bool):
# Simulated loss
if self.loss_rate > 0 and random.random() < self.loss_rate:
logging.debug("Dropping %d bytes on stream %d", len(data), stream_id)
return
# Simulated delay + jitter
delay = self.delay_ms / 1000.0
if self.jitter_ms > 0:
delay += random.uniform(0, self.jitter_ms / 1000.0)
if delay > 0:
await asyncio.sleep(delay)
self._quic.send_stream_data(
stream_id,
data,
end_stream=end_stream,
)
self.transmit()
stats = self.stream_stats[stream_id]
stats.bytes_sent += len(data)
def quic_event_received(self, event):
if isinstance(event, StreamDataReceived):
sid = event.stream_id
if sid not in self.stream_stats:
self.stream_stats[sid] = StreamStats(stream_id=sid)
stats = self.stream_stats[sid]
stats.bytes_received += len(event.data)
logging.debug(
"Stream %d: recv=%d end=%s",
sid,
len(event.data),
event.end_stream,
)
# Echo asynchronously (so we can delay/drop)
if event.data or event.end_stream:
asyncio.create_task(
self._echo_with_impairment(
sid,
event.data,
event.end_stream,
)
)
if event.end_stream:
stats.finish()
elif isinstance(event, ConnectionTerminated):
logging.info("Connection terminated")
self.dump_stats()
def dump_stats(self):
logging.info("==== Stream statistics ====")
for sid, s in sorted(self.stream_stats.items()):
logging.info(
"Stream %d: recv=%d sent=%d duration=%.3fs",
sid,
s.bytes_received,
s.bytes_sent,
s.duration or 0.0,
)
logging.info("===========================")
# -------------------------
# Main
# -------------------------
async def main():
p = argparse.ArgumentParser("Advanced QUIC echo server")
p.add_argument("--host", default="0.0.0.0")
p.add_argument("--port", type=int, default=4422)
p.add_argument("--certificate", required=True)
p.add_argument("--private-key", required=True)
# Flow control
p.add_argument("--max-data", type=int, default=64 * 1024,
help="Connection-level flow control window")
p.add_argument("--max-stream-data", type=int, default=16 * 1024,
help="Per-stream flow control window")
p.add_argument("--max-streams", type=int, default=100)
# Impairments
p.add_argument("--loss-rate", type=float, default=0.0)
p.add_argument("--delay-ms", type=int, default=0)
p.add_argument("--jitter-ms", type=int, default=0)
p.add_argument("-d", "--debug", type=int, default=0, choices=[0, 1, 2])
args = p.parse_args()
logging.basicConfig(
level=[logging.WARNING, logging.INFO, logging.DEBUG][args.debug]
)
logging.getLogger("quic").setLevel(
level=[logging.WARNING, logging.INFO, logging.DEBUG][args.debug]
)
config = QuicConfiguration(
is_client=False,
alpn_protocols=["echo-quic"],
)
config.load_cert_chain(args.certificate, args.private_key)
# Flow control tuning
config.max_data = args.max_data
config.max_stream_data_bidi_local = args.max_stream_data
config.max_stream_data_bidi_remote = args.max_stream_data
config.max_streams_bidi = args.max_streams
await serve(
args.host,
args.port,
configuration=config,
create_protocol=lambda *a, **kw: EchoServerProtocol(
*a,
loss_rate=args.loss_rate,
delay_ms=args.delay_ms,
jitter_ms=args.jitter_ms,
**kw,
),
)
logging.info(
"Listening on %s:%d (loss=%.2f delay=%dms jitter=%dms)",
args.host,
args.port,
args.loss_rate,
args.delay_ms,
args.jitter_ms,
)
await asyncio.Future()
if __name__ == "__main__":
asyncio.run(main())