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
        private void Run()
        {
            byte[]                 RxData = new byte[512]; // Rx buffer used for transferring data from the stream to the ring buffer
            NetworkStream          Stream = _Client.GetStream();
            StompRingBuffer <byte> Buffer = new StompRingBuffer <byte>(RxBufferSize);

            // Run while the client is connected, polling for data rx'd and dispatching frames as necessary
            // Also handle heartbeats and heartbeat disconnect
            while (_Client.Connected)
            {
                // If we should be heartbeating, and we're connected...
                if (_Heartbeat > 0 && ConnectionVersion > 0.0f)
                {
                    // Sleep up to 15ms, or less if we need to heartbeat sooner
                    int SleepAmt = Math.Min(_HeartbeatTxIntervalTimeout, 15);

                    // If we expect to recieve heartbeats...
                    if (_HeartbeatRxInterval > 0)
                    {
                        // ...check that we've recieved data within the rx interval.  If not, disconnect.
                        _HeartbeatRxIntervalTimeout -= SleepAmt;
                        if (_HeartbeatRxIntervalTimeout < 0)
                        {
                            lock (_Client)
                            {
                                _Client.Close();
                            }
                            return;
                        }
                    }

                    // If we need to send heartbeats...
                    if (_HeartbeatTxInterval > 0)
                    {
                        // ... send one if it's been too long since our last transmission
                        _HeartbeatTxIntervalTimeout -= SleepAmt;
                        if (_HeartbeatTxIntervalTimeout < 0 && _HeartbeatTxInterval > 0)
                        {
                            _Client.GetStream().WriteByte((byte)'\n');
                            _HeartbeatTxIntervalTimeout = _HeartbeatTxInterval;
                        }
                    }

                    Thread.Sleep(SleepAmt);
                }
                else // Otherwise just do a standard sleep
                {
                    Thread.Sleep(15);
                }

                // Read in as much data from the stream as we can into the ring buffer
                while (_Client.Available > 0 && Buffer.AvailableWrite > 0)
                {
                    // We've received data, so reset the heartbeat rx timeout
                    _HeartbeatRxIntervalTimeout = (int)(_HeartbeatRxInterval * 1.5); // +50% forgiveness for heartbeat loss

                    // Now read in from the networkstream to the ring buffer
                    int AmtRead = Stream.Read(RxData, 0, Buffer.AvailableWrite);
                    Buffer.Write(RxData, AmtRead);
                }

                // Advance through any heartbeats rx'd or frame separators
                while (Buffer.Peek() == '\r' || Buffer.Peek() == '\n' || Buffer.Peek() == '\0')
                {
                    Buffer.Read(1);
                }

                // Now try to build + dispatch the packet
                if (!TryBuildPacket(Buffer) && Buffer.AvailableWrite == 0)
                {
                    throw new InvalidOperationException("Ran out of receive ringbuffer space in STOMPClient");
                }
            }
        }
Esempio n. 3
0
        private void Run()
        {
            byte[] RxData = new byte[512]; // Rx buffer used for transferring data from the stream to the ring buffer
            NetworkStream Stream = _Client.GetStream();
            StompRingBuffer<byte> Buffer = new StompRingBuffer<byte>(RxBufferSize);

            // Run while the client is connected, polling for data rx'd and dispatching frames as necessary
            // Also handle heartbeats and heartbeat disconnect
            while (_Client.Connected)
            {
                // If we should be heartbeating, and we're connected...
                if (_Heartbeat > 0 && ConnectionVersion > 0.0f)
                {
                    // Sleep up to 15ms, or less if we need to heartbeat sooner
                    int SleepAmt = Math.Min(_HeartbeatTxIntervalTimeout, 15);

                    // If we expect to recieve heartbeats...
                    if (_HeartbeatRxInterval > 0)
                    {
                        // ...check that we've recieved data within the rx interval.  If not, disconnect.
                        _HeartbeatRxIntervalTimeout -= SleepAmt;
                        if (_HeartbeatRxIntervalTimeout < 0)
                        {
                            lock (_Client)
                            {
                                _Client.Close();
                            }
                            return;
                        }
                    }

                    // If we need to send heartbeats...
                    if (_HeartbeatTxInterval > 0)
                    {
                        // ... send one if it's been too long since our last transmission
                        _HeartbeatTxIntervalTimeout -= SleepAmt;
                        if (_HeartbeatTxIntervalTimeout < 0 && _HeartbeatTxInterval > 0)
                        {
                            _Client.GetStream().WriteByte((byte)'\n');
                            _HeartbeatTxIntervalTimeout = _HeartbeatTxInterval;
                        }
                    }

                    Thread.Sleep(SleepAmt);
                }
                else // Otherwise just do a standard sleep
                {
                    Thread.Sleep(15);
                }

                // Read in as much data from the stream as we can into the ring buffer
                while (_Client.Available > 0 && Buffer.AvailableWrite > 0)
                {
                    // We've received data, so reset the heartbeat rx timeout
                    _HeartbeatRxIntervalTimeout = (int)(_HeartbeatRxInterval * 1.5); // +50% forgiveness for heartbeat loss

                    // Now read in from the networkstream to the ring buffer
                    int AmtRead = Stream.Read(RxData, 0, Buffer.AvailableWrite);
                    Buffer.Write(RxData, AmtRead);
                }

                // Advance through any heartbeats rx'd or frame separators
                while (Buffer.Peek() == '\r' || Buffer.Peek() == '\n' || Buffer.Peek() == '\0')
                    Buffer.Read(1);

                // Now try to build + dispatch the packet
                if (!TryBuildPacket(Buffer) && Buffer.AvailableWrite == 0)
                    throw new InvalidOperationException("Ran out of receive ringbuffer space in STOMPClient");

            }
        }
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();
        }