From 8b153ee4bb9e02f1ad19a566375b8fcc4df3a84a Mon Sep 17 00:00:00 2001 From: jasonchenwork Date: Tue, 8 Apr 2025 17:47:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E6=92=ADCLOSE=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TCPServer.cs | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/TCPServer.cs b/TCPServer.cs index c25f594..a7db93f 100644 --- a/TCPServer.cs +++ b/TCPServer.cs @@ -82,14 +82,18 @@ namespace DualScreenDemo Console.WriteLine("Failed to invoke action after maximum retries"); } - public void Start() + public void Start() { + // 啟動 TCP 監聽器 listener.Start(); Console.WriteLine("Server started on port " + Port + "."); + try { + // 讀取初始狀態檔案 string stateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "states.txt"); string initialState = ReadStateFile(stateFilePath); + // 若初始狀態為 "CLOSE",則顯示送客畫面,並禁用所有主畫面的控制項 if (initialState.Equals("CLOSE", StringComparison.OrdinalIgnoreCase)) { _ = SafeInvoke(PrimaryForm.Instance, () => @@ -107,14 +111,17 @@ namespace DualScreenDemo }); } + // 不斷等待並處理 TCP 連線 while (true) { Console.WriteLine("Waiting for connections..."); + using (TcpClient client = listener.AcceptTcpClient()) { Console.WriteLine("Connected!"); NetworkStream stream = client.GetStream(); + // 處理來自 client 的指令 while (client.Connected) { byte[] buffer = new byte[1024]; @@ -125,16 +132,20 @@ namespace DualScreenDemo string request = Encoding.UTF8.GetString(buffer, 0, bytesRead); Console.WriteLine("Received: " + request.Trim()); + // 忽略長度太短的請求 if (request.Length < 5) { continue; } + // 解析 host 名稱字尾與指令本體 string requestHostSuffix = request.Substring(0, 3); string command = request.Substring(4); + // 比對主機名稱是否符合 if (requestHostSuffix.Equals(hostNameSuffix, StringComparison.OrdinalIgnoreCase)) { + // 指令為 "X":播放 CLOSE.MPG,並顯示送客畫面 if (command.Trim().Equals("X", StringComparison.OrdinalIgnoreCase)) { _ = SafeInvoke(VideoPlayerForm.Instance, async () => @@ -144,33 +155,43 @@ namespace DualScreenDemo await SafeInvoke(PrimaryForm.Instance, () => { PrimaryForm.Instance.ShowSendOffScreen(); - Console.WriteLine("開始設置新的播放列表"); - + string closePath = @"C:\video\CLOSE.MPG"; + if (File.Exists(closePath)) { + // 建立結束播放用的 SongData 實例 SongData closeSong = new SongData( "", "", "結束播放", 0, "", "", "", "", DateTime.Now, closePath, "", "", "", "", "", "", "", "", "", "", "", 1 ); - VideoPlayerForm.playingSongList = new List(); + // 建立新的播放清單 + VideoPlayerForm.publicPlaylist = new List(); + VideoPlayerForm.playingSongList = new List(); + + // 如果有正在播放的歌曲也加進去 if (VideoPlayerForm.Instance.currentPlayingSong != null) { VideoPlayerForm.playingSongList.Add(VideoPlayerForm.Instance.currentPlayingSong); } + VideoPlayerForm.publicPlaylist.Add(closeSong); + // 將 CLOSE.MPG 加入播放清單 VideoPlayerForm.playingSongList.Add(closeSong); + + // 清空使用者點播清單 PrimaryForm.userRequestedSongs = new List(); + // 隱藏 Overlay 的「下一首提示」 if (IsFormReady(OverlayForm.MainForm)) { OverlayForm.MainForm.nextSongLabel.Visible = false; } - + VideoPlayerForm.Instance.PlayNextSong(); Console.WriteLine("已設置新的播放列表,包含當前歌曲和 CLOSE.MPG"); } else @@ -181,22 +202,28 @@ namespace DualScreenDemo } }); + // 更新狀態檔案為 CLOSE UpdateStateFile(stateFilePath, "CLOSE"); continue; } + // 指令為 "O":開啟系統,隱藏送客畫面 if (command.Trim().Equals("O", StringComparison.OrdinalIgnoreCase)) { _ = SafeInvoke(PrimaryForm.Instance, () => { PrimaryForm.Instance.HideSendOffScreen(); }); - + VideoPlayerForm.publicPlaylist = new List(); + VideoPlayerForm.playingSongList = new List(); + VideoPlayerForm.Instance.PlayPublicPlaylist(); // 播放下一首歌曲 + // 更新狀態檔案為 OPEN UpdateStateFile(stateFilePath, "OPEN"); continue; } } + // 若 Overlay Form 準備好,嘗試顯示跑馬燈文字 if (IsFormReady(OverlayForm.MainForm)) { string message = request.Trim(); @@ -207,18 +234,21 @@ namespace DualScreenDemo { if (match.Success) { + // 若符合格式,顯示主跑馬燈文字 string marqueeMessage = message.Substring(match.Value.Length).Trim(); Color textColor = GetColorFromString(match.Groups[2].Value); OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, textColor); } else { + // 不符合格式,顯示在第二行跑馬燈 string marqueeMessage = "系統公告: " + message; OverlayForm.MainForm.UpdateMarqueeTextSecondLine(marqueeMessage); } }); } + // 指令為 "exit":結束此連線 if (request.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase)) { break; @@ -235,10 +265,12 @@ namespace DualScreenDemo } finally { + // 關閉 listener listener.Stop(); } } + private Color GetColorFromString(string colorName) { switch (colorName)