Exemple #1
0
 static void onSocket(Socket socket)
 {
     Way.Lib.NetStream client = null;
     try
     {
         client             = new Way.Lib.NetStream(socket);
         client.ReadTimeout = 0;
         if (ServerCert != null)
         {
             var sslts = new SslStream(client.InnerStream, false, new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback));
             sslts.AuthenticateAsServer(ServerCert, true, NetClient.SSLProtocols, false);
             client.InnerStream = sslts;
         }
         //data里面前面四个字节包含了长度
         client.Write(data);
         client.ReadInt();
     }
     catch (SocketException)
     {
     }
     catch (Exception ex)
     {
         Logger.LogError(ex, ex.Message);
     }
     finally
     {
         client?.Dispose();
     }
 }
Exemple #2
0
        static void onSocket(Socket socket)
        {
            Way.Lib.NetStream client = null;
            try
            {
                client             = new Way.Lib.NetStream(socket);
                client.ReadTimeout = 0;
                if (ServerCert != null)
                {
                    var sslts = new SslStream(client.InnerStream, false, new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback));
                    sslts.AuthenticateAsServer(ServerCert, true, NetClient.SSLProtocols, false);
                    client.InnerStream = sslts;
                }

                var flag = client.ReadInt();
                if (flag == 0 || flag == 1179010630)//健康检查
                {
                    client.Write(Encoding.UTF8.GetBytes("ok"));
                }
                else if (flag == 1)//get key
                {
                    //data里面前面四个字节包含了长度,所以不用先Write(数据长度)
                    client.Write(data);
                    client.ReadInt();
                }
                else if (flag == 2)                         //disable token
                {
                    var expireTimeLong = client.ReadLong(); //utc过期时间
                    var len            = client.ReadInt();
                    var token          = System.Text.Encoding.UTF8.GetString(client.ReceiveDatas(len));
                    _ClientManager.DisableToken(token, expireTimeLong);
                    client.Write(true);
                }
                else if (flag == 888)//for test
                {
                    client.Write(4);
                    client.Write(888);
                    client.ReadInt();
                }
                else if (flag == 999)
                {
                    //data里面前面四个字节包含了长度,所以不用先Write(数据长度)
                    client.Write(data);
                    _ClientManager.AddClient(client).Handle();
                }
            }
            catch (SocketException)
            {
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, ex.Message);
            }
            finally
            {
                client?.Dispose();
            }
        }
        public override void ReadValue(Command command, Action <int, PointValueType, object> onValueReceive)
        {
            string[] deviceAddressInfo = command.DeviceAddress.Split('/');
            var      deviceIP          = deviceAddressInfo[0];
            var      port = int.Parse(deviceAddressInfo[1]);

            PointDescription[] points = new PointDescription[command.Points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                points[i] = new PointDescription(command.Points[i], i);
            }

            //找出相邻的点
            List <PointGroup> groups = new List <PointGroup>();

            points = (from m in points
                      orderby m.Function, m.Address
                      select m).ToArray();
            PointGroup currentGroup = new PointGroup();

            groups.Add(currentGroup);
            foreach (var point in points)
            {
                if (currentGroup.Points.Count == 0)
                {
                    currentGroup.Function     = point.Function;
                    currentGroup.StartAddress = point.Address;
                    currentGroup.Points.Add(point);
                }
                else
                {
                    if (point.Address - currentGroup.Points.Last().Address == 1 && point.Function == currentGroup.Function)
                    {
                        currentGroup.Points.Add(point);
                    }
                    else
                    {
                        currentGroup = new PointGroup();
                        groups.Add(currentGroup);
                        currentGroup.Function     = point.Function;
                        currentGroup.StartAddress = point.Address;
                        currentGroup.Points.Add(point);
                    }
                }
            }


            Way.Lib.NetStream client = new Way.Lib.NetStream(deviceIP, port);
            readValues(client, groups);
            //把值通过stream发送出去
            for (int i = 0; i < points.Length; i++)
            {
                onValueReceive(points[i].Index, PointValueType.Short, points[i].Value);
            }
            client.Dispose();
        }
        public override bool[] WriteValue(Command command)
        {
            string[] deviceAddressInfo = command.DeviceAddress.Split('/');
            var      deviceIP          = deviceAddressInfo[0];
            var      port = int.Parse(deviceAddressInfo[1]);

            PointDescription[] points = new PointDescription[command.Points.Length];
            for (int i = 0; i < points.Length; i++)
            {
                points[i]       = new PointDescription(command.Points[i], i);
                points[i].Value = Convert.ToInt16(Convert.ToDouble(command.Values[i]));
                if (points[i].Function == FunctionCode.ReadCoilStatus)
                {
                    points[i].Function = FunctionCode.WriteCoilStatus;
                }
                if (points[i].Function == FunctionCode.ReadHoldingRegister)
                {
                    points[i].Function = FunctionCode.WriteHoldingRegister;
                }
            }

            Way.Lib.NetStream client = new Way.Lib.NetStream(deviceIP, port);
            bool[]            result = new bool[points.Length];
            foreach (var point in points)
            {
                try
                {
                    WriteValuePackage package = new WriteValuePackage(point.Function);
                    package.Address = point.Address;
                    package.Value   = point.Value;
                    var cmd = package.BuildCommand();
                    client.Write(cmd);

                    result[point.Index] = package.ParseAnswer((len) =>
                    {
                        return(client.ReceiveDatas(len));
                    });
                }
                catch
                {
                    client = new Way.Lib.NetStream(deviceIP, port);
                }
            }
            client.Dispose();
            return(result);
        }
        public override bool CheckDeviceExist(Command command)
        {
            string[] deviceAddressInfo = command.DeviceAddress.Split('/');
            var      deviceIP          = deviceAddressInfo[0];
            var      port = int.Parse(deviceAddressInfo[1]);

            try
            {
                Way.Lib.NetStream client = new Way.Lib.NetStream(deviceIP, port);

                client.Dispose();
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Exemple #6
0
 public ConnectionHandler(Socket socket)
 {
     _client        = new Way.Lib.NetStream(socket);
     socket.NoDelay = true;
 }