void RecvFired(object sender, PipeReceiveEventArgs args)
        {
            string msg = args.Received;

            // Must get a confirmation as a single message
            if (msg.Length > 0 && msg[0] == '+')
            {
                if (mSendBuffer.Count > 0) mSendBuffer.RemoveAt(0);
                msg = msg.Substring(1);
            }

            mInputBuffer.Append(msg);
            string inbuf = mInputBuffer.ToString();
            bool matched = false;
            bool parsed = false;

            // Leave if there's no '$' in the buffer.  We don't need to
            // process further
            if (inbuf.IndexOf('$') == -1)
            {
            mInputBuffer = new StringBuilder("");
            return;
            }

            Match m = mPacketRegex.Match(inbuf);

            while (m.Success)
            {
                string payload = m.Groups["payload"].ToString();
                string checksum = m.Groups["checksum"].ToString();
                string midstr = m.Groups["mid"].ToString();
                int mid = int.Parse(midstr, NumberStyles.HexNumber);
                matched = true;

                if (CheckSum(payload, int.Parse(checksum, NumberStyles.HexNumber)))
                {
                    parsed = true;
                    try
                    {
                        if (mid != mLastId)
                        {
                            mLastId = mid;
                            ReceivePacket(payload);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error parsing [{0}]: {1}", payload, e.Message);
                    }
                    mPipe.Write("+");
                }
                mInputBuffer = new StringBuilder("");
                mInputBuffer.Append(inbuf = m.Groups["tail"].ToString());
                m = mPacketRegex.Match(inbuf);
            }

            // On the safe side, send a nak if we got a packet, but it didn't
            // check
            if (matched && !parsed)
            {
                mPipe.Write("-");
            }
        }
Exemple #2
0
        void RecvFired(object sender, PipeReceiveEventArgs args)
        {
            string msg = args.Received;

            // Must get a confirmation as a single message
            if (msg.Length > 0 && msg[0] == '+')
            {
                if (mSendBuffer.Count > 0)
                {
                    mSendBuffer.RemoveAt(0);
                }
                msg = msg.Substring(1);
            }

            mInputBuffer.Append(msg);
            string inbuf   = mInputBuffer.ToString();
            bool   matched = false;
            bool   parsed  = false;

            // Leave if there's no '$' in the buffer.  We don't need to
            // process further
            if (inbuf.IndexOf('$') == -1)
            {
                mInputBuffer = new StringBuilder("");
                return;
            }

            Match m = mPacketRegex.Match(inbuf);

            while (m.Success)
            {
                string payload  = m.Groups["payload"].ToString();
                string checksum = m.Groups["checksum"].ToString();
                string midstr   = m.Groups["mid"].ToString();
                int    mid      = int.Parse(midstr, NumberStyles.HexNumber);
                matched = true;

                if (CheckSum(payload, int.Parse(checksum, NumberStyles.HexNumber)))
                {
                    parsed = true;
                    try
                    {
                        if (mid != mLastId)
                        {
                            mLastId = mid;
                            ReceivePacket(payload);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error parsing [{0}]: {1}", payload, e.Message);
                    }
                    mPipe.Write("+");
                }
                mInputBuffer = new StringBuilder("");
                mInputBuffer.Append(inbuf = m.Groups["tail"].ToString());
                m = mPacketRegex.Match(inbuf);
            }

            // On the safe side, send a nak if we got a packet, but it didn't
            // check
            if (matched && !parsed)
            {
                mPipe.Write("-");
            }
        }