ControlHttpServer.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using IvfTl.ControlHost.Debug;
  10. namespace IvfTl.ControlHost
  11. {
  12. /// <summary>
  13. /// control 进程内的本地 HTTP 小服务,只监听 127.0.0.1:port。
  14. /// 阶段1:/ping、/status。阶段2:/status 补全(rich) + /serial/pause|resume(借串口) + /shutdown(受护栏停止)。
  15. /// </summary>
  16. public class ControlHttpServer
  17. {
  18. private readonly int _port;
  19. private readonly Func<StatusDto> _pingProvider; // /ping 轻量存活
  20. private readonly Func<object> _statusProvider; // /status 完整快照(阶段2 §6 三块)
  21. private readonly Func<string, bool> _shutdownHandler; // /shutdown(token 校验后安全停机)
  22. private readonly Func<int, bool> _serialPauseHandler; // /serial/pause(借串口:control 让路该舱)
  23. private readonly Func<int, bool> _serialResumeHandler; // /serial/resume(归还:恢复采集)
  24. private readonly Action<string> _log;
  25. private readonly DebugSessionManager _debug;
  26. private HttpListener _listener;
  27. private CancellationTokenSource _cts;
  28. public ControlHttpServer(
  29. int port,
  30. Func<StatusDto> pingProvider,
  31. Func<object> statusProvider,
  32. Func<string, bool> shutdownHandler,
  33. Func<int, bool> serialPauseHandler,
  34. Func<int, bool> serialResumeHandler,
  35. Action<string> log,
  36. DebugSessionManager debug = null)
  37. {
  38. _port = port;
  39. _pingProvider = pingProvider;
  40. _statusProvider = statusProvider;
  41. _shutdownHandler = shutdownHandler;
  42. _serialPauseHandler = serialPauseHandler;
  43. _serialResumeHandler = serialResumeHandler;
  44. _log = log ?? (_ => { });
  45. _debug = debug;
  46. }
  47. public void Start()
  48. {
  49. _listener = new HttpListener();
  50. // 仅本机回环,拒绝外部访问(防外部调停机/借串口)。
  51. _listener.Prefixes.Add($"http://127.0.0.1:{_port}/");
  52. _listener.Start();
  53. _cts = new CancellationTokenSource();
  54. _log($"ControlHttpServer 监听 http://127.0.0.1:{_port}/");
  55. Task.Run(() => Loop(_cts.Token));
  56. }
  57. private async Task Loop(CancellationToken token)
  58. {
  59. while (!token.IsCancellationRequested)
  60. {
  61. HttpListenerContext ctx;
  62. try { ctx = await _listener.GetContextAsync(); }
  63. catch (Exception ex) { if (!token.IsCancellationRequested) _log("HttpListener 异常:" + ex.Message); break; }
  64. try { Handle(ctx); }
  65. catch (Exception ex) { _log("处理请求异常:" + ex.Message); }
  66. }
  67. }
  68. private void Handle(HttpListenerContext ctx)
  69. {
  70. string path = ctx.Request.Url.AbsolutePath.TrimEnd('/').ToLowerInvariant();
  71. string method = ctx.Request.HttpMethod.ToUpperInvariant();
  72. string body;
  73. int code = 200;
  74. switch (path)
  75. {
  76. case "/ping":
  77. body = JsonConvert.SerializeObject(_pingProvider());
  78. break;
  79. case "/status":
  80. body = JsonConvert.SerializeObject(_statusProvider());
  81. break;
  82. case "/shutdown":
  83. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  84. {
  85. string token = ReadField(ctx, "token");
  86. bool ok = _shutdownHandler != null && _shutdownHandler(token ?? "");
  87. code = ok ? 200 : 403;
  88. body = "{\"ok\":" + (ok ? "true" : "false") + (ok ? "" : ",\"error\":\"token invalid\"") + "}";
  89. }
  90. break;
  91. case "/serial/pause":
  92. case "/serial/resume":
  93. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  94. {
  95. int houseSn = ReadIntField(ctx, "houseSn");
  96. bool isPause = path == "/serial/pause";
  97. var handler = isPause ? _serialPauseHandler : _serialResumeHandler;
  98. bool ok = handler != null && houseSn > 0 && handler(houseSn);
  99. code = ok ? 200 : 400;
  100. body = "{\"ok\":" + (ok ? "true" : "false") + ",\"houseSn\":" + houseSn + (ok ? "" : ",\"error\":\"bad houseSn or handler\"") + "}";
  101. }
  102. break;
  103. case "/debug/acquire":
  104. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  105. {
  106. int houseSn = ReadIntField(ctx, "houseSn");
  107. var r = _debug != null ? _debug.Acquire(houseSn) : DebugCommandResult.Fail("NO_HANDLE", "debug 未装配");
  108. code = r.Ok ? 200 : 409; body = JsonConvert.SerializeObject(r);
  109. }
  110. break;
  111. case "/debug/heartbeat":
  112. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  113. {
  114. var r = _debug != null ? _debug.Heartbeat(ReadField(ctx, "sessionId")) : DebugCommandResult.Fail("SESSION_EXPIRED", "debug 未装配");
  115. code = r.Ok ? 200 : 410; body = JsonConvert.SerializeObject(r);
  116. }
  117. break;
  118. case "/debug/release":
  119. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  120. {
  121. var r = _debug != null ? _debug.Release(ReadField(ctx, "sessionId")) : DebugCommandResult.Okay();
  122. code = 200; body = JsonConvert.SerializeObject(r);
  123. }
  124. break;
  125. case "/debug/command":
  126. if (method != "POST") { code = 405; body = Err("method not allowed"); break; }
  127. {
  128. var jo = ReadBody(ctx);
  129. string sid = jo?["sessionId"]?.ToString();
  130. string op = jo?["op"]?.ToString();
  131. var argsObj = jo?["args"] as Newtonsoft.Json.Linq.JObject;
  132. var r = _debug != null ? _debug.Execute(sid, op, argsObj) : DebugCommandResult.Fail("SESSION_EXPIRED", "debug 未装配");
  133. code = r.Ok ? 200 : (r.Code == "SESSION_EXPIRED" ? 410 : (r.Code == "OUT_OF_RANGE" ? 400 : 200));
  134. body = JsonConvert.SerializeObject(r);
  135. }
  136. break;
  137. case "/debug/preview/stream":
  138. if (method != "GET") { code = 405; body = Err("method not allowed"); break; }
  139. {
  140. string sid = ctx.Request.QueryString["sessionId"];
  141. if (_debug == null || sid == null || !_debug.TryGet(sid, out var session))
  142. {
  143. code = 404; body = Err("session not found");
  144. break; // 走统一收尾返回 404
  145. }
  146. // 校验通过:分流起后台推流线程,不走统一收尾(那会 Close 流终止推流)。
  147. StartPreviewStream(ctx, session);
  148. return;
  149. }
  150. default:
  151. code = 404; body = Err("not found");
  152. break;
  153. }
  154. byte[] buf = Encoding.UTF8.GetBytes(body);
  155. ctx.Response.StatusCode = code;
  156. ctx.Response.ContentType = "application/json";
  157. ctx.Response.ContentLength64 = buf.Length;
  158. ctx.Response.OutputStream.Write(buf, 0, buf.Length);
  159. ctx.Response.OutputStream.Close();
  160. }
  161. private static string Err(string msg) => "{\"ok\":false,\"error\":\"" + msg + "\"}";
  162. /// <summary>读 POST JSON body 的某字符串字段(失败返回 null)。</summary>
  163. private string ReadField(HttpListenerContext ctx, string field)
  164. {
  165. try
  166. {
  167. using (var sr = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding ?? Encoding.UTF8))
  168. {
  169. string raw = sr.ReadToEnd();
  170. if (string.IsNullOrEmpty(raw)) return null;
  171. var jo = JObject.Parse(raw);
  172. return jo[field]?.ToString();
  173. }
  174. }
  175. catch (Exception ex) { _log("解析请求体异常:" + ex.Message); return null; }
  176. }
  177. private int ReadIntField(HttpListenerContext ctx, string field)
  178. {
  179. string s = ReadField(ctx, field);
  180. return int.TryParse(s, out int v) ? v : -1;
  181. }
  182. /// <summary>把 POST body 整体解析为 JObject(失败返回 null)。/debug/command 多字段用。</summary>
  183. private Newtonsoft.Json.Linq.JObject ReadBody(HttpListenerContext ctx)
  184. {
  185. try
  186. {
  187. using (var sr = new StreamReader(ctx.Request.InputStream, ctx.Request.ContentEncoding ?? Encoding.UTF8))
  188. {
  189. string raw = sr.ReadToEnd();
  190. return string.IsNullOrEmpty(raw) ? null : Newtonsoft.Json.Linq.JObject.Parse(raw);
  191. }
  192. }
  193. catch (Exception ex) { _log("解析 body 异常:" + ex.Message); return null; }
  194. }
  195. /// <summary>
  196. /// 推流分支:起专用后台线程,抓帧→JPEG→multipart 持续写。
  197. /// HttpListener 工作线程立即返回(本方法起线程后即返回),不被推流阻塞。
  198. /// 任何退出路径都标记 session.StreamBroken,会话靠心跳 TTL 看门狗最终回收(spec §7)。
  199. /// </summary>
  200. private void StartPreviewStream(HttpListenerContext ctx, IvfTl.ControlHost.Debug.DebugSession session)
  201. {
  202. var resp = ctx.Response;
  203. resp.StatusCode = 200;
  204. resp.ContentType = IvfTl.ControlHost.Debug.MjpegStreamWriter.ContentType;
  205. resp.SendChunked = true; // 流式,长度未知
  206. resp.Headers.Add("Cache-Control", "no-cache");
  207. var t = new Thread(() =>
  208. {
  209. int errCount = 0;
  210. var cam = session.Lease?.Camera;
  211. try
  212. {
  213. if (cam == null) { _log($"[debug] 推流舱{session.HouseSn} 无相机句柄,放弃"); return; }
  214. cam.SetOpMode(1); // 实时模式(0=单帧/1=实时,见 ICamera 注释)
  215. var outStream = resp.OutputStream;
  216. while (true)
  217. {
  218. // 会话已被回收(release/超时)→ 停推流
  219. if (!_debug.TryGet(session.SessionId, out _)) { _log($"[debug] 推流舱{session.HouseSn} 会话已失效,停"); break; }
  220. try
  221. {
  222. byte[] bgr = cam.GrabStable(); // 走全进程相机锁,与采集/对焦串行
  223. if (bgr == null) { Thread.Sleep(100); continue; }
  224. byte[] jpeg = IvfTl.ControlHost.Debug.MjpegStreamWriter.EncodeJpeg(bgr, cam.Width, cam.Height);
  225. if (jpeg == null) { Thread.Sleep(100); continue; }
  226. byte[] frame = IvfTl.ControlHost.Debug.MjpegStreamWriter.FrameBytes(jpeg);
  227. outStream.Write(frame, 0, frame.Length);
  228. outStream.Flush();
  229. errCount = 0;
  230. Thread.Sleep(66); // ~15fps(spec §4.2)
  231. }
  232. catch (IOException) { _log($"[debug] 推流舱{session.HouseSn} 客户端断开"); break; } // operate 关预览/崩溃:正常退出
  233. catch (HttpListenerException) { _log($"[debug] 推流舱{session.HouseSn} 连接断开"); break; }
  234. catch (Exception ex)
  235. {
  236. errCount++;
  237. _log($"[debug] 推流舱{session.HouseSn} 抓帧/编码异常({errCount}/5): {ex.Message}");
  238. if (errCount >= 5) { _log($"[debug] 推流舱{session.HouseSn} 连续错误过多,停"); break; }
  239. Thread.Sleep(500);
  240. }
  241. }
  242. }
  243. catch (Exception ex) { _log($"[debug] 推流舱{session.HouseSn} 线程异常: {ex.Message}"); }
  244. finally
  245. {
  246. session.StreamBroken = true; // 可回收快信号;会话最终由心跳 TTL 看门狗收(不在此 Dispose,避免与命令分发/超时回收争 lease)
  247. try { resp.OutputStream.Close(); } catch { }
  248. try { resp.Close(); } catch { }
  249. _log($"[debug] 推流舱{session.HouseSn} 线程结束");
  250. }
  251. });
  252. t.IsBackground = true;
  253. t.Name = $"MjpegStream-h{session.HouseSn}";
  254. t.Start();
  255. }
  256. public void Stop()
  257. {
  258. try { _cts?.Cancel(); _listener?.Stop(); _listener?.Close(); }
  259. catch (Exception ex) { _log("ControlHttpServer 停止异常:" + ex.Message); }
  260. }
  261. }
  262. }