WebTransport: add HTTP/3 WebTransport datagram server using aioquic; run mode switch via MODE=wt|quic|mem

This commit is contained in:
Vladyslav Doloman
2025-10-07 22:36:06 +03:00
parent 1f5ca02ca4
commit 088c36396b
2 changed files with 154 additions and 18 deletions

47
run.py
View File

@@ -3,34 +3,45 @@ import os
from server.server import GameServer
from server.config import ServerConfig
from server.transport import InMemoryTransport
async def main():
from server.server import GameServer
async def run_in_memory():
from server.transport import InMemoryTransport
cfg = ServerConfig()
server = GameServer(transport=InMemoryTransport(lambda d, p: server.on_datagram(d, p)), config=cfg)
await asyncio.gather(server.transport.run(), server.tick_loop())
async def run_quic():
from server.quic_transport import QuicWebTransportServer
cfg = ServerConfig()
host = os.environ.get("QUIC_HOST", "0.0.0.0")
port = int(os.environ.get("QUIC_PORT", "4433"))
cert = os.environ["QUIC_CERT"]
key = os.environ["QUIC_KEY"]
server = GameServer(transport=QuicWebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
await asyncio.gather(server.transport.run(), server.tick_loop())
async def run_webtransport():
from server.webtransport_server import WebTransportServer
cfg = ServerConfig()
host = os.environ.get("WT_HOST", os.environ.get("QUIC_HOST", "0.0.0.0"))
port = int(os.environ.get("WT_PORT", os.environ.get("QUIC_PORT", "4433")))
cert = os.environ.get("WT_CERT") or os.environ["QUIC_CERT"]
key = os.environ.get("WT_KEY") or os.environ["QUIC_KEY"]
server = GameServer(transport=WebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
await asyncio.gather(server.transport.run(), server.tick_loop())
if __name__ == "__main__":
try:
# Optional QUIC mode if env vars are provided
cert = os.environ.get("QUIC_CERT")
key = os.environ.get("QUIC_KEY")
host = os.environ.get("QUIC_HOST", "0.0.0.0")
port = int(os.environ.get("QUIC_PORT", "4433"))
if cert and key:
from server.quic_transport import QuicWebTransportServer
from server.server import GameServer
cfg = ServerConfig()
async def start_quic():
server = GameServer(transport=QuicWebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
await asyncio.gather(server.transport.run(), server.tick_loop())
asyncio.run(start_quic())
mode = os.environ.get("MODE", "mem").lower()
if mode == "wt":
asyncio.run(run_webtransport())
elif mode == "quic":
asyncio.run(run_quic())
else:
asyncio.run(main())
asyncio.run(run_in_memory())
except KeyboardInterrupt:
pass