xinjiang 发表于 2016-10-28 09:11:55

ServerSuperIO 2.4(SSIO)发布,物联网通讯框架

欢迎加入运维网交流群:263444886
  一、更新内容
  1.修改*Server类,以及承继关系。
2.增加IRunDevice的IServerProvider接口继承。
3.修复增加COM设备驱动可能造成的异常。
4.修复网络发送数据可能引发的异常。
5.完善协议驱动器。
  6.修改协议驱动接口。
7.修改协议命令接口。
8.修复协议命令,抽象基类情况下的异常BUG。
  9.增加协议接口GetPackageLength,数据交互更灵活。
10.修复一些BUG。
11.优化代码。
  二、GetPackageLength接口的使用
  这个接口主要的使用场景是:当协议中有请求发送数据长度的命令,例如先向服务器发送数据包长度命令,得到返回确定后,再发送实际数据包信息。在连接发送大
块数据的时候,例如文件内容、序列化后的内容等,内容有可能包含协议的头和尾,会影响数据包的完整性。主要用于交互连续的较大数据块内容。

  1.接口参数
/// <summary>
      /// 获得应该接收的数据长度,如果当前接收的数据小于这个返回值,那么继续接收数据,直到大于等于这个返回长度。如果接收数据超时,则直接返回当前已经接收的数据。
      /// </summary>
      /// <param name=&quot;data&quot;>接收的数据</param>
      /// <param name=&quot;channel&quot;>IO通道,用于返回确认数据</param>
      /// <param name=&quot;readTimeout&quot;>返回读数据超时间隔时间</param>
      /// <returns></returns>
      public abstract int GetPackageLength(byte[] data, IChannel channel, ref int readTimeout);  2.接口使用
  (1)设置配置参数
IServer server = new ServerFactory().CreateServer(new ServerConfig()
         {
               ServerName = &quot;服务1&quot;,
               SocketMode = SocketMode.Tcp,
               ControlMode = ControlMode.Loop,
               CheckSameSocketSession = false,
               StartCheckPackageLength = true, //开启检测数据包长度
               NetReceiveBufferSize = 20,
         });  (2)接口代码
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, data, data, data}, 0);
            byte[] okBytes = System.Text.Encoding.ASCII.GetBytes(&quot;ok&quot;);
            int num = channel.Write(okBytes);
            if (num > 0)
            {
                Console.WriteLine(&quot;返回数据&quot;);
            }
            return length;
      }
      catch (Exception)
      {
            return 0;
      }
    }
    else
    {
      Console.WriteLine(&quot;校验错误&quot;);
      return 0;
    }
}  三、增加宿主程序(ServerSuperIO.Host)
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(&quot;正在初始化服务程序......&quot;);
            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(&quot;增加设备:&quot; + devCfg.DeviceID + &quot; 失败!&quot;);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            continue;
                        }
                  }
                }
            }
            catch (Exception ex)
            {
                success = false;
                Console.WriteLine(ex.Message);
            }
            if (success)
            {
                Console.WriteLine(&quot;初始化服务程序完成&quot;);
            }
            while (&quot;exit&quot; == 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.单击树型菜单,修改配置属性。
页: [1]
查看完整版本: ServerSuperIO 2.4(SSIO)发布,物联网通讯框架