Inheritance: IDisposable
        int InsertIntoPacketTable(Packet p, Guid sourceID, Guid targetID, int subPacketType)
        {
            int retVal = -1;
            lock (lockingObject2)
            {
                using (SqlCommand cmd =
                    new SqlCommand("INSERT INTO Packets (SourceID, TargetID, Length, Origin, Unknown, PayloadLength, PacketType, SubPacketType, Payload) "
                    + "VALUES (@SourceID, @TargetID, @Length, @Origin, @Unknown, @PayloadLength, @PacketType, @SubPacketType, @Payload); SELECT SCOPE_IDENTITY();",
                    ActiveConnection))
                {

                    int packType = (int)p.PacketType;
                    cmd.Parameters.AddWithValue("@SourceID", sourceID.ToString());
                    cmd.Parameters.AddWithValue("@TargetID", targetID.ToString());
                    cmd.Parameters.AddWithValue("@Length", p.Length);
                    cmd.Parameters.AddWithValue("@Origin", (int)p.Origin);
                    cmd.Parameters.AddWithValue("@Unknown", p.Padding);
                    cmd.Parameters.AddWithValue("@PayloadLength", p.PayloadLength);
                    cmd.Parameters.AddWithValue("@PacketType", packType);


                    cmd.Parameters.AddWithValue("@SubPacketType", subPacketType);


                    cmd.Parameters.AddWithValue("@Payload", p.Payload.GetBuffer());
                    decimal insertedID = (decimal)cmd.ExecuteScalar();
                    retVal = Convert.ToInt32(insertedID);
                }
            }
            return retVal;
        }
 public object Process(Packet packet, Guid sourceID, Guid targetID, int subPacketType)
 {
     int seqID = 0;
     lock (lockingObject)
     {
         if (ActiveConnection == null)
         {
             ActiveConnection = GetConnection();
         }
         seqID = InsertIntoPacketTable(packet, sourceID, targetID, subPacketType);
        
     }
     return seqID;
 }
        public PackageEventArgs(Packet packet, Guid id)
            : base(id)
        {
            ReceivedPacket = packet;

        }
        /// <summary>
        /// Called when [convert].
        /// </summary>
        /// <param name="dataForProcessing">The data for processing.</param>
        /// <returns>Unprocessed, incomplete packet</returns>
        private string OnConvert(string dataForProcessing)
        {
            
            string retVal = string.Empty;
            //Could have multiple packets: need to code to handle.
            //Packet p = new Packet(me.RawData);
            //How Data looks from Wireshark:
            //ef:be:ad:de:8c:00:00:00:01:00:00:00:00:00:00:00:78:00:00:00:da:b3:04:6d:70:00:00:00:59:6f:75:20:68:61:76:65:20:63:6f:6e:6e:65:63:74:65:64:20:74:6f:20:54:68:6f:6d:20:52:6f:62:65:72:74:73:6f:6e:27:73:20:41:72:74:65:6d:69:73:20:42:72:69:64:67:65:20:53:69:6d:75:6c:61:74:6f:72:2e:20:20:50:6c:65:61:73:65:20:63:6f:6e:6e:65:63:74:20:77:69:74:68:20:61:6e:20:61:75:74:68:6f:72:69:7a:65:64:20:67:61:6d:65:20:63:6c:69:65:6e:74:2e
            //2222
            List<byte> byteArray = new List<byte>(ConvertToByteArray(dataForProcessing));

            //byteArray is now total message--need to parse out multiple messages.
            int ln = 0;
            List<byte> workArray = null;
            int startIndex = 0;
            if (byteArray.Count > 7)
            {
                do
                {

                    ln = BitConverter.ToInt32(byteArray.ToArray(), 4);


                    if (byteArray.Count < startIndex + ln)
                    {
                        break;
                    }


                    if (startIndex + ln > byteArray.Count)
                    {
                        ln = byteArray.Count - startIndex;
                    }

                    workArray = new List<byte>();
                    for (int i = startIndex; i < startIndex + ln; i++)
                    {
                        workArray.Add(byteArray[i]);
                    }
                    using (MemoryStream ms = new MemoryStream(byteArray.ToArray()))
                    {
                        using (Packet p = new Packet(ms.GetMemoryStream(0)))
                        {
                            bool ignore = false;

                            if (FilterPackets && p.PacketType == PacketType.ObjectStatusUpdatePacket)
                            {
                                ObjectStatusUpdatePacket objP = p.Package as ObjectStatusUpdatePacket;
                                if (objP != null && objP.SubPacketType == ArtemisComm.ObjectStatusUpdateSubPackets.ObjectStatusUpdateSubPacketType.UnknownSubPacket)
                                {
                                    ignore = true;
                                }
                            }
                            if (!ignore)
                            {
                                GetPropertyInformation(Result, p, p.PacketType.ToString(), 0);

                                PropertyValue pv = new PropertyValue("----------", "-----------", "-----------", "-----------");
                                Result.Add(pv);
                            }
                        }
                    }
                    startIndex += ln;
                } while (startIndex < byteArray.Count - ArtemisComm.Packet.HeaderLength);
            }
            if (startIndex < byteArray.Count)
            {
                List<string> elem = new List<string>();
                for (int i = startIndex; i < byteArray.Count; i++)
                {
                    elem.Add(byteArray[i].ToString("X", CultureInfo.InvariantCulture).PadLeft(2,'0'));
                }
                retVal = string.Join(":", elem.ToArray());
            }
            if (Result.Count > 0)
            {
                ResultList.ScrollIntoView(ResultList.Items[Result.Count - 1]);
            }
            System.Media.SystemSounds.Beep.Play();
            return retVal;
        }
        void AddPacket(Packet p)
        {
            if (this.Dispatcher != System.Windows.Threading.Dispatcher.CurrentDispatcher)
            {
                this.Dispatcher.Invoke(new Action<Packet>(AddPacket), p);
            }
            else
            {
                GetPropertyInformation(Result, p, p.PacketType.ToString(), 0);

                PropertyValue pv = new PropertyValue("----------", "-----------", "-----------", "-----------");
                Result.Add(pv);
                ResultList.ScrollIntoView(ResultList.Items[Result.Count - 1]);
            }
        }
 public CommsIncomingPacketControl()
 {
     Package = new ArtemisComm.Packet(new CommsIncomingPacket());
     InitializeComponent();
 }
 public ProxyPackageEventArgs(Guid sourceID, Guid targetID, Packet package)
     : base(package, sourceID)
 {
     TargetID = targetID;
 }
 public DestroyObjectPacketControl()
 {
     Package = new ArtemisComm.Packet(new DestroyObjectPacket());
     InitializeComponent();
 }
        void QueueToPacketProcessor()
        {
           
            do
            {
                lock (ProcessQueue)
                {
                    mreListener.WaitOne();
                    while (ProcessQueue.Count > 0)
                    {
                        KeyValuePair<Stream, Guid> que = ProcessQueue.Dequeue();

                        if (que.Key != null)
                        {
                            try
                            {

                                Packet p = new Packet(que.Key);
                                if (p != null)
                                {
                                    _log.InfoFormat("Processing packet: {0}, for connection {1}", p.PacketType.ToString(), que.Value.ToString());
                                    PackageEventArgs pea = new PackageEventArgs(p, que.Value);
                                    EnqueueReceivedPacket(pea);
                                    if (p.ConversionException != null)
                                    {
                                        if (CrashOnException)
                                        {
                                            throw new InvalidPacketException(p.ConversionException);
                                        }
                                        else
                                        {
                                            RaiseExceptionEncountered(p.ConversionException, que.Value);
                                        }
                                    }
                                    if (p.Package != null)
                                    {
                                        ReadOnlyCollection<Exception> packetErrors = p.Package.GetErrors();
                                        if (packetErrors.Count > 0)
                                        {
                                            foreach (Exception e in packetErrors)
                                            {
                                                RaiseExceptionEncountered(e, que.Value);
                                            }
                                        }
                                    }
                                    

                                    EnqueueSpecificPacket(pea);

                                }
                                else
                                {

                                }


                            }
                            
                            catch (Exception ex)
                            {
                                if (!abort)
                                {
                                    if (CrashOnException)
                                    {
                                        throw new InvalidPacketException(ex);
                                    }
                                    else
                                    {
                                        RaiseExceptionEncountered(ex, que.Value);
                                    }
                                }
                            }
                        }
                        if (abort)
                        {
                            break;
                        }
                    }


                }
                if (!abort)
                {
                    mreListener.Reset();
                    
                }
            } while (!abort);
        }
        public void Send(Packet packet)
        {
            if (packet != null && connections.Count > 0)
            {
                foreach (Connector conn in connections.Values)
                {
                    using (MemoryStream stream = packet.GetRawData())
                    {
                        conn.Send(stream);
                    }
                }

            }
        }
 public void Send(Guid connectionID, Packet packet)
 {
     if (packet != null)
     {
         _log.InfoFormat("Ordered to send {0} packet to {1}", packet.PacketType.ToString(), connectionID.ToString());
     }
     if (packet != null && connections != null && connections.ContainsKey(connectionID))
     {
         using (MemoryStream stream = packet.GetRawData())
         {
             connections[connectionID].Send(stream);
         }
     }
 }