fix: mock reads byte-by-byte, handles any line endings
This commit is contained in:
+22
-13
@@ -58,24 +58,30 @@ class ElmHandler(socketserver.StreamRequestHandler):
|
|||||||
print(f"\n🔌 Подключение: {self.client_address}")
|
print(f"\n🔌 Подключение: {self.client_address}")
|
||||||
|
|
||||||
# Отправляем приветствие
|
# Отправляем приветствие
|
||||||
self.wfile.write(b"ELM327 v1.5\r\n>")
|
self.wfile.write(b"\r\nELM327 v1.5\r\n>")
|
||||||
|
self.wfile.flush()
|
||||||
|
|
||||||
|
buf = b""
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data = self.rfile.readline()
|
# Читаем побайтово — принимает \r или \n как конец строки
|
||||||
if not data:
|
ch = self.rfile.read(1)
|
||||||
|
if not ch:
|
||||||
break
|
break
|
||||||
|
|
||||||
cmd = data.decode("utf-8", errors="ignore").strip().upper()
|
if ch in (b'\n', b'\r'):
|
||||||
if not cmd:
|
if buf:
|
||||||
continue
|
cmd = buf.decode("utf-8", errors="ignore").strip().upper()
|
||||||
|
buf = b""
|
||||||
|
if cmd:
|
||||||
|
response = self._process(cmd)
|
||||||
|
if response:
|
||||||
|
self.wfile.write(response)
|
||||||
|
self.wfile.flush()
|
||||||
|
else:
|
||||||
|
buf += ch
|
||||||
|
|
||||||
response = self._process(cmd)
|
except (ConnectionResetError, BrokenPipeError, OSError):
|
||||||
if response:
|
|
||||||
self.wfile.write(response)
|
|
||||||
self.wfile.flush()
|
|
||||||
|
|
||||||
except (ConnectionResetError, BrokenPipeError):
|
|
||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f" ⚠️ Ошибка: {e}")
|
print(f" ⚠️ Ошибка: {e}")
|
||||||
@@ -144,7 +150,10 @@ def main():
|
|||||||
parser.add_argument("--host", default="0.0.0.0")
|
parser.add_argument("--host", default="0.0.0.0")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
server = socketserver.ThreadingTCPServer((args.host, args.port), ElmHandler)
|
class ReuseTCPServer(socketserver.ThreadingTCPServer):
|
||||||
|
allow_reuse_address = True
|
||||||
|
|
||||||
|
server = ReuseTCPServer((args.host, args.port), ElmHandler)
|
||||||
print(f"🚗 Mock ELM327 на {args.host}:{args.port}")
|
print(f"🚗 Mock ELM327 на {args.host}:{args.port}")
|
||||||
print(f" Подключиться: telnet localhost {args.port}")
|
print(f" Подключиться: telnet localhost {args.port}")
|
||||||
print(f" Ctrl+C чтобы остановить")
|
print(f" Ctrl+C чтобы остановить")
|
||||||
|
|||||||
Reference in New Issue
Block a user