예제 #1
0
        private void ScannerOnToolAlive(HcToolInfo info)
        {
            var lockTakenScan     = false;
            var lockTakenRegister = false;

            try
            {
                // lock
                Monitor.TryEnter(ScannedTools, Timeout, ref lockTakenScan);
                Monitor.TryEnter(RegisteredTools, Timeout, ref lockTakenRegister);
                // check lock taken
                if (!lockTakenScan || !lockTakenRegister)
                {
                    return;
                }

                // get tools
                var register = FindToolInfo(RegisteredTools, info.Mac);
                var scan     = FindToolInfo(ScannedTools, info.Mac);
                // check scan/register tool
                if (scan == null && register == null)
                {
                    // add scan tool
                    ScannedTools.Add(info);
                }
                else if (register != null)
                {
                    // event
                    ToolAlive?.Invoke(info);
                }
            }
            finally
            {
                // unlock
                if (lockTakenScan)
                {
                    Monitor.Exit(ScannedTools);
                }
                if (lockTakenRegister)
                {
                    Monitor.Exit(RegisteredTools);
                }
            }
        }
예제 #2
0
        private void ClientReceived(IAsyncResult ar)
        {
            // end receive point
            var endPoint = new IPEndPoint(IPAddress.Any, McPort);
            // receive data
            var recv = Client.EndReceive(ar, ref endPoint);

            // begin receive
            Client.BeginReceive(ClientReceived, null);
            // check length
            if (recv.Length < 4)
            {
                return;
            }
            // check header
            var id  = recv[0] << 8 | recv[1];
            var cmd = (ScanCommand)(recv[2] << 8 | recv[3]);

            // check scan acknowledge and id and length
            if (cmd != ScanCommand.ScanAck || id != TransactionId || recv.Length - 4 != HcToolInfo.Count)
            {
                return;
            }
            // set tool information
            var info = new HcToolInfo(recv.Skip(4));

            // check MD/MDTC
            if (info.ToolType == HcToolInfo.ToolModelType.None ||
                info.ToolType == HcToolInfo.ToolModelType.MD || info.ToolType == HcToolInfo.ToolModelType.MDT)
            {
                return;
            }
            // lock searched tool list
            if (!Monitor.TryEnter(SearchTools, _timeoutSpan))
            {
                return;
            }
            try
            {
                // find tool
                var tool = SearchTools.Find(x => x.Mac == info.Mac);
                // check find tool
                if (tool == null)
                {
                    // set timeout time
                    info.Timeout = ScanPeriod + 1000;
                    // debug console
                    Console.WriteLine($@"Attach Tool: {info.Ip}");
                    Console.WriteLine($@"Attach Tool: {info.Mac}");
                    // add tool
                    SearchTools.Add(info);
                    // attached
                    ToolAttach?.Invoke(info);
                }
                else
                {
                    // refresh tool
                    tool.ResetTime();
                    // alive
                    ToolAlive?.Invoke(tool);
                }
            }
            finally
            {
                // unlock
                Monitor.Exit(SearchTools);
            }
        }