ICommunication 接口 - Snet Docs

📙 ICommunication 接口

命名空间: Snet.Core | 接口: ICommunication(客户端),ICommunicationService(服务器)

用于客户端和服务器端消息交换的通信接口。ICommunicationICommunicationService标记接口——它们自身不声明任何方法。所有功能均从底层通信合约继承。

接口定义

ICommunication(客户端)

ICommunication 是一个标记接口,继承自 14 个底层接口:

public interface ICommunication : IOn, IOff, ISend, ISendWait, IGetObject, IGetStatus,
    IEvent, ICreateInstance, ILog, IGetParam, ILanguage, IDisposable, IAsyncDisposable
{
    // 标记接口 — 自身无任何方法。
    // 所有通信方法均由继承的接口提供。
}

ICommunicationService(服务器)

ICommunicationService 是一个标记接口,继承自 12 个底层接口:

public interface ICommunicationService : IOn, IOff, ISendService, IGetObject,
    IGetStatus, IRemove, IEvent, ICreateInstance, ILog, ILanguage, IDisposable,
    IAsyncDisposable
{
    // 标记接口 — 自身无任何方法。
    // 所有通信方法均由继承的接口提供。
}

概述

接口 角色 继承的发送接口 使用场景
ICommunication 客户端 ISend, ISendWait 向远程端点发送数据和接收数据
ICommunicationService 服务器 ISendService 路由传入消息并分发响应

继承接口参考

接口 用途
IOn 事件订阅
IOff 事件取消订阅
ISend 发送即忘(客户端)
ISendWait 发送并等待响应(客户端,请求/应答)
ISendService 服务端发送
IGetObject 对象检索
IGetStatus 状态检索
IEvent 事件处理
ICreateInstance 实例创建
ILog 日志记录
IGetParam 参数检索(仅客户端)
IRemove 移除(仅服务端)
ILanguage 本地化
IDisposable 同步资源清理
IAsyncDisposable 异步资源清理

快速入门

客户端通信

ICommunicationISend 继承 Send / SendAsync(发送即忘),从 ISendWait 继承 SendWait / SendWaitAsync(请求/应答):

using Snet.Core;

ICommunication comm = GetCommunicationClient();

byte[] command = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x01 };

// 发送即忘 — 同步
var result = comm.Send(command);

// 发送即忘 — 异步
var resultAsync = await comm.SendAsync(command, CancellationToken.None);

// 发送并等待响应 — 同步
var response = comm.SendWait(command, CancellationToken.None);

// 发送并等待响应 — 异步
var responseAsync = await comm.SendWaitAsync(command, CancellationToken.None);

服务器端通信

ICommunicationServiceISendService 继承其发送方法:

using Snet.Core;

ICommunicationService service = GetCommunicationService();

byte[] notification = System.Text.Encoding.UTF8.GetBytes("服务器通知");

// 服务端同步发送
var result = service.Send(notification);

// 服务端异步发送
var resultAsync = await service.SendAsync(notification, CancellationToken.None);

方法参考

Send — 发送即忘(来自 ISend)

发送数据且不期望响应。适用于单向命令、通知或不需要确认的写入操作。

// 同步 — 发送即忘
comm.Send(new byte[] { 0x01, 0x02, 0x03 });

// 异步 — 发送即忘
await comm.SendAsync(data, CancellationToken.None);

SendWait — 请求/应答(来自 ISendWait)

发送数据并等待响应。用于需要远程设备应答的查询-响应模式。

// 同步 — 发送并等待
var response = comm.SendWait(requestData, CancellationToken.None);

// 异步 — 发送并等待
var response = await comm.SendWaitAsync(requestData, CancellationToken.None);

ISendService — 服务端推送

服务端发送方法,用于向已连接的客户端推送消息或响应,继承自 ISendService

// 同步 — 服务端推送
service.Send(alertData);

// 异步 — 服务端推送
await service.SendAsync(alertData, CancellationToken.None);

通信流程

客户端 (ICommunication)                    服务器 (ICommunicationService)
     |  Send(data) / SendAsync(data, ct) -------->|
     |                                            |
     |  SendWait(data, ct) /                      |
     |  SendWaitAsync(data, ct) ---------------->  |
     |<------- 响应 ------------------------------|
     |                                            |
     |<--- Send(data) / SendAsync(data, ct) ------|

与 IDaq 的集成

ICommunicationICommunicationService 被组合到 IDaq 中,这意味着每个协议驱动和中间件组件都支持这些方法:

IDaq daq = new ModbusOperate(new ModbusData.Basics { ... });
ICommunication comm = daq;       // 向上转型 — 继承 Send、SendAsync、SendWait、SendWaitAsync
ICommunicationService svc = daq; // 向上转型 — 继承服务端 Send、SendAsync

参见