Пример #1
0
        /// <summary>

        /// Handles incoming packet data when a message is received

        /// </summary>

        /// <param name="bytes">The raw packet data</param>

        /// <param name="receivedFrom">The host that sent the packet</param>

        /// <param name="isLocal">Indicates if the request came from the local machine</param>

        /// <param name="isLAN">Indicates if the request came from the LAN</param>

        protected virtual void udp_PacketReceived(byte[] bytes, string receivedFrom, bool isLocal, bool isLAN)

        {
            // if this is a network request and we dont allow them, stop here

            if (!isLocal && !this.AllowNetworkNotifications)
            {
                return;
            }



            StringBuilder sb = new StringBuilder();

            bool processed = false;

            List <string> extraLogInfo = new List <string>();



            // parse the packet

            if (bytes != null && bytes.Length > 18)

            {
                int protocolVersion = (int)bytes[0];

                PacketType packetType = (PacketType)bytes[1];



                bool passwordRequired = true;

                if (isLocal && !this.RequireLocalPassword)
                {
                    passwordRequired = false;
                }

                else if (isLAN && !this.RequireLANPassword)
                {
                    passwordRequired = false;
                }



                if (packetType == PacketType.Registration)

                {
                    RegistrationPacket rp = RegistrationPacket.FromPacket(bytes, this.passwordManager, passwordRequired);

                    if (rp != null)

                    {
                        this.OnRegistrationPacketReceived(rp, receivedFrom, ref extraLogInfo);

                        processed = true;



                        sb.AppendFormat("Protocol Version:         {0}\r\n", rp.ProtocolVersion);

                        sb.AppendFormat("Packet Type:              {0}\r\n", rp.PacketType);

                        sb.AppendFormat("Application Name:         {0}\r\n", rp.ApplicationName);

                        sb.AppendFormat("Notifications Registered: {0}\r\n\r\n", rp.NotificationTypes.Length);

                        foreach (NotificationType nt in rp.NotificationTypes)

                        {
                            sb.AppendFormat("  Notification Type: {0}\r\n", nt.Name);

                            sb.AppendFormat("  Enabled:           {0}\r\n\r\n", nt.Enabled);
                        }
                    }

                    else

                    {
                        sb.Append("Invalid message - either the message format was incorrect or the password was incorrect");
                    }
                }

                else if (packetType == PacketType.Notification)

                {
                    NotificationPacket np = NotificationPacket.FromPacket(bytes, this.passwordManager, passwordRequired);

                    if (np != null)

                    {
                        this.OnNotificationPacketReceived(np, receivedFrom, ref extraLogInfo);

                        processed = true;



                        sb.AppendFormat("Protocol Version:  {0}\r\n", np.ProtocolVersion);

                        sb.AppendFormat("Packet Type:       {0}\r\n", np.PacketType);

                        sb.AppendFormat("Application Name:  {0}\r\n", np.ApplicationName);

                        sb.AppendFormat("Notification Type: {0}\r\n", np.NotificationType.Name);

                        sb.AppendFormat("Title:             {0}\r\n", np.Title);

                        sb.AppendFormat("Description:       {0}\r\n", np.Description);

                        sb.AppendFormat("Sticky:            {0}\r\n", np.Sticky);

                        sb.AppendFormat("Priority:          {0}\r\n", np.Priority);
                    }

                    else

                    {
                        sb.Append("Invalid message - either the message format was incorrect or the password was incorrect");
                    }
                }

                else

                {
                    sb.Append("Malformed packet - unrecognized data");
                }
            }

            else

            {
                sb.Append("Malformed packet - not enough bytes");
            }

            Log(sb.ToString(), bytes, receivedFrom, processed, extraLogInfo);
        }