Esempio n. 1
0
        static void Main(string[] args)
        {
            STOMPClient Client = new STOMPClient();

            NextTest("Frame Serialize, Deserialize, Serialize");

            StompSendFrame Frame = new StompSendFrame("MyDestination", "This is the packet body here");

            Frame.Receipt = "ReceiptIdHere";
            Frame.AdditionalHeaders.Add(new KeyValuePair <string, string>("my-header", "MyValue"));

            string Output = Client.GetSerializedPacket(Frame);

            Console.WriteLine(Output);

            StompFrame Rebuilt = Client.GetBuiltFrame(System.Text.Encoding.UTF8.GetBytes(Output));

            Console.WriteLine("");
            Console.WriteLine("------");
            Console.WriteLine("");

            Output = Client.GetSerializedPacket(Rebuilt);
            Console.WriteLine(Output);

            Console.WriteLine("");
            Console.WriteLine("------");

            NextTest("RingBuffer Data Length");

            StompRingBuffer <int> Buffer = new StompRingBuffer <int>(128);

            int[] Data = new int[64];
            for (int i = 0; i < Data.Length; i++)
            {
                Data[i] = i + 1;
            }

            Buffer.Write(Data);

            Console.WriteLine(string.Format("Available Write before read: {0}", Buffer.AvailableWrite));

            int[] Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Available Write after read: {0}", Buffer.AvailableWrite));
            Console.WriteLine(string.Format("Lengths: {0} and {1}", Data.Length, Data2.Length));

            NextTest("Data Comparison");

            for (int i = 0; i < Math.Min(Data.Length, Data2.Length); i++)
            {
                Console.WriteLine(string.Format("{0} {1}", Data[i], Data2[i]));
            }

            NextTest("Seek Back 32");

            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(-32)));
            Console.WriteLine(string.Format("Available is {0}", Buffer.AvailableRead));
            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Read out {0}", Data2.Length));
            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(0)));

            NextTest("Old Data");


            foreach (int D in Data2)
            {
                Console.WriteLine(string.Format("{0} ", D));
            }

            NextTest("Write and Read");

            Buffer.Write(Data, 32);
            Buffer.Seek(-32);
            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Write 32; Read out {0}", Data2.Length));

            NextTest("Data");

            foreach (int D in Data2)
            {
                Console.WriteLine(string.Format("{0} ", D));
            }

            NextTest("Seek Forward");

            Console.WriteLine(string.Format("Available Read is {0}, should be 0", Buffer.AvailableRead));

            Console.WriteLine(string.Format("Write {0}", Data.Length));
            Buffer.Write(Data);

            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));
            Buffer.Seek(10);
            Console.WriteLine(string.Format("Seek; Ptr is {0}", Buffer.Seek(0)));
            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));

            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Read out {0}", Data2.Length, Data.Length));
            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));

            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(0)));

            NextTest("Data");

            foreach (int D in Data2)
            {
                Console.WriteLine(string.Format("{0} ", D));
            }

            Console.ReadLine();
        }
Esempio n. 2
0
        /// <summary>
        ///     Tries to build a packet from the given ringbuffer
        /// </summary>
        /// <param name="Buffer">
        ///     The Ringbuffer to build a packet from
        /// </param>
        /// <returns>
        ///     Whether it was able to build a packet or not
        /// </returns>
        public bool TryBuildPacket(StompRingBuffer <byte> Buffer)
        {
            // See if we have rx'd a packet separator or a \0 in a binary frame body
            int PacketLength = Buffer.DistanceTo(0);

            // We have, so what did we find?
            if (PacketLength > 0)
            {
                // This is a really messy block of code.

                // The goal is that it tries to determine whether it has a full packet or needs to wait for more data
                // before building the packet and dispatching it

                byte[]   Data             = Buffer.Peek(PacketLength);
                string   Header           = Encoding.UTF8.GetString(Data);
                string[] HeaderCheck      = Header.Split('\n');
                int      ContentLength    = 0;
                bool     HasContentLength = false;

                // First, we look to see if our "packet" has a content-length header.  Since we scanned out to a null (\0) byte, we're guaranteed to at least have the headers
                // of whatever packet we're examining

                for (int i = 0; i < HeaderCheck.Length && HeaderCheck[i] != "" && HeaderCheck[i] != "\r"; i++)
                {
                    // We found a content-length header?  Flag it and store how large in bytes the content should be
                    if (HeaderCheck[i].StartsWith("content-length:"))
                    {
                        HasContentLength = true;
                        ContentLength    = int.Parse(HeaderCheck[i].Substring(15));
                    }
                }
                StompFrame Frame = null;

                if (HasContentLength)
                {
                    // We have a content-length header.  We need to find the start of the frame body, in bytes,
                    // and then make sure we have (ContentLength) bytes available after that

                    // Look for the end of the headers, either 1.0/1.1 or 1.2 (\r\n)-friendly
                    int EndOfHeaders = Header.IndexOf("\r\n\r\n") + 4;
                    if (EndOfHeaders == 3) // (-1) + 4
                    {
                        EndOfHeaders = Header.IndexOf("\n\n") + 2;
                    }

                    // Get the byte length of the header
                    int Offset = Encoding.UTF8.GetByteCount(Header.Substring(0, EndOfHeaders));

                    // Now see if we have that many bytes available in the ring buffer (realistically, we should except for obscene frame sizes)
                    if (Offset + ContentLength <= Buffer.AvailableRead)
                    {
                        // If we do, peek the exact packet length we want and assemble
                        Frame = StompFrame.Build(Buffer.Peek(Offset + ContentLength), _FrameTypeMapping);
                        Buffer.Seek(Offset + ContentLength);
                        DispatchFrame(Frame);

                        return(true);
                    }
                }
                else // No content-length.  We're guaranteed to be a text packet without any overshoot; no special treatment needed
                {
                    Frame = StompFrame.Build(Data, _FrameTypeMapping);
                    Buffer.Seek(PacketLength);
                    DispatchFrame(Frame);

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 3
0
        /// <summary>
        ///     Tries to build a packet from the given ringbuffer
        /// </summary>
        /// <param name="Buffer">
        ///     The Ringbuffer to build a packet from
        /// </param>
        /// <returns>
        ///     Whether it was able to build a packet or not
        /// </returns>
        public bool TryBuildPacket(StompRingBuffer<byte> Buffer)
        {
            // See if we have rx'd a packet separator or a \0 in a binary frame body
            int PacketLength = Buffer.DistanceTo(0);

            // We have, so what did we find?
            if (PacketLength > 0)
            {
                // This is a really messy block of code.

                // The goal is that it tries to determine whether it has a full packet or needs to wait for more data
                // before building the packet and dispatching it

                byte[] Data = Buffer.Peek(PacketLength);
                string Header = Encoding.UTF8.GetString(Data);
                string[] HeaderCheck = Header.Split('\n');
                int ContentLength = 0;
                bool HasContentLength = false;

                // First, we look to see if our "packet" has a content-length header.  Since we scanned out to a null (\0) byte, we're guaranteed to at least have the headers
                // of whatever packet we're examining

                for (int i = 0; i < HeaderCheck.Length && HeaderCheck[i] != "" && HeaderCheck[i] != "\r"; i++)
                {
                    // We found a content-length header?  Flag it and store how large in bytes the content should be
                    if (HeaderCheck[i].StartsWith("content-length:"))
                    {
                        HasContentLength = true;
                        ContentLength = int.Parse(HeaderCheck[i].Substring(15));
                    }
                }
                StompFrame Frame = null;

                if (HasContentLength)
                {
                    // We have a content-length header.  We need to find the start of the frame body, in bytes,
                    // and then make sure we have (ContentLength) bytes available after that

                    // Look for the end of the headers, either 1.0/1.1 or 1.2 (\r\n)-friendly
                    int EndOfHeaders = Header.IndexOf("\r\n\r\n") + 4;
                    if (EndOfHeaders == 3) // (-1) + 4
                        EndOfHeaders = Header.IndexOf("\n\n") + 2;

                    // Get the byte length of the header
                    int Offset = Encoding.UTF8.GetByteCount(Header.Substring(0, EndOfHeaders));

                    // Now see if we have that many bytes available in the ring buffer (realistically, we should except for obscene frame sizes)
                    if (Offset + ContentLength <= Buffer.AvailableRead)
                    {
                        // If we do, peek the exact packet length we want and assemble
                        Frame = StompFrame.Build(Buffer.Peek(Offset + ContentLength), _FrameTypeMapping);
                        Buffer.Seek(Offset + ContentLength);
                        DispatchFrame(Frame);

                        return true;
                    }
                }
                else // No content-length.  We're guaranteed to be a text packet without any overshoot; no special treatment needed
                {
                    Frame = StompFrame.Build(Data, _FrameTypeMapping);
                    Buffer.Seek(PacketLength);
                    DispatchFrame(Frame);

                    return true;
                }
            }

            return false;
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            STOMPClient Client = new STOMPClient();

            NextTest("Frame Serialize, Deserialize, Serialize");

            StompSendFrame Frame = new StompSendFrame("MyDestination", "This is the packet body here");

            Frame.Receipt = "ReceiptIdHere";
            Frame.AdditionalHeaders.Add(new KeyValuePair<string, string>("my-header", "MyValue"));

            string Output = Client.GetSerializedPacket(Frame);

            Console.WriteLine(Output);

            StompFrame Rebuilt = Client.GetBuiltFrame(System.Text.Encoding.UTF8.GetBytes(Output));

            Console.WriteLine("");
            Console.WriteLine("------");
            Console.WriteLine("");

            Output = Client.GetSerializedPacket(Rebuilt);
            Console.WriteLine(Output);

            Console.WriteLine("");
            Console.WriteLine("------");

            NextTest("RingBuffer Data Length");

            StompRingBuffer<int> Buffer = new StompRingBuffer<int>(128);

            int[] Data = new int[64];
            for (int i = 0; i < Data.Length; i++)
                Data[i] = i + 1;

            Buffer.Write(Data);

            Console.WriteLine(string.Format("Available Write before read: {0}", Buffer.AvailableWrite));

            int[] Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Available Write after read: {0}", Buffer.AvailableWrite));
            Console.WriteLine(string.Format("Lengths: {0} and {1}", Data.Length, Data2.Length));

            NextTest("Data Comparison");

            for (int i = 0; i < Math.Min(Data.Length, Data2.Length); i++)
                Console.WriteLine(string.Format("{0} {1}", Data[i], Data2[i]));

            NextTest("Seek Back 32");

            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(-32)));
            Console.WriteLine(string.Format("Available is {0}", Buffer.AvailableRead));
            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Read out {0}", Data2.Length));
            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(0)));

            NextTest("Old Data");

            foreach (int D in Data2)
                Console.WriteLine(string.Format("{0} ", D));

            NextTest("Write and Read");

            Buffer.Write(Data, 32);
            Buffer.Seek(-32);
            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Write 32; Read out {0}", Data2.Length));

            NextTest("Data");

            foreach (int D in Data2)
                Console.WriteLine(string.Format("{0} ", D));

            NextTest("Seek Forward");

            Console.WriteLine(string.Format("Available Read is {0}, should be 0", Buffer.AvailableRead));

            Console.WriteLine(string.Format("Write {0}", Data.Length));
            Buffer.Write(Data);

            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));
            Buffer.Seek(10);
            Console.WriteLine(string.Format("Seek; Ptr is {0}", Buffer.Seek(0)));
            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));

            Data2 = Buffer.Read(Buffer.AvailableRead);
            Console.WriteLine(string.Format("Read out {0}", Data2.Length, Data.Length));
            Console.WriteLine(string.Format("Available Read is {0}", Buffer.AvailableRead));

            Console.WriteLine(string.Format("Seek Ptr is {0}", Buffer.Seek(0)));

            NextTest("Data");

            foreach (int D in Data2)
                Console.WriteLine(string.Format("{0} ", D));

            Console.ReadLine();
        }