public void TestSocketReadWithTwoMessagesAndBytesToSkipTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            string testReceive =
                "            SUBSCRIBE sip:[email protected] SIP/2.0" + CRLF +
                "Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport" + CRLF +
                "To: <sip:[email protected]>" + CRLF +
                "From: <sip:[email protected]>;tag=1902440575" + CRLF +
                "Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a" + CRLF +
                "CSeq: 3 SUBSCRIBE" + CRLF +
                "Contact: <sip:10.1.1.5:62647;transport=tcp>" + CRLF +
                "Max-Forwards: 70" + CRLF +
                "Expires: 600" + CRLF +
                "Content-Length: 15" + CRLF +
                "Content-Type: text/text" + CRLF +
                "Event: dialog" +
                CRLF + CRLF +
                "includesdp=true       " +
                CRLF +
                " SUBSCRIBE sip:[email protected] SIP/2.0" + CRLF +
                "Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport" + CRLF +
                "To: <sip:[email protected]>" + CRLF +
                "From: <sip:[email protected]>;tag=1902440575" + CRLF +
                "Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a" + CRLF +
                "CSeq: 3 SUBSCRIBE" + CRLF
                + CRLF +
                "SUBSCRIBE sip:[email protected]";

            byte[] testReceiveBytes = UTF8Encoding.UTF8.GetBytes(testReceive);

            Socket dummySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            dummySocket.Bind(new IPEndPoint(IPAddress.Any, 0));
            SIPStreamConnection testConnection = new SIPStreamConnection(dummySocket, new IPEndPoint(IPAddress.Loopback, 0), SIPProtocolsEnum.tcp);
            int sipMessages = 0;

            testConnection.SIPMessageReceived += (chan, localEp, ep, buffer) => { sipMessages++; return(Task.FromResult(0)); };
            Array.Copy(testReceiveBytes, 0, testConnection.RecvSocketArgs.Buffer, 0, testReceiveBytes.Length);

            MockSIPChannel mockChannel = new MockSIPChannel(new IPEndPoint(IPAddress.Any, 0));

            testConnection.ExtractSIPMessages(mockChannel, testConnection.RecvSocketArgs.Buffer, testReceiveBytes.Length);
            string remainingBytes = Encoding.UTF8.GetString(testConnection.RecvSocketArgs.Buffer, testConnection.RecvStartPosn, testConnection.RecvEndPosn - testConnection.RecvStartPosn);

            logger.LogDebug("SocketBufferEndPosition=" + testConnection.RecvEndPosn + ".");
            logger.LogDebug("SocketBuffer=" + remainingBytes + ".");

            Assert.True(sipMessages == 2, "The number of SIP messages parsed was incorrect.");
            Assert.True(708 == testConnection.RecvStartPosn, $"The receive buffer start position was incorrect, was {testConnection.RecvStartPosn}.");
            Assert.True(734 == testConnection.RecvEndPosn, $"The receive buffer end position was incorrect, was {testConnection.RecvEndPosn}.");
            Assert.True(remainingBytes == "SUBSCRIBE sip:[email protected]", $"The leftover bytes in the socket buffer were incorrect {remainingBytes}.");
        }
        public void TestSocketReadWithTwoMessagesAndBytesToSkipTest()
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            string testReceive =
                @"            SUBSCRIBE sip:[email protected] SIP/2.0" + CRLF +
                "Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport" + CRLF +
                "To: <sip:[email protected]>" + CRLF +
                "From: <sip:[email protected]>;tag=1902440575" + CRLF +
                "Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a" + CRLF +
                "CSeq: 3 SUBSCRIBE" + CRLF +
                "Contact: <sip:10.1.1.5:62647;transport=tcp>" + CRLF +
                "Max-Forwards: 70" + CRLF +
                "Expires: 600" + CRLF +
                "Content-Length: 15" + CRLF +
                "Content-Type: text/text" + CRLF +
                "Event: dialog" +
                CRLF + CRLF +
                "includesdp=true       " +
                CRLF +
                " SUBSCRIBE sip:[email protected] SIP/2.0" + CRLF +
                "Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport" + CRLF +
                "To: <sip:[email protected]>" + CRLF +
                "From: <sip:[email protected]>;tag=1902440575" + CRLF +
                "Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a" + CRLF +
                "CSeq: 3 SUBSCRIBE" + CRLF
                + CRLF +
                "SUBSCRIBE sip:[email protected]";

            byte[] testReceiveBytes = UTF8Encoding.UTF8.GetBytes(testReceive);

            SIPStreamConnection testConnection = new SIPStreamConnection(null, new IPEndPoint(IPAddress.Loopback, 0), SIPProtocolsEnum.tcp, SIPConnectionsEnum.Caller);
            int sipMessages = 0;

            testConnection.SIPMessageReceived += (chan, ep, buffer) => { sipMessages++; };
            Array.Copy(testReceiveBytes, 0, testConnection.RecvSocketArgs.Buffer, 0, testReceiveBytes.Length);

            testConnection.ExtractSIPMessages(null, testConnection.RecvSocketArgs.Buffer, testReceiveBytes.Length);
            string remainingBytes = Encoding.UTF8.GetString(testConnection.RecvSocketArgs.Buffer, testConnection.RecvStartPosn, testConnection.RecvEndPosn - testConnection.RecvStartPosn);

            Console.WriteLine("SocketBufferEndPosition=" + testConnection.RecvEndPosn + ".");
            Console.WriteLine("SocketBuffer=" + remainingBytes + ".");

            Assert.IsTrue(sipMessages == 2, "The number of SIP messages parsed was incorrect.");
            Assert.AreEqual(708, testConnection.RecvStartPosn, $"The receive buffer start position was incorrect, was {testConnection.RecvStartPosn}.");
            Assert.AreEqual(734, testConnection.RecvEndPosn, $"The receive buffer end position was incorrect, was {testConnection.RecvEndPosn}.");
            Assert.IsTrue(remainingBytes == "SUBSCRIBE sip:[email protected]", $"The leftover bytes in the socket buffer were incorrect {remainingBytes}.");
        }
        public void TestSocketReadWithBytesToSkipTest()
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            string testReceive =
                "            SUBSCRIBE sip:[email protected] SIP/2.0" + CRLF +
                "Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport" + CRLF +
                "To: <sip:[email protected]>" + CRLF +
                "From: <sip:[email protected]>;tag=1902440575" + CRLF +
                "Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a" + CRLF +
                "CSeq: 3 SUBSCRIBE" + CRLF +
                "Contact: <sip:10.1.1.5:62647;transport=tcp>" + CRLF +
                "Max-Forwards: 70" + CRLF +
                "Expires: 600" + CRLF +
                "Content-Length: 15" + CRLF +
                "Content-Type: text/text" + CRLF +
                "Event: dialog" +
                CRLF + CRLF +
                "includesdp=true";

            byte[] testReceiveBytes = UTF8Encoding.UTF8.GetBytes(testReceive);

            Socket dummySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            dummySocket.Bind(new IPEndPoint(IPAddress.Any, 0));
            SIPStreamConnection testConnection = new SIPStreamConnection(dummySocket, new IPEndPoint(IPAddress.Loopback, 0), SIPProtocolsEnum.tcp);
            int sipMessages = 0;

            testConnection.SIPMessageReceived += (chan, localEp, ep, buffer) => { sipMessages++; };

            MockSIPChannel mockChannel = new MockSIPChannel(new IPEndPoint(IPAddress.Any, 0));

            testConnection.ExtractSIPMessages(mockChannel, testReceiveBytes, testReceiveBytes.Length);

            Assert.True(sipMessages == 1, "The number of SIP messages parsed was incorrect, was " + sipMessages + ".");
            Assert.True(testConnection.RecvStartPosn == 0, $"The receive buffer start position was incorrect, was {testConnection.RecvStartPosn}.");
            Assert.True(testConnection.RecvEndPosn == 0, $"The receive buffer end position was incorrect, was {testConnection.RecvEndPosn}.");
        }