Ejemplo n.º 1
0
        public async Task RunReceiveLoopAsync()
        {
            while (!_backgroundReceiveCTS.IsCancellationRequested)
            {
                var lengthData = await _socket.ReceiveAsync(2, _backgroundReceiveCTS.Token);

                if (_backgroundReceiveCTS.IsCancellationRequested || lengthData.Length != 2)
                {
                    break;
                }

                var length = _numberEncoderService.DecodeNumber(lengthData);

                var packetData = await _socket.ReceiveAsync(length, _backgroundReceiveCTS.Token);

                if (_backgroundReceiveCTS.IsCancellationRequested || packetData.Length != length)
                {
                    break;
                }

                var packet = _packetProcessActions.DecodeData(packetData);
                LogReceivedPacket(packet);

                _packetHandlingActions.EnqueuePacketForHandling(packet);
            }
        }
Ejemplo n.º 2
0
        private void _checkRequiredInputs()
        {
            txtOutput.Text = "";

            if (txtDMulti.TextLength == 0 || txtEMulti.TextLength == 0 || txtOffset.TextLength == 0)
            {
                return;
            }

            if (txtLength.TextLength == 0 && m_type == DataTypes.FixedString)
            {
                return;
            }

            string inputData = txtInputData.Text;

            if (inputData.Length == 0)
            {
                return;
            }

            //input data is copied from wireshark. colon delimited.
            string bytes = inputData.Replace(":", "");
            string len   = bytes.Substring(0, 4);//first 2 bytes are the length!

            bytes = bytes.Substring(4);
            byte[] data = new byte[bytes.Length / 2];

            byte[] lenDat = new byte[2];
            for (int i = 0; i < len.Length; i += 2)
            {
                lenDat[i / 2] = Convert.ToByte(len.Substring(i, 2), 16);
            }
            lblPacketLength.Text = "Packet Length: " + OldPacket.DecodeNumber(lenDat).ToString();

            for (int i = 0; i < bytes.Length; i += 2)
            {
                data[i / 2] = Convert.ToByte(bytes.Substring(i, 2), 16);
            }

            var pkt = _packetProcessActions.DecodeData(data);

            pkt.Seek(m_packetOffset, SeekOrigin.Begin);

            lblFamily.Text = pkt.Family.ToString();
            lblAction.Text = pkt.Action.ToString();

            string decoded = "";

            for (int i = 0; i < data.Length; i++)
            {
                decoded += $"{data[i].ToString("D3")} ";
            }
            txtDecoded.Text = decoded;

            switch ((DataTypes)cmbOutputFmt.SelectedIndex)
            {
            case DataTypes.None:
                txtOutput.Text = pkt.ReadEndString();
                break;

            case DataTypes.PacketFamily:
                txtOutput.Text = ((PacketFamily)pkt.PeekByte()).ToString();
                break;

            case DataTypes.PacketAction:
                txtOutput.Text = ((PacketAction)pkt.PeekByte()).ToString();
                break;

            case DataTypes.Byte:
                txtOutput.Text = pkt.PeekByte().ToString();
                break;

            case DataTypes.Char:
                txtOutput.Text = pkt.PeekChar().ToString();
                break;

            case DataTypes.Short:
                txtOutput.Text = pkt.PeekShort().ToString();
                break;

            case DataTypes.Three:
                txtOutput.Text = pkt.PeekThree().ToString();
                break;

            case DataTypes.Int:
                txtOutput.Text = pkt.PeekInt().ToString();
                break;

            case DataTypes.BreakString:
                txtOutput.Text = pkt.PeekBreakString();
                break;

            case DataTypes.EndString:
                txtOutput.Text = pkt.PeekEndString();
                break;

            case DataTypes.FixedString:
                txtOutput.Text = pkt.PeekString(m_dataLength);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            int selLen;

            if (m_dataLength > 0)
            {
                selLen = m_dataLength;
            }
            else
            {
                switch (m_type)
                {
                case DataTypes.EndString:
                    selLen = 3 * (pkt.Length - pkt.ReadPosition) - 1;
                    break;

                case DataTypes.BreakString:
                    int oldPos = pkt.ReadPosition;
                    while (pkt.ReadByte() != 255)
                    {
                        ;
                    }
                    selLen = pkt.ReadPosition - oldPos;
                    pkt.Seek(oldPos, SeekOrigin.Begin);
                    break;

                default:
                    selLen = 0;
                    break;
                }
            }
            txtDecoded.Select(4 * m_packetOffset, 4 * selLen - 1);

            if (m_type == DataTypes.EndString || m_type == DataTypes.BreakString)
            {
                m_suppressEvent = true;
                txtLength.Text  = selLen.ToString();
                m_suppressEvent = false;
            }
        }