修复就绪屏障位被消费致FTP永不监听;新增栈可观测(水位探针+静态调用图报告)并补FTP实测
This commit is contained in:
@@ -1384,7 +1384,7 @@ void ch395f_test_run(void)
|
||||
* Phase 7(TCP Client)与阶段 6/8~11(TCP Server 监听循环)互斥,
|
||||
* 二者共用单连接 Socket 0,故按宏选择入口。 */
|
||||
/* 等待 netTask 广播网络就绪(取代 g_net_ready 忙等轮询) */
|
||||
(void)osEventFlagsWait(sys_evtHandle, SYS_EVT_NET_READY, osFlagsWaitAny, osWaitForever);
|
||||
(void)osEventFlagsWait(sys_evtHandle, SYS_EVT_NET_READY, osFlagsWaitAny | osFlagsNoClear, osWaitForever);
|
||||
DBG_INFO("netTask ready");
|
||||
#if defined(ENABLE_PHASE7_TESTS)
|
||||
net_layer_client_test_main();
|
||||
|
||||
+41
-13
@@ -37,6 +37,33 @@ def send_cmd(sock, cmd):
|
||||
time.sleep(0.1)
|
||||
return recv_line(sock)
|
||||
|
||||
def connect_with_retry(host, port, timeout=15.0, attempts=6, delay=3.0):
|
||||
"""连接控制通道(带重试)。
|
||||
|
||||
CH395F 单客户端复用:上一会话未 QUIT 就断开时,服务端可能仍在处理旧连接,
|
||||
新连接会被 RST(WinError 10061);实测约 30s 后服务端丢弃旧会话并重新 listen。
|
||||
注意不要用裸 TCP 探测端口(探测会占掉唯一会话)。
|
||||
"""
|
||||
last_err = None
|
||||
for i in range(1, attempts + 1):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(timeout)
|
||||
try:
|
||||
s.connect((host, port))
|
||||
return s, None
|
||||
except Exception as e:
|
||||
last_err = e
|
||||
try:
|
||||
s.close()
|
||||
except Exception:
|
||||
pass
|
||||
if i < attempts:
|
||||
print(f" connect failed ({e}); retry {i}/{attempts - 1} in {delay:.0f}s "
|
||||
f"(single-client: stale session blocks new connections)")
|
||||
time.sleep(delay)
|
||||
return None, last_err
|
||||
|
||||
|
||||
def parse_pasv(resp):
|
||||
m = re.search(r"(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)", resp.decode())
|
||||
if m:
|
||||
@@ -80,22 +107,23 @@ def test_speed(host, port, size_kb):
|
||||
|
||||
# ---- Connect + Login ----
|
||||
print("[Connect] Connecting...")
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(60.0)
|
||||
try:
|
||||
sock.connect((host, port))
|
||||
except Exception as e:
|
||||
print(f" CONNECT FAILED: {e}")
|
||||
check("Connect", False, str(e))
|
||||
return finish(sock)
|
||||
sock, err = connect_with_retry(host, port, timeout=60.0)
|
||||
if sock is None:
|
||||
print(f" CONNECT FAILED: {err}")
|
||||
check("Connect", False, str(err))
|
||||
return finish(None)
|
||||
recv_line(sock) # 220
|
||||
if not send_cmd(sock, "USER anonymous"):
|
||||
check("Login USER", False)
|
||||
resp = send_cmd(sock, "USER anonymous")
|
||||
if not (resp and resp.startswith(b"230")):
|
||||
check("Login USER 230", False,
|
||||
resp.decode(errors='replace').rstrip() if resp else "no resp")
|
||||
return finish(sock)
|
||||
if not send_cmd(sock, "PASS test@test.com"):
|
||||
check("Login PASS", False)
|
||||
resp = send_cmd(sock, "PASS test@test.com")
|
||||
if not (resp and resp.startswith(b"230")):
|
||||
check("Login PASS 230", False,
|
||||
resp.decode(errors='replace').rstrip() if resp else "no resp")
|
||||
return finish(sock)
|
||||
check("Login (USER/PASS)", True)
|
||||
check("Login (USER/PASS 230)", True)
|
||||
send_cmd(sock, "TYPE I")
|
||||
|
||||
# ---- Upload ----
|
||||
|
||||
+33
-79
@@ -48,6 +48,34 @@ def send_cmd(sock, cmd, expect_code=None):
|
||||
print(" < [NO RESPONSE]")
|
||||
return None
|
||||
|
||||
def connect_with_retry(host, port, timeout=TIMEOUT, attempts=6, delay=3.0):
|
||||
"""连接控制通道(带重试)。
|
||||
|
||||
CH395F 是单客户端复用:上一会话未 QUIT 就断开时,服务端仍在处理旧连接,
|
||||
新连接会被 RST(WinError 10061)。实测旧会话约 30s 后被服务端丢弃并重新
|
||||
listen,故这里等一会儿重试即可,不要用裸 TCP 探测端口(探测本身会占掉
|
||||
唯一会话)。
|
||||
"""
|
||||
last_err = None
|
||||
for i in range(1, attempts + 1):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(timeout)
|
||||
try:
|
||||
s.connect((host, port))
|
||||
return s, None
|
||||
except Exception as e:
|
||||
last_err = e
|
||||
try:
|
||||
s.close()
|
||||
except Exception:
|
||||
pass
|
||||
if i < attempts:
|
||||
print(f" connect failed ({e}); retry {i}/{attempts - 1} in {delay:.0f}s "
|
||||
f"(single-client: stale session blocks new connections)")
|
||||
time.sleep(delay)
|
||||
return None, last_err
|
||||
|
||||
|
||||
def parse_pasv(resp):
|
||||
m = re.search(r"(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)", resp.decode())
|
||||
if m:
|
||||
@@ -55,77 +83,6 @@ def parse_pasv(resp):
|
||||
return f"{a}.{b}.{c}.{d}", e * 256 + f
|
||||
return None, None
|
||||
|
||||
def ftp_transfer_data(host, pasv_ip, pasv_port, cmd, data_to_send=None, read_data=False):
|
||||
"""通用数据通道传输:发送命令 + 连数据端口 + 收发数据"""
|
||||
print(f"\n [{cmd}] Opening data channel {pasv_ip}:{pasv_port}...")
|
||||
data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
data_sock.settimeout(10.0)
|
||||
try:
|
||||
data_sock.connect((pasv_ip, pasv_port))
|
||||
print(f" Data channel connected.")
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(TIMEOUT)
|
||||
sock.connect((host, PORT))
|
||||
recv_line(sock) # 220
|
||||
send_cmd(sock, "USER anonymous")
|
||||
send_cmd(sock, "PASS test@test.com")
|
||||
|
||||
# PASV
|
||||
resp = send_cmd(sock, "PASV")
|
||||
_, data_port = parse_pasv(resp)
|
||||
print(f" PASV port: {data_port}")
|
||||
|
||||
# Connect data FIRST
|
||||
data_sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
data_sock2.settimeout(10.0)
|
||||
data_sock2.connect((pasv_ip, data_port))
|
||||
|
||||
# Send command
|
||||
time.sleep(0.2)
|
||||
send_cmd(sock, cmd)
|
||||
|
||||
result = None
|
||||
if data_to_send:
|
||||
print(f" Sending {len(data_to_send)} bytes to data channel...")
|
||||
total = 0
|
||||
while total < len(data_to_send):
|
||||
n = data_sock2.send(data_to_send[total:])
|
||||
if n <= 0:
|
||||
break
|
||||
total += n
|
||||
print(f" Sent {total} bytes.")
|
||||
data_sock2.shutdown(socket.SHUT_WR)
|
||||
|
||||
if read_data:
|
||||
result = b""
|
||||
while True:
|
||||
try:
|
||||
chunk = data_sock2.recv(4096)
|
||||
if not chunk:
|
||||
break
|
||||
result += chunk
|
||||
except socket.timeout:
|
||||
break
|
||||
print(f" Received {len(result)} bytes from data channel.")
|
||||
|
||||
data_sock2.close()
|
||||
resp = recv_line(sock)
|
||||
if resp:
|
||||
print(f" Control response: {resp.decode().rstrip()}")
|
||||
|
||||
send_cmd(sock, "QUIT")
|
||||
sock.close()
|
||||
data_sock.close()
|
||||
return result
|
||||
except Exception as e:
|
||||
print(f" Data transfer error: {e}")
|
||||
try:
|
||||
data_sock.close()
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
def test_upload_download(host, port):
|
||||
print(f"=== FTP Upload/Download Test: {host}:{port} ===\n")
|
||||
checks = []
|
||||
@@ -156,14 +113,11 @@ def test_upload_download(host, port):
|
||||
|
||||
# 1. Connect + Login
|
||||
print("[1] Connect + Login")
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(TIMEOUT)
|
||||
try:
|
||||
sock.connect((host, port))
|
||||
except Exception as e:
|
||||
print(f" CONNECT FAILED: {e}")
|
||||
check("Connect", False, str(e))
|
||||
return finish(sock)
|
||||
sock, err = connect_with_retry(host, port)
|
||||
if sock is None:
|
||||
print(f" CONNECT FAILED: {err}")
|
||||
check("Connect", False, str(err))
|
||||
return finish(None)
|
||||
recv_line(sock) # 220
|
||||
|
||||
if not send_cmd(sock, "USER anonymous", 230):
|
||||
|
||||
Reference in New Issue
Block a user