示例#1
0
        /// <summary>
        /// 实例
        /// </summary>
        public static void Instance()
        {
            TimeInstruction instruction = new TimeInstruction();
            int length = instruction.FrameFixedLength;

            if (Hardware.Socket != null)
            {
                while (true)
                {
                    byte[] buffer = new byte[ushort.MaxValue];
                    EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                    try
                    {
                        int count = Hardware.Socket.ReceiveFrom(buffer, ref remoteEP);
                        if (count >= length)
                        {
                            byte[] values = new byte[count];
                            Array.Copy(buffer, values, count);
                            Hardware.InstructionHandler.Process(Hardware.Socket.LocalEndPoint, remoteEP, values);

                            if (Hardware.InstructionHandlers != null)
                            {
                                for (int i = 0; i < Hardware.InstructionHandlers.Count; i++)
                                {
                                    Hardware.InstructionHandlers[i].Process(Hardware.Socket.LocalEndPoint, remoteEP, values);
                                }
                            }
                        }
                        else
                        {
                            bool shutdown = true;
                            for (int i = 0; i < Variable.Shutdown.Length; i++)
                            {
                                if (buffer[i] != Variable.Shutdown[i])
                                {
                                    shutdown = false;
                                }
                            }
                            if (shutdown)
                            {
                                Hardware.Socket.Close();
                                break;
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                    }
                    finally
                    {
                    }
                }
            }
        }
        public string Time(Machine value)
        {
            string result = null;

            string ipValue = value.Ip;
            DateTime now = DateTime.Now;

            IPAddress ip = IPAddress.Parse(ipValue);
            IPEndPoint remoteEP = new IPEndPoint(ip, Variable.Port);
            List<AbstractInstruction> instructions = new List<AbstractInstruction>();

            TimeInstruction instruction = new TimeInstruction(now);
            instructions.Add(instruction);

            ProcessResult processResult = Terminal.ExecuteInstruction(remoteEP, instructions);
            if (processResult != null)
            {
                result = processResult.Message;
            }

            return result;
        }
示例#3
0
        /// <summary>
        /// 执行指令
        /// </summary>
        /// <param name="remoteEP">IP</param>
        /// <param name="instructions">指令集合</param>
        /// <returns>指令集合</returns>
        public static ProcessResult ExecuteInstruction(EndPoint remoteEP, List<AbstractInstruction> instructions)
        {
            ProcessResult result = null;

            if (instructions != null)
            {
                int times = 0;
                while (times < Variable.ArqTimes)
                {
                    Socket socket = null;
                    try
                    {
                        EndPoint localEP = new IPEndPoint(IPAddress.Any, 0);
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        socket.Bind(localEP);
                        socket.SendTimeout = Variable.ArqTimeout;
                        socket.ReceiveTimeout = Variable.ArqTimeout;

                        byte[] sendBuffer = null;

                        for (int i = 0; i < instructions.Count; i++)
                        {
                            AbstractInstruction instruction = instructions[i];
                            sendBuffer = instruction.Encode();
                            socket.SendTo(sendBuffer, remoteEP);
                            IPEndPoint localIP = (IPEndPoint)socket.LocalEndPoint;
                            IPEndPoint remoteIP = (IPEndPoint)remoteEP;
                            EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(localIP, remoteIP, Direction.Send, sendBuffer);
                        }

                        TimeInstruction timeInstruction = new TimeInstruction();
                        int length = timeInstruction.FrameFixedLength;
                        List<InstructionHandler> instructionHandlers = new List<InstructionHandler>();
                        while (true)
                        {
                            byte[] receiveBuffer = new byte[ushort.MaxValue];
                            remoteEP = new IPEndPoint(IPAddress.Any, 0);
                            int count = socket.ReceiveFrom(receiveBuffer, ref remoteEP);
                            if (count >= length)
                            {
                                byte[] values = new byte[count];
                                Array.Copy(receiveBuffer, values, count);
                                result = Hardware.InstructionHandler.Process(socket.LocalEndPoint, remoteEP, values);

                                if (Hardware.InstructionHandlers != null)
                                {
                                    for (int i = 0; i < Hardware.InstructionHandlers.Count; i++)
                                    {
                                        Hardware.InstructionHandlers[i].Process(socket.LocalEndPoint, remoteEP, values);
                                    }
                                }
                            }

                            if (result != null)
                            {
                                break;
                            }
                        }

                        times = Variable.ArqTimes;
                    }
                    catch (Exception exception)
                    {
                        times++;
                        EnvironmentalMonitor.Support.Resource.Variable.Logger.Log(exception);
                    }
                    finally
                    {
                        if (socket != null)
                        {
                            socket.Close();
                        }
                    }
                }
            }

            if (result == null)
            {
                result = new ProcessResult();
                result.Done = false;
                result.Message = "访问检测仪出现错误!";
            }

            return result;
        }