一、更新内容
1.修改*Server类,以及承继关系。
2.增加IRunDevice的IServerProvider接口继承。
3.修复增加COM设备驱动可能造成的异常。
4.修复网络发送数据可能引发的异常。
5.完善协议驱动器。
6.修改协议驱动接口。
7.修改协议命令接口。
8.修复协议命令,抽象基类情况下的异常BUG。
9.增加协议接口GetPackageLength,数据交互更灵活。
10.修复一些BUG。
11.优化代码。
二、GetPackageLength接口的使用
这个接口主要的使用场景是:当协议中有请求发送数据长度的命令,例如先向服务器发送数据包长度命令,得到返回确定后,再发送实际数据包信息。在连接发送大块数据的时候,例如文件内容、序列化后的内容等,内容有可能包含协议的头和尾,会影响数据包的完整性。主要用于交互连续的较大数据块内容。
1.接口参数
1
2
3
4
5
6
7
8
|
/// <summary>
/// 获得应该接收的数据长度,如果当前接收的数据小于这个返回值,那么继续接收数据,直到大于等于这个返回长度。如果接收数据超时,则直接返回当前已经接收的数据。
/// </summary>
/// <param name="data">接收的数据</param>
/// <param name="channel">IO通道,用于返回确认数据</param>
/// <param name="readTimeout">返回读数据超时间隔时间</param>
/// <returns></returns>
public
abstract
int
GetPackageLength(
byte
[] data, IChannel channel,
ref
int
readTimeout);
|
2.接口使用
(1)设置配置参数
1
2
3
4
5
6
7
8
9
|
IServer server =
new
ServerFactory().CreateServer(
new
ServerConfig()
{
ServerName =
"服务1"
,
SocketMode = SocketMode.Tcp,
ControlMode = ControlMode.Loop,
CheckSameSocketSession =
false
,
StartCheckPackageLength =
true
,
//开启检测数据包长度
NetReceiveBufferSize = 20,
});
|
(2)接口代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public
override
int
GetPackageLength(
byte
[] data, IChannel channel,
ref
int
readTimeout)
{
if
(data ==
null
|| data.Length <= 0)
return
0;
readTimeout = 2000;
if
(CheckData(data))
{
try
{
int
length = BitConverter.ToInt32(
new
byte
[] {data[3], data[4], data[5], data[6]}, 0);
byte
[] okBytes = System.Text.Encoding.ASCII.GetBytes(
"ok"
);
int
num = channel.Write(okBytes);
if
(num > 0)
{
Console.WriteLine(
"返回数据"
);
}
return
length;
}
catch
(Exception)
{
return
0;
}
}
else
{
Console.WriteLine(
"校验错误"
);
return
0;
}
}
|
三、增加宿主程序(ServerSuperIO.Host)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
static
IServerFactory _serverFactory =
null
;
static
void
Main(
string
[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
ConsoleUtil.SetConsoleCtrlHandler(
new
ConsoleUtil.ControlCtrlDelegate(HandlerRoutine),
true
);
bool
success =
true
;
Console.WriteLine(
"正在初始化服务程序......"
);
IObjectBuilder builder =
new
TypeCreator();
_serverFactory =
new
ServerFactory();
try
{
GlobalConfig gc = GlobalConfigTool.Load();
foreach
(ServerSuperIO.Config.Server serverCfg
in
gc.Servers)
{
IServer server = _serverFactory.CreateServer(serverCfg.ServerConfig);
server.AddDeviceCompleted += server_AddDeviceCompleted;
server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
server.Start();
_serverFactory.AddServer(server);
foreach
(Config.Device devCfg
in
serverCfg.Devices)
{
try
{
IRunDevice runDev = builder.BuildUp<IRunDevice>(devCfg.AssemblyFile, devCfg.Instance);
runDev.DeviceParameter.DeviceID = devCfg.DeviceID;
runDev.DeviceDynamic.DeviceID = devCfg.DeviceID;
runDev.CommunicateType = devCfg.CommunicateType;
runDev.Initialize(devCfg.DeviceID);
if
(server.AddDevice(runDev) != devCfg.DeviceID)
{
Console.WriteLine(
"增加设备:"
+ devCfg.DeviceID +
" 失败!"
);
}
}
catch
(Exception ex)
{
Console.WriteLine(ex.Message);
continue
;
}
}
}
}
catch
(Exception ex)
{
success =
false
;
Console.WriteLine(ex.Message);
}
if
(success)
{
Console.WriteLine(
"初始化服务程序完成"
);
}
while
(
"exit"
== Console.ReadLine())
{
_serverFactory.RemoveAllServer();
break
;
}
}
private
static
bool
HandlerRoutine(
int
ctrlType)
{
if
(ctrlType == 0 || ctrlType == 2)
{
_serverFactory.RemoveAllServer();
}
return
false
;
}
}
|
四、增加配置工具(ServerSuperIO.Tool)
1.增加服务,如下图:
2.增加设备,如下图:
3.单击树型菜单,修改配置属性。
2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍
2.应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案
3.C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)
5.ServerSuperIO开源地址:https://github.com/wxzz/ServerSuperIO
物联网&集成技术(.NET) QQ群:54256083