示例#1
0
        /// <summary>
        /// Attempts to extract SIP messages from the data that has been received on the SIP stream connection.
        /// </summary>
        /// <param name="recvChannel">The receiving SIP channel.</param>
        /// <param name="buffer">The buffer holding the current data from the stream. Note that the buffer can
        /// stretch over multiple receives.</param>
        /// <param name="bytesRead">The bytes that were read by the latest receive operation (the new bytes available).</param>
        public void ExtractSIPMessages(SIPChannel recvChannel, byte[] buffer, int bytesRead)
        {
            RecvEndPosn += bytesRead;

            int bytesSkipped = 0;

            byte[] sipMsgBuffer = SIPMessageBuffer.ParseSIPMessageFromStream(buffer, RecvStartPosn, RecvEndPosn, out bytesSkipped);

            while (sipMsgBuffer != null)
            {
                // A SIP message is available.
                if (SIPMessageReceived != null)
                {
                    LastTransmission = DateTime.Now;
                    SIPEndPoint localEndPoint  = new SIPEndPoint(ConnectionProtocol, StreamSocket.LocalEndPoint as IPEndPoint, recvChannel.ID, ConnectionID);
                    SIPEndPoint remoteEndPoint = new SIPEndPoint(ConnectionProtocol, RemoteEndPoint, recvChannel.ID, ConnectionID);
                    SIPMessageReceived(recvChannel, localEndPoint, remoteEndPoint, sipMsgBuffer);
                }

                RecvStartPosn += (sipMsgBuffer.Length + bytesSkipped);

                if (RecvStartPosn == RecvEndPosn)
                {
                    // All data has been successfully extracted from the receive buffer.
                    RecvStartPosn = RecvEndPosn = 0;
                    break;
                }
                else
                {
                    // Try and extract another SIP message from the receive buffer.
                    sipMsgBuffer = SIPMessageBuffer.ParseSIPMessageFromStream(buffer, RecvStartPosn, RecvEndPosn, out bytesSkipped);
                }
            }
        }
        /// <summary>
        /// Attempts to extract SIP messages from the data that has been received on the client web socket SIP stream connection.
        /// </summary>
        /// <param name="recvChannel">The receiving SIP channel.</param>
        /// <param name="clientConn">The web socket client connection properties including pending receive buffer</param>
        /// <param name="buffer">The buffer holding the current data from the stream. Note that the buffer can
        /// stretch over multiple receives.</param>
        /// <param name="bytesRead">The bytes that were read by the latest receive operation (the new bytes available).</param>
        private void ExtractSIPMessages(SIPChannel recvChannel, ClientWebSocketConnection clientConn,
                                        ArraySegment <byte> buffer, int bytesRead)
        {
            if (bytesRead + clientConn.RecvEndPosn > clientConn.PendingReceiveBuffer.Length)
            {
                // Don't have enough space for the read. Throw away the pending buffer and hope the SIP transaction is re-attempted.
                clientConn.RecvStartPosn = clientConn.RecvEndPosn = 0;
            }

            Buffer.BlockCopy(buffer.ToArray(), 0, clientConn.PendingReceiveBuffer, clientConn.RecvEndPosn, bytesRead);
            clientConn.RecvEndPosn += bytesRead;

            int bytesSkipped = 0;

            byte[] sipMsgBuffer = SIPMessageBuffer.ParseSIPMessageFromStream(clientConn.PendingReceiveBuffer,
                                                                             clientConn.RecvStartPosn, clientConn.RecvEndPosn, out bytesSkipped);

            while (sipMsgBuffer != null)
            {
                // A SIP message is available.
                if (SIPMessageReceived != null)
                {
                    clientConn.LastTransmission = DateTime.Now;
                    SIPMessageReceived(recvChannel, clientConn.LocalEndPoint, clientConn.RemoteEndPoint, sipMsgBuffer)
                    .Wait();
                }

                clientConn.RecvStartPosn += (sipMsgBuffer.Length + bytesSkipped);

                if (clientConn.RecvStartPosn == clientConn.RecvEndPosn)
                {
                    // All data has been successfully extracted from the receive buffer.
                    clientConn.RecvStartPosn = clientConn.RecvEndPosn = 0;
                    break;
                }
                else
                {
                    // Try and extract another SIP message from the receive buffer.
                    sipMsgBuffer = SIPMessageBuffer.ParseSIPMessageFromStream(clientConn.PendingReceiveBuffer,
                                                                              clientConn.RecvStartPosn, clientConn.RecvEndPosn, out bytesSkipped);
                }
            }
        }