示例#1
0
        public short Calc_ChecksumOfPacketObj(PacketClass PktObj)
        {
            short checksum = 0;

            foreach (byte chData in PktObj.DeviceId)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Password)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.TypeId)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Packet_sno)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Continue_Sts)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Data)
            {
                checksum += chData;
            }
            /* Condition should be added if and only if checksum is greater than 65535(2bytes).*/
            //   checksum &= 0xff;
            return(checksum);
        }
示例#2
0
        public PacketClass DecodeRecievedBytestoPacket_Object(byte[] data)
        {
            try
            {
                PacketClass obj = new PacketClass();
                /* Need to place the bytes[] into Object */
                /* Get the data bytes length */
                int data_bytes_len           = data.Length - Global.Packet_overhead_len; // Packet_overhead_len is Fixed for all packets but data changes.
                int Packet_Checksum_StartIdx = Global.Packet_data_start_Idx + data_bytes_len;
                /* Frame Packet Object from byte[] data */

                Array.Copy(data, Global.Packet_Header_start_Idx, obj.packetHeader, 0, Global.Packet_Header_len);           // Copy PacketHeader
                Array.Copy(data, Global.PacketLength_start_Idx, obj.Packet_Length, 0, Global.PacketLength_len);            // Copy Length of Packet
                Array.Copy(data, Global.Packet_DeviceID_start_Idx, obj.DeviceId, 0, Global.Packet_DeviceID_len);           // Copy Device Id of Packet
                Array.Copy(data, Global.Packet_Password_start_Idx, obj.Password, 0, Global.Packet_Password_len);           // Copy System Password of Packet
                Array.Copy(data, Global.Packet_TypeId_start_Idx, obj.TypeId, 0, Global.Packet_TypeId_len);                 // Copy  TYpeID of Packet
                Array.Copy(data, Global.Packet_Sno_start_Idx, obj.Packet_sno, 0, Global.Packet_Sno_len);                   // Copy Packet SNo of Packet
                Array.Copy(data, Global.Packet_ContinueSts_start_Idx, obj.Continue_Sts, 0, Global.Packet_ContinueSts_len); // Copy Continue_Sts of th Packet
                Array.Copy(data, Global.Packet_data_start_Idx, obj.Data, 0, data_bytes_len);                               // Copy Data  of Packet

                Array.Copy(data, Packet_Checksum_StartIdx, obj.Checksum, 0, Global.Packet_Chcksum_len);                    // Copy Data  of Packet

                return(obj);
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Occured in DecodeRecievedBytestoPacket_Object:  " + ex.Message + time); // Logging the Execptions Ocuured
                return(null);
            }
        }
示例#3
0
        public short Calc_ChecksumOfPacketObj(PacketClass PktObj)
        {
            short checksum = 0;

            foreach (byte chData in PktObj.DeviceId)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Password)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.TypeId)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Packet_sno)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Continue_Sts)
            {
                checksum += chData;
            }
            foreach (byte chData in PktObj.Data)
            {
                checksum += chData;
            }
            checksum &= 0xff;
            return(checksum);
        }
示例#4
0
        private byte[] FrameCommunicationAck(PacketClass PacketObj)
        {
            try
            {
                PacketClass AckPacket = new PacketClass();
                AckPacket.packetHeader  = Global.CommandResponse_pkt_header;
                AckPacket.Packet_Length = PacketObj.Packet_Length;
                AckPacket.DeviceId      = PacketObj.DeviceId;
                AckPacket.Password      = PacketObj.Password;
                AckPacket.TypeId        = Global.CommInitAckPkt_TypeID;
                AckPacket.Packet_sno    = Global.CommInitAckPkt_SNo;
                AckPacket.Continue_Sts  = Global.CommInitAckContinue_Sts;
                AckPacket.Data          = new byte[] { };             // Empty Data Feild
                short checksum = Calc_ChecksumOfPacketObj(AckPacket);
                AckPacket.Checksum = BitConverter.GetBytes(checksum); // Getting Bytes from checksum..

                byte[] Final_bytes = ObjectToByteArray(AckPacket);    // Serializing Object to Byte Array.

                return(Final_bytes);                                  // Returning Serialozed Byte array.
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured While FrameCommunicationAck:  " + ex.Message + time); // Logging the Execptions Ocuured
                return(null);
            }
        }
示例#5
0
        public PacketClass Reverse_PacketBytes(PacketClass Packet_obj)
        {
            Array.Reverse(Packet_obj.packetHeader); // Reversing the Packet Header Bytes
            Array.Reverse(Packet_obj.Packet_Length);
            Array.Reverse(Packet_obj.DeviceId);
            Array.Reverse(Packet_obj.Password);
            Array.Reverse(Packet_obj.TypeId);
            Array.Reverse(Packet_obj.Packet_sno);
            Array.Reverse(Packet_obj.Continue_Sts);
            Array.Reverse(Packet_obj.Checksum);

            return(Packet_obj);
        }
示例#6
0
        public void ProcessCommInitPacket(PacketClass PacketObj, IPAddress Ipaddr)
        {
            /* Communication Init Packet */
            bool IsDeviceExists = false;
            long ParamDeviceId  = Convert.ToInt64(PacketObj.DeviceId); // Converting Byte[] Array to Long

            for (int i = 0; i <= Devices_Buffer.Count - 1; i++)
            {
                if (Devices_Buffer[i].DeviceId.Equals(ParamDeviceId))
                {
                    IsDeviceExists = true;             /* Comm had failed and it is trying with the new IP. */
                    /* Check IsConnected */
                    if (Devices_Buffer[i].IsConnected) // Because Connection_Checking Method still not Removed Old Socket
                    {
                        RemoveSocketByusingIP(Devices_Buffer[i].Ipaddr);
                    }

                    SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address
                    if (Socketobj != null)
                    {
                        Devices_Buffer[i].IsConnected    = true;
                        Devices_Buffer[i].LastActivetime = DateTime.Now;
                        Devices_Buffer[i].Ipaddr         = Socketobj.IpAddr;    // New Ip Address
                        Devices_Buffer[i].ClientObj      = Socketobj.Clientobj; // New Socket
                    }
                    /* Frame Ack for Communication Initialization Packet */
                    byte[] CommAckdata = FrameCommunicationAck(PacketObj);
                    SendDatatoClient(CommAckdata, Devices_Buffer[i].ClientObj);  // Device is Re Connected with new Ip Address
                    break;
                }
            }


            if (!IsDeviceExists)  // If No Device Exists
            {
                DeviceInfo Device = new DeviceInfo();
                Device.DeviceId       = ParamDeviceId; // It is Dummy
                Device.IsConnected    = true;
                Device.LastActivetime = DateTime.Now;
                SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address
                Device.Ipaddr    = Socketobj.IpAddr;
                Device.ClientObj = Socketobj.Clientobj;
                Devices_Buffer.Add(Device); // Adding New Device to the Devices_buff

                /* Frame Ack for Communication Initialization Packet */
                byte[] CommAckdata = FrameCommunicationAck(PacketObj);
                SendDatatoClient(CommAckdata, Device.ClientObj);  // Device is New & Sending  Bytes to the Client (Device ).
            }
        }
示例#7
0
        public byte[] GetFinalbytesOfCommand(byte[] Deviceid, byte[] Command_Pkt_TypeID, byte[] databytes)
        {
            /* Prepare PacketObject */
            PacketClass PktObj = FramePacketforCommand(Deviceid, Global.Command_KeyCodePkt_TypeID, databytes);
            /* Calculating Length of the PAcket */
            short length = (short)(Global.Packet_Default_length + PktObj.Data.Length);

            PktObj.Packet_Length = BitConverter.GetBytes(length);           // Getting Bytes from checksum.. Length

            ushort Chksum = ServerClass.Calc_ChecksumForCommandObj(PktObj); // Calculates Checksum Of the Packet

            PktObj.Checksum = BitConverter.GetBytes(Chksum);                //Checksum

            PktObj = ServerClass.Reverse_PacketBytes(PktObj);               // Reversing the Object byte[] arrays

            byte[] Final_bytes = ServerClass.PktObjectToByteArray(PktObj);  // Convering Object to byte[] array.

            return(Final_bytes);
        }
        public static byte[] PktObjectToByteArray(PacketClass obj)
        {
            try
            {
                /* Need to place the bytes[] into Object */
                /* Get the data bytes length */
                int array_len = 0;
                array_len = Global.Packet_overhead_len; // Asumming Packet has no data.

                int data_bytes_len = obj.Data.Length;   // Packet_overhead_len is Fixed for all packets but data changes.

                if (data_bytes_len > 0)                 /* Packet has Some Databyte need to change the length */
                {
                    array_len = array_len + data_bytes_len;
                }
                /* Creating new Array */
                byte[] Ack_bytes = new byte[array_len];

                int Packet_Checksum_StartIdx = Global.Packet_data_start_Idx + data_bytes_len;
                /* Frame Packet Object from byte[] data */

                Array.Copy(obj.packetHeader, 0, Ack_bytes, Global.Packet_Header_start_Idx, Global.Packet_Header_len);           // Copy PacketHeader
                Array.Copy(obj.Packet_Length, 0, Ack_bytes, Global.PacketLength_start_Idx, Global.PacketLength_len);            // Copy Length of Packet
                Array.Copy(obj.DeviceId, 0, Ack_bytes, Global.Packet_DeviceID_start_Idx, Global.Packet_DeviceID_len);           // Copy Device Id of Packet
                Array.Copy(obj.Password, 0, Ack_bytes, Global.Packet_Password_start_Idx, Global.Packet_Password_len);           // Copy System Password of Packet
                Array.Copy(obj.TypeId, 0, Ack_bytes, Global.Packet_TypeId_start_Idx, Global.Packet_TypeId_len);                 // Copy  TYpeID of Packet
                Array.Copy(obj.Packet_sno, 0, Ack_bytes, Global.Packet_Sno_start_Idx, Global.Packet_Sno_len);                   // Copy Packet SNo of Packet
                Array.Copy(obj.Continue_Sts, 0, Ack_bytes, Global.Packet_ContinueSts_start_Idx, Global.Packet_ContinueSts_len); // Copy Continue_Sts of th Packet
                Array.Copy(obj.Data, 0, Ack_bytes, Global.Packet_data_start_Idx, data_bytes_len);                               // Copy Data  of Packet

                Array.Copy(obj.Checksum, 0, Ack_bytes, Packet_Checksum_StartIdx, Global.Packet_Chcksum_len);                    // Copy Checksum  of Packet

                return(Ack_bytes);                                                                                              // Returning Ack bytes
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Occured in ObjectToByteArray:  " + ex.Message + time); // Logging the Execptions Ocuured
                return(null);
            }
        }
示例#9
0
        private byte[] FrameCommunicationAck(PacketClass PacketObj)
        {
            try
            {
                AckClass AckPacket = new AckClass();
                AckPacket.packetHeader = Global.CommInitResponse_pkt_header;
                // Length ?? 18
                // AckPacket.Packet_Length = PacketObj.Packet_Length;  { Should not do like this beacuse it is going to change Original object }
                PacketObj.Packet_Length.CopyTo(AckPacket.Packet_Length, 0);

                PacketObj.DeviceId.CopyTo(AckPacket.DeviceId, 0);

                PacketObj.Password.CopyTo(AckPacket.Password, 0);

                Global.CommInitAckPkt_TypeID.CopyTo(AckPacket.TypeId, 0);

                Global.CommInitAckPkt_SNo.CopyTo(AckPacket.Packet_sno, 0);

                Global.CommInitAckContinue_Sts.CopyTo(AckPacket.Continue_Sts, 0);

                AckPacket.Data = new byte[] { }; // Empty Data Feild

                short checksum = Calc_ChecksumOfAckPacketObj(AckPacket);

                AckPacket.Checksum = BitConverter.GetBytes(checksum); // Getting Bytes from checksum..

                AckPacket = Reverse_AckPacketBytes(AckPacket);        // Reversing the AckPacket Object Is Causing the Device Id in Reverse


                byte[] Final_bytes = ObjectToByteArray(AckPacket); // Serializing Object to Byte Array.

                return(Final_bytes);                               // Returning Serialozed Byte array.
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured While FrameCommunicationAck:  " + ex.Message + time); // Logging the Execptions Ocuured
                return(null);
            }
        }
示例#10
0
        public PacketClass DecodeRecievedBytestoPacket_Object(byte[] data)
        {
            PacketClass obj = new PacketClass();
            /* Need to place the bytes[] into Object */
            /* Get the data bytes length */
            int data_bytes_len = data.Length - Global.Packet_overhead_len; // Packet_overhead_len is Fixed for all packets but data changes.

            /* Frame Packet Object from byte[] data */

            Array.Copy(data, Global.Packet_Header_start_Idx, obj.packetHeader, 0, Global.Packet_Header_len);           // Copy PacketHeader
            Array.Copy(data, Global.PacketLength_start_Idx, obj.Packet_Length, 0, Global.PacketLength_len);            // Copy Length of Packet
            Array.Copy(data, Global.Packet_DeviceID_start_Idx, obj.DeviceId, 0, Global.Packet_DeviceID_len);           // Copy Device Id of Packet
            Array.Copy(data, Global.Packet_Password_start_Idx, obj.Password, 0, Global.Packet_Password_len);           // Copy System Password of Packet
            Array.Copy(data, Global.Packet_TypeId_start_Idx, obj.TypeId, 0, Global.Packet_TypeId_len);                 // Copy  TYpeID of Packet
            Array.Copy(data, Global.Packet_Sno_start_Idx, obj.Packet_sno, 0, Global.Packet_Sno_len);                   // Copy Packet SNo of Packet
            Array.Copy(data, Global.Packet_ContinueSts_start_Idx, obj.Continue_Sts, 0, Global.Packet_ContinueSts_len); // Copy Continue_Sts of th Packet
            Array.Copy(data, Global.Packet_data_start_Idx, obj.Data, 0, Global.Packet_data_len);                       // Copy Data  of Packet
            Array.Copy(data, Global.Packet_Checksum_Idx, obj.Checksum, 0, Global.Packet_Chcksum_len);                  // Copy Data  of Packet

            return(obj);
        }
示例#11
0
        public PacketClass Reverse_PacketBytes(PacketClass Packet_obj)
        {
            try
            {
                Array.Reverse(Packet_obj.packetHeader); // Reversing the Packet Header Bytes
                Array.Reverse(Packet_obj.Packet_Length);
                Array.Reverse(Packet_obj.DeviceId);
                Array.Reverse(Packet_obj.Password);
                Array.Reverse(Packet_obj.TypeId);
                Array.Reverse(Packet_obj.Packet_sno);
                Array.Reverse(Packet_obj.Continue_Sts);
                Array.Reverse(Packet_obj.Checksum);

                return(Packet_obj);
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Occured in Reverse_PacketBytes:  " + ex.Message + time); // Logging the Execptions Ocuured
                return(null);
            }
        }
示例#12
0
        public PacketClass FramePacketforCommand(byte[] Deviceid, byte[] Command_Pkt_TypeID, byte[] databytes)
        {
            PacketClass Obj = new PacketClass();

            Global.Command_pkt_header.CopyTo(Obj.packetHeader, 0); // Packet Header

            Deviceid.CopyTo(Obj.DeviceId, 0);                      // Device Id

            Global.SystemPassword.CopyTo(Obj.Password, 0);         // System Password

            Command_Pkt_TypeID.CopyTo(Obj.TypeId, 0);              // TypeID

            if (Command_Pkt_TypeID.SequenceEqual(Global.Command_KeyCodePkt_TypeID))
            {
                Global.Command_KeyCodePkt_Sno.CopyTo(Obj.Packet_sno, 0);       // Packet Serial Numbr
                Global.Command_KeyCodePkt_ContSts.CopyTo(Obj.Continue_Sts, 0); // Packet Continue Sts
            }

            Obj.Data = new byte[databytes.Length];

            databytes.CopyTo(Obj.Data, 0);            // Data

            return(Obj);
        }
示例#13
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; // It Is a Lock For this Procedure

                    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
                                {
                                    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.

                                    short cal_checksum = Calc_ChecksumOfPacketObj(Packet_obj);                                                  // Pasing the Packet object to calculate checksum;

                                    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
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Global.Is_dataprocessing = false;
                }// 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
                Global.Is_dataprocessing = false;
            }
        }
示例#14
0
 public void ProcessModuleAlivePacket(PacketClass PacketObj, IPAddress Ipaddr)
 {
     /* Module Alive Packet */
 }
示例#15
0
 public void ProcessEventPacket(PacketClass PacketObj, IPAddress Ipaddr)
 {
     /* Event Packt */
 }
示例#16
0
 public void ProcessCommandResponsePacket(PacketClass PacketObj, IPAddress Ipaddr)
 {
     /* Command Response Packet */
 }
示例#17
0
        public void ProcessCommInitPacket(PacketClass PacketObj, IPAddress Ipaddr)
        {
            /* Communication Init Packet */
            try
            {
                bool IsDeviceExists = false;


                for (int i = 0; i <= Devices_Buffer.Count - 1; i++)
                {
                    if (Devices_Buffer[i].DeviceId.SequenceEqual(PacketObj.DeviceId))
                    {
                        IsDeviceExists = true;             /* Comm had failed and it is trying with the new IP. */
                        /* Check IsConnected */
                        if (Devices_Buffer[i].IsConnected) // Because Connection_Checking Method still not Removed Old Socket
                        {
                            RemoveSocketByusingIP(Devices_Buffer[i].Ipaddr);
                        }

                        SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address
                        if (Socketobj != null)                               // Socketobj will be null when same IP requests
                        {
                            Devices_Buffer[i].IsConnected = true;

                            Devices_Buffer[i].LastActivetime = DateTime.Now;

                            Devices_Buffer[i].Ipaddr = Socketobj.IpAddr; // New Ip Address

                            // Devices_Buffer[i].ClientStream.Close();  // To Close the Previous Network Stream bw Client & Server.

                            Devices_Buffer[i].ClientStream = Socketobj.Clientobj.GetStream(); // New Socket Stream


                            /* Frame Ack for Communication Initialization Packet */
                            byte[] CommAckdata = FrameCommunicationAck(PacketObj);

                            SendDatatoClient(CommAckdata, Devices_Buffer[i].ClientStream); // Device is Re Connected with new Ip Address
                        }

                        break;
                    }
                }


                if (!IsDeviceExists) // If No Device Exists
                {
                    DeviceInfo Device = new DeviceInfo();
                    Device.DeviceId = new byte[PacketObj.DeviceId.Length]; // Need to Initialize the DeviceID array
                    PacketObj.DeviceId.CopyTo(Device.DeviceId, 0);         // It is Dummy
                    Device.IsConnected    = true;
                    Device.LastActivetime = DateTime.Now;
                    SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address
                    Device.Ipaddr       = Socketobj.IpAddr;
                    Device.ClientStream = Socketobj.Clientobj.GetStream();
                    Devices_Buffer.Add(Device); // Adding New Device to the Devices_buff

                    /* Frame Ack for Communication Initialization Packet */
                    byte[] CommAckdata = FrameCommunicationAck(PacketObj);

                    // Don't know why DeviceID is getting reversed when i reverse the DeviceID in AckPacket
                    //  Array.Reverse(Device.DeviceId);

                    SendDatatoClient(CommAckdata, Device.ClientStream); // Device is New & Sending  Bytes to the Client (Device ).
                }
            }
            catch (Exception ex)
            {
                DateTime now_time = DateTime.Now;
                string   time     = Convert.ToString(now_time);
                Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured in ProcessCommInitPacket:  " + ex.Message + time); // Logging the Execptions Ocuured
            }
        }