This repository has been archived on 2025-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
superstar/TCPServer.cs

367 lines
17 KiB
C#
Raw Permalink Normal View History

2025-03-18 11:35:10 +08:00
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading.Tasks;
using System.IO; // 為 Path 和 File 提供支持
using System.Windows.Forms; // 為 Invoke 和 Form 控件提供支持
using System.Collections.Generic;
using DBObj;
using OverlayFormObj;
2025-03-18 11:35:10 +08:00
namespace DualScreenDemo
{
public class TCPServer
{
private TcpListener listener;
private const int Port = 1000;
private readonly string hostNameSuffix;
2025-03-19 11:44:27 +08:00
//private bool isProcessingCommand = false;
2025-03-18 11:35:10 +08:00
public TCPServer()
{
listener = new TcpListener(IPAddress.Any, Port);
hostNameSuffix = GetHostNameSuffix();
}
private bool IsFormReady(Form form)
{
if (form == null) return false;
bool isReady = false;
try
{
if (form.IsHandleCreated && !form.IsDisposed)
{
if (form.InvokeRequired)
{
form.Invoke(new Action(() => isReady = true));
}
else
{
isReady = true;
}
}
}
catch
{
isReady = false;
}
return isReady;
}
private async Task SafeInvoke(Form form, Action action, int maxRetries = 10)
{
if (form == null) return;
for (int i = 0; i < maxRetries; i++)
{
try
{
if (IsFormReady(form))
{
if (form.InvokeRequired)
{
form.Invoke(action);
}
else
{
action();
}
return;
}
}
catch (Exception ex)
{
Console.WriteLine($"Invoke attempt {i + 1} failed: {ex.Message}");
}
await Task.Delay(500); // 等待500毫秒后重试
}
Console.WriteLine("Failed to invoke action after maximum retries");
}
2025-04-08 17:47:46 +08:00
public void Start()
2025-03-18 11:35:10 +08:00
{
2025-04-08 17:47:46 +08:00
// 啟動 TCP 監聽器
2025-05-13 15:19:19 +08:00
2025-03-18 11:35:10 +08:00
listener.Start();
Console.WriteLine("Server started on port " + Port + ".");
2025-04-08 17:47:46 +08:00
2025-03-18 11:35:10 +08:00
try {
2025-04-08 17:47:46 +08:00
// 讀取初始狀態檔案
2025-03-18 11:35:10 +08:00
string stateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "states.txt");
string initialState = ReadStateFile(stateFilePath);
2025-04-08 17:47:46 +08:00
// 若初始狀態為 "CLOSE",則顯示送客畫面,並禁用所有主畫面的控制項
2025-03-18 11:35:10 +08:00
if (initialState.Equals("CLOSE", StringComparison.OrdinalIgnoreCase))
{
_ = SafeInvoke(PrimaryForm.Instance, () =>
{
try {
foreach (Control ctrl in PrimaryForm.Instance.Controls)
{
ctrl.Enabled = false;
}
PrimaryForm.Instance.ShowSendOffScreen();
}
catch (Exception ex) {
Console.WriteLine($"顯示送客畫面時發生錯誤: {ex.Message}");
}
});
}
2025-04-08 17:47:46 +08:00
// 不斷等待並處理 TCP 連線
2025-03-18 11:35:10 +08:00
while (true)
{
Console.WriteLine("Waiting for connections...");
2025-04-08 17:47:46 +08:00
2025-03-18 11:35:10 +08:00
using (TcpClient client = listener.AcceptTcpClient())
{
Console.WriteLine("Connected!");
NetworkStream stream = client.GetStream();
2025-04-08 17:47:46 +08:00
// 處理來自 client 的指令
2025-03-18 11:35:10 +08:00
while (client.Connected)
{
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
string request = Encoding.UTF8.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received: " + request.Trim());
2025-04-08 17:47:46 +08:00
// 忽略長度太短的請求
2025-03-18 11:35:10 +08:00
if (request.Length < 5)
{
continue;
}
2025-04-08 17:47:46 +08:00
// 解析 host 名稱字尾與指令本體
2025-03-18 11:35:10 +08:00
string requestHostSuffix = request.Substring(0, 3);
string command = request.Substring(4);
2025-04-08 17:47:46 +08:00
// 比對主機名稱是否符合
2025-03-18 11:35:10 +08:00
if (requestHostSuffix.Equals(hostNameSuffix, StringComparison.OrdinalIgnoreCase))
{
2025-04-08 17:47:46 +08:00
// 指令為 "X":播放 CLOSE.MPG並顯示送客畫面
2025-03-18 11:35:10 +08:00
if (command.Trim().Equals("X", StringComparison.OrdinalIgnoreCase))
{
2025-05-19 13:02:09 +08:00
PrimaryForm.Instance.pictureBoxQRCode.Visible = false;
PrimaryForm.Instance.closeQRCodeButton.Visible = false;
2025-03-18 11:35:10 +08:00
_ = SafeInvoke(VideoPlayerForm.Instance, async () =>
{
if (IsFormReady(PrimaryForm.Instance))
{
2025-05-19 13:02:09 +08:00
await SafeInvoke(PrimaryForm.Instance, async () =>
2025-03-18 11:35:10 +08:00
{
PrimaryForm.Instance.ShowSendOffScreen();
2025-05-13 14:03:14 +08:00
PrimaryForm.Instance.HideFireScreen();
2025-05-13 14:43:21 +08:00
string marqueeMessage= "歡迎使用網路版系統,與你共度美好時光。";
2025-05-13 14:03:14 +08:00
OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, Color.White);
2025-03-18 11:35:10 +08:00
Console.WriteLine("開始設置新的播放列表");
2025-04-08 17:47:46 +08:00
2025-03-18 11:35:10 +08:00
string closePath = @"C:\video\CLOSE.MPG";
2025-04-08 17:47:46 +08:00
2025-03-18 11:35:10 +08:00
if (File.Exists(closePath))
{
2025-04-08 17:47:46 +08:00
// 建立結束播放用的 SongData 實例
2025-03-18 11:35:10 +08:00
SongData closeSong = new SongData(
2025-05-19 13:02:09 +08:00
"", "", "結束播放", 0, "", "", "", "",
DateTime.Now, closePath, "", "", "", "",
2025-03-18 11:35:10 +08:00
"", "", "", "", "", "", "", 1
);
2025-04-08 17:47:46 +08:00
// 建立新的播放清單
2025-05-19 13:02:09 +08:00
2025-04-08 17:47:46 +08:00
VideoPlayerForm.publicPlaylist = new List<SongData>();
VideoPlayerForm.playingSongList = new List<SongData>();
PrimaryForm.playedSongsHistory = new List<SongData>();
2025-04-08 17:47:46 +08:00
// 如果有正在播放的歌曲也加進去
2025-03-18 11:35:10 +08:00
if (VideoPlayerForm.Instance.currentPlayingSong != null)
{
VideoPlayerForm.playingSongList.Add(VideoPlayerForm.Instance.currentPlayingSong);
}
2025-05-19 13:02:09 +08:00
VideoPlayerForm.publicPlaylist.Add(closeSong);
2025-04-08 17:47:46 +08:00
// 將 CLOSE.MPG 加入播放清單
2025-03-18 11:35:10 +08:00
VideoPlayerForm.playingSongList.Add(closeSong);
2025-04-08 17:47:46 +08:00
// 清空使用者點播清單
2025-03-18 11:35:10 +08:00
PrimaryForm.userRequestedSongs = new List<SongData>();
2025-04-08 17:47:46 +08:00
// 隱藏 Overlay 的「下一首提示」
2025-03-18 11:35:10 +08:00
if (IsFormReady(OverlayForm.MainForm))
{
OverlayForm.MainForm.nextSongLabel.Visible = false;
}
2025-05-19 13:02:09 +08:00
VideoPlayerForm.Instance.PlayNextSong();
2025-03-18 11:35:10 +08:00
Console.WriteLine("已設置新的播放列表,包含當前歌曲和 CLOSE.MPG");
2025-05-19 13:02:09 +08:00
try
{
await HttpServer.RestartServer();
}
catch (Exception ex)
{
Console.WriteLine("RestartServer 發生錯誤: " + ex.Message);
}
2025-03-18 11:35:10 +08:00
}
else
{
Console.WriteLine($"錯誤: 找不到檔案 {closePath}");
}
});
}
});
2025-04-08 17:47:46 +08:00
// 更新狀態檔案為 CLOSE
2025-03-18 11:35:10 +08:00
UpdateStateFile(stateFilePath, "CLOSE");
continue;
}
2025-04-08 17:47:46 +08:00
// 指令為 "O":開啟系統,隱藏送客畫面
2025-03-18 11:35:10 +08:00
if (command.Trim().Equals("O", StringComparison.OrdinalIgnoreCase))
{
_ = SafeInvoke(PrimaryForm.Instance, () =>
{
PrimaryForm.Instance.HideSendOffScreen();
2025-05-13 14:03:14 +08:00
PrimaryForm.Instance.HideFireScreen();
2025-05-13 14:43:21 +08:00
string marqueeMessage= "歡迎使用網路版系統,與你共度美好時光。";
2025-05-13 14:03:14 +08:00
OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, Color.White);
2025-03-18 11:35:10 +08:00
});
2025-04-08 17:47:46 +08:00
VideoPlayerForm.publicPlaylist = new List<SongData>();
VideoPlayerForm.playingSongList = new List<SongData>();
2025-04-09 10:21:50 +08:00
VideoPlayerForm.Instance.PlayPublicPlaylist(); // 播放公播
2025-04-08 17:47:46 +08:00
// 更新狀態檔案為 OPEN
2025-03-18 11:35:10 +08:00
UpdateStateFile(stateFilePath, "OPEN");
continue;
}
2025-05-13 14:03:14 +08:00
if (command.Trim().Equals("F", StringComparison.OrdinalIgnoreCase))
{
_ = SafeInvoke(PrimaryForm.Instance, () =>
{
PrimaryForm.Instance.HideSendOffScreen();
PrimaryForm.Instance.ShowFireScreen();
VideoPlayerForm.Instance.Pause();
string marqueeMessage = "發生火災,請跟隨引導至逃生出口!!!";
OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, Color.Red);
});
// 更新狀態檔案(可選,若你要記錄狀態)
UpdateStateFile(stateFilePath, "PAUSE");
continue;
}
2025-03-18 11:35:10 +08:00
}
2025-04-08 17:47:46 +08:00
// 若 Overlay Form 準備好,嘗試顯示跑馬燈文字
2025-03-18 11:35:10 +08:00
if (IsFormReady(OverlayForm.MainForm))
{
string message = request.Trim();
string pattern = @"^(全部|\d{4})\((白色|紅色|綠色|黑色|藍色)\)-";
Match match = Regex.Match(message, pattern);
_ = SafeInvoke(OverlayForm.MainForm, () =>
{
if (match.Success)
{
2025-04-08 17:47:46 +08:00
// 若符合格式,顯示主跑馬燈文字
2025-03-18 11:35:10 +08:00
string marqueeMessage = message.Substring(match.Value.Length).Trim();
Color textColor = GetColorFromString(match.Groups[2].Value);
OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, textColor);
}
else
{
2025-04-08 17:47:46 +08:00
// 不符合格式,顯示在第二行跑馬燈
2025-03-18 11:35:10 +08:00
string marqueeMessage = "系統公告: " + message;
OverlayForm.MainForm.UpdateMarqueeTextSecondLine(marqueeMessage);
}
});
}
2025-04-08 17:47:46 +08:00
// 指令為 "exit":結束此連線
2025-03-18 11:35:10 +08:00
if (request.Trim().Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
}
Console.WriteLine("Connection closed.");
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
finally
{
2025-04-08 17:47:46 +08:00
// 關閉 listener
2025-03-18 11:35:10 +08:00
listener.Stop();
}
}
2025-04-08 17:47:46 +08:00
2025-03-18 11:35:10 +08:00
private Color GetColorFromString(string colorName)
{
switch (colorName)
{
case "白色":
return Color.White;
case "紅色":
return Color.Red;
case "綠色":
return Color.LightGreen;
case "黑色":
return Color.Black;
case "藍色":
return Color.LightBlue;
default:
return Color.Black;
}
}
private string GetHostNameSuffix()
{
string hostName = Dns.GetHostName();
return hostName.Length > 3 ? hostName.Substring(hostName.Length - 3) : hostName;
}
private string ReadStateFile(string filePath)
{
try
{
if (File.Exists(filePath))
{
string state = File.ReadAllText(filePath).Trim();
Console.WriteLine($"✅ State file read: {filePath} -> {state}");
return state;
}
else
{
Console.WriteLine("⚠️ State file not found. Creating new file with default state: OPEN");
UpdateStateFile(filePath, "OPEN");
return "OPEN";
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ Failed to read state file: {ex.Message}");
return "OPEN"; // 默認為 OPEN
}
}
private void UpdateStateFile(string filePath, string state)
{
try
{
File.WriteAllText(filePath, state);
Console.WriteLine($"✅ State file updated: {filePath} -> {state}");
}
catch (Exception ex)
{
Console.WriteLine($"❌ Failed to update state file: {ex.Message}");
}
}
}
}