Пример #1
0
        public void ProcessDataBuffer(object sender, ElapsedEventArgs elapsedEventArg) // To Process the DataBuffer
        {
            try
            {
                if (!Global.Is_dataprocessing) // If No dataprocessing beore the
                {
                    Global.Is_dataprocessing = true;
                    if (Data_Buffer != null) // If Data Buffer has some data elements
                    {
                        while (Data_Buffer.Count > 0)
                        {
                            DataClass data_item = Data_Buffer.Take(); // Takes the first data_item from the Data_Buffer Queue.

                            bool Is_IpExists = false;                 // Mark it as IP has not existed with data_item ip address

                            for (int i = 0; i <= Sockets_arr.Count - 1; i++)
                            {
                                if (Sockets_arr[i].IpAddr.Equals(data_item.Ip_addr))// If Ip_Exists In Connected IPs List
                                {
                                    Is_IpExists = true;
                                    break;
                                }
                            }
                            if (Is_IpExists)                                                                                                    // I.e If Ip Exists So that Only you need to process otherwise discard it
                            {
                                if (data_item.data.Length <= Global.Max_bytes && data_item.data.Length >= Global.Min_bytes)                     // Valid databyte
                                {
                                    short cal_checksum = Calculate_Checksum(data_item.data);                                                    // Pasing the Packet data to calculate checksum;

                                    PacketClass Packet_obj = DecodeRecievedBytestoPacket_Object(data_item.data);                                // Decoding bytes Array into Packetclass

                                    Packet_obj = Reverse_PacketBytes(Packet_obj);                                                               // Reversing the Packet_obj Except DataBytes.

                                    bool Is_valid = IsValidPacket(Packet_obj.Checksum, Packet_obj.Password, Packet_obj.DeviceId, cal_checksum); // Verifies Checksum, DeviceID
                                    if (Is_valid)                                                                                               // I.e Recieved Pakcet Is Valid
                                    {
                                        if (Packet_obj.packetHeader.SequenceEqual(Global.CommInit_pkt_header))
                                        {
                                            ProcessCommInitPacket(Packet_obj, data_item.Ip_addr); // Communication Initialization Packet
                                        }
                                        else if (Packet_obj.packetHeader.SequenceEqual(Global.CommInitResponse_pkt_header))
                                        {
                                            ProcessCommandResponsePacket(Packet_obj, data_item.Ip_addr); // Command Response Packet
                                        }
                                        else if (Packet_obj.packetHeader.SequenceEqual(Global.Event_pkt_header))
                                        {
                                            ProcessEventPacket(Packet_obj, data_item.Ip_addr);      // Event Packet
                                        }
                                        else if (Packet_obj.packetHeader.SequenceEqual(Global.Event_pkt_header))
                                        {
                                            ProcessModuleAlivePacket(Packet_obj, data_item.Ip_addr); // Module Alive Packet
                                        }
                                    }
                                }
                            }
                        }
                    }
                }// End of Isdataprocessing
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured on PrcoessDataBuffer:  " + ex.Message + time); // Logging the Execptions Ocuured
            }
        }
        private void HandleComm(object client)
        {
            TcpClient Tcp_Client = (TcpClient)client;

            using (NetworkStream stream = Tcp_Client.GetStream())
            {
                // get the ip address of the Client.
                IPEndPoint Endpoint = Tcp_Client.Client.RemoteEndPoint as IPEndPoint;

                IPAddress Ip_addr = Endpoint.Address;

                // Read the data from stream &  Insert it to Blocking Collection.
                Byte[] bytes    = new Byte[Global.Max_bytes_read];
                int    Read_len = 0;

                /*Open the Stream to Continuosly read the bytes from Client */
                while ((Read_len = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    try
                    {
                        // create  byte array based on the stream length.
                        byte[] msg = new byte[Read_len];

                        // Copy the bytes from bytes[] array up to i(220 Original data to msg byte[] array
                        Array.Copy(bytes, 0, msg, 0, Read_len);

                        // Create new dataobject,
                        bool IsValidClientIp = false;
                        for (int i = 0; i <= Sockets_arr.Count - 1; i++)
                        {
                            if (Sockets_arr[i].IpAddr.Equals(Ip_addr))
                            {
                                IsValidClientIp = true;
                                break;
                            }
                        }
                        if (IsValidClientIp)
                        {
                            DataClass data_item = new DataClass(Ip_addr, msg);

                            // Add the created dataobject to data_buffer(Blocking Collection)\
                            Data_Buffer.Add(data_item);
                        }
                        else
                        {
                            break;  // To Break the While Loop
                            //  Client_thread.Abort(); { termnating thread }
                        }
                    }

                    // stream.Write(msg,0, msg.Length);
                    catch (Exception ex)
                    {
                        if (ex != null)
                        {
                            DateTime now_time = DateTime.Now;
                            string   time     = Convert.ToString(now_time);
                            Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured While Reading Stream Data from the Client :  " + ex.Message + time); // Logging the Execptions Ocuured
                        }
                    }
                }
            }
        }