private void BtnSegmentNextPosition_OnClick(object sender, RoutedEventArgs e)
        {
            var index = CurrentPacket.MoveSegment2NextPosition(CurrentSegment);

            LbSegments.ItemsSource   = null;
            LbSegments.ItemsSource   = CurrentPacket.Segments.OrderBy(t => t.OrderId);
            LbSegments.SelectedIndex = index;
        }
        private void DecryptData(int to)
        {
            if (ClientConnection.Decrypt == null || DecryptPointer >= to)
            {
                return;
            }

            ClientConnection.Decrypt.Process(CurrentPacket.GetBuffer(), DecryptPointer, (to - DecryptPointer));
            DecryptPointer = to;
        }
        } //shortest is 2 byte size, 4 byte command

        public override PacketProcessResult ProcessData()
        {
            DecryptData(6);
            int sz = CurrentPacket.ReadUInt16BE();

            if ((sz & 0x8000) != 0) //large packet
            {
                DataNeeded = 3;     //we need 3 byte size to continue
                if (CurrentPacket.Length < DataNeeded)
                {
                    return(PacketProcessResult.RequiresData);
                }

                DecryptData(7);
                CurrentPacket.Position = 3;
                sz   = 0;
                sz  |= CurrentPacket.GetBuffer()[0] & 0x7F;
                sz <<= 8;
                sz  |= CurrentPacket.GetBuffer()[1];
                sz <<= 8;
                sz  |= CurrentPacket.GetBuffer()[2];

                DataNeeded = 3 + sz;
                if (CurrentPacket.Length < DataNeeded)
                {
                    return(PacketProcessResult.RequiresData);
                }
            }
            else
            {
                DataNeeded = 2 + sz;
                if (CurrentPacket.Length < DataNeeded)
                {
                    return(PacketProcessResult.RequiresData);
                }
            }

            CurrentOpcode = (RealmOp)CurrentPacket.ReadUInt32();

            Console.WriteLine("Received Packet {0} Length {1}", CurrentOpcode.ToString(), sz);

            //ok now we need sz + 2 to continue

            return(ProcessPacket());
        }
Exemple #4
0
        public bool ProcessNextSegment(byte[] nextSegment, int offset, int len, out IPacket packet)
        {
            packet        = null;
            CurrentPacket = null;

            lock (PacketBuffer)
            {
                if (nextSegment.Length > 0)
                {
                    PacketBuffer.AddRange(new ByteArraySegment(nextSegment, offset, len));
                }

                if (PacketBuffer.Count == 0)
                {
                    return(false);
                }

                if (CurrentPacket == null)
                {
                    var packetId = PacketBuffer[0];

                    var createPacket = ServerBound ? PacketReader.ServerBoundPackets[packetId] : PacketReader.ClientBoundPackets[packetId];
                    if (createPacket != null)
                    {
                        CurrentPacket = createPacket();
                    }
                    else
                    {
                        _trace.TraceData(TraceEventType.Error, 0, $"Unable to read packet type with Id #{packetId:X2}");
                        PacketBuffer.Clear();
                        return(false);
                    }
                }

                if (CurrentPacket != null)
                {
                    using (var listStream = new ByteListMemoryStream(PacketBuffer, 1))
                    {
                        using (var ms = new McStream(listStream))
                        {
                            try
                            {
                                CurrentPacket.ReadPacket(ms);
                            }
                            catch (EndOfStreamException)
                            {
                                _trace.TraceData(TraceEventType.Error, 0, $"unexpected end of packet stream");
                                return(false);
                            }
                        }

                        PacketBuffer.RemoveRange(0, (int)listStream.Position);
                    }

                    packet         = CurrentPacket;
                    CurrentPacket  = null;
                    PreviousPacket = packet;
                }

                return(PacketBuffer.Count > 0);
            }
        }
        /// <summary>
        /// Updates the state using the packets that were demuxed into the specified channel.
        /// This can be called outside the GUI thread.
        /// </summary>
        /// <param name="channel">The channel.</param>
        /// <param name="clockPosition">The clock position.</param>
        /// <returns>A boolean to determine if the display needs repainting.</returns>
        public bool UpdateState(CaptionsChannel channel, TimeSpan clockPosition)
        {
            lock (SyncLock)
            {
                var needsRepaint = false;

                // Reset the buffer state if the channels don't match or if we have a timeout
                if (channel != Channel || DateTime.UtcNow.Subtract(LastReceiveTime).TotalSeconds > TimeoutSeconds)
                {
                    Reset();
                    Channel      = channel;
                    needsRepaint = true;
                }

                if (channel == CaptionsChannel.CCP)
                {
                    return(needsRepaint);
                }

                // Dequeue packets for all channels but only process the current channel packets
                List <ClosedCaptionPacket> packets = null;
                for (var c = 1; c <= 4; c++)
                {
                    var currentChannel  = (CaptionsChannel)c;
                    var dequeuedPackets = DequeuePackets(ChannelPacketBuffer[currentChannel], clockPosition.Ticks);
                    if (currentChannel == Channel)
                    {
                        packets = dequeuedPackets;
                    }
                }

                // Check if we have at least 1 dequeued packet
                if (packets == null || packets.Count <= 0)
                {
                    return(needsRepaint);
                }

                // Update the last received time
                LastReceiveTime = DateTime.UtcNow;

                // Start processing the dequeued packets for the given channel
                foreach (var packet in packets)
                {
                    // Skip duplicated control codes
                    if (CurrentPacket != null && CurrentPacket.IsRepeatedControlCode(packet))
                    {
                        CurrentPacket = packet;
                        continue;
                    }

                    // Update the current packet (we need this to detect duplicated control codes)
                    CurrentPacket = packet;

                    // Now, go ahead and process the packet updating the state
                    switch (packet.PacketType)
                    {
                    case CaptionsPacketType.Color:
                    {
                        if (StateMode == ParserStateMode.Buffered || StateMode == ParserStateMode.Scrolling)
                        {
                            if (packet.Color == CaptionsColor.ForegroundBlackUnderline)
                            {
                                IsUnderlined = true;
                            }

                            if (packet.Color == CaptionsColor.WhiteItalics)
                            {
                                IsItalics = true;
                            }
                        }

                        break;
                    }

                    case CaptionsPacketType.MidRow:
                    {
                        if (StateMode == ParserStateMode.Buffered || StateMode == ParserStateMode.Scrolling)
                        {
                            IsItalics    = packet.IsItalics;
                            IsUnderlined = packet.IsUnderlined;
                        }

                        break;
                    }

                    case CaptionsPacketType.Command:
                    {
                        if (ProcessCommandPacket(packet))
                        {
                            needsRepaint = true;
                        }

                        break;
                    }

                    case CaptionsPacketType.Preamble:
                    {
                        ProcessPreamblePacket(packet);
                        break;
                    }

                    case CaptionsPacketType.Tabs:
                    {
                        if (StateMode == ParserStateMode.Scrolling || StateMode == ParserStateMode.Buffered)
                        {
                            CursorColumnIndex += packet.Tabs;
                        }

                        break;
                    }

                    case CaptionsPacketType.Text:
                    {
                        if (ProcessTextPacket(packet))
                        {
                            needsRepaint = true;
                        }

                        break;
                    }

                    case CaptionsPacketType.XdsClass:
                    {
                        // Change state back and forth
                        StateMode = packet.XdsClass == CaptionsXdsClass.EndAll ?
                                    ParserStateMode.None : ParserStateMode.XDS;
                        break;
                    }

                    case CaptionsPacketType.PrivateCharset:
                    case CaptionsPacketType.Unrecognized:
                    case CaptionsPacketType.NullPad:
                    default:
                    {
                        break;
                    }
                    }
                }

                return(needsRepaint);
            }
        }
        public override void Process(int step)
        {
            //Transmit next packet if possible
            if (CurrentPacket == null)
            {
                //If interrupt stack is not empty, service any interrupted packets first
                if (InterruptStack.Count > 0)
                {
                    //Check if there is still a higher priority packet in queue.
                    //If not, return the interrupted packet to the bus.
                    if (!(PacketQueue.Count > 0 && InterruptStack.Peek().Header[CSPPacket.Priority] > PacketQueue.Peek().Header[CSPPacket.Priority]))
                    {
                        CurrentPacket = InterruptStack.Pop();
                        //Log new packet returning to bus
                        EventLog.AddLog(new SimEvent(
                                            "Interrupted packet continuing transmission on bus " + Name + ": " + CurrentPacket.ToString(),
                                            EventSeverity.INFO));
                    }
                }
                else if (PacketQueue.Count > 0)
                {
                    CurrentPacket = PacketQueue.Dequeue();
                    //Log new packet on bus
                    EventLog.AddLog(new SimEvent(
                                        "New packet transmitting on bus " + Name + ": " + CurrentPacket.ToString(),
                                        EventSeverity.INFO));
                }
            }

            if (CurrentPacket != null)
            {
                //If next packet in queue is higher priority, interrupt this packet
                if (PacketQueue.Count > 0 && PacketQueue.Peek().Header[CSPPacket.Priority] < CurrentPacket.Header[CSPPacket.Priority])
                {
                    //Log interruption
                    EventLog.AddLog(new SimEvent(
                                        "Packet " + CurrentPacket.ToString() +
                                        " was interrupted by packet " + PacketQueue.Peek().ToString() +
                                        " on bus " + Name,
                                        EventSeverity.INFO));
                    InterruptStack.Push(CurrentPacket);
                    CurrentPacket = PacketQueue.Dequeue();
                }

                bool destination_exists = false;
                foreach (Module module in ConnectedModules)
                {
                    if (module.Address == CurrentPacket.Header[CSPPacket.DestinationAddress])
                    {
                        destination_exists = true;
                        if (module.Crashed)
                        {
                            //log packet going to crashed module
                            EventLog.AddLog(new SimEvent(
                                                "Packet was dropped because the destination module " + module.Name + " has crashed: " + CurrentPacket.ToString(),
                                                EventSeverity.WARNING));
                            //Take packet off the bus, since it will just go nowhere
                            CurrentPacket = null;
                            break;
                        }
                        //Transmit part of the packet based on data rate
                        CurrentPacket.PartTransmitted = (short)Math.Min(CurrentPacket.PartTransmitted + DataRate, CurrentPacket.DataSize);
                        if (CurrentPacket.PartTransmitted >= CurrentPacket.DataSize)
                        {
                            //If packet is fully transmitted, receive it
                            module.ReceiveCSPPacket(CurrentPacket, this);
                            //Remove packet after transmission
                            CurrentPacket = null;
                            break;
                        }
                    }
                }

                if (!destination_exists)
                {
                    //log packet going nowhere
                    EventLog.AddLog(new SimEvent(
                                        "Packet was dropped because it has no valid destination: " + CurrentPacket.ToString(),
                                        EventSeverity.WARNING));
                    //Take packet off the bus, since it will just go nowhere
                    CurrentPacket = null;
                }
            }

            Idle = (CurrentPacket == null && PacketQueue.Count == 0);
        }
Exemple #7
0
        public override PacketProcessResult ProcessData()
        {
            opcode = (AuthOp)CurrentPacket.ReadByte();

            return(ProcessPacket());
        }