From 5d9c24c02c8685fd931e0ae93e54c811726af8c6 Mon Sep 17 00:00:00 2001
From: wangxinhui <wangxinhui@hnkhzn.com>
Date: 星期一, 20 四月 2026 13:55:42 +0800
Subject: [PATCH] 模具库&综合库代码更新
---
代码管理/Integ/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs | 161 +++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 143 insertions(+), 18 deletions(-)
diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/Integ/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
similarity index 64%
rename from "\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
rename to "\344\273\243\347\240\201\347\256\241\347\220\206/Integ/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
index 202d892..5a1d072 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/Integ/WCS/WCSServers/WIDESEAWCS_Tasks/SocketServer/TcpSocketServer.Messaging.cs"
@@ -2,6 +2,7 @@
using System.Text;
using System.Text.Json;
using System.IO;
+using WIDESEAWCS_Tasks.StackerCraneJob;
namespace WIDESEAWCS_Tasks.SocketServer
{
@@ -18,7 +19,7 @@
/// <param name="cancellationToken">可用于取消客户端处理操作的取消令牌。如果请求取消,方法将立即终止处理。</param>
/// <param name="stackerState">表示与客户端关联的机器人起重机的当前状态对象。用于为消息处理和事件调用提供上下文。</param>
/// <returns>表示处理客户端连接的异步操作的任务。当客户端断开连接或请求取消时任务完成。</returns>
- public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState stackerState)
+ public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_BTI stackerState)
{
using (client)
using (NetworkStream networkStream = client.GetStream())
@@ -56,18 +57,14 @@
string messageLower = message.ToLowerInvariant();
- if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken))
- {
- continue;
- }
- if (MessageReceived != null)
+ if (MessageReceivedBTI != null)
{
try
{
// 判断是否为 JSON 格式
bool isJsonFormat = TryParseJsonSilent(message);
- _ = MessageReceived.Invoke(message, isJsonFormat, client, stackerState);
+ _ = MessageReceivedBTI.Invoke(message, isJsonFormat, client, stackerState);
}
catch { }
}
@@ -77,7 +74,135 @@
{
try { localCts?.Cancel(); localCts?.Dispose(); } catch { }
RemoveClient(clientId);
- try { _ = RobotReceived.Invoke(clientId); } catch { }
+ try { _ = StackerReceivedBTI.Invoke(clientId); } catch { }
+ }
+ }
+ }
+
+ public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_CP01 stackerState)
+ {
+ using (client)
+ using (NetworkStream networkStream = client.GetStream())
+ using (StreamReader reader = new(networkStream, _textEncoding, false, 1024, true))
+ using (StreamWriter writer = new(networkStream, _textEncoding, 1024, true) { AutoFlush = true })
+ {
+ CancellationTokenSource? localCts = null;
+ if (_options.EnableHeartbeat || _options.IdleTimeoutSeconds > 0)
+ {
+ localCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
+ }
+
+ try
+ {
+ while (!cancellationToken.IsCancellationRequested && client.Connected)
+ {
+ string? message;
+ try
+ {
+ var ct = localCts?.Token ?? cancellationToken;
+ message = await ReceiveFullMessageAsync(networkStream, _textEncoding, ct);
+ //message = await reader.ReadLineAsync().WaitAsync(ct);
+ }
+ catch (OperationCanceledException)
+ {
+ break;
+ }
+
+ if (message == null)
+ {
+ break;
+ }
+
+ UpdateClientStatus(clientId, message);
+
+ string messageLower = message.ToLowerInvariant();
+
+ //if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken))
+ //{
+ // continue;
+ //}
+
+ if (MessageReceivedCP01 != null)
+ {
+ try
+ {
+ // 判断是否为 JSON 格式
+ bool isJsonFormat = TryParseJsonSilent(message);
+ _ = MessageReceivedCP01.Invoke(message, isJsonFormat, client, stackerState);
+ }
+ catch { }
+ }
+ }
+ }
+ finally
+ {
+ try { localCts?.Cancel(); localCts?.Dispose(); } catch { }
+ RemoveClient(clientId);
+ try { _ = StackerReceivedCP01.Invoke(clientId); } catch { }
+ }
+ }
+ }
+
+ public async Task HandleClientAsync(TcpClient client, string clientId, CancellationToken cancellationToken, StackerSocketState_CP02 stackerState)
+ {
+ using (client)
+ using (NetworkStream networkStream = client.GetStream())
+ using (StreamReader reader = new(networkStream, _textEncoding, false, 1024, true))
+ using (StreamWriter writer = new(networkStream, _textEncoding, 1024, true) { AutoFlush = true })
+ {
+ CancellationTokenSource? localCts = null;
+ if (_options.EnableHeartbeat || _options.IdleTimeoutSeconds > 0)
+ {
+ localCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
+ }
+
+ try
+ {
+ while (!cancellationToken.IsCancellationRequested && client.Connected)
+ {
+ string? message;
+ try
+ {
+ var ct = localCts?.Token ?? cancellationToken;
+ message = await ReceiveFullMessageAsync(networkStream, _textEncoding, ct);
+ //message = await reader.ReadLineAsync().WaitAsync(ct);
+ }
+ catch (OperationCanceledException)
+ {
+ break;
+ }
+
+ if (message == null)
+ {
+ break;
+ }
+
+ UpdateClientStatus(clientId, message);
+
+ string messageLower = message.ToLowerInvariant();
+
+ //if (TryHandleRegister(messageLower, message, clientId, networkStream, cancellationToken))
+ //{
+ // continue;
+ //}
+
+ if (MessageReceivedCP02 != null)
+ {
+ try
+ {
+ // 判断是否为 JSON 格式
+ bool isJsonFormat = TryParseJsonSilent(message);
+ _ = MessageReceivedCP02.Invoke(message, isJsonFormat, client, stackerState);
+ }
+ catch { }
+ }
+ }
+ }
+ finally
+ {
+ try { localCts?.Cancel(); localCts?.Dispose(); } catch { }
+ RemoveClient(clientId);
+ try { _ = StackerReceivedCP02.Invoke(clientId); } catch { }
}
}
}
@@ -96,21 +221,21 @@
/// <returns>如果消息被识别并作为注册请求处理,则返回 true;否则返回 false。</returns>
private bool TryHandleRegister(string messageLower, string message, string clientId, NetworkStream networkStream, CancellationToken cancellationToken)
{
- if (!messageLower.StartsWith("register,"))
- {
- return false;
- }
+ //if (!messageLower.StartsWith("register,"))
+ //{
+ // return false;
+ //}
- string deviceId = message.Substring("register,".Length).Trim();
- if (!string.IsNullOrEmpty(deviceId))
- {
+ //string deviceId = message.Substring("register,".Length).Trim();
+ //if (!string.IsNullOrEmpty(deviceId))
+ //{
lock (_syncRoot)
{
- _deviceBindings[deviceId] = clientId;
+ _deviceBindings[clientId] = clientId;
}
- _ = WriteToClientAsync(clientId, networkStream, $"Registered,{deviceId}", cancellationToken);
- }
+ _ = WriteToClientAsync(clientId, networkStream, $"Registered,{clientId}", cancellationToken);
+ //}
return true;
}
--
Gitblit v1.9.3