public void ReadLine_WrapAround2()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("test\n1");
            socket.AddResponse("2\n34");
            socket.AddResponse("56\n");

            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();

            Assert.AreEqual("test\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("12\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("3456\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #2
0
        /// <summary>
        /// Attempts to establish a connection with the i2cproxy running on the robot.
        /// </summary>
        /// <exception cref="InvalidOperationException">If Host, CommandPort or PollPort aren't setup correctly, or is not in the Disconnected state.</exception>
        /// <returns>True if successful, false otherwise.</returns>
        public bool Connect()
        {
            if (String.IsNullOrEmpty(Host))
            {
                throw new InvalidOperationException("Host hasn't been set.");
            }
            if (CommandPort == 0)
            {
                throw new InvalidOperationException("CommandPort hasn't been set.");
            }
            if (PollPort == 0)
            {
                throw new InvalidOperationException("PollPort hasn't been set.");
            }

            lock (stateLock)
            {
                if (State != BusState.Disconnected)
                {
                    throw new InvalidOperationException("Can't connect unless in the disconnected state.");
                }
                State = BusState.Connecting;
            }

            Debug.WriteLine(String.Format("Creating command socket: host={0}, port={1}", Host, CommandPort));
            commandSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            commandSocket.ReceiveTimeout = 5000;
            commandSocket.SendTimeout    = 5000;
            try { commandSocket.Connect(Host, CommandPort); }
            catch (SocketException e)
            {
                Debug.WriteLine(String.Format("Couldn't connect to {0}:{1}. The error was: {2}", Host, CommandPort, e.Message));
                lock (stateLock) State = BusState.Disconnected;
                commandSocket.Close();
                return(false);
            }
            commandLineReader = new BufferedLineReader((buffer, index, count) => commandSocket.Receive(buffer, index, count, SocketFlags.None), CommandSocketBufferSize);

            Debug.WriteLine(String.Format("Creating poll socket: host={0}, port={1}", Host, PollPort));
            pollSocket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            pollSocket.SendTimeout = 5000;
            try { pollSocket.Connect(Host, PollPort); }
            catch (SocketException e)
            {
                Debug.WriteLine(String.Format("Couldn't connect to {0}:{1}. The error was: {2}", Host, PollPort, e.Message));
                lock (stateLock) State = BusState.Disconnected;
                pollSocket.Close();
                commandSocket.Close();
                return(false);
            }
            pollLineReader = new BufferedLineReader((buffer, index, count) => ReadFromSocket(pollSocket, buffer, index, count), PollSocketBufferSize);

            lock (stateLock) State = BusState.Connected;
            channelConnectEvent.Set();

            Debug.WriteLine("Connected");
            return(true);
        }
        public void ReadLine_OverflowsBuffer_ThrowsException()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("12345");
            socket.AddResponse("67890");
            socket.AddResponse("\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();
        }
        public void ReadLine_OverflowsBuffer_ThrowsException()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("12345");
            socket.AddResponse("67890");
            socket.AddResponse("\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();
        }
        public void ReadLine_FillBuffer()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("123456789\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();

            Assert.AreEqual("123456789\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
        public void ReadLine_FillBuffer()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("123456789\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();

            Assert.AreEqual("123456789\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
        public void ReadLine_OneLineInMultiplePieces()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("te");
            socket.AddResponse("st\n");
            var reader = new BufferedLineReader(socket.Receive, 20);

            string result = reader.ReadLine();

            Assert.AreEqual("test\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
        public void ReadLine_OneLineInMultiplePieces()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("te");
            socket.AddResponse("st\n");
            var reader = new BufferedLineReader(socket.Receive, 20);

            string result = reader.ReadLine();

            Assert.AreEqual("test\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
        public void ReadLine_RandomTest()
        {
            // Generate some random test data.
            random = new Random(RandomSeed);
            int countTillNextLine = random.Next() % MaxLineLength;

            for (int i = 0; i < SendBufferSize - 1; i++)
            {
                if (countTillNextLine-- > 0)
                {
                    sendBuffer[i] = (byte)((random.Next() % 26) + 65);
                }
                else
                {
                    sendBuffer[i]     = 10;
                    countTillNextLine = random.Next() % MaxLineLength;
                }
            }
            sendBuffer[SendBufferSize - 1] = 10;

            // Setup a line reader.
            var reader = new BufferedLineReader(ReadTestData, CircularBufferSize);
            int startOfNextExpectedLine = 0, numLines = 0;

            while (true)
            {
                string result = reader.ReadLine();
                if (result == null)
                {
                    break;
                }

                Assert.IsTrue(result.EndsWith("\n"));

                int expectedEndOfLine = Array.IndexOf(sendBuffer, (byte)10, startOfNextExpectedLine, SendBufferSize - startOfNextExpectedLine);
                int expectedLength    = expectedEndOfLine - startOfNextExpectedLine + 1;
                Assert.AreEqual(expectedLength, result.Length);

                string expectedResult = ASCIIEncoding.ASCII.GetString(sendBuffer, startOfNextExpectedLine, expectedEndOfLine - startOfNextExpectedLine + 1);
                Assert.AreEqual(expectedResult, result);

                startOfNextExpectedLine += expectedLength;
                numLines++;
            }

            Assert.IsTrue(startOfNextExpectedLine == SendBufferSize);
        }
Пример #10
0
        public void ReadLine_ToTheEndOfTheBuffer()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("TEST\ntest");
            socket.AddResponse("\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();

            Assert.AreEqual("TEST\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #11
0
        public void ReadLine_ReceiveMultipleLinesAtOnce()
        {
            MockSocket socket = new MockSocket();

            socket.AddResponse("test1\ntest2\ntest3\n");
            var reader = new BufferedLineReader(socket.Receive, 20);

            string result = reader.ReadLine();

            Assert.AreEqual("test1\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test2\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test3\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #12
0
        public void ReadLine_RandomTest()
        {
            // Generate some random test data.
            random = new Random(RandomSeed);
            int countTillNextLine = random.Next() % MaxLineLength;
            for (int i = 0; i < SendBufferSize-1; i++)
                if (countTillNextLine-- > 0)
                    sendBuffer[i] = (byte)((random.Next() % 26) + 65);
                else
                {
                    sendBuffer[i] = 10;
                    countTillNextLine = random.Next() % MaxLineLength;
                }
            sendBuffer[SendBufferSize - 1] = 10;

            // Setup a line reader.
            var reader = new BufferedLineReader(ReadTestData, CircularBufferSize);
            int startOfNextExpectedLine = 0, numLines = 0;
            while (true)
            {
                string result = reader.ReadLine();
                if (result == null) break;

                Assert.IsTrue(result.EndsWith("\n"));

                int expectedEndOfLine = Array.IndexOf(sendBuffer, (byte)10, startOfNextExpectedLine, SendBufferSize - startOfNextExpectedLine);
                int expectedLength = expectedEndOfLine - startOfNextExpectedLine + 1;
                Assert.AreEqual(expectedLength, result.Length);

                string expectedResult = ASCIIEncoding.ASCII.GetString(sendBuffer, startOfNextExpectedLine, expectedEndOfLine - startOfNextExpectedLine + 1);
                Assert.AreEqual(expectedResult, result);

                startOfNextExpectedLine += expectedLength;
                numLines++;
            }

            Assert.IsTrue(startOfNextExpectedLine == SendBufferSize);
        }
Пример #13
0
        public void ReadLine_WrapAround2()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("test\n1");
            socket.AddResponse("2\n34");
            socket.AddResponse("56\n");

            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();
            Assert.AreEqual("test\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("12\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("3456\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #14
0
        public void ReadLine_ToTheEndOfTheBuffer()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("TEST\ntest");
            socket.AddResponse("\n");
            var reader = new BufferedLineReader(socket.Receive, 10);

            string result = reader.ReadLine();
            Assert.AreEqual("TEST\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #15
0
        public void ReadLine_ReceiveMultipleLinesAtOnce()
        {
            MockSocket socket = new MockSocket();
            socket.AddResponse("test1\ntest2\ntest3\n");
            var reader = new BufferedLineReader(socket.Receive, 20);

            string result = reader.ReadLine();
            Assert.AreEqual("test1\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test2\n", result);
            Assert.IsFalse(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);

            result = reader.ReadLine();
            Assert.AreEqual("test3\n", result);
            Assert.IsTrue(reader.IsEmpty);
            Assert.IsFalse(reader.IsFull);
        }
Пример #16
0
        /// <summary>
        /// Attempts to establish a connection with the i2cproxy running on the robot.
        /// </summary>
        /// <exception cref="InvalidOperationException">If Host, CommandPort or PollPort aren't setup correctly, or is not in the Disconnected state.</exception>
        /// <returns>True if successful, false otherwise.</returns>
        public bool Connect()
        {
            if (String.IsNullOrEmpty(Host)) throw new InvalidOperationException("Host hasn't been set.");
            if (CommandPort == 0) throw new InvalidOperationException("CommandPort hasn't been set.");
            if (PollPort == 0) throw new InvalidOperationException("PollPort hasn't been set.");

            lock (stateLock)
            {
                if (State != BusState.Disconnected) throw new InvalidOperationException("Can't connect unless in the disconnected state.");
                State = BusState.Connecting;
            }

            Debug.WriteLine(String.Format("Creating command socket: host={0}, port={1}", Host, CommandPort));
            commandSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            commandSocket.ReceiveTimeout = 5000;
            commandSocket.SendTimeout = 5000;
            try { commandSocket.Connect(Host, CommandPort); }
            catch (SocketException e)
            {
                Debug.WriteLine(String.Format("Couldn't connect to {0}:{1}. The error was: {2}", Host, CommandPort, e.Message));
                lock (stateLock) State = BusState.Disconnected;
                commandSocket.Close();
                return false;
            }
            commandLineReader = new BufferedLineReader((buffer, index, count) => commandSocket.Receive(buffer, index, count, SocketFlags.None), CommandSocketBufferSize);

            Debug.WriteLine(String.Format("Creating poll socket: host={0}, port={1}", Host, PollPort));
            pollSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            pollSocket.SendTimeout = 5000;
            try { pollSocket.Connect(Host, PollPort); }
            catch (SocketException e)
            {
                Debug.WriteLine(String.Format("Couldn't connect to {0}:{1}. The error was: {2}", Host, PollPort, e.Message));
                lock (stateLock) State = BusState.Disconnected;
                pollSocket.Close();
                commandSocket.Close();
                return false;
            }
            pollLineReader = new BufferedLineReader((buffer, index, count) => ReadFromSocket(pollSocket, buffer, index, count), PollSocketBufferSize);

            lock (stateLock) State = BusState.Connected;
            channelConnectEvent.Set();

            Debug.WriteLine("Connected");
            return true;
        }