Open
Description
The following code is to create a TCP network component with configuration properties and configure the rule engine script to decode messages
///The following code is to create a TCP network component with configuration properties and configure the rule engine script to ///decode messages
var config =new Dictionary<string, object>();
config.Add("script", @"parser.Fixed(4).Handler(""function(buffer){
var buf = BytesUtils.Slice(buffer,1,4);
parser.Fixed(buffer.ReadableBytes).Result(buf);
}"").Handler(""function(buffer){parser.Result(buffer).Complete();}"")");
var network= serviceProvider.ServiceProvoider.GetInstances<INetworkProvider<TcpServerProperties>>().CreateNetwork(new TcpServerProperties
{
ParserType = PayloadParserType.Script,
PayloadType = PayloadType.String,
Host = "127.0.0.1",
Port = 322,
ParserConfiguration = config
});
network.StartAsync();
The following code is used to create a TCP network component with configuration properties and configure direct to decode messages
var network1 = serviceProvider.ServiceProvoider.GetInstances<INetworkProvider<TcpServerProperties>>().CreateNetwork(new TcpServerProperties
{
ParserType = PayloadParserType.Direct,
PayloadType = PayloadType.String,
Host = "127.0.0.1",
Port = 321
});
network1.StartAsync();
There are also two types of payload parser: fixedlength and delimited
Create tcp service for message processing
public class TcpService : TcpBehavior, ITcpService
{
private readonly IDeviceProvider _deviceProvider;
public TcpService(IDeviceProvider deviceProvider)
{
_deviceProvider = deviceProvider;
}
public override void Load(string clientId,TcpServerProperties tcpServerProperties)
{
var deviceStatus =_deviceProvider.IsConnected(clientId);
this.Parser.HandlePayload().Subscribe(buffer=>ParserBuffer(buffer));
}
public override void DeviceStatusProcess(DeviceStatus status, string clientId, TcpServerProperties tcpServerProperties)
{
//throw new NotImplementedException();
}
public async Task ParserBuffer(IByteBuffer buffer)
{
List<string> result = new List<string>();
while (buffer.ReadableBytes > 0)
{
result.Add(buffer.ReadString(this.Parser.GetNextFixedRecordLength(),
Encoding.GetEncoding("gb2312")));
}
// var str= buffer.ReadString(buffer.ReadableBytes, Encoding.GetEncoding("gb2312"));
var byteBuffer= Unpooled.Buffer();
byteBuffer.WriteString("\r\n", Encoding.UTF8);
byteBuffer.WriteString("processing complete", Encoding.UTF8);
await Sender.SendAndFlushAsync(byteBuffer);
buffer.Release();
// await Sender.SendAndFlushAsync("message received",Encoding.UTF8);
this.Parser.Close();
}
}
Metadata
Metadata
Assignees
Labels
No labels