V0.3.0版本优化传输速度和提示
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
import re
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
gradle = (ROOT/'android/app/build.gradle.kts').read_text()
|
||||
manifest = (ROOT/'android/app/src/main/AndroidManifest.xml').read_text()
|
||||
wifi = (ROOT/'android/app/src/main/java/com/qft/optical/WifiConnector.kt').read_text()
|
||||
upload = (ROOT/'android/app/src/main/java/com/qft/optical/UploadClient.kt').read_text()
|
||||
main = (ROOT/'android/app/src/main/java/com/qft/optical/MainActivity.kt').read_text()
|
||||
gray = (ROOT/'android/app/src/main/java/com/qft/optical/GrayFrame.kt').read_text()
|
||||
security = (ROOT/'android/app/src/main/res/xml/network_security_config.xml').read_text()
|
||||
|
||||
assert re.search(r'minSdk\s*=\s*24', gradle)
|
||||
assert 'WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"' in manifest
|
||||
assert 'ACCESS_FINE_LOCATION" android:maxSdkVersion="32"' in manifest
|
||||
assert 'NEARBY_WIFI_DEVICES' in manifest
|
||||
assert 'networkSecurityConfig="@xml/network_security_config"' in manifest
|
||||
assert 'cleartextTrafficPermitted="true"' in security
|
||||
assert 'Build.VERSION.SDK_INT >= 29' in wifi and 'connectLegacy' in wifi
|
||||
assert 'subnetEndpoints' in wifi and 'directedBroadcast' in wifi
|
||||
assert 'MAX_WORKERS = 8' in upload and 'QFTDISC2:' in upload and 'workers = 24' in upload and 'desiredChunkSize' in upload and 'CHUNK_LARGE' in upload
|
||||
assert 'Size(640, 480)' in main and 'Size(1280, 720)' in main
|
||||
assert 'STRATEGY_KEEP_ONLY_LATEST' in main
|
||||
assert 'rotate90' not in gray and 'image.imageInfo.rotationDegrees' not in gray
|
||||
all_kotlin = '\n'.join(path.read_text() for path in (ROOT/'android/app/src/main/java/com/qft/optical').glob('*.kt'))
|
||||
assert 'java.util.Base64' not in all_kotlin
|
||||
print('PASS Android compatibility/static guards: minSdk24, API29 split, legacy storage/location, Android13 nearby-WiFi, cleartext local config, subnet fallback, fast scanner paths')
|
||||
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Loopback integration test for QFTDISC2 + Parallel v2 init/chunk/finish semantics."""
|
||||
import hashlib, http.client, json, os, random, socket, tempfile, threading, time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from pathlib import Path
|
||||
|
||||
TOKEN = "integration_session_128bit_ABC"
|
||||
DISCOVERY_MAGIC = f"QFTDISC2:{TOKEN}".encode()
|
||||
CHUNK = 8 * 1024 * 1024
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
root = Path(td)
|
||||
source = root / "src.bin"
|
||||
source.write_bytes(os.urandom(40 * 1024 * 1024 + 12345))
|
||||
source_sha = hashlib.sha256(source.read_bytes()).hexdigest()
|
||||
part = root / "recv.qftpart"
|
||||
part.write_bytes(b"")
|
||||
part_fp = open(part, "r+b", buffering=0)
|
||||
part_fp.truncate(source.stat().st_size)
|
||||
lock = threading.Lock()
|
||||
received = set()
|
||||
upload_id = "up_test_01"
|
||||
total_chunks = (source.stat().st_size + CHUNK - 1) // CHUNK
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def log_message(self, *_): pass
|
||||
def _auth(self): return self.headers.get("X-QFT-Token") == TOKEN
|
||||
def _reply(self, code, body=b"{}"):
|
||||
self.send_response(code); self.send_header("Content-Length", str(len(body))); self.end_headers(); self.wfile.write(body)
|
||||
def do_GET(self):
|
||||
if not self._auth(): return self._reply(403)
|
||||
if self.path == "/qft/ping": return self._reply(200, b'{"ok":true,"protocol":2,"parallel":true}')
|
||||
self._reply(404)
|
||||
def do_POST(self):
|
||||
if not self._auth(): return self._reply(403)
|
||||
if self.path == "/qft/upload/init":
|
||||
body = json.dumps({"ok":True,"upload_id":upload_id,"chunk_size":CHUNK,"total_chunks":total_chunks}).encode()
|
||||
return self._reply(200, body)
|
||||
prefix = f"/qft/upload/chunk/{upload_id}/"
|
||||
if self.path.startswith(prefix):
|
||||
idx = int(self.path[len(prefix):]); n = int(self.headers["Content-Length"]); data = self.rfile.read(n)
|
||||
offset = idx * CHUNK
|
||||
assert n == min(CHUNK, source.stat().st_size - offset)
|
||||
with lock:
|
||||
part_fp.seek(offset); part_fp.write(data); received.add(idx)
|
||||
return self._reply(200)
|
||||
if self.path == f"/qft/upload/finish/{upload_id}":
|
||||
if len(received) != total_chunks: return self._reply(409)
|
||||
with lock: part_fp.flush(); os.fsync(part_fp.fileno())
|
||||
return self._reply(200, json.dumps({"ok":True}).encode())
|
||||
self._reply(404)
|
||||
|
||||
httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
||||
port = httpd.server_address[1]
|
||||
threading.Thread(target=httpd.serve_forever, daemon=True).start()
|
||||
|
||||
udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); udp.bind(("127.0.0.1", 0)); udp_port = udp.getsockname()[1]
|
||||
stop = threading.Event()
|
||||
def udp_server():
|
||||
while not stop.is_set():
|
||||
udp.settimeout(.2)
|
||||
try: data, peer = udp.recvfrom(512)
|
||||
except socket.timeout: continue
|
||||
except OSError:
|
||||
if stop.is_set(): break
|
||||
raise
|
||||
if data == DISCOVERY_MAGIC: udp.sendto(f"QFTHERE2:{TOKEN}:{port}".encode(), peer)
|
||||
threading.Thread(target=udp_server, daemon=True).start()
|
||||
|
||||
# UDP discovery client.
|
||||
c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); c.settimeout(1); c.sendto(DISCOVERY_MAGIC, ("127.0.0.1", udp_port))
|
||||
resp, _ = c.recvfrom(512); assert resp == f"QFTHERE2:{TOKEN}:{port}".encode(); c.close()
|
||||
|
||||
def request(method, path, body=b"", headers=None):
|
||||
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=5)
|
||||
h = {"X-QFT-Token": TOKEN}; h.update(headers or {})
|
||||
conn.request(method, path, body=body, headers=h); r = conn.getresponse(); payload = r.read(); status = r.status; conn.close(); return status, payload
|
||||
assert request("GET", "/qft/ping")[0] == 200
|
||||
status, body = request("POST", "/qft/upload/init", headers={"Content-Length":"0"}); assert status == 200
|
||||
init = json.loads(body); assert init["chunk_size"] == CHUNK and init["total_chunks"] == total_chunks
|
||||
|
||||
indices = list(range(total_chunks)); random.Random(42).shuffle(indices)
|
||||
def send(idx):
|
||||
with open(source, "rb") as f:
|
||||
f.seek(idx * CHUNK); data = f.read(min(CHUNK, source.stat().st_size - idx * CHUNK))
|
||||
status, _ = request("POST", f"/qft/upload/chunk/{upload_id}/{idx}", data, {"Content-Length":str(len(data))})
|
||||
assert status == 200
|
||||
started = time.perf_counter()
|
||||
with ThreadPoolExecutor(max_workers=8) as ex: list(ex.map(send, indices))
|
||||
assert request("POST", f"/qft/upload/finish/{upload_id}", headers={"Content-Length":"0"})[0] == 200
|
||||
elapsed = time.perf_counter() - started
|
||||
part_fp.close(); recv_sha = hashlib.sha256(part.read_bytes()).hexdigest(); assert recv_sha == source_sha
|
||||
stop.set(); udp.close(); httpd.shutdown(); httpd.server_close()
|
||||
print(f"PASS loopback Parallel v2 integration: UDP discovery + HTTP auth + {total_chunks} out-of-order chunks / 8 workers; "
|
||||
f"{source.stat().st_size} bytes SHA-256 identical in {elapsed:.2f}s")
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
MAX_CACHE_MIB = 768
|
||||
MAX_PAGES = 192
|
||||
HARD_MIB = 1100
|
||||
|
||||
def calc(side):
|
||||
bpp = side * side * 4
|
||||
pages = max(4, min(MAX_PAGES, MAX_CACHE_MIB * 1024 * 1024 // bpp))
|
||||
managed = pages * bpp / (1024 * 1024)
|
||||
return pages, managed
|
||||
|
||||
p4, m4 = calc(960)
|
||||
p1, m1 = calc(760)
|
||||
# Include a conservative 64 MiB transient batch/decoder allowance in this design-level bound.
|
||||
assert m4 + 64 < HARD_MIB
|
||||
assert m1 + 64 < HARD_MIB
|
||||
print(f"PASS memory budget: 4QR cache={p4} pages/{m4:.1f} MiB; 1QR cache={p1} pages/{m1:.1f} MiB; hard design ceiling={HARD_MIB} MiB")
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Synthetic visual-path smoke test for the 4-QR optical page and QFTPAIR2.
|
||||
Requires: qrcode, numpy, opencv-python, pyzbar/libzbar.
|
||||
This is intentionally not a substitute for a real display/camera test.
|
||||
"""
|
||||
import base64, json, os, time
|
||||
import cv2
|
||||
import numpy as np
|
||||
import qrcode
|
||||
from pyzbar.pyzbar import decode as zdecode, ZBarSymbol
|
||||
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M
|
||||
|
||||
|
||||
def make_qr(text: str, ec, target: int):
|
||||
qr = qrcode.QRCode(version=None, error_correction=ec, box_size=1, border=4)
|
||||
qr.add_data(text); qr.make(fit=True)
|
||||
mat = np.asarray(qr.get_matrix(), dtype=np.uint8)
|
||||
scale = max(1, target // mat.shape[0])
|
||||
return ((1 - np.kron(mat, np.ones((scale, scale), np.uint8))) * 255).astype(np.uint8), qr.version
|
||||
|
||||
# Four data QR symbols at the same density used by v0.3 (about 1243 chars/frame).
|
||||
texts = ["QFT1:" + base64.urlsafe_b64encode(os.urandom(928)).decode().rstrip("=") for _ in range(4)]
|
||||
canvas = np.full((960, 960), 255, np.uint8)
|
||||
gutter = 15
|
||||
cell = (960 - gutter * 3) // 2
|
||||
for idx, text in enumerate(texts):
|
||||
img, version = make_qr(text, ERROR_CORRECT_L, cell)
|
||||
row, col = divmod(idx, 2)
|
||||
x = gutter + col * (cell + gutter) + (cell - img.shape[1]) // 2
|
||||
y = gutter + row * (cell + gutter) + (cell - img.shape[0]) // 2
|
||||
canvas[y:y+img.shape[0], x:x+img.shape[1]] = img
|
||||
started = time.perf_counter()
|
||||
found = {r.data.decode() for r in zdecode(canvas, symbols=[ZBarSymbol.QRCODE])}
|
||||
elapsed = time.perf_counter() - started
|
||||
assert found == set(texts), (len(found), len(texts))
|
||||
|
||||
# Pairing QR: downscale + rotate + slight blur to exercise acquisition margin.
|
||||
v2 = {
|
||||
"v":2,"s":"qft_test_session_128bit_XY","n":"DESKTOP-QFT","p":39393,
|
||||
"e":["http://192.168.137.1:39393"],
|
||||
"w":["QFT-DESKTOP-abc123","3xamplePassw0rd"],
|
||||
}
|
||||
raw = json.dumps(v2, separators=(",", ":"), ensure_ascii=False).encode()
|
||||
pair_text = "QFTPAIR2:" + base64.urlsafe_b64encode(raw).decode().rstrip("=")
|
||||
pair, pair_ver = make_qr(pair_text, ERROR_CORRECT_M, 520)
|
||||
pair = cv2.resize(pair, (260, 260), interpolation=cv2.INTER_NEAREST)
|
||||
rot = cv2.getRotationMatrix2D((130, 130), 7.0, 1.0)
|
||||
pair = cv2.warpAffine(pair, rot, (300, 300), borderValue=255)
|
||||
pair = cv2.GaussianBlur(pair, (3, 3), 0.5)
|
||||
started2 = time.perf_counter()
|
||||
pair_found = [r.data.decode() for r in zdecode(pair, symbols=[ZBarSymbol.QRCODE])]
|
||||
elapsed2 = time.perf_counter() - started2
|
||||
assert pair_text in pair_found
|
||||
print(f"PASS synthetic QR decode: 4/4 optical QR v{version} decoded from 960px page in {elapsed*1000:.1f} ms; "
|
||||
f"QFTPAIR2 QR-M v{pair_ver} decoded after 7deg rotate+blur in {elapsed2*1000:.1f} ms")
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64, json
|
||||
import qrcode
|
||||
from qrcode.constants import ERROR_CORRECT_M
|
||||
|
||||
session = "qft_test_session_128bit_XY"
|
||||
ssid = "QFT-DESKTOP-abc123"
|
||||
password = "3xamplePassw0rd"
|
||||
|
||||
v2 = {"v":2,"s":session,"n":"DESKTOP-QFT","p":39393,"e":["http://192.168.137.1:39393"],"w":[ssid,password]}
|
||||
raw2 = json.dumps(v2, separators=(",", ":"), ensure_ascii=False).encode()
|
||||
text2 = "QFTPAIR2:" + base64.urlsafe_b64encode(raw2).decode().rstrip("=")
|
||||
decoded = json.loads(base64.urlsafe_b64decode(text2.split(":",1)[1] + "=" * (-len(text2.split(":",1)[1]) % 4)))
|
||||
assert decoded == v2
|
||||
|
||||
v1 = {
|
||||
"v":1,"session":session,"pc_name":"DESKTOP-QFT","transport":"http-stream-v1","port":39393,
|
||||
"endpoints":["http://192.168.137.1:39393","http://192.168.49.1:39393"],
|
||||
"wifi":{"ssid":ssid,"passphrase":password,"security":"WPA2"},
|
||||
}
|
||||
raw1 = json.dumps(v1, separators=(",", ":")).encode()
|
||||
text1 = "QFTPAIR1:" + base64.urlsafe_b64encode(raw1).decode().rstrip("=")
|
||||
assert len(text2) < len(text1) * 0.60, (len(text2), len(text1))
|
||||
|
||||
qr = qrcode.QRCode(version=None, error_correction=ERROR_CORRECT_M, box_size=1, border=4)
|
||||
qr.add_data(text2)
|
||||
qr.make(fit=True)
|
||||
assert qr.version <= 10, qr.version
|
||||
print(f"PASS pairing: QFTPAIR2={len(text2)} chars vs v1={len(text1)} chars ({len(text2)/len(text1):.1%}), QR-M version={qr.version}")
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
import hashlib, os, tempfile
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
SIZE = 64 * 1024 * 1024 + 12345
|
||||
CHUNK = 8 * 1024 * 1024
|
||||
WORKERS = 8
|
||||
IO = 1024 * 1024
|
||||
|
||||
|
||||
def sha(path):
|
||||
h = hashlib.sha256()
|
||||
with open(path, "rb", buffering=0) as f:
|
||||
while True:
|
||||
b = f.read(4 * 1024 * 1024)
|
||||
if not b: break
|
||||
h.update(b)
|
||||
return h.digest()
|
||||
|
||||
|
||||
def main():
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
src = os.path.join(d, "source.bin")
|
||||
dst = os.path.join(d, "received.qftpart")
|
||||
# deterministic 1 MiB pattern, streamed to avoid holding the file in RAM
|
||||
pattern = bytes((i * 131 + 17) & 0xFF for i in range(IO))
|
||||
remaining = SIZE
|
||||
with open(src, "wb", buffering=0) as f:
|
||||
while remaining:
|
||||
n = min(remaining, len(pattern))
|
||||
f.write(pattern[:n]); remaining -= n
|
||||
with open(dst, "wb") as f: f.truncate(SIZE)
|
||||
|
||||
src_fd = os.open(src, os.O_RDONLY)
|
||||
dst_fd = os.open(dst, os.O_RDWR)
|
||||
try:
|
||||
total = (SIZE + CHUNK - 1) // CHUNK
|
||||
def worker(index):
|
||||
offset = index * CHUNK
|
||||
remaining = min(CHUNK, SIZE - offset)
|
||||
pos = 0
|
||||
while remaining:
|
||||
n = min(IO, remaining)
|
||||
block = os.pread(src_fd, n, offset + pos)
|
||||
assert len(block) == n
|
||||
wrote = os.pwrite(dst_fd, block, offset + pos)
|
||||
assert wrote == n
|
||||
remaining -= n; pos += n
|
||||
return index
|
||||
with ThreadPoolExecutor(max_workers=WORKERS) as pool:
|
||||
completed = sorted(pool.map(worker, reversed(range(total))))
|
||||
assert completed == list(range(total))
|
||||
finally:
|
||||
os.close(src_fd); os.close(dst_fd)
|
||||
|
||||
assert sha(src) == sha(dst)
|
||||
print(f"PASS parallel upload: {SIZE} bytes, {CHUNK//(1024*1024)} MiB chunks, {WORKERS} workers, 1 MiB worker I/O, SHA-256 identical")
|
||||
|
||||
|
||||
if __name__ == "__main__": main()
|
||||
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64, binascii, hashlib, math, os, random, struct
|
||||
import qrcode
|
||||
from qrcode.constants import ERROR_CORRECT_L
|
||||
|
||||
PREFIX = "QFT1:"
|
||||
CHUNK = 900
|
||||
LANES = 4
|
||||
DISPLAY_FPS = 60
|
||||
|
||||
|
||||
def enc(kind, sid, seq, total, payload=b""):
|
||||
body = b"QFT1" + bytes([1, kind]) + struct.pack(">QIIH", sid, seq, total, len(payload)) + payload
|
||||
crc = binascii.crc32(body) & 0xFFFFFFFF
|
||||
return PREFIX + base64.urlsafe_b64encode(body + struct.pack(">I", crc)).decode().rstrip("=")
|
||||
|
||||
|
||||
def dec(text):
|
||||
assert text.startswith(PREFIX)
|
||||
b64 = text[len(PREFIX):]
|
||||
raw = base64.urlsafe_b64decode(b64 + "=" * (-len(b64) % 4))
|
||||
body, crc_raw = raw[:-4], raw[-4:]
|
||||
assert (binascii.crc32(body) & 0xFFFFFFFF) == struct.unpack(">I", crc_raw)[0]
|
||||
assert body[:4] == b"QFT1" and body[4] == 1
|
||||
kind = body[5]
|
||||
sid, seq, total, plen = struct.unpack(">QIIH", body[6:24])
|
||||
payload = body[24:]
|
||||
assert len(payload) == plen
|
||||
return kind, sid, seq, total, payload
|
||||
|
||||
|
||||
def test_qr_density(sample):
|
||||
qr = qrcode.QRCode(version=None, error_correction=ERROR_CORRECT_L, box_size=1, border=4)
|
||||
qr.add_data(sample)
|
||||
qr.make(fit=True)
|
||||
modules_with_quiet = qr.modules_count + 8
|
||||
canvas = 960
|
||||
gutter = max(12, min(24, canvas // 64))
|
||||
cell = (canvas - gutter * 3) // 2
|
||||
module_px = cell // modules_with_quiet
|
||||
assert module_px >= 3, (qr.version, qr.modules_count, cell, module_px)
|
||||
return qr.version, module_px, len(sample)
|
||||
|
||||
|
||||
def main():
|
||||
random.seed(20260810)
|
||||
data = os.urandom(2_500_123)
|
||||
digest = hashlib.sha256(data).digest()
|
||||
total = math.ceil(len(data) / CHUNK)
|
||||
sid = 0x1234567890ABCDEF
|
||||
name = b"sample.bin"
|
||||
device = b"DESKTOP-QFT"
|
||||
manifest = (
|
||||
struct.pack(">QH", len(data), CHUNK)
|
||||
+ digest
|
||||
+ struct.pack(">H", len(name)) + name
|
||||
+ struct.pack(">H", len(device)) + device
|
||||
)
|
||||
playlist = [enc(1, sid, 0, total, manifest)]
|
||||
playlist += [enc(2, sid, i, total, data[i*CHUNK:(i+1)*CHUNK]) for i in range(total)]
|
||||
playlist += [enc(3, sid, 0, total)]
|
||||
pages = [playlist[i:i+LANES] for i in range(0, len(playlist), LANES)]
|
||||
|
||||
version, module_px, chars = test_qr_density(playlist[min(2, len(playlist)-1)])
|
||||
|
||||
got = [None] * total
|
||||
rounds = 0
|
||||
manifests = 0
|
||||
# Model both whole-camera-frame losses and per-lane decode failures.
|
||||
while any(x is None for x in got):
|
||||
rounds += 1
|
||||
for page in pages:
|
||||
if random.random() < 0.24: # display page/camera frame missed
|
||||
continue
|
||||
for text in page:
|
||||
if random.random() < 0.14: # one quadrant not decoded
|
||||
continue
|
||||
kind, rsid, seq, rtotal, payload = dec(text)
|
||||
assert rsid == sid and rtotal == total
|
||||
if kind == 1:
|
||||
manifests += 1
|
||||
elif kind == 2 and got[seq] is None:
|
||||
got[seq] = payload
|
||||
assert rounds < 60
|
||||
|
||||
rebuilt = b"".join(got)[:len(data)]
|
||||
assert rebuilt == data
|
||||
assert hashlib.sha256(rebuilt).digest() == digest
|
||||
raw_rate = CHUNK * LANES * DISPLAY_FPS
|
||||
print(
|
||||
f"PASS optical: {len(data)} bytes, {total} chunks, {LANES} QR/page @ {DISPLAY_FPS} FPS, "
|
||||
f"24% page loss + 14% lane loss, recovered in {rounds} rounds; "
|
||||
f"QR v{version}, {module_px}px/module, frame chars={chars}, theoretical payload={raw_rate/1024:.1f} KiB/s"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
import subprocess, sys
|
||||
from pathlib import Path
|
||||
root = Path(__file__).resolve().parent
|
||||
scripts = [
|
||||
'protocol_selftest.py',
|
||||
'pairing_selftest.py',
|
||||
'parallel_upload_selftest.py',
|
||||
'udp_discovery_selftest.py',
|
||||
'local_transfer_integration_selftest.py',
|
||||
'memory_budget_selftest.py',
|
||||
'android_compat_selftest.py',
|
||||
]
|
||||
for name in scripts:
|
||||
print(f'== {name} ==', flush=True)
|
||||
subprocess.run([sys.executable, str(root/name)], check=True)
|
||||
print('ALL CORE PYTHON SELFTESTS PASSED')
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python3
|
||||
import socket, threading, time
|
||||
|
||||
TOKEN = "test_session_1234567890AB"
|
||||
PORT = 39392
|
||||
TCP = 39393
|
||||
ready = threading.Event()
|
||||
|
||||
|
||||
def server():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind(("127.0.0.1", PORT))
|
||||
s.settimeout(2)
|
||||
ready.set()
|
||||
data, peer = s.recvfrom(512)
|
||||
assert data.decode() == f"QFTDISC2:{TOKEN}"
|
||||
s.sendto(f"QFTHERE2:{TOKEN}:{TCP}".encode(), peer)
|
||||
s.close()
|
||||
|
||||
|
||||
def main():
|
||||
t = threading.Thread(target=server, daemon=True); t.start(); ready.wait(1)
|
||||
c = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); c.settimeout(1)
|
||||
c.sendto(f"QFTDISC2:{TOKEN}".encode(), ("127.0.0.1", PORT))
|
||||
data, peer = c.recvfrom(512); c.close(); t.join(1)
|
||||
assert data.decode() == f"QFTHERE2:{TOKEN}:{TCP}"
|
||||
print(f"PASS UDP discovery: request/response token+port round-trip from {peer[0]}")
|
||||
|
||||
|
||||
if __name__ == "__main__": main()
|
||||
Reference in New Issue
Block a user