示例#1
0
        public void SendBreak()
        {
            lock (sync)
            {
                commandQueue.Clear();

                //Debug.WriteLine("SENT: BREAK");

                currentCommand = new GetReasonHalted();
                stream.Write(breakData, 0, 1);
            }
        }
示例#2
0
        private void SendPackets()
        {
            lock (sync)
            {
                // waiting for current command to reply
                if (currentCommand != null)
                {
                    return;
                }

                if (commandQueue.Count == 0)
                {
                    return;
                }

                currentCommand = commandQueue.Dequeue();

                Debug.WriteLine("SENT: " + currentCommand.Pack);

                var data = ToBinary(currentCommand);
                stream.Write(data, 0, data.Length);
            }
        }
示例#3
0
        private void IncomingPatcket()
        {
            int len = receivedData.Count;

            if (len == 0)
            {
                return;
            }

            if (len == 1 && receivedData[0] == '+')
            {
                receivedData.Clear();
                return;
            }

            if (len == 1 && receivedData[0] == '-')
            {
                // todo: re-transmit
                receivedData.Clear();
                return;
            }

            if (len >= 4 && receivedData[0] == '$' && receivedData[len - 3] == '#')
            {
                //Debug.WriteLine($"RECEIVED: {Encoding.UTF8.GetString(receivedData.ToArray())}");
                LogEvent?.Invoke($"RECEIVED: {Encoding.UTF8.GetString(receivedData.ToArray())}");

                if (currentCommand == null)
                {
                    receivedData.Clear();
                    return;
                }

                var data = Rle.Decode(receivedData, 1, receivedData.Count - 3).ToArray();

                bool ok = false;

                if (data != null)
                {
                    // Compare checksum
                    byte receivedChecksum   = HexToDecimal(receivedData[len - 2], receivedData[len - 1]);
                    uint calculatedChecksum = Checksum.Calculate(data);

                    if (receivedChecksum == calculatedChecksum)
                    {
                        ok = true;
                    }
                }

                currentCommand.IsResponseOk = ok;
                currentCommand.ResponseData = data;

                if (ok)
                {
                    currentCommand.Decode();
                }

                var cmd = currentCommand;
                currentCommand = null;
                receivedData.Clear();

                if (cmd.Callback != null)
                {
                    ThreadPool.QueueUserWorkItem(state => { cmd.Callback.Invoke(cmd); });
                }

                return;
            }

            return;
        }