70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using System;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using YunDa.Server.ISMSTcp.Controllers;
|
|||
|
|
using YunDa.Server.ISMSTcp.Interfaces;
|
|||
|
|
using YunDa.SOMS.ExternalInteraction.DataTransferObject.InspectionEquipment;
|
|||
|
|
|
|||
|
|
namespace YunDa.Server.ISMSTcp.Services
|
|||
|
|
{
|
|||
|
|
public class ZzTcpService
|
|||
|
|
{
|
|||
|
|
private readonly ILogger<QueryController> _logger;
|
|||
|
|
private ITcpClient _tcpClient = null;
|
|||
|
|
|
|||
|
|
public ZzTcpService(ILogger<QueryController> logger)
|
|||
|
|
{
|
|||
|
|
_logger = logger;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void Init(ITcpClient tcpClient)
|
|||
|
|
{
|
|||
|
|
_tcpClient = tcpClient;
|
|||
|
|
}
|
|||
|
|
public async Task<(bool Success, string ErrorMessage)> SendTcpMessageAsync(string message, CancellationToken cancellationToken)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
// 使用QueryService的TCP客户端发送消息
|
|||
|
|
// 注意:这里直接通过ITcpClient发送,不需要等待响应
|
|||
|
|
var tcpClient = GetTcpClientFromQueryService();
|
|||
|
|
if (tcpClient == null)
|
|||
|
|
{
|
|||
|
|
return (false, "TCP客户端未初始化");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!tcpClient.IsConnected)
|
|||
|
|
{
|
|||
|
|
return (false, "TCP连接未建立");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var sendResult = await tcpClient.SendMessageAsync(message, cancellationToken);
|
|||
|
|
|
|||
|
|
if (!sendResult)
|
|||
|
|
{
|
|||
|
|
return (false, "TCP消息发送失败");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return (true, string.Empty);
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "发送TCP消息时发生异常");
|
|||
|
|
return (false, ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 从QueryService获取TCP客户端
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns>TCP客户端实例</returns>
|
|||
|
|
private ITcpClient GetTcpClientFromQueryService()
|
|||
|
|
{
|
|||
|
|
return _tcpClient;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|