Example #1
0
        public static MacTcpUdp GetRelation(string address, int port)
        {
            ///throw new Exception("haha");

            MacTcpUdp value = null;

            ///锁住macRelation
            lock (macRelation)
            {
                foreach (var key in macRelation.Keys)
                {
                    try
                    {
                        var item = macRelation[key];
                        if (item != null && !string.IsNullOrEmpty(item.Address) && item.port > 0)
                        {
                            if (item.Address.Equals(address) && item.port.Equals(port))
                            {
                                value     = macRelation[key];
                                value.mac = key;
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Func <bool> func = () => true;
                        LogHelper.LogFilter(func, ex.StackTrace + "    " + ex.Message);
                        return(value);
                    }
                }
                return(value);
            }
        }
Example #2
0
        /// <summary>
        /// 根据
        /// </summary>
        /// <param name="mac"></param>
        /// <returns></returns>
        public static MacTcpUdp GetRelation(string mac)
        {
            MacTcpUdp value = null;

            if (!string.IsNullOrEmpty(mac))
            {
                var bvalue = macRelation.TryGetValue(mac, out value);
            }
            return(value);
        }
Example #3
0
        private static void SendMsgToClient(byte[] ds, MacTcpUdp mu)
        {
            var rdata = StrHelper.GetHexStr(ds);
            var mac   = mu.mac;

            if (!string.IsNullOrEmpty(mac))
            {
                if (mac.Equals("98d8634a0344", StringComparison.OrdinalIgnoreCase))
                {
                    LogHelper.LogFilter(true, "返回=>" + rdata);
                }
            }
            else
            {
                mac = string.Empty;
            }
            var bdata = Encoding.UTF8.GetBytes(JsonHelper <Status <string> > .GetJson(new Status <string>()
            {
                code = "1", data = rdata, mac = mac, msg = "", commandType = "1"
            }));
            var queue = mu.clients;
            ///能否正常发通UDP
            var flag = true;

            while (queue != null && queue.Count > 0)
            {
                var el = queue.Dequeue();
                if (el != null && !(el.time.AddSeconds(10) < DateTime.Now))
                {
                    try
                    {
                        if (mu.udpServer != null)
                        {
                            LogHelper.Info("设备mac:" + mac + " 数据" + rdata);
                            var iet = (IPEndPoint)el.point;
                            if (!iet.Address.Equals(IPAddress.Any))
                            {
                                try
                                {
                                    if (flag)
                                    {
                                        mu.udpServer.SendTo(bdata, el.point);
                                    }
                                }
                                catch (SocketException ex)
                                {
                                    flag = false;
                                    queue.Clear();
                                    //LogHelper.Info("TCP:378:   " + ex.Message);

                                    LogHelper.LogFilter(true, ex.StackTrace);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("TCP:395" + ex.Message);


                        //Func<bool> func1 = () => true;
                        //var msgx = "TCP:395" + ex.Message;
                        //LogHelper.LogFilter(func1, msgx);

                        LogHelper.LogFilter(true, ex.StackTrace);
                    }
                }
            }
        }