public void ReceiveCallback(IAsyncResult ar)
        {
            SIPConnection sipTLSConnection = (SIPConnection)ar.AsyncState;

            if (sipTLSConnection != null && sipTLSConnection.SIPStream != null && sipTLSConnection.SIPStream.CanRead)
            {
                try
                {
                    int bytesRead = sipTLSConnection.SIPStream.EndRead(ar);
                    if (sipTLSConnection.SocketReadCompleted(bytesRead))
                    {
                        sipTLSConnection.SIPStream.BeginRead(sipTLSConnection.SocketBuffer, sipTLSConnection.SocketBufferEndPosition, MaxSIPTCPMessageSize - sipTLSConnection.SocketBufferEndPosition, new AsyncCallback(ReceiveCallback), sipTLSConnection);
                    }
                }
                catch (SocketException sockExcp)  // Occurs if the remote end gets disconnected.
                {
                    logger.LogWarning("SocketException SIPTLSChannel ReceiveCallback. " + sockExcp);
                }
                catch (Exception excp)
                {
                    logger.LogWarning("Exception SIPTLSChannel ReceiveCallback. " + excp);
                    SIPTLSSocketDisconnected(sipTLSConnection.RemoteEndPoint);
                }
            }
        }
Ejemplo n.º 2
0
        private void SocketRead_Completed(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                if (m_sipConnection.SocketReadCompleted(e.BytesTransferred))
                {
                    SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
                    receiveArgs.SetBuffer(m_sipConnection.SocketBuffer, m_sipConnection.SocketBufferEndPosition, MaxSIPTCPMessageSize - m_sipConnection.SocketBufferEndPosition);
                    receiveArgs.Completed += SocketRead_Completed;

                    if (receiveArgs != null)
                    {
                        m_socket.ReceiveAsync(receiveArgs);
                    }
                }
            }
            catch (Exception excp)
            {
                logger.Error("Exception SilverlightTCPSIPChannel SocketRead_Completed. " + excp.Message);
                throw;
            }
        }
            public void TestSocketReadWithTwoMessagesAndBytesToSkipTest()
            {
                Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

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

includesdp=true       

 
 SUBSCRIBE sip:[email protected] SIP/2.0
Via: SIP/2.0/TCP 10.1.1.5:62647;branch=z9hG4bKa58b912c426f415daa887289efda50cd;rport
To: <sip:[email protected]>
From: <sip:[email protected]>;tag=1902440575
Call-ID: 1b569032-d1e4-4869-be9f-67d4ba8a4e3a
CSeq: 3 SUBSCRIBE

SUBSCRIBE sip:[email protected]";

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

                SIPConnection testConnection = new SIPConnection(null, (Stream)null, new IPEndPoint(IPAddress.Loopback, 0), SIPProtocolsEnum.tcp, SIPConnectionsEnum.Caller);
                int sipMessages = 0;
                testConnection.SIPMessageReceived += (chan, ep, buffer) => { sipMessages++; };
                Array.Copy(testReceiveBytes, testConnection.SocketBuffer, testReceiveBytes.Length);

                bool result = testConnection.SocketReadCompleted(testReceiveBytes.Length);
                string remainingBytes =  Encoding.UTF8.GetString(testConnection.SocketBuffer, 0, testConnection.SocketBufferEndPosition);

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

                Assert.IsTrue(result, "The result from processing the socket read should have been true.");
                Assert.IsTrue(sipMessages == 2, "The number of SIP messages parsed was incorrect.");
                Assert.IsTrue(testConnection.SocketBufferEndPosition == 26, "The receive buffer end position was incorrect.");
                Assert.IsTrue(remainingBytes == "SUBSCRIBE sip:[email protected]", "The leftover bytes in the socket buffer were incorrect.");
            }
            public void TestSocketReadSingleMessageTest()
            {
                Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

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

includesdp=true";

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

                SIPConnection testConnection = new SIPConnection(null, (Stream)null, null, SIPProtocolsEnum.tcp, SIPConnectionsEnum.Caller);
                Array.Copy(testReceiveBytes, testConnection.SocketBuffer, testReceiveBytes.Length);

                bool result = testConnection.SocketReadCompleted(testReceiveBytes.Length);

                Assert.IsTrue(result, "The result of processing the receive should have been true.");
                Assert.IsTrue(testConnection.SocketBufferEndPosition == 0, "The receive buffer end position should have been 0.");
            }